perf(store): lazy-init sortedCache in cachekv.Store#2804
Open
perf(store): lazy-init sortedCache in cachekv.Store#2804
Conversation
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 <[email protected]>
|
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 56.58% 56.57% -0.01%
==========================================
Files 2033 2033
Lines 166263 166267 +4
==========================================
- Hits 94074 94065 -9
- Misses 63926 63946 +20
+ Partials 8263 8256 -7
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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 <[email protected]>
5 tasks
codchen
approved these changes
Feb 6, 2026
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.
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 (M4 Max, EVMTransfer 1000 txs/batch)
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