mirror of
https://github.com/opencontainers/go-digest.git
synced 2025-04-18 03:24:02 +03:00
remove unneeded fmt.Sprintf and indirections
Remove fmt.Sprintf and use string-concatenation instead to reduce some allocations. Before / After: BenchmarkNewDigestFromEncoded-10 8474174 128.4 ns/op 112 B/op 3 allocs/op BenchmarkNewDigestFromEncoded-10 37912695 31.55 ns/op 80 B/op 1 allocs/op BenchmarkNewDigestFromBytes-10 5087299 237.2 ns/op 200 B/op 5 allocs/op BenchmarkNewDigestFromBytes-10 8416543 146.8 ns/op 168 B/op 3 allocs/op Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
e3a8687c79
commit
247c259e4d
@ -47,19 +47,19 @@ func NewDigest(alg Algorithm, h hash.Hash) Digest {
|
||||
// functions. This is also useful for rebuilding digests from binary
|
||||
// serializations.
|
||||
func NewDigestFromBytes(alg Algorithm, p []byte) Digest {
|
||||
return NewDigestFromEncoded(alg, alg.Encode(p))
|
||||
return Digest(string(alg) + ":" + alg.Encode(p))
|
||||
}
|
||||
|
||||
// NewDigestFromHex returns a Digest from alg and the hex encoded digest.
|
||||
//
|
||||
// Deprecated: use [NewDigestFromEncoded] instead.
|
||||
func NewDigestFromHex(alg, hex string) Digest {
|
||||
return NewDigestFromEncoded(Algorithm(alg), hex)
|
||||
return Digest(alg + ":" + hex)
|
||||
}
|
||||
|
||||
// NewDigestFromEncoded returns a Digest from alg and the encoded digest.
|
||||
func NewDigestFromEncoded(alg Algorithm, encoded string) Digest {
|
||||
return Digest(fmt.Sprintf("%s:%s", alg, encoded))
|
||||
return Digest(string(alg) + ":" + encoded)
|
||||
}
|
||||
|
||||
// DigestRegexp matches valid digest types.
|
||||
|
@ -15,6 +15,7 @@
|
||||
package digest_test
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"testing"
|
||||
|
||||
"github.com/opencontainers/go-digest"
|
||||
@ -118,3 +119,21 @@ func TestParseDigest(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkNewDigestFromEncoded(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = digest.NewDigestFromEncoded("sha256", "e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkNewDigestFromBytes(b *testing.B) {
|
||||
s := sha256.Sum256([]byte("hello world"))
|
||||
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = digest.NewDigestFromBytes("sha256", s[:])
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user