@@ -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.
708713func (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
0 commit comments