Skip to content

Commit

Permalink
perf: inline RLP encode functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Sep 27, 2024
1 parent 8a1eaae commit 2d72588
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/nodes/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ impl fmt::Debug for BranchNode {
}

impl Encodable for BranchNode {
#[inline]
fn encode(&self, out: &mut dyn BufMut) {
self.as_ref().encode(out)
}

#[inline]
fn length(&self) -> usize {
self.as_ref().length()
}
Expand Down Expand Up @@ -115,6 +117,7 @@ impl fmt::Debug for BranchNodeRef<'_> {
/// Encode it as a 17-element list consisting of 16 slots that correspond to
/// each child of the node (0-f) and an additional slot for a value.
impl Encodable for BranchNodeRef<'_> {
#[inline]
fn encode(&self, out: &mut dyn BufMut) {
Header { list: true, payload_length: self.rlp_payload_length() }.encode(out);

Expand All @@ -133,6 +136,7 @@ impl Encodable for BranchNodeRef<'_> {
out.put_u8(EMPTY_STRING_CODE);
}

#[inline]
fn length(&self) -> usize {
let payload_length = self.rlp_payload_length();
payload_length + length_of_length(payload_length)
Expand Down
4 changes: 4 additions & 0 deletions src/nodes/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ impl fmt::Debug for ExtensionNode {
}

impl Encodable for ExtensionNode {
#[inline]
fn encode(&self, out: &mut dyn BufMut) {
self.as_ref().encode(out)
}

#[inline]
fn length(&self) -> usize {
self.as_ref().length()
}
Expand Down Expand Up @@ -99,13 +101,15 @@ impl fmt::Debug for ExtensionNodeRef<'_> {
}

impl Encodable for ExtensionNodeRef<'_> {
#[inline]
fn encode(&self, out: &mut dyn BufMut) {
Header { list: true, payload_length: self.rlp_payload_length() }.encode(out);
self.key.encode_path_leaf(false).as_slice().encode(out);
// Pointer to the child is already RLP encoded.
out.put_slice(self.child);
}

#[inline]
fn length(&self) -> usize {
let payload_length = self.rlp_payload_length();
payload_length + length_of_length(payload_length)
Expand Down
4 changes: 4 additions & 0 deletions src/nodes/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ impl fmt::Debug for LeafNode {
}

impl Encodable for LeafNode {
#[inline]
fn encode(&self, out: &mut dyn BufMut) {
self.as_ref().encode(out)
}

#[inline]
fn length(&self) -> usize {
self.as_ref().length()
}
Expand Down Expand Up @@ -98,12 +100,14 @@ impl fmt::Debug for LeafNodeRef<'_> {

/// Manual implementation of encoding for the leaf node of Merkle Patricia Trie.
impl Encodable for LeafNodeRef<'_> {
#[inline]
fn encode(&self, out: &mut dyn BufMut) {
Header { list: true, payload_length: self.rlp_payload_length() }.encode(out);
self.key.encode_path_leaf(true).as_slice().encode(out);
self.value.encode(out);
}

#[inline]
fn length(&self) -> usize {
let payload_length = self.rlp_payload_length();
payload_length + length_of_length(payload_length)
Expand Down
2 changes: 2 additions & 0 deletions src/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum TrieNode {
}

impl Encodable for TrieNode {
#[inline]
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
match self {
Self::EmptyRoot => {
Expand All @@ -46,6 +47,7 @@ impl Encodable for TrieNode {
}
}

#[inline]
fn length(&self) -> usize {
match self {
Self::EmptyRoot => 1,
Expand Down

0 comments on commit 2d72588

Please sign in to comment.