Skip to content

Commit

Permalink
perf: Make CacheKV store interleaved iterator and insertion not O(n^2…
Browse files Browse the repository at this point in the history
…) (backport cosmos#10026) (cosmos#10167)

* perf: Make CacheKV store interleaved iterator and insertion not O(n^2) (cosmos#10026)

(cherry picked from commit 28bf2c1)

* fix changelog conflict

* Update store.go

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
Co-authored-by: Robert Zaremba <robert@zaremba.ch>
  • Loading branch information
3 people authored and Eengineer1 committed Aug 25, 2022
1 parent 9e7fbf5 commit 23ff227
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ Ref: https://keepachangelog.com/en/1.0.0/
## [Unreleased]

### Improvements

* (types) [\#10021](https://github.com/cosmos/cosmos-sdk/pull/10021) Speedup coins.AmountOf(), by removing many intermittent regex calls.
* (store) [\#10026](https://github.com/cosmos/cosmos-sdk/pull/10026) Improve CacheKVStore datastructures / algorithms, to no longer take O(N^2) time when interleaving iterators and insertions.

### Bug Fixes

Expand Down
10 changes: 10 additions & 0 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,17 @@ func findEndIndex(strL []string, endQ string) int {
} else { // midStrL > startQ
right = mid - 1
}
} else {
// else do a linear scan to determine if the unsorted pairs are in the pool.
for key := range store.unsortedCache {
if dbm.IsKeyInDomain(conv.UnsafeStrToBytes(key), start, end) {
cacheValue := store.cache[key]
unsorted = append(unsorted, &kv.Pair{Key: []byte(key), Value: cacheValue.value})
}
}
}
store.clearUnsortedCacheSubset(unsorted)
}

// Binary search failed, now let's find a value less than endQ.
for i := right; i >= 0; i-- {
Expand Down

0 comments on commit 23ff227

Please sign in to comment.