Skip to content

Commit

Permalink
sha256: drop minio in favor of crypto/sha256 for go1.21 and above
Browse files Browse the repository at this point in the history
In go1.21 the go std has a new SHANI accelerated rountine (just like minio's sha256). See golang/go#50543.
Reduce the code surface by dropping what is now a redondent librairy.

Benchmarks using go1.21 and go1.20 shows no significant difference between minio and the std for sha256:
```
name                         old time/op    new time/op    delta
SumAllLarge/sha2-256-12        8.63ms ± 3%    8.63ms ± 1%      ~     (p=0.220 n=20+17)
SumAllLarge/sha2-512-12        23.1ms ± 3%    23.0ms ± 3%      ~     (p=0.361 n=18+20)
SumAllLarge/dbl-sha2-256-12    36.0ms ± 4%     8.6ms ± 4%   -76.16%  (p=0.000 n=20+20)

name                         old speed      new speed      delta
SumAllLarge/sha2-256-12      1.94GB/s ± 4%  1.95GB/s ± 1%      ~     (p=0.220 n=20+17)
SumAllLarge/sha2-512-12       725MB/s ± 3%   729MB/s ± 3%      ~     (p=0.350 n=18+20)
SumAllLarge/dbl-sha2-256-12   467MB/s ± 4%  1958MB/s ± 4%  +319.53%  (p=0.000 n=20+20)
```
`dlb-sha2-256` quadrupled in speed because it was never wired to use minio's sha256 implementation and thus
was brought back up to speed by `crypto/sha256`'s improvement.

I think we should remove `github.com/multiformats/go-multihash/register/miniosha256` one go1.22 is released (~1 year) since it would be purposeless,
we could also stub it to github.com/multiformats/go-multihash/register/sha256 for all versions but [no one imports it directly](https://pkg.go.dev/github.com/multiformats/go-multihash/register/miniosha256?tab=importedby).
  • Loading branch information
Jorropo committed May 24, 2023
1 parent dc3bd68 commit e162f27
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 23 deletions.
23 changes: 0 additions & 23 deletions register/miniosha256/multihash_miniosha256.go

This file was deleted.

22 changes: 22 additions & 0 deletions register/miniosha256/post_go1.21.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build go1.21

// This package has no purpose except to perform registration of multihashes.
//
// It is meant to be used as a side-effecting import, e.g.
//
// import (
// _ "github.com/multiformats/go-multihash/register/miniosha256"
// )
//
// This package registers alternative implementations for sha2-256, using
// the github.com/minio/sha256-simd library for go1.20 and bellow. Go 1.21 and
// later fallback to [github.com/multiformats/go-multihash/register/sha256].
//
// Deprecated: please switch to [github.com/multiformats/go-multihash/register/sha256]
// as of go1.21 the go std has a SHANI implementation that is just as fast. See https://go.dev/issue/50543.
// This will be removed shortly after go1.22 is released.
package miniosha256

import (
_ "github.com/multiformats/go-multihash/register/sha256"
)
29 changes: 29 additions & 0 deletions register/miniosha256/pre_go1_21.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !go1.21

// This package has no purpose except to perform registration of multihashes.
//
// It is meant to be used as a side-effecting import, e.g.
//
// import (
// _ "github.com/multiformats/go-multihash/register/miniosha256"
// )
//
// This package registers alternative implementations for sha2-256, using
// the github.com/minio/sha256-simd library for go1.20 and bellow. Go 1.21 and
// later fallback to [github.com/multiformats/go-multihash/register/sha256].
//
// Note if you are using go1.21 or above this package is deprecated in favor of
// [github.com/multiformats/go-multihash/register/sha256] because as of go1.21
// the go std has a SHANI implementation that is just as fast. See https://go.dev/issue/50543.
// This will be removed shortly after go1.22 is released.
package miniosha256

import (
"github.com/minio/sha256-simd"

multihash "github.com/multiformats/go-multihash/core"
)

func init() {
multihash.Register(multihash.SHA2_256, sha256.New)
}
21 changes: 21 additions & 0 deletions register/sha256/sha256.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This package has no purpose except to perform registration of multihashes.
//
// It is meant to be used as a side-effecting import, e.g.
//
// import (
// _ "github.com/multiformats/go-multihash/register/sha256"
// )
//
// This package an implementation of sha256 using the go std, this is recomanded
// if you are using go1.21 or above.
package sha256

import (
"crypto/sha256"

multihash "github.com/multiformats/go-multihash/core"
)

func init() {
multihash.Register(multihash.SHA2_256, sha256.New)
}

0 comments on commit e162f27

Please sign in to comment.