From 404f3a27d88306173bc77cd44221705213496a47 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Tue, 25 Jul 2023 14:03:32 +0200 Subject: [PATCH 1/2] core/rawdb: reduce allocations --- core/rawdb/schema.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index 18722ed5d4cb..5f3c520d0171 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -188,7 +188,11 @@ func accountSnapshotKey(hash common.Hash) []byte { // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash func storageSnapshotKey(accountHash, storageHash common.Hash) []byte { - return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...) + res := make([]byte, 1+len(accountHash)*2) + res[0] = SnapshotStoragePrefix[0] + copy(res[1:], accountHash[:]) + copy(res[1+len(accountHash):], storageHash[:]) + return res } // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash @@ -247,7 +251,11 @@ func accountTrieNodeKey(path []byte) []byte { // storageTrieNodeKey = trieNodeStoragePrefix + accountHash + nodePath. func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte { - return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...) + res := make([]byte, 1+len(accountHash)+len(path)) + res[0] = trieNodeStoragePrefix[0] + copy(res[1:], accountHash[:]) + copy(res[1+len(accountHash):], path) + return res } // IsLegacyTrieNode reports whether a provided database entry is a legacy trie From 35537b4d19e7a3ec23a0b1eec8380d9ec1f40e9f Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Thu, 27 Jul 2023 13:31:46 +0200 Subject: [PATCH 2/2] core/rawdb: add garys suggestion --- core/rawdb/schema.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index 5f3c520d0171..65c9fe8ac6ae 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -188,11 +188,11 @@ func accountSnapshotKey(hash common.Hash) []byte { // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash func storageSnapshotKey(accountHash, storageHash common.Hash) []byte { - res := make([]byte, 1+len(accountHash)*2) - res[0] = SnapshotStoragePrefix[0] - copy(res[1:], accountHash[:]) - copy(res[1+len(accountHash):], storageHash[:]) - return res + buf := make([]byte, len(SnapshotStoragePrefix)+common.HashLength+common.HashLength) + n := copy(buf, SnapshotStoragePrefix) + n += copy(buf[n:], accountHash.Bytes()) + copy(buf[n:], storageHash.Bytes()) + return buf } // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash @@ -251,11 +251,11 @@ func accountTrieNodeKey(path []byte) []byte { // storageTrieNodeKey = trieNodeStoragePrefix + accountHash + nodePath. func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte { - res := make([]byte, 1+len(accountHash)+len(path)) - res[0] = trieNodeStoragePrefix[0] - copy(res[1:], accountHash[:]) - copy(res[1+len(accountHash):], path) - return res + buf := make([]byte, len(trieNodeStoragePrefix)+common.HashLength+len(path)) + n := copy(buf, trieNodeStoragePrefix) + n += copy(buf[n:], accountHash.Bytes()) + copy(buf[n:], path) + return buf } // IsLegacyTrieNode reports whether a provided database entry is a legacy trie