This PR reduce to the bare minimum the allocations in GenerateKeyRSA and GenerateKeyECDSA by reusing the same backing array when creating each BigInt, instead of allocating a new byte slice each time.
The trick is that BigInt is defined to have the same representation as a CNG big integer, so we can create new BigInts by just slicing the blob returned by BCryptExportKey. Go garbage collector is smart enough to keep the backing array live meanwhile there is a slice referencing it.
This PR reduce to the bare minimum the allocations in
GenerateKeyRSA
andGenerateKeyECDSA
by reusing the same backing array when creating eachBigInt
, instead of allocating a new byte slice each time.The trick is that
BigInt
is defined to have the same representation as a CNG big integer, so we can create newBigInt
s by just slicing the blob returned byBCryptExportKey
. Go garbage collector is smart enough to keep the backing array live meanwhile there is a slice referencing it.These are the allocation improvements: