]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: use static maps rather than an init func
authorDaniel Martí <mvdan@mvdan.cc>
Sun, 6 Mar 2022 20:31:33 +0000 (20:31 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Mon, 7 Mar 2022 21:31:03 +0000 (21:31 +0000)
go/build is one of the packages that contributes the most towards
cmd/go's init cost, which adds up to any call to the tool.

One piece of low-hanging fruit is knownOS and knownArch,
maps which are filled via an init func from a space-separated list.
Using GODEBUG=inittrace=1, we can get three samples:

init go/build @0.36 ms, 0.024 ms clock, 6568 bytes, 74 allocs
init go/build @0.33 ms, 0.025 ms clock, 6888 bytes, 76 allocs
init go/build @0.36 ms, 0.025 ms clock, 6728 bytes, 75 allocs

After using a static map instead, we see an improvement:

init go/build @0.33 ms, 0.018 ms clock, 5096 bytes, 69 allocs
init go/build @0.36 ms, 0.021 ms clock, 5096 bytes, 69 allocs
init go/build @0.33 ms, 0.019 ms clock, 5096 bytes, 69 allocs

The speedup isn't huge, but it helps, and also reduces allocs.
One can also imagine that the compiler may get better with static,
read-only maps in the future, whereas the init func will likely always
have a linear cost and extra allocations.

Change-Id: I430212bad03d25358d2cc7b1eab4536ad88d05a8
Reviewed-on: https://go-review.googlesource.com/c/go/+/390274
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Trust: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/go/build/build.go
src/go/build/syslist.go

index dce0304ba48cc5e9a8c5a3aae3bdea21fa70b27f..baf76e6b7ff7165caefe89cd5592e9f0644f77ef 100644 (file)
@@ -1965,18 +1965,6 @@ func (ctxt *Context) goodOSArchFile(name string, allTags map[string]bool) bool {
        return true
 }
 
-var knownOS = make(map[string]bool)
-var knownArch = make(map[string]bool)
-
-func init() {
-       for _, v := range strings.Fields(goosList) {
-               knownOS[v] = true
-       }
-       for _, v := range strings.Fields(goarchList) {
-               knownArch[v] = true
-       }
-}
-
 // ToolDir is the directory containing build tools.
 var ToolDir = getToolDir()
 
index 0f6e3369253ff2a84e6f5607ee3d4ef50f46d94f..6b62b63042bb0bdcae68b73b74a23da7b14d8c13 100644 (file)
@@ -4,8 +4,51 @@
 
 package build
 
-// List of past, present, and future known GOOS and GOARCH values.
+// Past, present, and future known GOOS and GOARCH values.
 // Do not remove from this list, as these are used for go/build filename matching.
 
-const goosList = "aix android darwin dragonfly freebsd hurd illumos ios js linux nacl netbsd openbsd plan9 solaris windows zos "
-const goarchList = "386 amd64 amd64p32 arm armbe arm64 arm64be loong64 mips mipsle mips64 mips64le mips64p32 mips64p32le ppc ppc64 ppc64le riscv riscv64 s390 s390x sparc sparc64 wasm "
+var knownOS = map[string]bool{
+       "aix":       true,
+       "android":   true,
+       "darwin":    true,
+       "dragonfly": true,
+       "freebsd":   true,
+       "hurd":      true,
+       "illumos":   true,
+       "ios":       true,
+       "js":        true,
+       "linux":     true,
+       "nacl":      true,
+       "netbsd":    true,
+       "openbsd":   true,
+       "plan9":     true,
+       "solaris":   true,
+       "windows":   true,
+       "zos":       true,
+}
+var knownArch = map[string]bool{
+       "386":         true,
+       "amd64":       true,
+       "amd64p32":    true,
+       "arm":         true,
+       "armbe":       true,
+       "arm64":       true,
+       "arm64be":     true,
+       "loong64":     true,
+       "mips":        true,
+       "mipsle":      true,
+       "mips64":      true,
+       "mips64le":    true,
+       "mips64p32":   true,
+       "mips64p32le": true,
+       "ppc":         true,
+       "ppc64":       true,
+       "ppc64le":     true,
+       "riscv":       true,
+       "riscv64":     true,
+       "s390":        true,
+       "s390x":       true,
+       "sparc":       true,
+       "sparc64":     true,
+       "wasm":        true,
+}