Skip to content

perf(trie): reserve space for new proof nodes ahead of time #15637

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

Merged
merged 14 commits into from
Apr 10, 2025

Conversation

shekhirin
Copy link
Collaborator

@shekhirin shekhirin commented Apr 9, 2025

Problem

When revealing multiproofs, we insert new nodes into the nodes hashmap of the RevealedSparseTrie struct. If there's not enough capacity in the hashmap, it allocates new memory with more space, copies old elements into this memory, and inserts a new element. This can happen multiple times when revealing the proof, because proofs can be large.

Solution

Iterate over the proof nodes once, count the number of new nodes, reserve the space in the nodes hashmap ahead of time for them, and proceed as before.

Before

image

After

image

@shekhirin shekhirin added C-perf A change motivated by improving speed, memory usage or disk footprint A-trie Related to Merkle Patricia Trie implementation labels Apr 9, 2025
@github-project-automation github-project-automation bot moved this to Backlog in Reth Tracker Apr 9, 2025
@shekhirin shekhirin changed the title perf(trie): reserve space in hashmap for sparse trie branch children perf(trie): reserve space for sparse trie branch children Apr 9, 2025
@shekhirin shekhirin marked this pull request as ready for review April 9, 2025 14:03
@shekhirin shekhirin marked this pull request as draft April 9, 2025 14:04
@shekhirin shekhirin force-pushed the alexey/sparse-trie-children-reserve branch from 85854e7 to af50233 Compare April 9, 2025 14:27
@shekhirin shekhirin force-pushed the alexey/sparse-trie-children-reserve branch from af50233 to e249a4c Compare April 9, 2025 14:27
@shekhirin shekhirin force-pushed the alexey/sparse-trie-children-reserve branch from 93afc06 to b6a2a7e Compare April 9, 2025 14:57
@shekhirin shekhirin changed the title perf(trie): reserve space for sparse trie branch children perf(trie): reserve space for new proof nodes ahead of time Apr 9, 2025
@shekhirin shekhirin marked this pull request as ready for review April 9, 2025 15:52
@jenpaff jenpaff moved this from Backlog to In Review in Reth Tracker Apr 9, 2025
@shekhirin shekhirin force-pushed the alexey/sparse-trie-children-reserve branch from 9c95e46 to c8dfb3f Compare April 9, 2025 15:54
@shekhirin shekhirin force-pushed the alexey/sparse-trie-children-reserve branch from c8dfb3f to 060f965 Compare April 9, 2025 15:54
Copy link
Member

@fgimenez fgimenez left a comment

Choose a reason for hiding this comment

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

looks good, smol nits

skipped_nodes: u64,
/// Number of new nodes that will be revealed. This includes all children of branch nodes, even
/// if they are not in the proof.
new_nodes: usize,
Copy link
Member

Choose a reason for hiding this comment

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

why is new_nodes usize and total_nodes u64? according to the logic, always new_nodes >= total_nodes. i guess it is bc new_nodes is used to reserve, why not everything usize?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

was just more convenient to return u64 because it's reported in metrics as u64, but think it's nicer to have consistency in types, so fixed


/// Decodes the proof nodes returning additional information about the number of total, skipped, and
/// new nodes.
fn decode_proof_nodes(
Copy link
Member

Choose a reason for hiding this comment

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

can we have a unit test for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added

@@ -36,24 +36,24 @@ impl SparseStateTrieMetrics {
.record(self.multiproof_total_storage_nodes as f64);
}

Copy link
Member

Choose a reason for hiding this comment

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

should we have new_nodes metrics too, same as skipped and total nodes?

Comment on lines +284 to +287
let DecodedProofNodes { nodes, total_nodes, skipped_nodes, new_nodes } =
decode_proof_nodes(account_subtree, &self.revealed_account_paths)?;
self.metrics.increment_total_account_nodes(total_nodes as u64);
self.metrics.increment_skipped_account_nodes(skipped_nodes as u64);
Copy link
Member

Choose a reason for hiding this comment

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

let's pass metrics to fn and avoid returning that in the result

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

state and storage tries use different metrics

Comment on lines +791 to +797
/// Number of nodes in the proof.
total_nodes: usize,
/// Number of nodes that were skipped because they were already revealed.
skipped_nodes: usize,
/// Number of new nodes that will be revealed. This includes all children of branch nodes, even
/// if they are not in the proof.
new_nodes: usize,
Copy link
Member

Choose a reason for hiding this comment

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

let's get rid of all these

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

new_nodes is needed for reserving, the rest is metrics

@github-project-automation github-project-automation bot moved this from In Review to In Progress in Reth Tracker Apr 10, 2025
@shekhirin shekhirin added this pull request to the merge queue Apr 10, 2025
Merged via the queue into main with commit 2563e93 Apr 10, 2025
44 checks passed
@shekhirin shekhirin deleted the alexey/sparse-trie-children-reserve branch April 10, 2025 17:44
@github-project-automation github-project-automation bot moved this from In Progress to Done in Reth Tracker Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trie Related to Merkle Patricia Trie implementation C-perf A change motivated by improving speed, memory usage or disk footprint
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants