perf(types): cache gaskv.Store wrappers in Context #2808
Merged
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
gaskv.Storewrappers in amap[StoreKey]KVStoreonContext, eliminating repeated heap allocations fromctx.KVStore(key)andctx.TransientStore(key)WithMultiStore/WithGasMeter(the only methods that change gaskv inputs)Problem
Every
ctx.KVStore(key)andctx.TransientStore(key)call allocates a newgaskv.Storeon the heap (5-field struct, ~40 bytes). Each wraps the same underlying KVStore + same gas meter + same gas config — pure waste to reallocate.Profiling after #2806 (30s, M4 Max, 1000 EVM transfers/block):
gaskv.NewStorealloc_spacegaskv.NewStorealloc_objectsAccountKeeper.GetAccount(104.5M),params.Subspace.kvStore(46.6M),bank.getAccountStore(20.7M)Changes
sei-cosmos/types/context.go(single file):gaskvStores map[StoreKey]KVStoreon Context structNewContext: Initialize cache withmake(map[StoreKey]KVStore, 8)WithMultiStore/WithGasMeter: Invalidate cache (new empty map)KVStore/TransientStore: Check cache first; populate on missBenchmark Results (M4 Max, 1000 EVM transfers/block, 30s profile)
gaskv.NewStorealloc_spacegaskv.NewStorealloc_objectsmemclrNoHeapPointersCPUbtree.NewFreeListGallocsync.Map nodesalloccachemulti.newStoreWithoutGigaallocNote: TPS uplift is modest because freed CPU shifts to idle (usleep/kevent up) — workers complete faster but the bottleneck has shifted elsewhere (likely OCC scheduling / commit pipeline).
pprof -alloc_space -diff_basehighlightspprof -top -diff_basehighlights (CPU)Why safe
MultiStore(),GasMeter(),StoreTracer()are stableKVGasConfig()/TransientGasConfig()return constantsWithMultiStoreandWithGasMeterare the only methods that change gaskv inputs — both invalidateTest plan
go test github.com/cosmos/cosmos-sdk/types -count=1— passgo test ./giga/tests/... -count=1— pass (all 14 giga tests)go test ./sei-cosmos/store/cachemulti/... -count=1— passgofmt -s -wclean🤖 Generated with Claude Code