Open yerden opened 1 year ago
Change https://go.dev/cl/471975 mentions this issue: hash/crc32: make Update escape free
CC @golang/security.
@RaduBerinde hi, maybe you had some rationale behind declaring updateCastagnoli
as a closure?
@yerden I think it was just to avoid checking every time which implementation to use, but I did not consider the heap escape issue.
See https://go-review.googlesource.com/c/go/+/346209 for an example from crypto. We could do something similar here.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
I haven't tried the latest release but from what I see there's not much change to affected code so far.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I wrote a simple benchmark to showcase the problem. https://go.dev/play/p/H_cHsnLhMiW
Or you can see
runtime.MemStats
with growing fieldTotalAlloc
here: https://go.dev/play/p/8geJhJ6OOqSWhat did you expect to see?
The
data
variable in the benchmark should be allocated on the stack becauseUpdate
does not retain the slice. Hence no allocations should be made per operation.What did you see instead?
We have an allocation due to
data
escaping to heap. I believe the reason is thatupdateCastagnoli
which does the calculation is declared as the closure and it is initialized during runtime depending on CPU capabilities (SSE4.2 etc).Maybe we could rework initialization of crc32 algorithm without additional level of indirection. I relocated some of Go assembly code for SSE4.2 to my private project and it shows no allocation and a significant performance boost (by a factor of 3 or 4 on some data).