Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spurious allocation in hasher.Merkleize #171

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading