Skip to content

Commit 2f4dbb4

Browse files
core/rawdb: allocate database keys with explicit size to avoid slice growth (ethereum#27772)
1 parent 4abc412 commit 2f4dbb4

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

core/rawdb/schema.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ func accountSnapshotKey(hash common.Hash) []byte {
195195

196196
// storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
197197
func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
198-
return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
198+
buf := make([]byte, len(SnapshotStoragePrefix)+common.HashLength+common.HashLength)
199+
n := copy(buf, SnapshotStoragePrefix)
200+
n += copy(buf[n:], accountHash.Bytes())
201+
copy(buf[n:], storageHash.Bytes())
202+
return buf
199203
}
200204

201205
// storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
@@ -259,7 +263,11 @@ func accountTrieNodeKey(path []byte) []byte {
259263

260264
// storageTrieNodeKey = trieNodeStoragePrefix + accountHash + nodePath.
261265
func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte {
262-
return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...)
266+
buf := make([]byte, len(trieNodeStoragePrefix)+common.HashLength+len(path))
267+
n := copy(buf, trieNodeStoragePrefix)
268+
n += copy(buf[n:], accountHash.Bytes())
269+
copy(buf[n:], path)
270+
return buf
263271
}
264272

265273
// IsLegacyTrieNode reports whether a provided database entry is a legacy trie

0 commit comments

Comments
 (0)