-
Notifications
You must be signed in to change notification settings - Fork 124
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
mssmt: add new batched ms-smt leaf insertion with recursive merge update #1347
Draft
Roasbeef
wants to merge
47
commits into
main
Choose a base branch
from
batch-smt-insert
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains 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
…n in batched_insert
…o avoid nil dereference
…= compaction level
Roasbeef
commented
Feb 3, 2025
…n in compacted_tree.go
Pull Request Test Coverage Report for Build 13108154176Details
💛 - Coveralls |
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.
Implement Batched Insertion for MS-SMT to Optimize Database Operations (Fix #451)
Aider Usage
All modifications in these files were produced using
aider
(including this PR description). The changes were generated with aider v0.73.0 using:The command executed was similar to:
aider --model o3-mini --reasoning-effort high --architect
This ensured that all modifications to the batched insertion algorithm, merge logic, and recursive update functionality were handled consistently across the codebase.
Overview
This PR introduces a new batched insertion routine for the Merkle-Sum Sparse Merkle Tree (MS-SMT), significantly reducing the number of database transactions by updating the root and internal nodes only once per batch. The new algorithm recursively partitions a sorted batch of leaf insertions by
examining individual bits of the insertion keys at each tree level. When a subtree is empty and a single leaf is inserted, a compacted leaf is created using
NewCompactedLeafNode
.In collision cases—when an existing compacted leaf is encountered with a different key—the algorithm calls the
merge
function to combine the new and existing leaves into a newly reconstructed subtree. For multiple inbound insertions, the algorithm recursively processes left and right partitions and finally creates a branch node to combine the updated children.Recursive Merge & Time Complexity
Recursive Partitioning:
The routine processes the tree level by level (from height 0 to last bit index) and partitions the sorted batch based on the bit value (0 or 1) at that level. This recursive process ensures that the insertion position is determined in O(log U) time per leaf (where U is the number of bits in th
key, ~256).
Merge Optimization:
Instead of performing N separate insertions (each incurring O(log U) database operations), the new batched algorithm processes the entire batch within one transaction. Although it still performs O(log U) work per leaf internally, the overall database overhead is reduced from
O(N log U)
toO(log U) + O(1)
per batch, greatly improving efficiency for large batches.Conclusion
This change improves the efficiency of bulk insertions in the MS-SMT by reducing database write transactions and by applying a robust recursive merge/update algorithm. The new implementation replaces multiple single insert calls with one batched update, leading to significant performance gains in
high-load scenarios.
This PR fixes issue #451.