microsoft / go-crypto-openssl

Go crypto backend for Linux using OpenSSL
MIT License
55 stars 14 forks source link

Implement one shot SHA and reduce allocs #24

Closed qmuntal closed 2 years ago

qmuntal commented 2 years ago

This PR contains two performance improvements which landed into dev.boringbranch during this development cycle, both committed in https://go-review.googlesource.com/c/go/+/395876/11.

The benchmarks demonstrates that the new code does not allocate:

name          old time/op    new time/op    delta
Hash8Bytes-4    4.57µs ± 4%    4.46µs ± 4%    -2.55%  (p=0.009 n=10+10)

name          old speed      new speed      delta
Hash8Bytes-4   224MB/s ± 4%   230MB/s ± 4%    +2.62%  (p=0.009 n=10+10)

name          old bytes/op   new bytes/op   delta
Hash8Bytes-4     24.0B ± 0%      0.0B       -100.00%  (p=0.000 n=10+10)

name          old allocs/op  new allocs/op  delta
Hash8Bytes-4      1.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)

Removing the SHA allocations is important for when openssl is integrated into go1.19, else we would have to disable a new test that checks boring SHAs don't allocate.

qmuntal commented 2 years ago

I've found something good: noescapeCtx is not necessary because we are passing OpenSSL C pointers as uintptr instead of unsafe.Pointer, and the former are already safely hidden from the escape analysis.

So there are only three remaining usages of noescape, but we can't avoid them because they work on Go pointers.