Skip to content

Commit 74a6156

Browse files
committed
pkg/reference: SplitObject: zero allocations
Before / After: BenchmarkSplitObject-10 2785656 428.1 ns/op 416 B/op 13 allocs/op BenchmarkSplitObjectNew-10 13510520 88.2 ns/op 0 B/op 0 allocs/op Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 799bca9 commit 74a6156

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

pkg/reference/reference.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ func (r Spec) String() string {
157157
// Either may be empty and it is the callers job to validate them
158158
// appropriately.
159159
func SplitObject(obj string) (tag string, dgst digest.Digest) {
160-
parts := strings.SplitAfterN(obj, "@", 2)
161-
if len(parts) < 2 {
162-
return parts[0], ""
160+
if i := strings.Index(obj, "@"); i >= 0 {
161+
// Offset by one so preserve the "@" in the tag returned.
162+
return obj[:i+1], digest.Digest(obj[i+1:])
163163
}
164-
return parts[0], digest.Digest(parts[1])
164+
return obj, ""
165165
}

pkg/reference/reference_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,33 @@ func TestReferenceParser(t *testing.T) {
192192
})
193193
}
194194
}
195+
196+
func BenchmarkSplitObject(b *testing.B) {
197+
inputs := []string{
198+
"",
199+
"@",
200+
"docker.io@",
201+
"@digest",
202+
"docker.io/library/redis:foo?fooo=asdf",
203+
"docker.io/library/redis:foo@sha256:abcdef?fooo=asdf",
204+
"docker.io/library/redis@sha256:abcdef?fooo=asdf",
205+
"docker.io/library/redis:obj@abcdef?fooo=asdf",
206+
"localhost:5000/library/redis:obj@abcdef?fooo=asdf",
207+
"/docker.io/library/redis:obj@abcdef?fooo=asdf",
208+
"docker.io/library/redis?fooo=asdf",
209+
"sub-dom1.foo.com/bar/baz/quux:latest",
210+
"sub-dom1.foo.com/bar/baz/quux:some-long-tag",
211+
"b.gcr.io/test.example.com/my-app:test.example.com",
212+
"xn--n3h.com/myimage:xn--n3h.com", // ☃.com in punycode
213+
"xn--7o8h.com/myimage:xn--7o8h.com@sha512:fffffff",
214+
"http://xn--7o8h.com/myimage:xn--7o8h.com@sha512:fffffff",
215+
}
216+
217+
b.ReportAllocs()
218+
219+
for i := 0; i < b.N; i++ {
220+
for _, input := range inputs {
221+
_, _ = SplitObject(input)
222+
}
223+
}
224+
}

0 commit comments

Comments
 (0)