core/rawdb: reduce allocations in *key

This commit is contained in:
MariusVanDerWijden 2025-10-23 12:25:35 +02:00
parent 116c916753
commit 09e36d729f

View file

@ -226,7 +226,8 @@ func accountSnapshotKey(hash common.Hash) []byte {
// storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
func storageSnapshotKey(accountHash, storageHash common.Hash) []byte { func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
buf := make([]byte, len(SnapshotStoragePrefix)+common.HashLength+common.HashLength) //len(SnapshotStoragePrefix) == 1
buf := make([]byte, 1+common.HashLength+common.HashLength)
n := copy(buf, SnapshotStoragePrefix) n := copy(buf, SnapshotStoragePrefix)
n += copy(buf[n:], accountHash.Bytes()) n += copy(buf[n:], accountHash.Bytes())
copy(buf[n:], storageHash.Bytes()) copy(buf[n:], storageHash.Bytes())
@ -352,34 +353,32 @@ func IsStorageTrieNode(key []byte) bool {
return ok return ok
} }
// filterMapRowKey = filterMapRowPrefix + mapRowIndex (uint64 big endian)
func filterMapRowKey(mapRowIndex uint64, base bool) []byte { func filterMapRowKey(mapRowIndex uint64, base bool) []byte {
extLen := 8 // len(filterMapRowPrefix) + extLen
if base { key := make([]byte, 4+9)
extLen = 9 copy(key[:4], filterMapRowPrefix)
binary.BigEndian.PutUint64(key[4:4+8], mapRowIndex)
if !base {
return key[0 : 4+8]
} }
l := len(filterMapRowPrefix)
key := make([]byte, l+extLen)
copy(key[:l], filterMapRowPrefix)
binary.BigEndian.PutUint64(key[l:l+8], mapRowIndex)
return key return key
} }
// filterMapLastBlockKey = filterMapLastBlockPrefix + mapIndex (uint32 big endian) // filterMapLastBlockKey = filterMapLastBlockPrefix + mapIndex (uint32 big endian)
func filterMapLastBlockKey(mapIndex uint32) []byte { func filterMapLastBlockKey(mapIndex uint32) []byte {
l := len(filterMapLastBlockPrefix) // len(filterMapLastBlockPrefix) + len(uint32)
key := make([]byte, l+4) key := make([]byte, 4+4)
copy(key[:l], filterMapLastBlockPrefix) copy(key[:4], filterMapLastBlockPrefix)
binary.BigEndian.PutUint32(key[l:], mapIndex) binary.BigEndian.PutUint32(key[4:], mapIndex)
return key return key
} }
// filterMapBlockLVKey = filterMapBlockLVPrefix + num (uint64 big endian) // filterMapBlockLVKey = filterMapBlockLVPrefix + num (uint64 big endian)
func filterMapBlockLVKey(number uint64) []byte { func filterMapBlockLVKey(number uint64) []byte {
l := len(filterMapBlockLVPrefix) //len(filterMapBlockLVPrefix) + len(uint64)
key := make([]byte, l+8) key := make([]byte, 4+8)
copy(key[:l], filterMapBlockLVPrefix) copy(key[:4], filterMapBlockLVPrefix)
binary.BigEndian.PutUint64(key[l:], number) binary.BigEndian.PutUint64(key[4:], number)
return key return key
} }
@ -390,8 +389,8 @@ 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 {
totalLen := len(StateHistoryStorageMetadataPrefix) + 2*common.HashLength // len(StateHistoryStorageMetadataPrefix) == 2
out := make([]byte, totalLen) out := make([]byte, 2+2*common.HashLength)
off := 0 off := 0
off += copy(out[off:], StateHistoryStorageMetadataPrefix) off += copy(out[off:], StateHistoryStorageMetadataPrefix)
@ -416,8 +415,8 @@ func trienodeHistoryIndexKey(addressHash common.Hash, path []byte) []byte {
// accountHistoryIndexBlockKey = StateHistoryAccountBlockPrefix + addressHash + blockID // accountHistoryIndexBlockKey = StateHistoryAccountBlockPrefix + addressHash + blockID
func accountHistoryIndexBlockKey(addressHash common.Hash, blockID uint32) []byte { func accountHistoryIndexBlockKey(addressHash common.Hash, blockID uint32) []byte {
totalLen := len(StateHistoryAccountBlockPrefix) + common.HashLength + 4 // len(StateHistoryAccountBlockPrefix) + len(common.Hash) + len(uint32)
out := make([]byte, totalLen) out := make([]byte, 3+common.HashLength+4)
off := 0 off := 0
off += copy(out[off:], StateHistoryAccountBlockPrefix) off += copy(out[off:], StateHistoryAccountBlockPrefix)
@ -429,8 +428,8 @@ func accountHistoryIndexBlockKey(addressHash common.Hash, blockID uint32) []byte
// storageHistoryIndexBlockKey = StateHistoryStorageBlockPrefix + addressHash + storageHash + blockID // storageHistoryIndexBlockKey = StateHistoryStorageBlockPrefix + addressHash + storageHash + blockID
func storageHistoryIndexBlockKey(addressHash common.Hash, storageHash common.Hash, blockID uint32) []byte { func storageHistoryIndexBlockKey(addressHash common.Hash, storageHash common.Hash, blockID uint32) []byte {
totalLen := len(StateHistoryStorageBlockPrefix) + 2*common.HashLength + 4 // len(StateHistoryStorageBlockPrefix) + 2*common.HashLength + len(uint32)
out := make([]byte, totalLen) out := make([]byte, 3+2*common.HashLength+4)
off := 0 off := 0
off += copy(out[off:], StateHistoryStorageBlockPrefix) off += copy(out[off:], StateHistoryStorageBlockPrefix)