velas/triedb

remove redundant clones in serialization

Opened this issue · 0 comments

One can remove nibbles.clone()
in

impl<'a> fastrlp::Encodable for MerkleNode<'a> {
    fn encode(&self, out: &mut dyn bytes::BufMut) {
        match self {
            MerkleNode::Leaf(Leaf { nibbles, data }) => {
                let pair = NibblePair(nibbles.clone(), NibbleType::Leaf);
                let len = pair.length() + data.length();
                fastrlp::Header {
                    list: true,
                    payload_length: len,
                }
                .encode(out);

and

    fn length(&self) -> usize {
        match self {
            MerkleNode::Leaf(Leaf { nibbles, data }) => {
                let pair = NibblePair(nibbles.clone(), NibbleType::Leaf);
                let len = pair.length() + data.length();
                fastrlp::Header {
                    list: true,
                    payload_length: len,
                }
                .length()
                    + len
            }
            MerkleNode::Extension(Extension { nibbles, value }) => {
                let pair = NibblePair(nibbles.clone(), NibbleType::Extension);

to further improve performance.

length() is called on every inlinable() call