Skip to content
Draft
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
24 changes: 14 additions & 10 deletions firewood/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::merkle::MerkleError;
use sha2::{Digest, Sha256};
use storage::{
BranchNode, Hashable, NibblesIterator, PathIterItem, Preimage, TrieHash, ValueDigest,
BranchNode, Child, Hashable, NibblesIterator, PathIterItem, Preimage, TrieHash, ValueDigest,
};
use thiserror::Error;

Expand Down Expand Up @@ -71,15 +71,19 @@ impl Hashable for ProofNode {

impl From<PathIterItem> for ProofNode {
fn from(item: PathIterItem) -> Self {
let mut child_hashes: [Option<TrieHash>; BranchNode::MAX_CHILDREN] = Default::default();

if let Some(branch) = item.node.as_branch() {
// TODO danlaine: can we avoid indexing?
#[allow(clippy::indexing_slicing)]
for (i, hash) in branch.children_iter() {
child_hashes[i] = Some(hash.clone());
}
}
let child_hashes = if let Some(branch) = item.node.as_branch() {
let mut iter = branch.children.iter().map(|child| match child {
None => None,
Some(Child::Node(_)) => unreachable!("TODO make unreachable"),
Some(Child::AddressWithHash(_, hash)) => Some(hash.clone()),
});
std::array::from_fn(|_| {
iter.next()
.expect("there will always be the correct number of values")
})
} else {
Default::default()
};
Copy link
Contributor

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.


Self {
key: item.key_nibbles,
Expand Down
Loading