less alloc at hash state key transformation #2441 (#2441)

This commit is contained in:
Alex Sharov 2021-07-24 18:14:10 +07:00 committed by GitHub
parent 6bf54c951a
commit 12c578b84d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 7 deletions

View File

@ -79,9 +79,10 @@ func GenerateCompositeTrieKey(addressHash common.Hash, seckey common.Hash) []byt
// AddrHash + incarnation + KeyHash
// For contract storage
func GenerateCompositeStorageKey(addressHash common.Hash, incarnation uint64, seckey common.Hash) []byte {
compositeKey := make([]byte, 0, common.HashLength+common.IncarnationLength+common.HashLength)
compositeKey = append(compositeKey, GenerateStoragePrefix(addressHash[:], incarnation)...)
compositeKey = append(compositeKey, seckey[:]...)
compositeKey := make([]byte, common.HashLength+common.IncarnationLength+common.HashLength)
copy(compositeKey, addressHash[:])
binary.BigEndian.PutUint64(compositeKey[common.HashLength:], incarnation)
copy(compositeKey[common.HashLength+common.IncarnationLength:], seckey[:])
return compositeKey
}

View File

@ -3,6 +3,7 @@ package stagedsync
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"os"
@ -211,16 +212,16 @@ func transformPlainStateKey(key []byte) ([]byte, error) {
return hash[:], err
case common.AddressLength + common.IncarnationLength + common.HashLength:
// storage
address, incarnation, key := dbutils.PlainParseCompositeStorageKey(key)
addrHash, err := common.HashData(address[:])
addrHash, err := common.HashData(key[:common.AddressLength])
if err != nil {
return nil, err
}
secKey, err := common.HashData(key[:])
inc := binary.BigEndian.Uint64(key[common.AddressLength:])
secKey, err := common.HashData(key[common.AddressLength+common.IncarnationLength:])
if err != nil {
return nil, err
}
compositeKey := dbutils.GenerateCompositeStorageKey(addrHash, incarnation, secKey)
compositeKey := dbutils.GenerateCompositeStorageKey(addrHash, inc, secKey)
return compositeKey, nil
default:
// no other keys are supported