Skip to content

Commit c4feb72

Browse files
feat(perf): Use a pooling cachekv for runTx's cache.
This prevents a per-tx allocation of the cache.
1 parent acbbfb9 commit c4feb72

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

baseapp/baseapp.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,11 +703,21 @@ func (app *BaseApp) getContextForTx(mode execMode, txBytes []byte) sdk.Context {
703703
return ctx
704704
}
705705

706+
type poolingStore interface {
707+
storetypes.MultiStore
708+
CacheMultiStorePooled() storetypes.PooledCacheMultiStore
709+
}
710+
706711
// cacheTxContext returns a new context based off of the provided context with
707712
// a branched multi-store.
708713
func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (sdk.Context, storetypes.CacheMultiStore) {
709714
ms := ctx.MultiStore()
710-
msCache := ms.CacheMultiStore()
715+
var msCache storetypes.CacheMultiStore
716+
if msPooled, ok := ms.(poolingStore); ok {
717+
msCache = msPooled.CacheMultiStorePooled()
718+
} else {
719+
msCache = ms.CacheMultiStore()
720+
}
711721
if msCache.TracingEnabled() {
712722
msCache = msCache.SetTracingContext(
713723
storetypes.TraceContext(
@@ -923,6 +933,9 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte, tx sdk.Tx) (gInfo sdk.G
923933
// writes do not happen if aborted/failed. This may have some
924934
// performance benefits, but it'll be more difficult to get right.
925935
anteCtx, msCache = app.cacheTxContext(ctx, txBytes)
936+
if pooledMSCache, ok := msCache.(storetypes.PooledCacheMultiStore); ok {
937+
defer pooledMSCache.Release()
938+
}
926939
anteCtx = anteCtx.WithEventManager(sdk.NewEventManager())
927940
newCtx, err := app.anteHandler(anteCtx, tx, mode == execModeSimulate)
928941

@@ -973,6 +986,9 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte, tx sdk.Tx) (gInfo sdk.G
973986
// in case message processing fails. At this point, the MultiStore
974987
// is a branch of a branch.
975988
runMsgCtx, msCache := app.cacheTxContext(ctx, txBytes)
989+
if pooledMSCache, ok := msCache.(storetypes.PooledCacheMultiStore); ok {
990+
defer pooledMSCache.Release()
991+
}
976992

977993
// Attempt to execute all messages and only update state if all messages pass
978994
// and we're in DeliverTx. Note, runMsgs will never return a reference to a

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,5 @@ retract (
204204
// do not use
205205
v0.43.0
206206
)
207+
208+
replace cosmossdk.io/store => github.com/hyphacoop/cosmos-sdk/store v0.0.0-20250429164929-acbbfb9baf40

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ cosmossdk.io/math v1.5.3 h1:WH6tu6Z3AUCeHbeOSHg2mt9rnoiUWVWaQ2t6Gkll96U=
1616
cosmossdk.io/math v1.5.3/go.mod h1:uqcZv7vexnhMFJF+6zh9EWdm/+Ylyln34IvPnBauPCQ=
1717
cosmossdk.io/schema v1.1.0 h1:mmpuz3dzouCoyjjcMcA/xHBEmMChN+EHh8EHxHRHhzE=
1818
cosmossdk.io/schema v1.1.0/go.mod h1:Gb7pqO+tpR+jLW5qDcNOSv0KtppYs7881kfzakguhhI=
19-
cosmossdk.io/store v1.1.2 h1:3HOZG8+CuThREKv6cn3WSohAc6yccxO3hLzwK6rBC7o=
20-
cosmossdk.io/store v1.1.2/go.mod h1:60rAGzTHevGm592kFhiUVkNC9w7gooSEn5iUBPzHQ6A=
2119
cosmossdk.io/x/tx v0.14.0 h1:hB3O25kIcyDW/7kMTLMaO8Ripj3yqs5imceVd6c/heA=
2220
cosmossdk.io/x/tx v0.14.0/go.mod h1:Tn30rSRA1PRfdGB3Yz55W4Sn6EIutr9xtMKSHij+9PM=
2321
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
@@ -421,6 +419,8 @@ github.com/huandu/go-assert v1.1.5/go.mod h1:yOLvuqZwmcHIC5rIzrBhT7D3Q9c3GFnd0Jr
421419
github.com/huandu/skiplist v1.2.1 h1:dTi93MgjwErA/8idWTzIw4Y1kZsMWx35fmI2c8Rij7w=
422420
github.com/huandu/skiplist v1.2.1/go.mod h1:7v3iFjLcSAzO4fN5B8dvebvo/qsfumiLiDXMrPiHF9w=
423421
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
422+
github.com/hyphacoop/cosmos-sdk/store v0.0.0-20250429164929-acbbfb9baf40 h1:bNPGeuCvdmzN5j7Rp9D0emqZD4XJB5nLCKFtV59TyY8=
423+
github.com/hyphacoop/cosmos-sdk/store v0.0.0-20250429164929-acbbfb9baf40/go.mod h1:hMQGoX8OASEjyoShate33bT6zgSM1X9gIEhUBh7D084=
424424
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
425425
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
426426
github.com/improbable-eng/grpc-web v0.15.0 h1:BN+7z6uNXZ1tQGcNAuaU1YjsLTApzkjt2tzCixLaUPQ=

0 commit comments

Comments
 (0)