浏览代码

types/model: reduce Name.Filepath allocs from 5 to 2 (#4039)

Blake Mizerany 1 年之前
父节点
当前提交
588901f449
共有 2 个文件被更改,包括 21 次插入6 次删除
  1. 6 6
      types/model/name.go
  2. 15 0
      types/model/name_test.go

+ 6 - 6
types/model/name.go

@@ -231,12 +231,12 @@ func (n Name) Filepath() string {
 	if !n.IsFullyQualified() {
 	if !n.IsFullyQualified() {
 		panic("illegal attempt to get filepath of invalid name")
 		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.
 // LogValue returns a slog.Value that represents the name as a string.

+ 15 - 0
types/model/name_test.go

@@ -2,6 +2,7 @@ package model
 
 
 import (
 import (
 	"reflect"
 	"reflect"
+	"runtime"
 	"testing"
 	"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) {
 func FuzzName(f *testing.F) {
 	for s := range testCases {
 	for s := range testCases {
 		f.Add(s)
 		f.Add(s)