Skip to content

Commit

Permalink
Optimize first() and last()
Browse files Browse the repository at this point in the history
This makes these methods also 2x faster by reducing the number of tree
traversals from two to one.
  • Loading branch information
cberner committed Sep 30, 2024
1 parent 840a537 commit 70baeb9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ impl<'txn, K: Key + 'static, V: Value + 'static> ReadableTable<K, V> for Table<'
.range(&range)
.map(|x| Range::new(x, self.transaction.transaction_guard()))
}

fn first(&self) -> Result<Option<(AccessGuard<K>, AccessGuard<V>)>> {
self.tree.first()
}

fn last(&self) -> Result<Option<(AccessGuard<K>, AccessGuard<V>)>> {
self.tree.last()
}
}

impl<K: Key, V: Value> Sealed for Table<'_, K, V> {}
Expand Down Expand Up @@ -392,14 +400,10 @@ pub trait ReadableTable<K: Key + 'static, V: Value + 'static>: ReadableTableMeta
KR: Borrow<K::SelfType<'a>> + 'a;

/// Returns the first key-value pair in the table, if it exists
fn first(&self) -> Result<Option<(AccessGuard<K>, AccessGuard<V>)>> {
self.iter()?.next().transpose()
}
fn first(&self) -> Result<Option<(AccessGuard<K>, AccessGuard<V>)>>;

/// Returns the last key-value pair in the table, if it exists
fn last(&self) -> Result<Option<(AccessGuard<K>, AccessGuard<V>)>> {
self.iter()?.next_back().transpose()
}
fn last(&self) -> Result<Option<(AccessGuard<K>, AccessGuard<V>)>>;

/// Returns a double-ended iterator over all elements in the table
fn iter(&self) -> Result<Range<K, V>> {
Expand Down Expand Up @@ -520,6 +524,14 @@ impl<K: Key + 'static, V: Value + 'static> ReadableTable<K, V> for ReadOnlyTable
.range(&range)
.map(|x| Range::new(x, self.transaction_guard.clone()))
}

fn first(&self) -> Result<Option<(AccessGuard<K>, AccessGuard<V>)>> {
self.tree.first()
}

fn last(&self) -> Result<Option<(AccessGuard<K>, AccessGuard<V>)>> {
self.tree.last()
}
}

impl<K: Key, V: Value> Sealed for ReadOnlyTable<K, V> {}
Expand Down
62 changes: 62 additions & 0 deletions src/tree_store/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@ impl<K: Key + 'static, V: Value + 'static> BtreeMut<'_, K, V> {
self.read_tree()?.get(key)
}

pub(crate) fn first(&self) -> Result<Option<(AccessGuard<'static, K>, AccessGuard<'static, V>)>> {
self.read_tree()?.first()
}

pub(crate) fn last(&self) -> Result<Option<(AccessGuard<'static, K>, AccessGuard<'static, V>)>> {
self.read_tree()?.last()
}

pub(crate) fn range<'a0, T: RangeBounds<KR> + 'a0, KR: Borrow<K::SelfType<'a0>> + 'a0>(
&self,
range: &'_ T,
Expand Down Expand Up @@ -726,6 +734,60 @@ impl<K: Key, V: Value> Btree<K, V> {
}
}

pub(crate) fn first(&self) -> Result<Option<(AccessGuard<'static, K>, AccessGuard<'static, V>)>> {
if let Some(ref root) = self.cached_root {
self.first_helper(root.clone())
} else {
Ok(None)
}
}

fn first_helper(&self, page: PageImpl) -> Result<Option<(AccessGuard<'static, K>, AccessGuard<'static, V>)>> {
let node_mem = page.memory();
match node_mem[0] {
LEAF => {
let accessor = LeafAccessor::new(page.memory(), K::fixed_width(), V::fixed_width());
let (key_range, value_range) = accessor.entry_ranges(0).unwrap();
let key_guard = AccessGuard::with_page(page.clone(), key_range);
let value_guard = AccessGuard::with_page(page, value_range);
Ok(Some((key_guard, value_guard)))
}
BRANCH => {
let accessor = BranchAccessor::new(&page, K::fixed_width());
let child_page = accessor.child_page(0).unwrap();
self.first_helper(self.mem.get_page_extended(child_page, self.hint)?)
}
_ => unreachable!(),
}
}

pub(crate) fn last(&self) -> Result<Option<(AccessGuard<'static, K>, AccessGuard<'static, V>)>> {
if let Some(ref root) = self.cached_root {
self.last_helper(root.clone())
} else {
Ok(None)
}
}

fn last_helper(&self, page: PageImpl) -> Result<Option<(AccessGuard<'static, K>, AccessGuard<'static, V>)>> {
let node_mem = page.memory();
match node_mem[0] {
LEAF => {
let accessor = LeafAccessor::new(page.memory(), K::fixed_width(), V::fixed_width());
let (key_range, value_range) = accessor.entry_ranges(accessor.num_pairs() - 1).unwrap();
let key_guard = AccessGuard::with_page(page.clone(), key_range);
let value_guard = AccessGuard::with_page(page, value_range);
Ok(Some((key_guard, value_guard)))
}
BRANCH => {
let accessor = BranchAccessor::new(&page, K::fixed_width());
let child_page = accessor.child_page(accessor.count_children() - 1).unwrap();
self.last_helper(self.mem.get_page_extended(child_page, self.hint)?)
}
_ => unreachable!(),
}
}

pub(crate) fn range<'a0, T: RangeBounds<KR>, KR: Borrow<K::SelfType<'a0>>>(
&self,
range: &'_ T,
Expand Down
8 changes: 8 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,14 @@ impl<K: Key + 'static, V: Value + 'static, T: ReadableTable<K, V>> ReadableTable
{
self.inner.range(range)
}

fn first(&self) -> redb::Result<Option<(AccessGuard<K>, AccessGuard<V>)>> {
self.inner.first()
}

fn last(&self) -> redb::Result<Option<(AccessGuard<K>, AccessGuard<V>)>> {
self.inner.last()
}
}

impl<K: Key + 'static, V: Value + 'static, T: ReadableTable<K, V>> ReadableTableMetadata
Expand Down

0 comments on commit 70baeb9

Please sign in to comment.