From 4f306fca4d0b1e76eef3f31a1356f4bf44102ee5 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Thu, 6 May 2021 20:33:01 +0700 Subject: [PATCH] store/cachekv: reduce allocation with []byte -> string in map keys (#9275) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Uses internal/conv throughout store/kv which shows performance gains. Benchmark for store/cachekv: name old time/op new time/op delta CacheKVStoreIterator500-8 23.4µs ± 1% 23.3µs ± 1% ~ (p=0.095 n=5+5) CacheKVStoreIterator1000-8 46.7µs ± 1% 46.2µs ± 0% -0.96% (p=0.008 n=5+5) CacheKVStoreIterator10000-8 457µs ± 1% 455µs ± 1% ~ (p=1.000 n=5+5) CacheKVStoreIterator50000-8 2.59ms ± 2% 2.47ms ± 1% -4.64% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 7.33ms ± 3% 6.91ms ± 1% -5.75% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 423ns ± 1% 391ns ± 2% -7.41% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 267ns ± 3% 264ns ± 2% ~ (p=0.595 n=5+5) name old alloc/op new alloc/op delta CacheKVStoreIterator500-8 5.18kB ± 0% 5.18kB ± 0% ~ (all equal) CacheKVStoreIterator1000-8 9.29kB ± 0% 9.29kB ± 0% ~ (p=0.079 n=4+5) CacheKVStoreIterator10000-8 85.2kB ± 0% 84.9kB ± 0% -0.30% (p=0.008 n=5+5) CacheKVStoreIterator50000-8 468kB ± 1% 458kB ± 0% -2.17% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 1.16MB ± 1% 1.10MB ± 0% -5.34% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 222B ± 1% 214B ± 0% -3.78% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 51.0B ± 0% 51.0B ± 0% ~ (all equal) name old allocs/op new allocs/op delta CacheKVStoreIterator500-8 13.0 ± 0% 13.0 ± 0% ~ (all equal) CacheKVStoreIterator1000-8 13.0 ± 0% 13.0 ± 0% ~ (all equal) CacheKVStoreIterator10000-8 51.0 ± 0% 43.0 ± 0% -15.69% (p=0.008 n=5+5) CacheKVStoreIterator50000-8 1.22k ± 4% 0.94k ± 1% -23.04% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 6.48k ± 4% 4.85k ± 1% -25.12% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 5.00 ± 0% 4.00 ± 0% -20.00% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) Benchmark for x/auth/keeper: name old time/op new time/op delta AccountMapperGetAccountFound-8 1.27µs ± 3% 1.26µs ± 1% ~ (p=0.270 n=5+5) AccountMapperSetAccount-8 3.53µs ± 0% 3.44µs ± 1% -2.59% (p=0.008 n=5+5) name old alloc/op new alloc/op delta AccountMapperGetAccountFound-8 440B ± 0% 440B ± 0% ~ (all equal) AccountMapperSetAccount-8 2.13kB ± 0% 2.08kB ± 0% -2.31% (p=0.008 n=5+5) name old allocs/op new allocs/op delta AccountMapperGetAccountFound-8 10.0 ± 0% 10.0 ± 0% ~ (all equal) AccountMapperSetAccount-8 42.0 ± 0% 38.0 ± 0% -9.52% (p=0.008 n=5+5) Fixes #9274 --- store/cachekv/store.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/store/cachekv/store.go b/store/cachekv/store.go index 74122dfe3b..48c59d8da8 100644 --- a/store/cachekv/store.go +++ b/store/cachekv/store.go @@ -59,7 +59,7 @@ func (store *Store) Get(key []byte) (value []byte) { types.AssertValidKey(key) - cacheValue, ok := store.cache[string(key)] + cacheValue, ok := store.cache[conv.UnsafeBytesToStr(key)] if !ok { value = store.parent.Get(key) store.setCacheValue(key, value, false, false) @@ -238,12 +238,12 @@ func (store *Store) dirtyItems(start, end []byte) { // Only entrypoint to mutate store.cache. func (store *Store) setCacheValue(key, value []byte, deleted bool, dirty bool) { - store.cache[string(key)] = &cValue{ + store.cache[conv.UnsafeBytesToStr(key)] = &cValue{ value: value, deleted: deleted, dirty: dirty, } if dirty { - store.unsortedCache[string(key)] = struct{}{} + store.unsortedCache[conv.UnsafeBytesToStr(key)] = struct{}{} } }