Skip to content

Commit

Permalink
avoid clone
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoletta committed Nov 7, 2024
1 parent 345ae96 commit 0fa490f
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions crates/storage/trie/trie_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,31 @@ impl Iterator for TrieIterator {
return None;
};
// Fetch the last node in the stack
let (current_path, next_node_hash) = self.stack.pop()?;
let (mut path, next_node_hash) = self.stack.pop()?;
let next_node = self.trie.state.get_node(next_node_hash).ok()??;
let mut next_path = current_path.clone();
match &next_node {
Node::Branch(branch_node) => {
// Add all children to the stack (in reverse order so we process first child frist)
for (choice, child) in branch_node.choices.iter().enumerate().rev() {
if child.is_valid() {
let mut child_path = current_path.clone();
let mut child_path = path.clone();
child_path.append(choice as u8);
self.stack.push((child_path, child.clone()))
}
}
}
Node::Extension(extension_node) => {
// Update path
next_path.extend(&extension_node.prefix);
path.extend(&extension_node.prefix);
// Add child to the stack
self.stack
.push((next_path.clone(), extension_node.child.clone()));
.push((path.clone(), extension_node.child.clone()));
}
Node::Leaf(leaf) => {
next_path.extend(&leaf.partial);
path.extend(&leaf.partial);
}
}
Some((next_path, next_node))
Some((path, next_node))
}
}

Expand Down

0 comments on commit 0fa490f

Please sign in to comment.