feat: Optimize CommitDiff using efficient merge_diff_copy that avoids heap allocation #113
Conversation
WalkthroughThis PR refactors diff application in the commitment processor by introducing a Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant CommitState as commit_state.rs
participant DiffModule as diff/algorithm.rs
participant Account as Delegated Account
Caller->>CommitState: process_commit_state_internal(args: NewState)
alt args is FullBytes
CommitState->>CommitState: copy FullBytes to destination
else args is Diff
CommitState->>Account: read original account data
Account-->>CommitState: original bytes
CommitState->>DiffModule: merge_diff_copy(destination, original, diffset)
DiffModule->>DiffModule: apply diff segments to destination
DiffModule-->>CommitState: Result
end
CommitState-->>Caller: commit state applied
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This stack of pull requests is managed by Graphite. Learn more about stacking. |
CommitDiff using efficient merge_diff_copy that avoids heap allocation
0f5b8cb to
b32a0f7
Compare
534505a to
78e483c
Compare
b32a0f7 to
b39ff4e
Compare
78e483c to
98e5f41
Compare
d639a5c to
04fe332
Compare
98e5f41 to
a09685f
Compare
04fe332 to
1db562c
Compare
01758bc to
02c6224
Compare
cb49fb2 to
5d1c8e6
Compare
02c6224 to
c37ba3e
Compare
5d1c8e6 to
ffefbfd
Compare
731e318 to
b33f9f1
Compare
ffefbfd to
35d4201
Compare
35d4201 to
fe0e096
Compare
b33f9f1 to
2fed9a7
Compare
merge_diff_copy that avoids heap allocation CommitDiff using efficient merge_diff_copy that avoids heap allocation
e29cca1 to
4a5156b
Compare
9728017 to
6c9fc44
Compare
57a4834 to
a4a5218
Compare
b4992ce to
0af4ec2
Compare
6a14dcc to
97ac612
Compare
0af4ec2 to
b0922a1
Compare
6a85e7f to
59aca02
Compare
b0922a1 to
3099cc4
Compare
3a4b154 to
a92c9e3
Compare
a76b7db to
3705937
Compare
a92c9e3 to
3836029
Compare
3836029 to
383919b
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (5)
src/diff/algorithm.rs(5 hunks)src/error.rs(1 hunks)src/processor/fast/commit_diff.rs(2 hunks)src/processor/fast/commit_state.rs(5 hunks)src/processor/fast/commit_state_from_buffer.rs(2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-15T11:45:25.802Z
Learnt from: snawaz
Repo: magicblock-labs/delegation-program PR: 107
File: src/entrypoint.rs:141-144
Timestamp: 2025-10-15T11:45:25.802Z
Learning: In the delegation-program codebase, prefer using `log!` (from pinocchio_log) over `msg!` for error and panic scenarios in the entrypoint code, as per maintainer preference.
Applied to files:
src/processor/fast/commit_state.rs
🧬 Code graph analysis (1)
src/processor/fast/commit_state.rs (1)
src/diff/algorithm.rs (1)
merge_diff_copy(240-262)

It is the third PR in the current stack.
The previous PR #110 used an inefficient approach to diff application that allocates memory on the heap, which can be problematic for larger accounts.
Solution
This PR adds a new
merge_diff_copy()function that applies diffs directly to a destination buffer without requiring additional memory allocation. This function takes adestination buffer,original data, and aDiffSet.. and then merges the original with the diff to produce the updated version in the destination buffer. The whole approach avoids unnecessary memory allocation.Apart from that, it also adds
enum NewStateto represent either full bytes or a diff, andprocess_commit_state_internal()is modified to handle both full data and diffs usingNewState.Summary by CodeRabbit
Refactor
Bug Fixes