From b8777ee213f2ebad0bb4a5dcdb812fd356d6d086 Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Wed, 14 Sep 2022 11:06:42 +0100 Subject: [PATCH] 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. --- src/hash/crc32/crc32_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/hash/crc32/crc32_test.go b/src/hash/crc32/crc32_test.go index cbb869dfd6..f084612f6f 100644 --- a/src/hash/crc32/crc32_test.go +++ b/src/hash/crc32/crc32_test.go @@ -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] } }