From cb97c48cb6a84703dcc269fbed2cd8b61c464a2d Mon Sep 17 00:00:00 2001 From: alex017 Date: Fri, 30 Jan 2026 14:14:15 +0100 Subject: [PATCH] triedb/pathdb: preallocate slices in decodeRestartTrailer (#33715) Preallocate capacity for `keyOffsets` and `valOffsets` slices in `decodeRestartTrailer` since the exact size (`nRestarts`) is known upfront. --------- Co-authored-by: rjl493456442 --- triedb/pathdb/history_trienode.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/triedb/pathdb/history_trienode.go b/triedb/pathdb/history_trienode.go index 05a43808c2..11d6112806 100644 --- a/triedb/pathdb/history_trienode.go +++ b/triedb/pathdb/history_trienode.go @@ -391,10 +391,6 @@ func decodeKeyEntry(keySection []byte, offset int) (uint64, uint64, []byte, int, // decodeRestartTrailer resolves all the offsets recorded at the trailer. func decodeRestartTrailer(keySection []byte) ([]uint32, []uint32, int, error) { - var ( - keyOffsets []uint32 - valOffsets []uint32 - ) // Decode the number of restart section if len(keySection) < 4 { return nil, nil, 0, fmt.Errorf("key section too short, size: %d", len(keySection)) @@ -402,6 +398,10 @@ func decodeRestartTrailer(keySection []byte) ([]uint32, []uint32, int, error) { nRestarts := binary.BigEndian.Uint32(keySection[len(keySection)-4:]) // Decode the trailer + var ( + keyOffsets = make([]uint32, 0, int(nRestarts)) + valOffsets = make([]uint32, 0, int(nRestarts)) + ) if len(keySection) < int(8*nRestarts)+4 { return nil, nil, 0, fmt.Errorf("key section too short, restarts: %d, size: %d", nRestarts, len(keySection)) }