From f1e6b72d1ca16ada033dcccabe6bb63fabd8e995 Mon Sep 17 00:00:00 2001 From: rizkyikiw42 Date: Fri, 28 Nov 2025 14:06:26 +0800 Subject: [PATCH] core/rawdb: optimize database key construction --- core/rawdb/schema.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index d9140c5fd658..e1eb3f07bcd6 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -191,12 +191,22 @@ func headerKeyPrefix(number uint64) []byte { // headerKey = headerPrefix + num (uint64 big endian) + hash func headerKey(number uint64, hash common.Hash) []byte { - return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...) + key := make([]byte, len(headerPrefix)+8+common.HashLength) + off := copy(key, headerPrefix) + binary.BigEndian.PutUint64(key[off:], number) + off += 8 + copy(key[off:], hash.Bytes()) + return key } // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix func headerHashKey(number uint64) []byte { - return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...) + key := make([]byte, len(headerPrefix)+8+len(headerHashSuffix)) + off := copy(key, headerPrefix) + binary.BigEndian.PutUint64(key[off:], number) + off += 8 + copy(key[off:], headerHashSuffix) + return key } // headerNumberKey = headerNumberPrefix + hash @@ -206,12 +216,22 @@ func headerNumberKey(hash common.Hash) []byte { // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash func blockBodyKey(number uint64, hash common.Hash) []byte { - return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) + key := make([]byte, len(blockBodyPrefix)+8+common.HashLength) + off := copy(key, blockBodyPrefix) + binary.BigEndian.PutUint64(key[off:], number) + off += 8 + copy(key[off:], hash.Bytes()) + return key } // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash func blockReceiptsKey(number uint64, hash common.Hash) []byte { - return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...) + key := make([]byte, len(blockReceiptsPrefix)+8+common.HashLength) + off := copy(key, blockReceiptsPrefix) + binary.BigEndian.PutUint64(key[off:], number) + off += 8 + copy(key[off:], hash.Bytes()) + return key } // txLookupKey = txLookupPrefix + hash