mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-08 07:58:40 +00:00
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 <garyrong0905@gmail.com>
This commit is contained in:
parent
845009f684
commit
cb97c48cb6
1 changed files with 4 additions and 4 deletions
|
|
@ -391,10 +391,6 @@ func decodeKeyEntry(keySection []byte, offset int) (uint64, uint64, []byte, int,
|
||||||
|
|
||||||
// decodeRestartTrailer resolves all the offsets recorded at the trailer.
|
// decodeRestartTrailer resolves all the offsets recorded at the trailer.
|
||||||
func decodeRestartTrailer(keySection []byte) ([]uint32, []uint32, int, error) {
|
func decodeRestartTrailer(keySection []byte) ([]uint32, []uint32, int, error) {
|
||||||
var (
|
|
||||||
keyOffsets []uint32
|
|
||||||
valOffsets []uint32
|
|
||||||
)
|
|
||||||
// Decode the number of restart section
|
// Decode the number of restart section
|
||||||
if len(keySection) < 4 {
|
if len(keySection) < 4 {
|
||||||
return nil, nil, 0, fmt.Errorf("key section too short, size: %d", len(keySection))
|
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:])
|
nRestarts := binary.BigEndian.Uint32(keySection[len(keySection)-4:])
|
||||||
|
|
||||||
// Decode the trailer
|
// Decode the trailer
|
||||||
|
var (
|
||||||
|
keyOffsets = make([]uint32, 0, int(nRestarts))
|
||||||
|
valOffsets = make([]uint32, 0, int(nRestarts))
|
||||||
|
)
|
||||||
if len(keySection) < int(8*nRestarts)+4 {
|
if len(keySection) < int(8*nRestarts)+4 {
|
||||||
return nil, nil, 0, fmt.Errorf("key section too short, restarts: %d, size: %d", nRestarts, len(keySection))
|
return nil, nil, 0, fmt.Errorf("key section too short, restarts: %d, size: %d", nRestarts, len(keySection))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue