Skip to content

Conversation

@xqft
Copy link
Contributor

@xqft xqft commented Oct 22, 2025

Motivation

Optimizes trie decoding in zkVM stateless execution by adding NodeHash::finalize_mut and NodeRef::compute_hash_finalized to calculate a node's finalized hash, store it and return a reference to it, instead of cloning the hash or even hashing each time if it's an inline node.

Also refactors the get_embedded_node function into NodeRef::resolve_subtrie to remove the nested function and simplify the code.

Flamegraphs
After all other trie zkVM optimizations, from_nodes is one of the most expensive calls in block execution. This PR reduces Trie::from_nodes from 131k to 101k, 22%.

before:
image
after:
image

@github-actions github-actions bot added the L2 Rollup client label Oct 22, 2025
@github-actions
Copy link

github-actions bot commented Oct 22, 2025

Lines of code report

Total lines added: 60
Total lines removed: 35
Total lines changed: 95

Detailed view
+----------------------------------------+-------+------+
| File                                   | Lines | Diff |
+----------------------------------------+-------+------+
| ethrex/crates/common/trie/node.rs      | 228   | +45  |
+----------------------------------------+-------+------+
| ethrex/crates/common/trie/node_hash.rs | 119   | +15  |
+----------------------------------------+-------+------+
| ethrex/crates/common/trie/trie.rs      | 917   | -35  |
+----------------------------------------+-------+------+

@github-actions
Copy link

Benchmark for ad8c4f4

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 33.2±1.16ms 32.6±0.56ms -1.81%
Trie/cita-trie insert 1k 3.3±0.09ms 3.5±0.27ms +6.06%
Trie/ethrex-trie insert 10k 61.9±1.55ms 62.0±1.32ms +0.16%
Trie/ethrex-trie insert 1k 8.0±0.16ms 7.9±0.16ms -1.25%

@xqft xqft marked this pull request as ready for review October 22, 2025 20:06
@xqft xqft requested a review from a team as a code owner October 22, 2025 20:06
Copilot AI review requested due to automatic review settings October 22, 2025 20:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors trie decoding to eliminate hash cloning and simplify the codebase. The changes optimize stateless execution in zkVM by introducing new methods for efficient hash computation and storage, resulting in a 22% performance improvement in Trie::from_nodes.

Key changes:

  • Added NodeHash::finalize_mut to compute and store finalized hashes in-place, avoiding repeated hashing and clones
  • Refactored the nested get_embedded_node function into NodeRef::resolve_subtrie for better code organization
  • Introduced NodeRef::compute_hash_finalized to obtain hash references without cloning

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
crates/common/trie/trie.rs Simplified Trie::from_nodes by replacing nested function with resolve_subtrie method
crates/common/trie/node_hash.rs Added finalize_mut method to compute and cache finalized hash in-place
crates/common/trie/node.rs Added compute_hash_finalized and resolve_subtrie methods to support refactored trie construction

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@github-actions
Copy link

Benchmark for 4bcaa8d

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 41.5±2.54ms 36.6±1.59ms -11.81%
Trie/cita-trie insert 1k 3.5±0.01ms 3.6±0.15ms +2.86%
Trie/ethrex-trie insert 10k 52.3±1.99ms 55.0±2.31ms +5.16%
Trie/ethrex-trie insert 1k 6.4±0.04ms 6.4±0.05ms 0.00%

@github-actions
Copy link

Benchmark for b5b710f

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 34.8±0.58ms 35.0±0.83ms +0.57%
Trie/cita-trie insert 1k 3.5±0.02ms 3.5±0.05ms 0.00%
Trie/ethrex-trie insert 10k 48.7±1.08ms 48.8±1.35ms +0.21%
Trie/ethrex-trie insert 1k 6.4±0.13ms 6.3±0.03ms -1.56%


/// Returns a reference to the finalized hash, hashing `self` if it's `NodeHash::Inline`, and storing the result by
/// changing it to the `NodeHash::Hashed` variant.
pub fn finalize_mut(&mut self) -> &H256 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function leaves the tree in an inconsistent state, right? If any function expects compute_hash to return the node's MPT hash, they'll receive an incorrect result. We should document that in a big warning message to avoid weird errors going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

L2 Rollup client

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants