-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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: Make CacheKV store interleaved iterator and insertion not O(n^2) #10026
Merged
alexanderbez
merged 5 commits into
cosmos:master
from
osmosis-labs:upstream_osmosis_cachekv_improvements
Sep 10, 2021
+76
−115
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b9e6389
V0.42.9 with cache kv improvement (#24)
ValarDragon 4567473
Add Changelog
ValarDragon 3a4d278
Gofmt file
ValarDragon f0aa753
Use internal/conv
ValarDragon 8de7613
Merge branch 'master' into upstream_osmosis_cachekv_improvements
ValarDragon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really a magic number. Its intended to capture that doing a direct scan is actually a good idea for small
n
that you are likely to encounter within execution of a tx.I imagine an optimal
n
here is actually smaller.