Skip to content

perf: use Box #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
570 changes: 285 additions & 285 deletions benchmarks/btreemap/canbench_results.yml

Large diffs are not rendered by default.

200 changes: 100 additions & 100 deletions benchmarks/btreeset/canbench_results.yml

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions benchmarks/compare/canbench_results.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ benches:
read_chunks_btreemap_1:
total:
calls: 1
instructions: 148723640
instructions: 148724087
heap_increase: 1601
stable_memory_increase: 0
scopes: {}
read_chunks_btreemap_1k:
total:
calls: 1
instructions: 499445396
instructions: 502477706
heap_increase: 0
stable_memory_increase: 0
scopes: {}
read_chunks_btreemap_1m:
total:
calls: 1
instructions: 43947440627
instructions: 47944788626
heap_increase: 0
stable_memory_increase: 0
scopes: {}
Expand Down Expand Up @@ -65,21 +65,21 @@ benches:
write_chunks_btreemap_1:
total:
calls: 1
instructions: 357205117
instructions: 357205457
heap_increase: 13
stable_memory_increase: 1536
scopes: {}
write_chunks_btreemap_1k:
total:
calls: 1
instructions: 4188695663
instructions: 4191572675
heap_increase: 2
stable_memory_increase: 1536
scopes: {}
write_chunks_btreemap_1m:
total:
calls: 1
instructions: 88827798790
instructions: 92604995881
heap_increase: 0
stable_memory_increase: 3072
scopes: {}
Expand Down Expand Up @@ -107,21 +107,21 @@ benches:
write_chunks_vec_1:
total:
calls: 1
instructions: 549903573
instructions: 549903517
heap_increase: 0
stable_memory_increase: 1536
scopes: {}
write_chunks_vec_1k:
total:
calls: 1
instructions: 562257515
instructions: 562203513
heap_increase: 0
stable_memory_increase: 1536
scopes: {}
write_chunks_vec_1m:
total:
calls: 1
instructions: 1896593101
instructions: 1842593099
heap_increase: 0
stable_memory_increase: 1536
scopes: {}
Expand Down
24 changes: 17 additions & 7 deletions src/btreemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,12 @@ where
}

/// Inserts an entry into a node that is *not full*.
fn insert_nonfull(&mut self, mut node: Node<K>, key: K, value: Vec<u8>) -> Option<Vec<u8>> {
fn insert_nonfull(
&mut self,
mut node: Box<Node<K>>,
key: K,
value: Vec<u8>,
) -> Option<Vec<u8>> {
// We're guaranteed by the caller that the provided node is not full.
assert!(!node.is_full());

Expand Down Expand Up @@ -669,7 +674,7 @@ where
/// Recursively traverses from `node_addr`, invoking `f` if `key` is found. Stops at a leaf if not.
fn traverse<F, R>(&self, node_addr: Address, key: &K, f: F) -> Option<R>
where
F: Fn(Node<K>, usize) -> R + Clone,
F: Fn(Box<Node<K>>, usize) -> R + Clone,
{
let node = self.load_node(node_addr);
// Look for the key in the current node.
Expand Down Expand Up @@ -779,7 +784,7 @@ where
}

/// A helper method for recursively removing a key from the B-tree.
fn remove_helper(&mut self, mut node: Node<K>, key: &K) -> Option<Vec<u8>> {
fn remove_helper(&mut self, mut node: Box<Node<K>>, key: &K) -> Option<Vec<u8>> {
if node.address() != self.root_addr {
// We're guaranteed that whenever this method is called an entry can be
// removed from the node without it needing to be merged into a sibling.
Expand Down Expand Up @@ -1266,13 +1271,18 @@ where
/// Output:
/// [1, 2, 3, 4, 5, 6, 7] (stored in the `into` node)
/// `source` is deallocated.
fn merge(&mut self, source: Node<K>, mut into: Node<K>, median: Entry<K>) -> Node<K> {
fn merge(
&mut self,
source: Box<Node<K>>,
mut into: Box<Node<K>>,
median: Entry<K>,
) -> Box<Node<K>> {
into.merge(source, median, &mut self.allocator);
into
}

/// Allocates a new node of the given type.
fn allocate_node(&mut self, node_type: NodeType) -> Node<K> {
fn allocate_node(&mut self, node_type: NodeType) -> Box<Node<K>> {
match self.version {
Version::V1(page_size) => Node::new_v1(self.allocator.allocate(), node_type, page_size),
Version::V2(page_size) => Node::new_v2(self.allocator.allocate(), node_type, page_size),
Expand All @@ -1281,13 +1291,13 @@ where

/// Deallocates a node.
#[inline]
fn deallocate_node(&mut self, node: Node<K>) {
fn deallocate_node(&mut self, node: Box<Node<K>>) {
node.deallocate(self.allocator_mut());
}

/// Loads a node from memory.
#[inline]
fn load_node(&self, address: Address) -> Node<K> {
fn load_node(&self, address: Address) -> Box<Node<K>> {
Node::load(address, self.version.page_size(), self.memory())
}

Expand Down
19 changes: 8 additions & 11 deletions src/btreemap/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
// We found the key exactly matching the left bound.
// Here is where we'll start the iteration.
self.forward_cursors.push(Cursor::Node {
node: Box::new(node),
node,
next: Index::Entry(idx),
});
break;
Expand All @@ -118,7 +118,7 @@ where
&& self.range.contains(node.key(idx + 1, self.map.memory()))
{
self.forward_cursors.push(Cursor::Node {
node: Box::new(node),
node,
next: Index::Entry(idx + 1),
});
}
Expand Down Expand Up @@ -156,7 +156,7 @@ where
&& self.range.contains(node.key(idx, self.map.memory()))
{
self.forward_cursors.push(Cursor::Node {
node: Box::new(node),
node,
next: Index::Entry(idx),
});
}
Expand Down Expand Up @@ -196,7 +196,7 @@ where
// We found the key exactly matching the right bound.
// Here is where we'll start the iteration.
self.backward_cursors.push(Cursor::Node {
node: Box::new(node),
node,
next: Index::Entry(idx),
});
break;
Expand All @@ -214,7 +214,7 @@ where
&& self.range.contains(node.key(idx - 1, self.map.memory()))
{
self.backward_cursors.push(Cursor::Node {
node: Box::new(node),
node,
next: Index::Entry(idx - 1),
});
}
Expand Down Expand Up @@ -250,7 +250,7 @@ where
if idx > 0 && self.range.contains(node.key(idx - 1, self.map.memory()))
{
self.backward_cursors.push(Cursor::Node {
node: Box::new(node),
node,
next: Index::Entry(idx - 1),
});
}
Expand Down Expand Up @@ -293,7 +293,7 @@ where
// Iterate on leaf nodes starting from the first entry.
NodeType::Leaf => Index::Entry(0),
},
node: Box::new(node),
node,
});
}
self.next_map(map)
Expand Down Expand Up @@ -377,10 +377,7 @@ where
}
_ => None,
} {
self.backward_cursors.push(Cursor::Node {
next,
node: Box::new(node),
});
self.backward_cursors.push(Cursor::Node { next, node });
}
}
self.next_back_map(map)
Expand Down
4 changes: 2 additions & 2 deletions src/btreemap/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct Node<K: Storable + Ord + Clone> {

impl<K: Storable + Ord + Clone> Node<K> {
/// Loads a node from memory at the given address.
pub fn load<M: Memory>(address: Address, page_size: PageSize, memory: &M) -> Self {
pub fn load<M: Memory>(address: Address, page_size: PageSize, memory: &M) -> Box<Self> {
// Load the header to determine which version the node is, then load the node accordingly.
let header: NodeHeader = read_struct(address, memory);
assert_eq!(&header.magic, MAGIC, "Bad magic.");
Expand Down Expand Up @@ -371,7 +371,7 @@ impl<K: Storable + Ord + Clone> Node<K> {
/// order.
pub fn merge<M: Memory>(
&mut self,
mut source: Node<K>,
mut source: Box<Node<K>>,
median: Entry<K>,
allocator: &mut Allocator<M>,
) {
Expand Down
4 changes: 2 additions & 2 deletions src/btreemap/node/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl NodeV1Data {
node.push_child(child);
}

node
*node
}

fn children(&self) -> Vec<Address> {
Expand Down Expand Up @@ -100,7 +100,7 @@ impl NodeV2Data {
node.push_child(child);
}

node
*node
}

fn children(&self) -> Vec<Address> {
Expand Down
16 changes: 10 additions & 6 deletions src/btreemap/node/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ use super::*;

impl<K: Storable + Ord + Clone> Node<K> {
/// Creates a new v1 node at the given address.
pub fn new_v1(address: Address, node_type: NodeType, page_size: DerivedPageSize) -> Node<K> {
Node {
pub fn new_v1(
address: Address,
node_type: NodeType,
page_size: DerivedPageSize,
) -> Box<Node<K>> {
Box::new(Node {
address,
node_type,
entries: vec![],
children: vec![],
version: Version::V1(page_size),
overflows: Vec::with_capacity(0),
}
})
}

/// Loads a v1 node from memory at the given address.
Expand All @@ -62,7 +66,7 @@ impl<K: Storable + Ord + Clone> Node<K> {
max_key_size: u32,
max_value_size: u32,
memory: &M,
) -> Self {
) -> Box<Self> {
#[cfg(feature = "bench_scope")]
let _p = canbench_rs::bench_scope("node_load_v1"); // May add significant overhead.

Expand Down Expand Up @@ -95,7 +99,7 @@ impl<K: Storable + Ord + Clone> Node<K> {
assert_eq!(children.len(), entries.len() + 1);
}

Self {
Box::new(Self {
address,
entries,
children,
Expand All @@ -109,7 +113,7 @@ impl<K: Storable + Ord + Clone> Node<K> {
max_value_size,
}),
overflows: Vec::with_capacity(0),
}
})
}

pub(super) fn save_v1<M: Memory>(&self, memory: &M) {
Expand Down
12 changes: 6 additions & 6 deletions src/btreemap/node/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ const MINIMUM_PAGE_SIZE: u32 = 128;

impl<K: Storable + Ord + Clone> Node<K> {
/// Creates a new v2 node at the given address.
pub fn new_v2(address: Address, node_type: NodeType, page_size: PageSize) -> Node<K> {
pub fn new_v2(address: Address, node_type: NodeType, page_size: PageSize) -> Box<Node<K>> {
assert!(page_size.get() >= MINIMUM_PAGE_SIZE);

Node {
Box::new(Node {
address,
node_type,
version: Version::V2(page_size),
entries: vec![],
children: vec![],
overflows: Vec::with_capacity(0),
}
})
}

/// Loads a v2 node from memory at the given address.
Expand All @@ -110,7 +110,7 @@ impl<K: Storable + Ord + Clone> Node<K> {
page_size: PageSize,
header: NodeHeader,
memory: &M,
) -> Self {
) -> Box<Self> {
#[cfg(feature = "bench_scope")]
let _p = canbench_rs::bench_scope("node_load_v2"); // May add significant overhead.

Expand Down Expand Up @@ -191,14 +191,14 @@ impl<K: Storable + Ord + Clone> Node<K> {
offset += U32_SIZE + Bytes::from(value_size as u64);
}

Self {
Box::new(Self {
address,
entries,
children,
node_type,
version: Version::V2(page_size),
overflows,
}
})
}

// Saves the node to memory.
Expand Down
Loading