refactor(trie): extract hybrid merge algorithm for sorted trie types#21157
Closed
refactor(trie): extract hybrid merge algorithm for sorted trie types#21157
Conversation
20c1702 to
2687317
Compare
Batches trie updates across all blocks in `save_blocks` instead of writing per-block, reducing cursor open/close overhead from N to 1. ## Changes ### lazy_overlay.rs - Move MERGE_BATCH_THRESHOLD constant inside function - Use data directly instead of Arc::clone (avoids unnecessary refcount bump) - Move Arc::make_mut into the loop for proper copy-on-write semantics ### provider.rs Add batched trie updates with hybrid merge algorithm: - 0 blocks: default - 1 block: Arc::try_unwrap to avoid clone if refcount is 1 - < 30 blocks: extend_ref with Arc::make_mut (copy-on-write) - >= 30 blocks: k-way merge_batch for O(n log k) complexity ## Allocation Behavior (No Regression) Small batches avoid collect() by using Arc::make_mut directly in the loop. Only large batches (>= 30) collect Arcs for k-way merge. ## Expected Impact - ~50% reduction in write_trie_updates time for b2b scenarios - Maintains same allocation characteristics as original code ## Related - Based on optimizations from Slack #eng-perf thread - Learned from PR #21142 review: avoid unnecessary collect(), use Arc::make_mut
2687317 to
690b167
Compare
mattsse
reviewed
Jan 19, 2026
Collaborator
mattsse
left a comment
There was a problem hiding this comment.
I believe this has been merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Batches trie updates across all blocks in
save_blocksinstead of writing per-block, reducing cursor open/close overhead from N to 1.Problem
Per #eng-perf profiling,
write_trie_updateswas taking ~25% of persistence time. The current implementation callswrite_trie_updates_sortedonce per block, opening/closing cursors N times.In back-to-back (b2b) scenarios with 75-250 accumulated blocks, this overhead compounds significantly.
Solution
lazy_overlay.rs
MERGE_BATCH_THRESHOLDconstant inside functionArc::clone(avoids unnecessary refcount bump)Arc::make_mutinto the loop for proper copy-on-write semanticsprovider.rs
Add batched trie updates with hybrid merge algorithm:
Arc::try_unwrapextend_refwithArc::make_mutmerge_batchAllocation Behavior (No Regression)
Learned from PR #21142 review feedback:
collect()unnecessarily in small-k pathArc::make_mutdirectly in the loop for copy-on-writeArc::try_unwrapto avoid final clone when refcount is 1Expected Impact
write_trie_updatestime for b2b scenariosTesting
reth-chain-stateandreth-providertests passRelated