Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(context): avoid unnecessary copies in KVStore, TransientStore and CacheContext methods #14354

Merged
merged 15 commits into from
Dec 19, 2022
Prev Previous commit
Next Next commit
feat: gaskv config as pointer
  • Loading branch information
testinginprod committed Dec 7, 2022
commit 940980d311c83df1139c4b1bcddecb209e7699b3
12 changes: 8 additions & 4 deletions baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ func TestABCI_CheckTx(t *testing.T) {
require.Empty(t, r.GetEvents())
}

checkStateStore := getCheckStateCtx(suite.baseApp).KVStore(capKey1)
ctx := getCheckStateCtx(suite.baseApp)
checkStateStore := ctx.KVStore(capKey1)
storedCounter := getIntFromStore(t, checkStateStore, counterKey)

// ensure AnteHandler ran
Expand All @@ -625,7 +626,8 @@ func TestABCI_CheckTx(t *testing.T) {
suite.baseApp.EndBlock(abci.RequestEndBlock{})
suite.baseApp.Commit()

checkStateStore = getCheckStateCtx(suite.baseApp).KVStore(capKey1)
stateCtx := getCheckStateCtx(suite.baseApp)
checkStateStore = stateCtx.KVStore(capKey1)
storedBytes := checkStateStore.Get(counterKey)
require.Nil(t, storedBytes)
}
Expand Down Expand Up @@ -697,7 +699,8 @@ func TestABCI_DeliverTx_MultiMsg(t *testing.T) {
res := suite.baseApp.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))

store := getDeliverStateCtx(suite.baseApp).KVStore(capKey1)
ctx := getDeliverStateCtx(suite.baseApp)
store := ctx.KVStore(capKey1)

// tx counter only incremented once
txCounter := getIntFromStore(t, store, anteKey)
Expand Down Expand Up @@ -725,7 +728,8 @@ func TestABCI_DeliverTx_MultiMsg(t *testing.T) {
res = suite.baseApp.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))

store = getDeliverStateCtx(suite.baseApp).KVStore(capKey1)
ctx = getDeliverStateCtx(suite.baseApp)
store = ctx.KVStore(capKey1)

// tx counter only incremented once
txCounter = getIntFromStore(t, store, anteKey)
Expand Down
8 changes: 4 additions & 4 deletions store/gaskv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ var _ types.KVStore = &Store{}
// KVStore interface.
type Store struct {
gasMeter types.GasMeter
gasConfig types.GasConfig
gasConfig *types.GasConfig
parent types.KVStore
}

// NewStore returns a reference to a new GasKVStore.
func NewStore(parent types.KVStore, gasMeter types.GasMeter, gasConfig types.GasConfig) *Store {
func NewStore(parent types.KVStore, gasMeter types.GasMeter, gasConfig *types.GasConfig) *Store {
kvs := &Store{
gasMeter: gasMeter,
gasConfig: gasConfig,
Expand Down Expand Up @@ -108,11 +108,11 @@ func (gs *Store) iterator(start, end []byte, ascending bool) types.Iterator {

type gasIterator struct {
gasMeter types.GasMeter
gasConfig types.GasConfig
gasConfig *types.GasConfig
parent types.Iterator
}

func newGasIterator(gasMeter types.GasMeter, gasConfig types.GasConfig, parent types.Iterator) types.Iterator {
func newGasIterator(gasMeter types.GasMeter, gasConfig *types.GasConfig, parent types.Iterator) types.Iterator {
return &gasIterator{
gasMeter: gasMeter,
gasConfig: gasConfig,
Expand Down
8 changes: 4 additions & 4 deletions store/types/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ type GasConfig struct {
}

// KVGasConfig returns a default gas config for KVStores.
func KVGasConfig() GasConfig {
return GasConfig{
func KVGasConfig() *GasConfig {
return &GasConfig{
HasCost: 1000,
DeleteCost: 1000,
ReadCostFlat: 1000,
Expand All @@ -241,8 +241,8 @@ func KVGasConfig() GasConfig {
}

// TransientGasConfig returns a default gas config for TransientStores.
func TransientGasConfig() GasConfig {
return GasConfig{
func TransientGasConfig() *GasConfig {
return &GasConfig{
HasCost: 100,
DeleteCost: 100,
ReadCostFlat: 100,
Expand Down
2 changes: 1 addition & 1 deletion store/types/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestAddUint64Overflow(t *testing.T) {
func TestTransientGasConfig(t *testing.T) {
t.Parallel()
config := TransientGasConfig()
require.Equal(t, config, GasConfig{
require.Equal(t, config, &GasConfig{
HasCost: 100,
DeleteCost: 100,
ReadCostFlat: 100,
Expand Down
15 changes: 8 additions & 7 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type Context struct {
consParams *tmproto.ConsensusParams
eventManager *EventManager
priority int64 // The tx priority, only relevant in CheckTx
kvGasConfig storetypes.GasConfig
transientKVGasConfig storetypes.GasConfig
kvGasConfig *storetypes.GasConfig
transientKVGasConfig *storetypes.GasConfig
}

// Proposed rename, not done to avoid API breakage
Expand All @@ -62,8 +62,8 @@ func (c Context) IsReCheckTx() bool { return c.recheckT
func (c Context) MinGasPrices() DecCoins { return c.minGasPrice }
func (c Context) EventManager() *EventManager { return c.eventManager }
func (c Context) Priority() int64 { return c.priority }
func (c Context) KVGasConfig() storetypes.GasConfig { return c.kvGasConfig }
func (c Context) TransientKVGasConfig() storetypes.GasConfig { return c.transientKVGasConfig }
func (c Context) KVGasConfig() storetypes.GasConfig { return *c.kvGasConfig }
func (c Context) TransientKVGasConfig() storetypes.GasConfig { return *c.transientKVGasConfig }

// clone the header before returning
func (c Context) BlockHeader() tmproto.Header {
Expand Down Expand Up @@ -203,14 +203,14 @@ func (c Context) WithBlockGasMeter(meter GasMeter) Context {
// WithKVGasConfig returns a Context with an updated gas configuration for
// the KVStore
func (c Context) WithKVGasConfig(gasConfig storetypes.GasConfig) Context {
c.kvGasConfig = gasConfig
c.kvGasConfig = &gasConfig
return c
}

// WithTransientKVGasConfig returns a Context with an updated gas configuration for
// the transient KVStore
func (c Context) WithTransientKVGasConfig(gasConfig storetypes.GasConfig) Context {
c.transientKVGasConfig = gasConfig
c.transientKVGasConfig = &gasConfig
return c
}

Expand Down Expand Up @@ -279,7 +279,8 @@ func (c Context) Value(key interface{}) interface{} {
// KVStore fetches a KVStore from the MultiStore.
// NOTE: Uses pointer receiver to save on execution time.
func (c *Context) KVStore(key storetypes.StoreKey) KVStore {
return gaskv.NewStore(c.ms.GetKVStore(key), c.gasMeter, c.kvGasConfig)
kv := c.ms.GetKVStore(key)
return gaskv.NewStore(kv, c.gasMeter, c.kvGasConfig)
}

// TransientStore fetches a TransientStore from the MultiStore.
Expand Down
3 changes: 2 additions & 1 deletion x/group/internal/orm/testsupport.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func NewGasCountingMockContext() *GasCountingMockContext {
}

func (g GasCountingMockContext) KVStore(store sdk.KVStore) sdk.KVStore {
return gaskv.NewStore(store, g.GasMeter, storetypes.KVGasConfig())
gasConfig := storetypes.KVGasConfig()
return gaskv.NewStore(store, g.GasMeter, gasConfig)
}

func (g GasCountingMockContext) GasConsumed() storetypes.Gas {
Expand Down