Skip to content

Get and set account is kind of time consuming #2652

Closed
@yutianwu

Description

hi, recently, I have done some stress testing for our Dapp, I found that besides performance of secp256, encode and decode of account is also time consuming.

I have done a benchmark test in my laptop(MacBook Pro (15-inch, 2018), 2.2 GHz Intel Core i7).

GetAccount:

func BenchmarkAccountMapperGetAccountFound(b *testing.B) {
	ms, capKey, _ := setupMultiStore()
	cdc := codec.New()
	RegisterBaseAccount(cdc)

	// make context and mapper
	ctx := sdk.NewContext(ms, abci.Header{}, false, log.NewNopLogger())
	mapper := NewAccountKeeper(cdc, capKey, ProtoBaseAccount)

	// assumes b.N < 2**24
	for i := 0; i < b.N; i++ {
		arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)}
		addr := sdk.AccAddress(arr)
		acc := mapper.NewAccountWithAddress(ctx, addr)
		acc.SetCoins(sdk.Coins{sdk.NewCoin("LTC", sdk.NewInt(1000)),
			sdk.NewCoin("BTC", sdk.NewInt(1000)),
			sdk.NewCoin("ETH", sdk.NewInt(1000)),
			sdk.NewCoin("XRP", sdk.NewInt(1000)),
			sdk.NewCoin("BCH", sdk.NewInt(1000)),
			sdk.NewCoin("EOS", sdk.NewInt(1000))})
		mapper.SetAccount(ctx, acc)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)}
		mapper.GetAccount(ctx, sdk.AccAddress(arr))
	}
}

/*
goos: darwin
goarch: amd64
pkg: github.com/cosmos/cosmos-sdk/x/auth
BenchmarkAccountMapperGetAccountFound-12    	  100000	     16937 ns/op
PASS
*/

SetAccount

func BenchmarkAccountMapperSetAccount(b *testing.B) {
	ms, capKey, _ := setupMultiStore()
	cdc := codec.New()
	RegisterBaseAccount(cdc)

	// make context and mapper
	ctx := sdk.NewContext(ms, abci.Header{}, false, log.NewNopLogger())
	mapper := NewAccountKeeper(cdc, capKey, ProtoBaseAccount)

	b.ResetTimer()
	// assumes b.N < 2**24
	for i := 0; i < b.N; i++ {
		arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)}
		addr := sdk.AccAddress(arr)
		acc := mapper.NewAccountWithAddress(ctx, addr)
		acc.SetCoins(sdk.Coins{sdk.NewCoin("LTC", sdk.NewInt(1000)),
			sdk.NewCoin("BTC", sdk.NewInt(1000)),
			sdk.NewCoin("ETH", sdk.NewInt(1000)),
			sdk.NewCoin("XRP", sdk.NewInt(1000)),
			sdk.NewCoin("BCH", sdk.NewInt(1000)),
			sdk.NewCoin("EOS", sdk.NewInt(1000))})
		mapper.SetAccount(ctx, acc)
	}
}

/*
goos: darwin
goarch: amd64
pkg: github.com/cosmos/cosmos-sdk/x/auth
BenchmarkAccountMapperSetAccount-12    	  100000	     22989 ns/op
PASS
*/

Assume that there is 4000tps in our dapp, set and get account will cost (16937 + 22989) * 4000 = 159.7 ms. The result is kind of unacceptable.

IMHO, I think we can cache the deserialized result so we may don't need to deserialize accounts again and again. It will save a half of the time.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions