Closed
Description
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.