-
Notifications
You must be signed in to change notification settings - Fork 132
perf(l1): avoid temporary allocations when decoding and hashing trie nodes #5353
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
Changes from all commits
552c144
fcc0dd3
6552ef3
868eb65
fd039c2
0573414
3699fc2
ff4f8c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -263,7 +263,16 @@ impl BranchNode { | |||||||
|
|
||||||||
| /// Computes the node's hash | ||||||||
| pub fn compute_hash(&self) -> NodeHash { | ||||||||
| NodeHash::from_encoded(&self.encode_to_vec()) | ||||||||
| self.compute_hash_no_alloc(&mut vec![]) | ||||||||
| } | ||||||||
|
|
||||||||
| /// Computes the node's hash, using the provided buffer | ||||||||
|
||||||||
| /// Computes the node's hash, using the provided buffer | |
| /// Computes the node's hash using the provided buffer to avoid allocations. | |
| /// This method reuses the given buffer to minimize heap allocations when encoding the node. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -215,7 +215,16 @@ impl ExtensionNode { | |||||
|
|
||||||
| /// Computes the node's hash | ||||||
| pub fn compute_hash(&self) -> NodeHash { | ||||||
| NodeHash::from_encoded(&self.encode_to_vec()) | ||||||
| self.compute_hash_no_alloc(&mut vec![]) | ||||||
| } | ||||||
|
|
||||||
| /// Computes the node's hash, using the provided buffer | ||||||
|
||||||
| /// Computes the node's hash, using the provided buffer | |
| /// Computes the node's hash using the provided buffer to avoid allocations. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -139,7 +139,16 @@ impl LeafNode { | |||||
|
|
||||||
| /// Computes the node's hash | ||||||
| pub fn compute_hash(&self) -> NodeHash { | ||||||
| NodeHash::from_encoded(&self.encode_to_vec()) | ||||||
| self.compute_hash_no_alloc(&mut vec![]) | ||||||
| } | ||||||
|
|
||||||
| /// Computes the node's hash, using the provided buffer | ||||||
|
||||||
| /// Computes the node's hash, using the provided buffer | |
| /// Computes the node's hash using the provided buffer to avoid allocations. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -187,7 +187,9 @@ impl Trie { | |||||
| /// Returns keccak(RLP_NULL) if the trie is empty | ||||||
| pub fn hash_no_commit(&self) -> H256 { | ||||||
| if self.root.is_valid() { | ||||||
| self.root.compute_hash().finalize() | ||||||
| // 512 is the maximum size of an encoded node | ||||||
|
||||||
| // 512 is the maximum size of an encoded node | |
| // 512 is an estimated typical size for an encoded node; some nodes (e.g., branch nodes with many children) may exceed this size |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -85,6 +85,7 @@ fn add_center_to_parent_and_write_queue( | |||||||
| ) -> Result<(), TrieGenerationError> { | ||||||||
| debug!("{:x?}", center_side.path); | ||||||||
| debug!("{:x?}", parent_element.path); | ||||||||
| let mut nodehash_buffer = Vec::with_capacity(512); | ||||||||
|
||||||||
| let mut nodehash_buffer = Vec::with_capacity(512); | |
| // Increased initial capacity to 700 bytes to avoid reallocations for large branch nodes. | |
| let mut nodehash_buffer = Vec::with_capacity(700); |
Copilot
AI
Nov 14, 2025
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.
[nitpick] The initial capacity of 512 bytes may be insufficient for some branch nodes. A branch node can have 16 children (each encoded as up to 33 bytes) plus a value field, potentially exceeding 512 bytes with RLP overhead. While Vec will automatically grow, this may cause reallocations. Consider using a larger initial capacity (e.g., 600-700 bytes) or document this trade-off.
| let mut nodehash_buffer = Vec::with_capacity(512); | |
| // Increased initial capacity to 700 bytes to avoid reallocations for large branch nodes (16 children * 33 bytes + value + RLP overhead) | |
| let mut nodehash_buffer = Vec::with_capacity(700); |
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.
The doc comment is identical to the one for
compute_hashabove. Consider clarifying that this function uses a provided buffer to avoid allocations, e.g., "Computes the node's hash using the provided buffer to avoid allocations."