Skip to content

Improve performance of v1.NewHash#2194

Merged
Subserial merged 2 commits intogoogle:mainfrom
bmoylan:bm/optimize-NewHash
Feb 5, 2026
Merged

Improve performance of v1.NewHash#2194
Subserial merged 2 commits intogoogle:mainfrom
bmoylan:bm/optimize-NewHash

Conversation

@bmoylan
Copy link
Copy Markdown
Contributor

@bmoylan bmoylan commented Jan 24, 2026

Previously, NewHash() made multiple unnecessary heap allocations that I have been removed in this PR. I've also added a simple benchmark test to measure the impact. Results below:

Before:

go test ./pkg/v1 -run XXX -bench BenchmarkNewHash
goos: darwin
goarch: arm64
pkg: github.com/google/go-containerregistry/pkg/v1
cpu: Apple M1 Pro
BenchmarkNewHash-10    	 8977393	       123.1 ns/op	     160 B/op	       2 allocs/op
PASS
ok  	github.com/google/go-containerregistry/pkg/v1	1.711s

After:

$ go test ./pkg/v1 -run XXX -bench BenchmarkNewHash
goos: darwin
goarch: arm64
pkg: github.com/google/go-containerregistry/pkg/v1
cpu: Apple M1 Pro
BenchmarkNewHash-10    	15214291	        76.09 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/google/go-containerregistry/pkg/v1	1.805s

Previously, NewHash() made multiple unnecessary heap allocations that have been removed.

I've also added a simple benchmark test to measure the impact. Results below:

Before:

```
go test ./pkg/v1 -run XXX -bench BenchmarkNewHash
goos: darwin
goarch: arm64
pkg: github.com/google/go-containerregistry/pkg/v1
cpu: Apple M1 Pro
BenchmarkNewHash-10    	 8977393	       123.1 ns/op	     160 B/op	       2 allocs/op
PASS
ok  	github.com/google/go-containerregistry/pkg/v1	1.711s
```

After:

```
$ go test ./pkg/v1 -run XXX -bench BenchmarkNewHash
goos: darwin
goarch: arm64
pkg: github.com/google/go-containerregistry/pkg/v1
cpu: Apple M1 Pro
BenchmarkNewHash-10    	15214291	        76.09 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/google/go-containerregistry/pkg/v1	1.805s
```
@bmoylan bmoylan force-pushed the bm/optimize-NewHash branch from 2d90a7c to baf6dbc Compare January 24, 2026 15:45
func (h *Hash) parse(unquoted string) error {
parts := strings.Split(unquoted, ":")
if len(parts) != 2 {
algo, body, ok := strings.Cut(unquoted, ":")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First removed allocation: strings.Split creates a slice but we know we expect two parts.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, sha256:abc:def would have failed. Do we still have that behavior with this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imjasonh thanks for looking, yes! But just to be sure, I've added additional cases to TestBadHashes.

var wantBytes int
switch algo {
case "sha256":
wantBytes = crypto.SHA256.Size()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second removed allocation: Hasher calls sha256.New(), which can be avoided if we only need the size.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jan 29, 2026

Codecov Report

❌ Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 53.06%. Comparing base (8b3c303) to head (1d42d90).
⚠️ Report is 68 commits behind head on main.

Files with missing lines Patch % Lines
pkg/v1/hash.go 93.75% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (8b3c303) and HEAD (1d42d90). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (8b3c303) HEAD (1d42d90)
2 1
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2194       +/-   ##
===========================================
- Coverage   71.67%   53.06%   -18.61%     
===========================================
  Files         123      164       +41     
  Lines        9935    10946     +1011     
===========================================
- Hits         7121     5809     -1312     
- Misses       2115     4431     +2316     
- Partials      699      706        +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Subserial Subserial merged commit 93aa273 into google:main Feb 5, 2026
17 checks passed
@bmoylan bmoylan deleted the bm/optimize-NewHash branch February 6, 2026 23:46
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.

4 participants