triedb/pathdb: preallocate slices in decodeRestartTrailer (#33715)
Some checks failed
/ Linux Build (arm) (push) Has been cancelled
/ Keeper Build (push) Has been cancelled
/ Windows Build (push) Has been cancelled
/ Linux Build (push) Has been cancelled
/ Docker Image (push) Has been cancelled

Preallocate capacity for `keyOffsets` and `valOffsets` slices in
`decodeRestartTrailer` since the exact size (`nRestarts`) is known
upfront.

---------

Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
This commit is contained in:
alex017 2026-01-30 14:14:15 +01:00 committed by GitHub
parent 845009f684
commit cb97c48cb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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))
}