-
Notifications
You must be signed in to change notification settings - Fork 19
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…
…) (#10026)
- Loading branch information
1 parent
3d3bc7c
commit 28bf2c1
Showing
3 changed files
with
76 additions
and
115 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