From 38174b3a3514629b84dcd76878b2f536b189dd7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 6 Mar 2022 20:31:33 +0000 Subject: [PATCH] go/build: use static maps rather than an init func MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Trust: Daniel Martí TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor --- src/go/build/build.go | 12 ---------- src/go/build/syslist.go | 49 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/go/build/build.go b/src/go/build/build.go index dce0304ba48..baf76e6b7ff 100644 --- a/src/go/build/build.go +++ b/src/go/build/build.go @@ -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() diff --git a/src/go/build/syslist.go b/src/go/build/syslist.go index 0f6e3369253..6b62b63042b 100644 --- a/src/go/build/syslist.go +++ b/src/go/build/syslist.go @@ -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, +}