Skip to content

Add NewDigest, NewDigest64, NewDigest128#29

Merged
klauspost merged 1 commit intominio:masterfrom
neilalexander:concrete-types
Oct 30, 2025
Merged

Add NewDigest, NewDigest64, NewDigest128#29
klauspost merged 1 commit intominio:masterfrom
neilalexander:concrete-types

Conversation

@neilalexander
Copy link
Contributor

@neilalexander neilalexander commented Oct 29, 2025

The New, New64 and New128 functions return a hash.Hash interface from the standard library, but this prevents Go's heap escape analysis from being able to determine whether or not the arguments to the Sum functions should escape to the heap or not, therefore they always escape.

For example, when trying to avoid the heap with a stack array allocation:

hh, _ := highwayhash.New64(...)
var hb [highwayhash.Size64]byte
if !bytes.Equal(hh.Sum(hb[:0]), ...) { ... }

... the hash.Hash type for hh guarantees that hb will always escape whereas a concrete return type allows the heap escape analysis to run correctly and avoid the heap escape.

Signed-off-by: Neil Twigg git@neilalexander.dev

@neilalexander neilalexander changed the title Return concrete types from New, New128, New64 Add NewDigest, NewDigest64, NewDigest128 Oct 30, 2025
Copy link
Contributor

@klauspost klauspost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

The `New`, `New64` and `New128` functions return a `hash.Hash` interface
from the standard library, but this prevents Go's heap escape analysis from
being able to determine whether or not the arguments to the `Sum` functions
should escape to the heap or not, therefore they _always_ escape.

For example, when trying to avoid the heap with a stack array allocation:

```
hh, _ := highwayhash.New64(...)
var hb [highwayhash.Size64]byte
if !bytes.Equal(hh.Sum(hb[:0]), ...) { ... }
```

... the `hash.Hash` type for `hh` guarantees that `hb` will always escape
whereas a concrete return type allows the heap escape analysis to run
correctly and avoid the heap escape.

Signed-off-by: Neil Twigg <git@neilalexander.dev>
@neilalexander
Copy link
Contributor Author

Linter should be fixed now.

@klauspost klauspost merged commit 070ab1a into minio:master Oct 30, 2025
12 checks passed
@neilalexander
Copy link
Contributor Author

Thanks for the fast review, any chance you'd be willing to tag as v1.0.4 please?

neilalexander added a commit to nats-io/nats-server that referenced this pull request Nov 10, 2025
This avoids heap escapes, see minio/highwayhash#29.

Signed-off-by: Neil Twigg <neil@nats.io>
neilalexander added a commit to nats-io/nats-server that referenced this pull request Nov 10, 2025
This avoids heap escapes, see minio/highwayhash#29.

Signed-off-by: Neil Twigg <neil@nats.io>
neilalexander added a commit to nats-io/nats-server that referenced this pull request Nov 10, 2025
This avoids heap escapes, see minio/highwayhash#29.

Signed-off-by: Neil Twigg <neil@nats.io>
neilalexander added a commit to nats-io/nats-server that referenced this pull request Nov 10, 2025
This avoids heap escapes, see minio/highwayhash#29.

Signed-off-by: Neil Twigg <neil@nats.io>
delaneyj pushed a commit to delaneyj/nats-server that referenced this pull request Nov 13, 2025
This avoids heap escapes, see minio/highwayhash#29.

Signed-off-by: Neil Twigg <neil@nats.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants