Skip to content

Improve LedgerSnapshot entry read/write performance#1786

Draft
mootz12 wants to merge 3 commits into
mainfrom
snapshot-ledger-map
Draft

Improve LedgerSnapshot entry read/write performance#1786
mootz12 wants to merge 3 commits into
mainfrom
snapshot-ledger-map

Conversation

@mootz12

@mootz12 mootz12 commented Mar 23, 2026

Copy link
Copy Markdown
Contributor

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, as ledger_entries is now a private struct, and data can only be accessed through helper functions implemented on the LedgerSnapshot struct.

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

  • Improvements weren't as impactful as I'd hope. In testing across different repos, I saw almost no change for most contracts in integration style tests that record test results in snapshots. In general, the read/write performance of the snapshot does not appear to be the bottleneck here.
  • Individual read/write performance was significantly improved for large snapshots (>10MB)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with LedgerEntries (HashMap + insertion-ordered key Vec) and added count_entries().
  • Updated snapshot writing logic in Env to use count_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.

Comment thread soroban-ledger-snapshot/src/lib.rs Outdated
Comment thread soroban-ledger-snapshot/src/lib.rs
Comment thread soroban-ledger-snapshot/src/tests.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve LedgerSnapshot lookup and update efficiency

2 participants