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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2024-06-27 11:44:36 +02:00
parent 799bca97f2
commit 74a6156ac2
2 changed files with 34 additions and 4 deletions

View File

@@ -157,9 +157,9 @@ func (r Spec) String() string {
// Either may be empty and it is the callers job to validate them
// appropriately.
func SplitObject(obj string) (tag string, dgst digest.Digest) {
parts := strings.SplitAfterN(obj, "@", 2)
if len(parts) < 2 {
return parts[0], ""
if i := strings.Index(obj, "@"); i >= 0 {
// Offset by one so preserve the "@" in the tag returned.
return obj[:i+1], digest.Digest(obj[i+1:])
}
return parts[0], digest.Digest(parts[1])
return obj, ""
}