Skip to content

Commit 340e732

Browse files
committed
cache parsed runtime Go minor during package loading
Result: {"status":"keep","warm_internal_build_wall":28.574,"go_reported_s":27.996,"baseline_s":30.326,"patched_s":28.574,"delta_s":-1.752,"base_go_reported_s":29.768,"patched_go_reported_s":27.996,"wall_s":28.574}
1 parent 05e3548 commit 340e732

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

internal/packages/load.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ const (
6767
DebugPackagesLoad = false
6868
)
6969

70+
var runtimeGoMinor = parseRuntimeGoMinor(runtime.Version())
71+
72+
func parseRuntimeGoMinor(version string) int {
73+
const prefix = "go1."
74+
if !strings.HasPrefix(version, prefix) {
75+
return 0
76+
}
77+
minor := 0
78+
for i := len(prefix); i < len(version); i++ {
79+
c := version[i]
80+
if c < '0' || c > '9' {
81+
break
82+
}
83+
minor = minor*10 + int(c-'0')
84+
}
85+
return minor
86+
}
87+
7088
// A Config specifies details about how packages should be loaded.
7189
// The zero value is a valid configuration.
7290
// Calls to Load do not modify this struct.
@@ -303,8 +321,7 @@ func loadPackageEx(dedup Deduper, ld *loader, lpkg *loaderPackage) {
303321
// - golang.org/issue/55883 (go/packages confusing error)
304322
//
305323
// Should we assert a hard minimum of (currently) go1.16 here?
306-
var runtimeVersion int
307-
if _, err := fmt.Sscanf(runtime.Version(), "go1.%d", &runtimeVersion); err == nil && runtimeVersion < lpkg.goVersion {
324+
if runtimeVersion := runtimeGoMinor; runtimeVersion != 0 && runtimeVersion < lpkg.goVersion {
308325
defer func() {
309326
if len(lpkg.Errors) > 0 {
310327
appendError(packages.Error{

internal/packages/load_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package packages
2+
3+
import "testing"
4+
5+
func TestParseRuntimeGoMinor(t *testing.T) {
6+
tests := []struct {
7+
version string
8+
want int
9+
}{
10+
{"go1.21.13", 21},
11+
{"go1.26.0", 26},
12+
{"go1.26rc1", 26},
13+
{"devel go1.27", 0},
14+
{"go2.0", 0},
15+
{"", 0},
16+
}
17+
for _, tt := range tests {
18+
if got := parseRuntimeGoMinor(tt.version); got != tt.want {
19+
t.Fatalf("parseRuntimeGoMinor(%q) = %d, want %d", tt.version, got, tt.want)
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)