Skip to content
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

Merge cosmos sdk pr for performance.(6811,10339,8811) #2099

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions libs/cosmos-sdk/store/cachekv/memiterator.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cachekv

import (
"container/list"
"errors"

tmkv "github.com/okex/exchain/libs/tendermint/libs/kv"
kv "github.com/okex/exchain/libs/cosmos-sdk/types/kv"
dbm "github.com/okex/exchain/libs/tm-db"
)

Expand All @@ -13,15 +11,16 @@ import (
// Implements Iterator.
type memIterator struct {
start, end []byte
items []*tmkv.Pair
items []*kv.Pair
ascending bool
}

func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator {
itemsInDomain := make([]*tmkv.Pair, 0)
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.(*tmkv.Pair)
item := e.Value
if !dbm.IsKeyInDomain(item.Key, start, end) {
if entered {
break
Expand Down
15 changes: 8 additions & 7 deletions libs/cosmos-sdk/store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cachekv

import (
"bytes"
"container/list"
//"github.com/okex/exchain/libs/cosmos-sdk/types/kv"
"io"
"reflect"
"sort"
Expand All @@ -13,11 +13,12 @@ import (

"github.com/tendermint/go-amino"

tmkv "github.com/okex/exchain/libs/tendermint/libs/kv"
//tmkv "github.com/okex/exchain/libs/tendermint/libs/kv"
dbm "github.com/okex/exchain/libs/tm-db"

"github.com/okex/exchain/libs/cosmos-sdk/store/tracekv"
"github.com/okex/exchain/libs/cosmos-sdk/store/types"
kv "github.com/okex/exchain/libs/cosmos-sdk/types/kv"
)

// If value is nil but deleted is false, it means the parent doesn't have the
Expand All @@ -35,7 +36,7 @@ type Store struct {
dirty map[string]cValue
readList map[string][]byte
unsortedCache map[string]struct{}
sortedCache *list.List // always ascending sorted
sortedCache *kv.List // always ascending sorted
parent types.KVStore

preChangesHandler PreChangesHandler
Expand All @@ -48,7 +49,7 @@ func NewStore(parent types.KVStore) *Store {
dirty: make(map[string]cValue),
readList: make(map[string][]byte),
unsortedCache: make(map[string]struct{}),
sortedCache: list.New(),
sortedCache: kv.NewList(),
parent: parent,
}
}
Expand Down Expand Up @@ -309,13 +310,13 @@ func byteSliceToStr(b []byte) string {

// Constructs a slice of dirty items, to use w/ memIterator.
func (store *Store) dirtyItems(start, end []byte) {
unsorted := make([]*tmkv.Pair, 0)
unsorted := make([]*kv.Pair, 0)

n := len(store.unsortedCache)
for key := range store.unsortedCache {
if dbm.IsKeyInDomain(strToByte(key), start, end) {
cacheValue := store.dirty[key]
unsorted = append(unsorted, &tmkv.Pair{Key: []byte(key), Value: cacheValue.value})
unsorted = append(unsorted, &kv.Pair{Key: []byte(key), Value: cacheValue.value})
}
}

Expand All @@ -335,7 +336,7 @@ func (store *Store) dirtyItems(start, end []byte) {

for e := store.sortedCache.Front(); e != nil && len(unsorted) != 0; {
uitem := unsorted[0]
sitem := e.Value.(*tmkv.Pair)
sitem := e.Value
comp := bytes.Compare(uitem.Key, sitem.Key)
switch comp {
case -1:
Expand Down
15 changes: 6 additions & 9 deletions libs/cosmos-sdk/types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,18 +558,15 @@ func (coins Coins) IsAnyGTE(coinsB Coins) bool {

// removeZeroCoins removes all zero coins from the given coin set in-place.
func removeZeroCoins(coins Coins) Coins {
i, l := 0, len(coins)
for i < l {
if coins[i].IsZero() {
// remove coin
coins = append(coins[:i], coins[i+1:]...)
l--
} else {
i++
result := make([]Coin, 0, len(coins))

for _, coin := range coins {
if !coin.IsZero() {
result = append(result, coin)
}
}

return coins[:i]
return result
}

//-----------------------------------------------------------------------------
Expand Down
52 changes: 52 additions & 0 deletions libs/cosmos-sdk/types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,58 @@ func TestCoinIsZero(t *testing.T) {
require.False(t, res)
}

func TestFilteredZeroCoins(t *testing.T) {
cases := []struct {
name string
input Coins
original string
expected string
}{
{
name: "all greater than zero",
input: Coins{
NewInt64Coin("testa", 1),
NewInt64Coin("testb", 2),
NewInt64Coin("testc", 3),
NewInt64Coin("testd", 4),
NewInt64Coin("teste", 5),
},
original: "1.000000000000000000testa,2.000000000000000000testb,3.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
expected: "1.000000000000000000testa,2.000000000000000000testb,3.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
},
{
name: "zero coin in middle",
input: Coins{
NewInt64Coin("testa", 1),
NewInt64Coin("testb", 2),
NewInt64Coin("testc", 0),
NewInt64Coin("testd", 4),
NewInt64Coin("teste", 5),
},
original: "1.000000000000000000testa,2.000000000000000000testb,0.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
expected: "1.000000000000000000testa,2.000000000000000000testb,4.000000000000000000testd,5.000000000000000000teste",
},
{
name: "zero coin end (unordered)",
input: Coins{
NewInt64Coin("teste", 5),
NewInt64Coin("testc", 3),
NewInt64Coin("testa", 1),
NewInt64Coin("testd", 4),
NewInt64Coin("testb", 0),
},
original: "5.000000000000000000teste,3.000000000000000000testc,1.000000000000000000testa,4.000000000000000000testd,0.000000000000000000testb",
expected: "1.000000000000000000testa,3.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
},
}

for _, tt := range cases {
undertest := NewCoins(tt.input...)
require.Equal(t, tt.expected, undertest.String(), "NewCoins must return expected results")
require.Equal(t, tt.original, tt.input.String(), "input must be unmodified and match original")
}
}

// ----------------------------------------------------------------------------
// Coins tests

Expand Down
15 changes: 6 additions & 9 deletions libs/cosmos-sdk/types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,18 +659,15 @@ func (coins DecCoins) IsAllPositive() bool {
}

func removeZeroDecCoins(coins DecCoins) DecCoins {
i, l := 0, len(coins)
for i < l {
if coins[i].IsZero() {
// remove coin
coins = append(coins[:i], coins[i+1:]...)
l--
} else {
i++
result := make([]DecCoin, 0, len(coins))

for _, coin := range coins {
if !coin.IsZero() {
result = append(result, coin)
}
}

return coins[:i]
return result
}

//-----------------------------------------------------------------------------
Expand Down
51 changes: 51 additions & 0 deletions libs/cosmos-sdk/types/dec_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,57 @@ func TestAddDecCoins(t *testing.T) {
}
}

func TestFilteredZeroDecCoins(t *testing.T) {
cases := []struct {
name string
input DecCoins
original string
expected string
}{
{
name: "all greater than zero",
input: DecCoins{
{"testa", NewDec(1)},
{"testb", NewDec(2)},
{"testc", NewDec(3)},
{"testd", NewDec(4)},
{"teste", NewDec(5)},
},
original: "1.000000000000000000testa,2.000000000000000000testb,3.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
expected: "1.000000000000000000testa,2.000000000000000000testb,3.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
},
{
name: "zero coin in middle",
input: DecCoins{
{"testa", NewDec(1)},
{"testb", NewDec(2)},
{"testc", NewDec(0)},
{"testd", NewDec(4)},
{"teste", NewDec(5)},
},
original: "1.000000000000000000testa,2.000000000000000000testb,0.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
expected: "1.000000000000000000testa,2.000000000000000000testb,4.000000000000000000testd,5.000000000000000000teste",
},
{
name: "zero coin end (unordered)",
input: DecCoins{
{"teste", NewDec(5)},
{"testc", NewDec(3)},
{"testa", NewDec(1)},
{"testd", NewDec(4)},
{"testb", NewDec(0)},
},
original: "5.000000000000000000teste,3.000000000000000000testc,1.000000000000000000testa,4.000000000000000000testd,0.000000000000000000testb",
expected: "1.000000000000000000testa,3.000000000000000000testc,4.000000000000000000testd,5.000000000000000000teste",
},
}

for _, tt := range cases {
undertest := NewDecCoins(tt.input...)
require.Equal(t, tt.expected, undertest.String(), "NewDecCoins must return expected results")
require.Equal(t, tt.original, tt.input.String(), "input must be unmodified and match original")
}
}
func TestIsValid(t *testing.T) {
tests := []struct {
coin DecCoin
Expand Down