Skip to content

Commit

Permalink
Fix spurious allocation in hasher.Merkleize
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Jul 10, 2024
1 parent eac385e commit 519c6f5
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions hasher.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package ssz

import (
"encoding/binary"
"fmt"
"hash"
"math/bits"
"sync"

"encoding/binary"

"github.com/minio/sha256-simd"
)

Expand Down Expand Up @@ -263,6 +262,16 @@ func (h *Hasher) Index() int {

// Merkleize is used to merkleize the last group of the hasher
func (h *Hasher) Merkleize(indx int) {
// merkleizeImpl will expand the `input` by 32 bytes if some hashing depth
// hits an odd chunk length. But if we're at the end of `h.buf` already,
// appending to `input` will allocate a new buffer, *not* expand `h.buf`,
// so the next invocation will realloc, over and over and over. We can pre-
// emptively cater for that by ensuring that an extra 32 bytes is always
// available.
if len(h.buf) == cap(h.buf) {
h.buf = append(h.buf, zeroBytes...)
h.buf = h.buf[:len(h.buf)-len(zeroBytes)]
}
input := h.buf[indx:]

// merkleize the input
Expand Down

0 comments on commit 519c6f5

Please sign in to comment.