-
Notifications
You must be signed in to change notification settings - Fork 864
perf(store): lazy-init sortedCache in cachekv.Store
#2804
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
Open
pdrobnjak
wants to merge
2
commits into
main
Choose a base branch
from
perf/lazy-init-sorted-cache
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.
+12
−5
Conversation
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
|
The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2804 +/- ##
===========================================
+ Coverage 46.28% 56.75% +10.47%
===========================================
Files 2005 2070 +65
Lines 163018 168303 +5285
===========================================
+ Hits 75449 95527 +20078
+ Misses 81047 64287 -16760
- Partials 6522 8489 +1967
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
5 tasks
codchen
approved these changes
Feb 6, 2026
7abf219 to
6ac2e41
Compare
Each cachekv.NewStore() was eagerly allocating a MemDB (btree + FreeList) for its sortedCache, even though sortedCache is only used when iterators are requested. During OCC parallel execution, each block creates ~42,000 cachekv stores (1000 txs * ~2 snapshots * 21 module stores), and for EVM transfer workloads, none of them ever iterate — they only use Get/Set/Delete which operate on the sync.Map cache, not sortedCache. This was the single largest source of allocation pressure: btree.NewFreeListG (57 GB/30s) + tm-db.NewMemDB (70 GB/30s) = 127 GB of the 286 GB total allocation rate. All 24 OCC workers contending on the heap allocator lock (runtime.mheap.lock) made this the #1 CPU bottleneck at 23%. The fix defers MemDB allocation to the first iterator call via ensureSortedCache(). For the common case (no iteration), the cost is zero. For the rare case (EndBlock operations, Cosmos native txs), the MemDB is allocated on demand with identical behavior. Benchmark results (M4 Max, EVM transfer workload): - TPS: ~7,800 -> ~8,200 median (+5%) - runtime.lock2 (heap contention): 23.0% -> 15.4% CPU - runtime.gcDrain: 21.6% -> 19.0% CPU - runtime.(*mheap).allocSpan: 18.4% -> 11.4% CPU - btree/MemDB/FreeList allocation: essentially eliminated - Total CPU samples over 30s: 142.2s -> 124.9s (-12%) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Replace ensureSortedCache() + direct field access with a getOrInitSortedCache() getter at all 3 access sites, preventing possible nil dereference if sortedCache is reached outside iterator(). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
6ac2e41 to
4a4adcb
Compare
4fcb74a to
2802f96
Compare
sortedCache in cachekv.Store
stevenlanders
approved these changes
Feb 11, 2026
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
MemDB(btree + FreeList) allocation incachekv.Storeuntil the first iterator is actually requestedsortedCacheis never accessed — onlyGet/Set/Deleteare called, which usestore.cache(sync.Map), notsortedCacheProblem
Profiling the Giga OCC executor on
origin/mainshowed:runtime.lock2)runtime.gcDrain)evmone)Top allocators over 30s:
btree.NewFreeListG(57 GB) +tm-db.NewMemDB(70 GB) = 127 GB of the 286 GB total — all fromcachekv.NewStore()eagerly creating asortedCachethat is never used.sortedCacheis only read in theiterator()→dirtyItems()→clearUnsortedCacheSubset()path. For EVM transfers, iteration never happens — the stores only do point reads/writes. Iteration only occurs during EndBlock (receipt flush, ante surplus, pruning) and Cosmos native transactions (gov, staking, bank queries), which account for a tiny fraction of stores created.Changes
4 lines changed in
sei-cosmos/store/cachekv/store.go:NewStore:sortedCache: nilinstead ofdbm.NewMemDB()Write():store.sortedCache = nilinstead ofdbm.NewMemDB()ensureSortedCache(): allocates MemDB on first useiterator(): callsensureSortedCache()beforedirtyItems()Benchmark Results
runtime.lock2(heap contention)runtime.gcDrainruntime.(*mheap).allocSpanbtree.New/NewMemDB/FreeListTest plan
cd giga/tests && go test ./... -count=1— all 14 tests passgofmt -s -l sei-cosmos/store/cachekv/store.go— clean🤖 Generated with Claude Code