core/rawdb: refactor storageHistoryIndexKey and accountHistoryIndexBlockKey to prealloc + copy

This commit is contained in:
cuiweixie 2025-08-15 15:25:43 +08:00
parent c0f04bd2d9
commit a8291445f7

View file

@ -384,14 +384,31 @@ func accountHistoryIndexKey(addressHash common.Hash) []byte {
// storageHistoryIndexKey = StateHistoryStorageMetadataPrefix + addressHash + storageHash // storageHistoryIndexKey = StateHistoryStorageMetadataPrefix + addressHash + storageHash
func storageHistoryIndexKey(addressHash common.Hash, storageHash common.Hash) []byte { func storageHistoryIndexKey(addressHash common.Hash, storageHash common.Hash) []byte {
return append(append(StateHistoryStorageMetadataPrefix, addressHash.Bytes()...), storageHash.Bytes()...) totalLen := len(StateHistoryStorageMetadataPrefix) + len(addressHash) + len(storageHash)
out := make([]byte, totalLen)
off := 0
off += copy(out[off:], StateHistoryStorageMetadataPrefix)
off += copy(out[off:], addressHash.Bytes())
copy(out[off:], storageHash.Bytes())
return out
} }
// accountHistoryIndexBlockKey = StateHistoryAccountBlockPrefix + addressHash + blockID // accountHistoryIndexBlockKey = StateHistoryAccountBlockPrefix + addressHash + blockID
func accountHistoryIndexBlockKey(addressHash common.Hash, blockID uint32) []byte { func accountHistoryIndexBlockKey(addressHash common.Hash, blockID uint32) []byte {
var buf [4]byte var buf4 [4]byte
binary.BigEndian.PutUint32(buf[:], blockID) binary.BigEndian.PutUint32(buf4[:], blockID)
return append(append(StateHistoryAccountBlockPrefix, addressHash.Bytes()...), buf[:]...)
totalLen := len(StateHistoryAccountBlockPrefix) + len(addressHash) + 4
out := make([]byte, totalLen)
off := 0
off += copy(out[off:], StateHistoryAccountBlockPrefix)
off += copy(out[off:], addressHash.Bytes())
copy(out[off:], buf4[:])
return out
} }
// storageHistoryIndexBlockKey = StateHistoryStorageBlockPrefix + addressHash + storageHash + blockID // storageHistoryIndexBlockKey = StateHistoryStorageBlockPrefix + addressHash + storageHash + blockID