Skip to content

Conversation

@anatoly-kussul
Copy link

goos: linux
goarch: amd64
pkg: github.com/google/uuid
cpu: 12th Gen Intel(R) Core(TM) i5-12400F
                      │     old     │                 new                 │
                      │   sec/op    │   sec/op     vs base                │
MD5-12                  163.2n ± 0%   106.2n ± 0%  -34.95% (p=0.000 n=10)
SHA1-12                 176.0n ± 1%   115.8n ± 1%  -34.18% (p=0.000 n=10)

                      │     old      │                   new                   │
                      │     B/op     │    B/op     vs base                     │
MD5-12                  144.0 ± 0%       0.0 ± 0%  -100.00% (p=0.000 n=10)
SHA1-12                 168.0 ± 0%       0.0 ± 0%  -100.00% (p=0.000 n=10)

                      │     old      │                   new                   │
                      │  allocs/op   │ allocs/op   vs base                     │
MD5-12                  4.000 ± 0%     0.000 ± 0%  -100.00% (p=0.000 n=10)
SHA1-12                 4.000 ± 0%     0.000 ± 0%  -100.00% (p=0.000 n=10)

@anatoly-kussul anatoly-kussul requested a review from a team as a code owner January 6, 2025 02:59
@bormanp
Copy link
Contributor

bormanp commented Jan 6, 2025

Would you get the same speedup if NewHas as not public (e.g., newHash)?

@anatoly-kussul
Copy link
Author

Would you get the same speedup if NewHas as not public (e.g., newHash)?

Doesn't seem like so.
3 allocations come from calling sha1.New() outside of NewHash:

func newHash(h hash.Hash, space UUID, data []byte, version int) UUID {
	h.Reset()
	h.Write(space[:]) //nolint:errcheck
	h.Write(data)     //nolint:errcheck
	s := h.Sum(nil)
	var uuid UUID
	copy(uuid[:], s)
	uuid[6] = (uuid[6] & 0x0f) | uint8((version&0xf)<<4)
	uuid[8] = (uuid[8] & 0x3f) | 0x80 // RFC 9562 variant
	return uuid
}

func NewSHA1(space UUID, data []byte) UUID {
	return newHash(sha1.New(), space, data, 5)
}

produces

BenchmarkSHA1-12    	 7047667	       170.1 ns/op	     168 B/op	       4 allocs/op

while

func newHash(_ hash.Hash, space UUID, data []byte, version int) UUID {
	h := sha1.New()
	h.Write(space[:]) //nolint:errcheck
	h.Write(data)     //nolint:errcheck
	s := h.Sum(nil)
	var uuid UUID
	copy(uuid[:], s)
	uuid[6] = (uuid[6] & 0x0f) | uint8((version&0xf)<<4)
	uuid[8] = (uuid[8] & 0x3f) | 0x80 // RFC 9562 variant
	return uuid
}

func NewSHA1(space UUID, data []byte) UUID {
	return newHash(nil, space, data, 5)
}

already gets it down to

BenchmarkSHA1-12    	 9286918	       127.9 ns/op	      24 B/op	       1 allocs/op

last allocation comes from

s := h.Sum(nil)

changing that to slice with compile-time known cap removes last allocation

s := h.Sum(make([]byte, 0, sha1.Size))

and gets

BenchmarkSHA1-12    	10027432	       120.4 ns/op	       0 B/op	       0 allocs/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants