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

Rustify some code, removing a TODO #720

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

Rustify some code, removing a TODO #720

wants to merge 5 commits into from

Conversation

rkuris
Copy link
Collaborator

@rkuris rkuris commented Sep 15, 2024

No description provided.

@rkuris rkuris changed the base branch from main to rkuris/prometheus October 7, 2024 18:47
Base automatically changed from rkuris/prometheus to main October 7, 2024 19:12
Copy link
Contributor

@richardpringle richardpringle left a 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()
};
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.

@rkuris rkuris marked this pull request as draft October 9, 2024 19:16
@rkuris rkuris self-assigned this Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants