starknet_patricia: use OnceLock for write-once filled-tree output maps#14622
Conversation
PR SummaryMedium Risk Overview
The Reviewed by Cursor Bugbot for commit e5f7ec4. Bugbot is set up for automated code reviews on this repo. Configure here. |
The filled-tree output maps are written exactly once per node and then reclaimed, yet they used `Mutex<Option<T>>` and locked on every write and on every reclaim - even though after `Arc::into_inner` the map is uniquely owned and can never be contended. Replace the output maps with `OnceLock<T>`, the primitive that matches this write-once access pattern: `write_to_output_map` becomes a lock-free `set` (a failed `set` is the double-update error), and reclaiming via `OnceLock::into_inner` drops the locking entirely. Pre-allocate the collected map with the known capacity. `Leaf::Output` gains a `Sync` bound, required for `OnceLock<L::Output>: Sync`. The leaf *input* map keeps `Mutex<Option<_>>`, since its values are moved out (take-once), not written. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1e5c797 to
a7255c4
Compare
b00c8c7 to
e5f7ec4
Compare
nimrod-starkware
left a comment
There was a problem hiding this comment.
@nimrod-starkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on yoavGrs).
crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree.rs line 112 at r1 (raw file):
existing_value_as_string: format!("{existing_node:?}"), } }),
I think that should work, the error is the value that was already set
Suggestion:
Some(slot) => slot.set(output).map_err(|exisitng_node| {
FilledTreeError::DoubleUpdate {
index,
existing_value_as_string: format!("{existing_node:?}"),
}
}),
yoavGrs
left a comment
There was a problem hiding this comment.
@yoavGrs made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on nimrod-starkware).
crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree.rs line 112 at r1 (raw file):
Previously, nimrod-starkware wrote…
I think that should work, the
erroris the value that was already set
The Err contains the value you just tried to set (the rejected output), not the value already
stored in the lock.
nimrod-starkware
left a comment
There was a problem hiding this comment.
@nimrod-starkware made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on yoavGrs).
crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree.rs line 112 at r1 (raw file):
Previously, yoavGrs wrote…
The
Errcontains the value you just tried to set (the rejected output), not the value already
stored in the lock.
try_insert returns a reference to the stored value. you can use that
nimrod-starkware
left a comment
There was a problem hiding this comment.
@nimrod-starkware made 1 comment and resolved 1 discussion.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on yoavGrs).
crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree.rs line 112 at r1 (raw file):
Previously, nimrod-starkware wrote…
try_insertreturns a reference to the stored value. you can use that
nvm it's nightly only, unblocking
|
Security scan complete — no issues detected. Generated by Claude Code |

The filled-tree output maps are written exactly once per node and then
reclaimed, yet they used
Mutex<Option<T>>and locked on every write and onevery reclaim - even though after
Arc::into_innerthe map is uniquely ownedand can never be contended.
Replace the output maps with
OnceLock<T>, the primitive that matches thiswrite-once access pattern:
write_to_output_mapbecomes a lock-freeset(a failed
setis the double-update error), and reclaiming viaOnceLock::into_innerdrops the locking entirely. Pre-allocate the collectedmap with the known capacity.
Leaf::Outputgains aSyncbound, required forOnceLock<L::Output>: Sync. The leaf input map keepsMutex<Option<_>>,since its values are moved out (take-once), not written.
Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com