-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Make CacheKV store interleaved iterator and insertion not O(n^2…
…) (backport #10026) (#10114) * perf: Make CacheKV store interleaved iterator and insertion not O(n^2) (#10026) (cherry picked from commit 28bf2c1) # Conflicts: # CHANGELOG.md # store/cachekv/store.go * Fix merge conflict * fix changelog Co-authored-by: Dev Ojha <[email protected]> Co-authored-by: ValarDragon <[email protected]> Co-authored-by: Robert Zaremba <[email protected]>
- Loading branch information
1 parent
2e871eb
commit a7e5071
Showing
3 changed files
with
82 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,50 @@ | ||
package cachekv | ||
|
||
import ( | ||
"errors" | ||
|
||
dbm "github.com/tendermint/tm-db" | ||
|
||
"github.com/cosmos/cosmos-sdk/types/kv" | ||
"github.com/cosmos/cosmos-sdk/store/types" | ||
) | ||
|
||
// Iterates over iterKVCache items. | ||
// if key is nil, means it was deleted. | ||
// Implements Iterator. | ||
type memIterator struct { | ||
start, end []byte | ||
items []*kv.Pair | ||
ascending bool | ||
} | ||
|
||
func newMemIterator(start, end []byte, items *kv.List, ascending bool) *memIterator { | ||
itemsInDomain := make([]*kv.Pair, 0, items.Len()) | ||
|
||
var entered bool | ||
|
||
for e := items.Front(); e != nil; e = e.Next() { | ||
item := e.Value | ||
if !dbm.IsKeyInDomain(item.Key, start, end) { | ||
if entered { | ||
break | ||
} | ||
|
||
continue | ||
} | ||
|
||
itemsInDomain = append(itemsInDomain, item) | ||
entered = true | ||
} | ||
types.Iterator | ||
|
||
return &memIterator{ | ||
start: start, | ||
end: end, | ||
items: itemsInDomain, | ||
ascending: ascending, | ||
} | ||
deleted map[string]struct{} | ||
} | ||
|
||
func (mi *memIterator) Domain() ([]byte, []byte) { | ||
return mi.start, mi.end | ||
} | ||
func newMemIterator(start, end []byte, items *dbm.MemDB, deleted map[string]struct{}, ascending bool) *memIterator { | ||
var iter types.Iterator | ||
var err error | ||
|
||
func (mi *memIterator) Valid() bool { | ||
return len(mi.items) > 0 | ||
} | ||
if ascending { | ||
iter, err = items.Iterator(start, end) | ||
} else { | ||
iter, err = items.ReverseIterator(start, end) | ||
} | ||
|
||
func (mi *memIterator) assertValid() { | ||
if err := mi.Error(); err != nil { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (mi *memIterator) Next() { | ||
mi.assertValid() | ||
|
||
if mi.ascending { | ||
mi.items = mi.items[1:] | ||
} else { | ||
mi.items = mi.items[:len(mi.items)-1] | ||
newDeleted := make(map[string]struct{}) | ||
for k, v := range deleted { | ||
newDeleted[k] = v | ||
} | ||
} | ||
|
||
func (mi *memIterator) Key() []byte { | ||
mi.assertValid() | ||
return &memIterator{ | ||
Iterator: iter, | ||
|
||
if mi.ascending { | ||
return mi.items[0].Key | ||
deleted: newDeleted, | ||
} | ||
|
||
return mi.items[len(mi.items)-1].Key | ||
} | ||
|
||
func (mi *memIterator) Value() []byte { | ||
mi.assertValid() | ||
|
||
if mi.ascending { | ||
return mi.items[0].Value | ||
} | ||
|
||
return mi.items[len(mi.items)-1].Value | ||
} | ||
|
||
func (mi *memIterator) Close() error { | ||
mi.start = nil | ||
mi.end = nil | ||
mi.items = nil | ||
|
||
return nil | ||
} | ||
|
||
// Error returns an error if the memIterator is invalid defined by the Valid | ||
// method. | ||
func (mi *memIterator) Error() error { | ||
if !mi.Valid() { | ||
return errors.New("invalid memIterator") | ||
key := mi.Iterator.Key() | ||
if _, ok := mi.deleted[string(key)]; ok { | ||
return nil | ||
} | ||
|
||
return nil | ||
return mi.Iterator.Value() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters