crypto/sha256: optimize sha256 implementation #34037
Labels
FrozenDueToAge
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Performance
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I test the performance in the following three cases:
openssl
library withC
crypto
library withGo
openssl
library with Go'sCgo
For different length strings, do 10,000,000 sha256 calculations.
The result is as follows, the unit of data is second.
What did you expect to see?
Test results in different cases are similar.
What did you see instead?
It can be seen that when the length of the string is small, openssl is more than 30% faster than golang.
When string length is long, using cgo to call openssl is also 10% faster than golang.
Here is my demo (does not contain loops and time statistics), please indicate if there is an error, thank you.
Call the
openssl
library withC
(need to install openssl-dev)golang and cgo
So can we optimize the sha256 of golang? Because this is a hot function in my application, I am concerned about the time efficiency of sha256.
Here are some preliminary analysis, I hope it can provide some help.
The longest running function in the test, in golang is
$GOROOT/src/crypto/sha256/sha256block_amd64.s
block
avx2
case,in openssl is
sha256_block_data_order_avx2
.You can download openssl 1.0.2g source file,
make
, then can found it inopenssl-1.0.2g/crypto/sha/sha256-x86_64.s
.sha256-x86_64.s.zip
Since I am not very familiar with assembly, I don't know how to optimize.
Look forward to your reply, thank you.
The text was updated successfully, but these errors were encountered: