Skip to content

perf(types): cache gaskv.Store wrappers in Context#2853

Draft
pdrobnjak wants to merge 3 commits intomainfrom
perf/cache-gaskv-context
Draft

perf(types): cache gaskv.Store wrappers in Context#2853
pdrobnjak wants to merge 3 commits intomainfrom
perf/cache-gaskv-context

Conversation

@pdrobnjak
Copy link
Contributor

Summary

  • Cache gaskv.Store wrappers in Context to avoid repeated allocations
  • Includes thread-safety fix (RWMutex) and tracing invalidation fix

Stack

6/19 — depends on perf/optimize-updatereadset-alloc (replaces auto-closed #2808)

🤖 Generated with Claude Code

@github-actions
Copy link

github-actions bot commented Feb 11, 2026

The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedFeb 11, 2026, 1:35 PM

pdrobnjak and others added 3 commits February 11, 2026 14:32
Every ctx.KVStore(key) / ctx.TransientStore(key) call was allocating a
new gaskv.Store on the heap (~40 bytes, 5 fields). Profiling showed
155M allocations / 18.9 GB from gaskv.NewStore over 30s, with top
callers being AccountKeeper.GetAccount (104.5M), params.Subspace (46.6M),
and bank.getAccountStore (20.7M). Each wraps the same underlying KVStore,
gas meter, and gas config — pure waste to reallocate.

Add a gaskvStores map[StoreKey]KVStore cache to Context. On first
KVStore(key) call, create and cache. On subsequent calls, return cached.
Cache is invalidated (fresh empty map) in WithMultiStore and WithGasMeter,
which are the only methods that change gaskv.NewStore inputs.

Results (M4 Max benchmark):
- gaskv.NewStore completely eliminated from heap profiles (-18.3 GB)
- Cascading reductions: cachemulti -17.4 GB, btree -16.5 GB
- TPS steady-state: 7,800-9,000 (median ~8,400)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The gaskvStores cache map on Context is read/written concurrently
when multiple goroutines share a Context (e.g., slashing BeginBlocker's
HandleValidatorSignatureConcurrent). Add *sync.RWMutex with RLock for
cache hits, Lock for cache misses.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
WithIsTracing changes how gaskv.Store wraps the underlying store
(tracing vs non-tracing). Cached stores created before tracing was
enabled would miss trace events. Reset the cache on tracing change.

Fixes TestViewKeeperStoreTrace failure introduced by gaskv caching.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@pdrobnjak pdrobnjak force-pushed the perf/cache-gaskv-context branch from f1c3021 to a4c49dc Compare February 11, 2026 13:34
@pdrobnjak pdrobnjak force-pushed the perf/optimize-updatereadset-alloc branch from de1b10e to 9581921 Compare February 11, 2026 13:34
@pdrobnjak pdrobnjak changed the base branch from perf/optimize-updatereadset-alloc to main February 11, 2026 13:34
@codecov
Copy link

codecov bot commented Feb 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 56.77%. Comparing base (21e991f) to head (a4c49dc).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2853       +/-   ##
===========================================
+ Coverage   46.28%   56.77%   +10.49%     
===========================================
  Files        2005     2070       +65     
  Lines      163018   168332     +5314     
===========================================
+ Hits        75449    95577    +20128     
+ Misses      81047    64261    -16786     
- Partials     6522     8494     +1972     
Flag Coverage Δ
sei-chain 40.87% <100.00%> (-0.05%) ⬇️
sei-cosmos 48.15% <100.00%> (+0.04%) ⬆️
sei-db 68.94% <ø> (+0.22%) ⬆️
sei-tendermint 58.73% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sei-cosmos/types/context.go 94.48% <100.00%> (+0.66%) ⬆️

... and 358 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pdrobnjak pdrobnjak marked this pull request as draft February 11, 2026 14:05
@pdrobnjak pdrobnjak self-assigned this Feb 11, 2026
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.

1 participant