-
Notifications
You must be signed in to change notification settings - Fork 13
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
Rustify some code, removing a TODO #720
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a comment with effectively the same changes as the one you have with a couple of other suggestions.
}) | ||
} else { | ||
Default::default() | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would write the whole From
implementation like this:
impl From<PathIterItem> for ProofNode {
fn from(item: PathIterItem) -> Self {
let PathIterItem {
key_nibbles: key,
node,
..
} = item;
let (value, child_hashes) = match node.as_ref() {
storage::Node::Branch(branch_node) => {
let value = branch_node.value.clone();
let mut iter = branch_node.children.iter().map(|child| {
child.as_ref().map(|child| match child {
Child::AddressWithHash(_, hash) => *hash,
Child::Node(_) => {
unreachable!("cannot create `ProofNode` from in-memory `Node`")
}
})
});
(
value,
std::array::from_fn(move |_| iter.next().expect("always the same size")),
)
}
storage::Node::Leaf(leaf_node) => (Some(leaf_node.value.clone()), Default::default()),
};
let value_digest = value.map(ValueDigest::Value);
Self {
key,
value_digest,
child_hashes,
}
}
}
You need to derive Copy
for storage::TrieHash
for this to work, but you should do that anyway. There are a bunch of places where you could remove a call to clone
. I'm not sure that you'll get an actual speed up as I would hope that the compiler is smart enough not to perform any allocations on clone
.
I've also removed the to_vec
call on value since it's already a boxed slice.
You could further reduce allocations if you used bytes::Bytes
for the value
as I'm guessing it's rare that this changes.
Is it ever the case the children array contains both a mix of Child::Node
and ChildAddressWithHash
? If not, I would recommend wrapping the array in an enum:
enum Children {
WithHashes([(LinearAddress, TrieHash); SIZE]),
Mixed([Child; SIZE]),
}
Then you could just use the map
function for the WithHashes
case since the [(LinearAddress, TrieHash); SIZE]
type is Copy
.
No description provided.