diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index 72f9bd34ec..194c28afce 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -396,9 +396,19 @@ func accountHistoryIndexBlockKey(addressHash common.Hash, blockID uint32) []byte // storageHistoryIndexBlockKey = StateHistoryStorageBlockPrefix + addressHash + storageHash + blockID func storageHistoryIndexBlockKey(addressHash common.Hash, storageHash common.Hash, blockID uint32) []byte { - var buf [4]byte - binary.BigEndian.PutUint32(buf[:], blockID) - return append(append(append(StateHistoryStorageBlockPrefix, addressHash.Bytes()...), storageHash.Bytes()...), buf[:]...) + var buf4 [4]byte + binary.BigEndian.PutUint32(buf4[:], blockID) + + totalLen := len(StateHistoryStorageBlockPrefix) + len(addressHash) + len(storageHash) + 4 + out := make([]byte, totalLen) + + off := 0 + off += copy(out[off:], StateHistoryStorageBlockPrefix) + off += copy(out[off:], addressHash.Bytes()) + off += copy(out[off:], storageHash.Bytes()) + copy(out[off:], buf4[:]) + + return out } // transitionStateKey = transitionStatusKey + hash diff --git a/core/rawdb/schema_test.go b/core/rawdb/schema_test.go new file mode 100644 index 0000000000..e544f603f4 --- /dev/null +++ b/core/rawdb/schema_test.go @@ -0,0 +1,22 @@ +package rawdb + +import ( + "math/rand" + "testing" + + "github.com/ethereum/go-ethereum/common" +) + +var sink []byte + +func BenchmarkStorageHistoryIndexBlockKey(b *testing.B) { + var h1, h2 common.Hash + for i := range h1 { + h1[i] = byte(rand.Intn(256)) + h2[i] = byte(rand.Intn(256)) + } + b.ResetTimer() + for i := 0; i < b.N; i++ { + sink = storageHistoryIndexBlockKey(h1, h2, uint32(i)) + } +}