Skip to content
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

prealloc children #1

Merged
merged 1 commit into from
Dec 20, 2023
Merged
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
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