-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
85854e7
to
af50233
Compare
af50233
to
e249a4c
Compare
93afc06
to
b6a2a7e
Compare
9c95e46
to
c8dfb3f
Compare
c8dfb3f
to
060f965
Compare
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.
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, |
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.
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?
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.
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( |
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.
can we have a unit test for this?
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.
added
@@ -36,24 +36,24 @@ impl SparseStateTrieMetrics { | |||
.record(self.multiproof_total_storage_nodes as f64); | |||
} | |||
|
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.
should we have new_nodes metrics too, same as skipped and total nodes?
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); |
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.
let's pass metrics
to fn and avoid returning that in the result
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.
state and storage tries use different metrics
/// 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, |
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.
let's get rid of all these
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.
new_nodes
is needed for reserving, the rest is metrics
Problem
When revealing multiproofs, we insert new nodes into the
nodes
hashmap of theRevealedSparseTrie
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
After