Skip to content

Commit

Permalink
Don't allocate when creating 'from' and 'dup_of' iterators
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Porof <[email protected]>
  • Loading branch information
victorporof committed Nov 14, 2019
1 parent c8fac3b commit a538900
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/backend/impl_lmdb/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ impl<'c> BackendRoCursor<'c> for RoCursorImpl<'c> {

fn into_iter_from<K>(self, key: K) -> Self::Iter
where
K: AsRef<[u8]>,
K: AsRef<[u8]> + 'c,
{
IterImpl::new(self.0, |cursor| cursor.iter_from(key))
}

fn into_iter_dup_of<K>(self, key: K) -> Self::Iter
where
K: AsRef<[u8]>,
K: AsRef<[u8]> + 'c,
{
IterImpl::new(self.0, |cursor| cursor.iter_dup_of(key))
}
Expand All @@ -55,14 +55,14 @@ impl<'c> BackendRoCursor<'c> for RwCursorImpl<'c> {

fn into_iter_from<K>(self, key: K) -> Self::Iter
where
K: AsRef<[u8]>,
K: AsRef<[u8]> + 'c,
{
IterImpl::new(self.0, |cursor| cursor.iter_from(key))
}

fn into_iter_dup_of<K>(self, key: K) -> Self::Iter
where
K: AsRef<[u8]>,
K: AsRef<[u8]> + 'c,
{
IterImpl::new(self.0, |cursor| cursor.iter_dup_of(key))
}
Expand Down
16 changes: 6 additions & 10 deletions src/backend/impl_safe/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,16 @@ impl<'c> BackendRoCursor<'c> for RoCursorImpl<'c> {

fn into_iter_from<K>(self, key: K) -> Self::Iter
where
K: AsRef<[u8]>,
K: AsRef<[u8]> + 'c,
{
// FIXME: Don't allocate.
let key = key.as_ref().to_vec();
IterImpl(Box::new(self.0.iter().skip_while(move |&(k, _)| k < key.as_slice())))
IterImpl(Box::new(self.0.iter().skip_while(move |&(k, _)| k < key.as_ref())))
}

fn into_iter_dup_of<K>(self, key: K) -> Self::Iter
where
K: AsRef<[u8]>,
K: AsRef<[u8]> + 'c,
{
// FIXME: Don't allocate.
let key = key.as_ref().to_vec();
IterImpl(Box::new(self.0.iter().filter(move |&(k, _)| k == key.as_slice())))
IterImpl(Box::new(self.0.iter().filter(move |&(k, _)| k == key.as_ref())))
}
}

Expand All @@ -55,14 +51,14 @@ impl<'c> BackendRoCursor<'c> for RwCursorImpl<'c> {

fn into_iter_from<K>(self, _key: K) -> Self::Iter
where
K: AsRef<[u8]>,
K: AsRef<[u8]> + 'c,
{
unimplemented!()
}

fn into_iter_dup_of<K>(self, _key: K) -> Self::Iter
where
K: AsRef<[u8]>,
K: AsRef<[u8]> + 'c,
{
unimplemented!()
}
Expand Down
4 changes: 2 additions & 2 deletions src/backend/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ pub trait BackendRoCursor<'c>: Debug {

fn into_iter_from<K>(self, key: K) -> Self::Iter
where
K: AsRef<[u8]>;
K: AsRef<[u8]> + 'c;

fn into_iter_dup_of<K>(self, key: K) -> Self::Iter
where
K: AsRef<[u8]>;
K: AsRef<[u8]> + 'c;
}

pub trait BackendIter<'i> {
Expand Down
1 change: 1 addition & 0 deletions src/store/integermulti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ where
R: Readable<'r, Database = D, RoCursor = C>,
I: BackendIter<'r>,
C: BackendRoCursor<'r, Iter = I>,
K: 'r,
{
self.inner.get(reader, Key::new(&k)?)
}
Expand Down
2 changes: 1 addition & 1 deletion src/store/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ where
R: Readable<'r, Database = D, RoCursor = C>,
I: BackendIter<'r>,
C: BackendRoCursor<'r, Iter = I>,
K: AsRef<[u8]>,
K: AsRef<[u8]> + 'r,
{
let cursor = reader.open_ro_cursor(&self.db)?;
let iter = cursor.into_iter_dup_of(k);
Expand Down
2 changes: 1 addition & 1 deletion src/store/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ where
R: Readable<'r, Database = D, RoCursor = C>,
I: BackendIter<'r>,
C: BackendRoCursor<'r, Iter = I>,
K: AsRef<[u8]>,
K: AsRef<[u8]> + 'r,
{
let cursor = reader.open_ro_cursor(&self.db)?;
let iter = cursor.into_iter_from(k);
Expand Down
2 changes: 1 addition & 1 deletion tests/test_txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn read_many() {
}
}

fn get_ids_by_field<'t, T>(txn: &'t T, store: MultiStore, field: &str) -> Vec<u64>
fn get_ids_by_field<'t, T>(txn: &'t T, store: MultiStore, field: &'t str) -> Vec<u64>
where
T: Readable<'t, Database = LmdbDatabase, RoCursor = LmdbRoCursor<'t>>,
{
Expand Down

0 comments on commit a538900

Please sign in to comment.