Improve LedgerSnapshot entry read/write performance#1786
Draft
mootz12 wants to merge 3 commits into
Draft
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves LedgerSnapshot performance during tests by replacing linear scans over a Vec of entries with a new LedgerEntries storage that provides O(1) keyed lookup while still preserving insertion order for JSON serialization.
Changes:
- Replaced
LedgerSnapshot’s Vec-backed entry storage withLedgerEntries(HashMap + insertion-ordered key Vec) and addedcount_entries(). - Updated snapshot writing logic in
Envto usecount_entries()instead of iterating to count. - Added/expanded tests covering ledger info and entry set/update/remove + serialization behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
soroban-sdk/src/env.rs |
Uses the new count_entries() API for fast “empty snapshot” checks. |
soroban-ledger-snapshot/src/lib.rs |
Introduces LedgerEntries storage and count_entries(), refactors serde handling. |
soroban-ledger-snapshot/src/tests.rs |
Adds tests for ledger info and entry update/remove/roundtrip behavior. |
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.
What
Replaces the Vec-backed ledger entries storage with a new LedgerEntries struct combining a HashMap (O(1) keyed access) and a Vec<Box> (insertion-order preservation for serialization). Also adds count_entries(), cleans up the serde_with dependency for this type, and adds new tests.
This is a breaking change for consumers of
LedgerSnapshot, asledger_entriesis now a private struct, and data can only be accessed through helper functions implemented on theLedgerSnapshotstruct.Why
Entry lookups previously did a linear O(n) scan through the Vec on every read/write — with (extremely) large snapshots this compounds significantly across all contract operations during tests. The HashMap brings those to O(1).
Closes #1781
Known limitations