@@ -11,6 +11,8 @@ import (
1111 "github.com/cosmos/cosmos-sdk/telemetry"
1212)
1313
14+ const preStateMaxCacheValueBytes = 4096
15+
1416var (
1517 _ storetypes.KVStore = (* GMVMemoryView [[]byte ])(nil )
1618 _ storetypes.ObjKVStore = (* GMVMemoryView [any ])(nil )
@@ -23,35 +25,57 @@ type GMVMemoryView[V any] struct {
2325 storage storetypes.GKVStore [V ]
2426 mvData * GMVData [V ]
2527 scheduler * Scheduler
28+ preState * preStateCache
2629 store int
2730
2831 txn TxnIndex
2932 readSet * ReadSet
3033 writeSet * GMemDB [V ]
3134}
3235
33- func NewMVView (store int , storage storetypes.Store , mvData MVStore , scheduler * Scheduler , txn TxnIndex ) MVView {
36+ func NewMVView (store int , storage storetypes.Store , mvData MVStore , scheduler * Scheduler , txn TxnIndex , preState * preStateCache ) MVView {
3437 switch data := mvData .(type ) {
3538 case * GMVData [any ]:
36- return NewGMVMemoryView (store , storage .(storetypes.ObjKVStore ), data , scheduler , txn )
39+ return NewGMVMemoryView (store , storage .(storetypes.ObjKVStore ), data , scheduler , txn , preState )
3740 case * GMVData [[]byte ]:
38- return NewGMVMemoryView (store , storage .(storetypes.KVStore ), data , scheduler , txn )
41+ return NewGMVMemoryView (store , storage .(storetypes.KVStore ), data , scheduler , txn , preState )
3942 default :
4043 panic ("unsupported value type" )
4144 }
4245}
4346
44- func NewGMVMemoryView [V any ](store int , storage storetypes.GKVStore [V ], mvData * GMVData [V ], scheduler * Scheduler , txn TxnIndex ) * GMVMemoryView [V ] {
47+ func NewGMVMemoryView [V any ](store int , storage storetypes.GKVStore [V ], mvData * GMVData [V ], scheduler * Scheduler , txn TxnIndex , preState * preStateCache ) * GMVMemoryView [V ] {
4548 return & GMVMemoryView [V ]{
4649 store : store ,
4750 storage : storage ,
4851 mvData : mvData ,
4952 scheduler : scheduler ,
53+ preState : preState ,
5054 txn : txn ,
5155 readSet : new (ReadSet ),
5256 }
5357}
5458
59+ func (s * GMVMemoryView [V ]) getFromPreState (key []byte ) V {
60+ if s .preState != nil {
61+ if cached , ok := s .preState .Load (key ); ok {
62+ return cached .(V )
63+ }
64+ }
65+
66+ result := s .storage .Get (key )
67+ if s .preState != nil {
68+ if bytesValue , ok := any (result ).([]byte ); ok {
69+ if len (bytesValue ) <= preStateMaxCacheValueBytes {
70+ s .preState .Store (key , any (result ))
71+ }
72+ } else {
73+ s .preState .Store (key , any (result ))
74+ }
75+ }
76+ return result
77+ }
78+
5579func (s * GMVMemoryView [V ]) init () {
5680 if s .writeSet == nil {
5781 s .writeSet = NewWriteSet (s .mvData .isZero , s .mvData .valueLen )
@@ -105,7 +129,7 @@ func (s *GMVMemoryView[V]) Get(key []byte) V {
105129 // if not found, record version ⊥ when reading from storage.
106130 s .readSet .Reads = append (s .readSet .Reads , ReadDescriptor {key , version })
107131 if ! version .Valid () {
108- result := s .storage . Get (key )
132+ result := s .getFromPreState (key )
109133 telemetry .MeasureSince (start , TelemetrySubsystem , KeyMVViewReadStorage ) //nolint:staticcheck // TODO: switch to OpenTelemetry
110134 return result
111135 }
0 commit comments