Skip to content

Commit

Permalink
perf(x/group/internal/orm): move expensive prefix.NewStore calls to c…
Browse files Browse the repository at this point in the history
…lose usage (#18286)
  • Loading branch information
odeke-em authored Oct 28, 2023
1 parent 7ad7f47 commit 32151e5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 4 additions & 0 deletions x/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Improvements

* [18286](https://github.com/cosmos/cosmos-sdk/pull/18286) Move prefix store creation down after error checks.

### Features

### API Breaking Changes
9 changes: 6 additions & 3 deletions x/group/internal/orm/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,25 @@ func newIndex(tb Indexable, prefix byte, indexer *Indexer, indexerF IndexerFunc,

// Has checks if a key exists. Returns an error on nil key.
func (i MultiKeyIndex) Has(store types.KVStore, key interface{}) (bool, error) {
pStore := prefix.NewStore(store, []byte{i.prefix})
encodedKey, err := keyPartBytes(key, false)
if err != nil {
return false, err
}

pStore := prefix.NewStore(store, []byte{i.prefix})
it := pStore.Iterator(PrefixRange(encodedKey))
defer it.Close()
return it.Valid(), nil
}

// Get returns a result iterator for the searchKey. Parameters must not be nil.
func (i MultiKeyIndex) Get(store types.KVStore, searchKey interface{}) (Iterator, error) {
pStore := prefix.NewStore(store, []byte{i.prefix})
encodedKey, err := keyPartBytes(searchKey, false)
if err != nil {
return nil, err
}

pStore := prefix.NewStore(store, []byte{i.prefix})
it := pStore.Iterator(PrefixRange(encodedKey))
return indexIterator{store: store, it: it, rowGetter: i.rowGetter, indexKey: i.indexKey}, nil
}
Expand All @@ -98,7 +100,6 @@ func (i MultiKeyIndex) Get(store types.KVStore, searchKey interface{}) (Iterator
// starting from pageRequest.Key if provided.
// The pageRequest.Key is the rowID while searchKey is a MultiKeyIndex key.
func (i MultiKeyIndex) GetPaginated(store types.KVStore, searchKey interface{}, pageRequest *query.PageRequest) (Iterator, error) {
pStore := prefix.NewStore(store, []byte{i.prefix})
encodedKey, err := keyPartBytes(searchKey, false)
if err != nil {
return nil, err
Expand All @@ -112,6 +113,8 @@ func (i MultiKeyIndex) GetPaginated(store types.KVStore, searchKey interface{},
return nil, err
}
}

pStore := prefix.NewStore(store, []byte{i.prefix})
it := pStore.Iterator(start, end)
return indexIterator{store: store, it: it, rowGetter: i.rowGetter, indexKey: i.indexKey}, nil
}
Expand Down
7 changes: 3 additions & 4 deletions x/group/internal/orm/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ func (a table) Set(store types.KVStore, rowID RowID, newValue proto.Message) err
return err
}

pStore := prefix.NewStore(store, a.prefix[:])

var oldValue proto.Message
if a.Has(store, rowID) {
oldValue = reflect.New(a.model).Interface().(proto.Message)
Expand All @@ -129,6 +127,7 @@ func (a table) Set(store types.KVStore, rowID RowID, newValue proto.Message) err
return errorsmod.Wrapf(err, "failed to serialize %T", newValue)
}

pStore := prefix.NewStore(store, a.prefix[:])
pStore.Set(rowID, newValueEncoded)
for i, itc := range a.afterSet {
if err := itc(store, rowID, newValue, oldValue); err != nil {
Expand All @@ -154,12 +153,12 @@ func assertValid(obj proto.Message) error {
// Delete iterates through the registered callbacks that remove secondary index
// keys.
func (a table) Delete(store types.KVStore, rowID RowID) error {
pStore := prefix.NewStore(store, a.prefix[:])

oldValue := reflect.New(a.model).Interface().(proto.Message)
if err := a.GetOne(store, rowID, oldValue); err != nil {
return errorsmod.Wrap(err, "load old value")
}

pStore := prefix.NewStore(store, a.prefix[:])
pStore.Delete(rowID)

for i, itc := range a.afterDelete {
Expand Down

0 comments on commit 32151e5

Please sign in to comment.