Skip to content

Commit

Permalink
opt: BranchNode::set_prefix uses bitwise_memcpy
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriele-0201 committed Nov 19, 2024
1 parent 9e68d06 commit f3e7dfd
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions nomt/src/beatree/branch/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ impl BranchNode {
self.view().raw_prefix()
}

fn set_prefix(&mut self, prefix: &BitSlice<u8, Msb0>) {
// Set the prefix extracting `self.prefix_len` bits from the provided key
fn set_prefix(&mut self, key: &[u8; 32]) {
let start = BRANCH_NODE_HEADER_SIZE + self.n() as usize * 2;
let prefix_len = self.prefix_len() as usize;
self.as_mut_slice()[start..].view_bits_mut()[..prefix_len].copy_from_bitslice(prefix);
let end = start + ((prefix_len + 7) / 8).next_multiple_of(8);
bitwise_memcpy(&mut self.as_mut_slice()[start..end], 0, key, 0, prefix_len);
}

fn cells_mut(&self) -> &mut [[u8; 2]] {
Expand Down Expand Up @@ -361,8 +363,7 @@ impl BranchNodeBuilder {
assert!(self.index < self.branch.n() as usize);

if self.index == 0 {
let prefix = &key.view_bits::<Msb0>()[..self.prefix_len];
self.branch.set_prefix(prefix);
self.branch.set_prefix(&key);
}

let separator = if self.index < self.prefix_compressed {
Expand Down Expand Up @@ -391,10 +392,9 @@ impl BranchNodeBuilder {
assert!(self.index < self.branch.n() as usize);

if self.index == 0 {
// extract the prefix if this is the first inserted item
// set the prefix if this is the first inserted item
let key = get_key(base, from);
self.branch
.set_prefix(&key.view_bits::<Msb0>()[..self.prefix_len]);
self.branch.set_prefix(&key);
}

let n_items = to - from;
Expand Down

0 comments on commit f3e7dfd

Please sign in to comment.