Go version
go1.22
Output of go env
in your module/workspace:
n/a
What did you do?
func Benchmark(b *testing.B) {
b.ReportAllocs()
for range b.N {
sink = cryptoRandRead()
}
}
//go:noinline
func cryptoRandRead() (out [8]byte) {
rand.Read(out[:])
return out
}
What did you see happen?
Benchmark-24 1877874 616.4 ns/op 8 B/op 1 allocs/op
What did you expect to see?
Benchmark-24 1877874 616.4 ns/op 0 B/op 0 allocs/op
The Read
function is a static function. With some work to the underlying implementation (and avoid going through an io.Reader
), we should be able to make it not escape the input.
Thus, you could do something like:
var arr [32]byte
rand.Read(arr[:])
and know that it doesn't allocate.
Comment From: thepudds
See also #66779 and https://go.dev/cl/578516.
Comment From: gabyhelp
Related Issues
- https://github.com/golang/go/issues/66779
- https://github.com/golang/go/issues/33256
- https://github.com/golang/go/issues/66801
- https://github.com/golang/go/issues/16593
- https://github.com/golang/go/issues/45217
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: rsc
Duplicate of #66779.