1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:14:41 -07:00

Reduce crc32 benchmark memory usage

Reset the length of the slice where save the result to recycle the same allocated memory and prevent further allocations.
The Sum function uses append, so the length of the "in" slice increased with each cycle of the benchmark.
This commit is contained in:
Erik Pellizzon 2022-09-14 11:06:42 +01:00
parent 7d1bf79faa
commit b8777ee213

View File

@ -329,11 +329,14 @@ func benchmark(b *testing.B, h hash.Hash32, n, alignment int64) {
h.Reset()
h.Write(data)
h.Sum(in)
// Avoid further allocations
in = in[:0]
b.ResetTimer()
for i := 0; i < b.N; i++ {
h.Reset()
h.Write(data)
h.Sum(in)
in = in[:0]
}
}