Skip to content

Commit

Permalink
prealloc children
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Dec 20, 2023
1 parent a8ac765 commit 8511c6a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/hash_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl HashBuilder {
let state_mask = self.groups[len];
let hash_mask = self.hash_masks[len];
let branch_node = BranchNode::new(&self.stack);
let children = branch_node.children(state_mask, hash_mask).collect();
let children = branch_node.children(state_mask, hash_mask);

self.rlp_buf.clear();
let rlp = branch_node.rlp(state_mask, &mut self.rlp_buf);
Expand Down
16 changes: 6 additions & 10 deletions src/nodes/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,18 @@ impl<'a> BranchNode<'a> {

/// Given the hash and state mask of children present, return an iterator over the stack items
/// that match the mask.
pub fn children(
&self,
state_mask: TrieMask,
hash_mask: TrieMask,
) -> impl Iterator<Item = B256> + '_ {
pub fn children(&self, state_mask: TrieMask, hash_mask: TrieMask) -> Vec<B256> {
let mut index = self.stack.len() - state_mask.count_ones() as usize;
CHILD_INDEX_RANGE.filter_map(move |digit| {
let mut child = None;
let mut children = Vec::with_capacity(CHILD_INDEX_RANGE.len());
for digit in CHILD_INDEX_RANGE {
if state_mask.is_bit_set(digit) {
if hash_mask.is_bit_set(digit) {
child = Some(&self.stack[index]);
children.push(B256::from_slice(&self.stack[index][1..]));
}
index += 1;
}
child.map(|child| B256::from_slice(&child[1..]))
})
}
children
}

/// Returns the RLP encoding of the branch node given the state mask of children present.
Expand Down

0 comments on commit 8511c6a

Please sign in to comment.