From 588901f449e4aec505a539ca6a99a1d55a7c721d Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Tue, 30 Apr 2024 11:09:19 -0700 Subject: [PATCH] types/model: reduce Name.Filepath allocs from 5 to 2 (#4039) --- types/model/name.go | 12 ++++++------ types/model/name_test.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/types/model/name.go b/types/model/name.go index 9c1fbd77..ec129be5 100644 --- a/types/model/name.go +++ b/types/model/name.go @@ -231,12 +231,12 @@ func (n Name) Filepath() string { if !n.IsFullyQualified() { panic("illegal attempt to get filepath of invalid name") } - return filepath.Join( - strings.ToLower(n.Host), - strings.ToLower(n.Namespace), - strings.ToLower(n.Model), - strings.ToLower(n.Tag), - ) + return strings.ToLower(filepath.Join( + n.Host, + n.Namespace, + n.Model, + n.Tag, + )) } // LogValue returns a slog.Value that represents the name as a string. diff --git a/types/model/name_test.go b/types/model/name_test.go index c8588395..f1416fd2 100644 --- a/types/model/name_test.go +++ b/types/model/name_test.go @@ -2,6 +2,7 @@ package model import ( "reflect" + "runtime" "testing" ) @@ -217,6 +218,20 @@ func TestNameIsValidPart(t *testing.T) { } +func TestFilepathAllocs(t *testing.T) { + n := ParseNameBare("HOST/NAMESPACE/MODEL:TAG") + allocs := testing.AllocsPerRun(1000, func() { + n.Filepath() + }) + allowedAllocs := 2.0 + if runtime.GOOS == "windows" { + allowedAllocs = 4 + } + if allocs > allowedAllocs { + t.Errorf("allocs = %v; allowed %v", allocs, allowedAllocs) + } +} + func FuzzName(f *testing.F) { for s := range testCases { f.Add(s)