-
Notifications
You must be signed in to change notification settings - Fork 216
perf(l1): keep the memoize_hashes guard out of the recursive call #7101
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -323,15 +323,36 @@ impl NodeRef { | |
| } | ||
| } | ||
|
|
||
| /// Memoizes this reference's subtrie hash, if there is anything to do. | ||
| /// | ||
| /// The guard is deliberately split from the recursive body. A branch node | ||
| /// visits all 16 of its children, but the vast majority of child references | ||
| /// are `NodeRef::Hash` or empty slots with nothing to memoize (measured on a | ||
| /// mainnet block witness: ~93%). Because the body is recursive LLVM will not | ||
| /// inline it, so keeping the guard in a separate `#[inline]` shell makes the | ||
| /// overwhelmingly common no-op case a load and a branch at the call site | ||
| /// instead of a full call. | ||
| #[inline] | ||
| pub fn memoize_hashes(&self, buf: &mut Vec<u8>, crypto: &dyn Crypto) { | ||
| if let NodeRef::Node(node, hash) = &self | ||
| && hash.get().is_none() | ||
| { | ||
| node.memoize_hashes(buf, crypto); | ||
| let _ = hash.set(node.compute_hash_no_alloc(buf, crypto)); | ||
| Self::memoize_hashes_uncached(node, hash, buf, crypto); | ||
| } | ||
| } | ||
|
|
||
| /// Cold path of [`Self::memoize_hashes`]: this reference is an embedded node | ||
| /// whose hash is not memoized yet, so descend into it and compute it. | ||
| fn memoize_hashes_uncached( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: worth pinning this with The split works because the recursion cycle is cut here: the shell can be inlined into #[inline(never)]
fn memoize_hashes_uncached(The doc comment already explains the intent — this just makes the compiler honour it. One thing I'd avoid: |
||
| node: &Arc<Node>, | ||
| hash: &OnceLock<NodeHash>, | ||
| buf: &mut Vec<u8>, | ||
| crypto: &dyn Crypto, | ||
| ) { | ||
| node.memoize_hashes(buf, crypto); | ||
| let _ = hash.set(node.compute_hash_no_alloc(buf, crypto)); | ||
| } | ||
|
|
||
| /// Resets the memoized hash of this Node | ||
| /// | ||
| /// This is used when mutating a node in place, in which case the memoized hash | ||
|
|
||
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.
Might be worth having an inline(never) to ensure it behaves as expected.