From: Matthew Dempsky Date: Wed, 29 Apr 2020 23:45:05 +0000 (-0700) Subject: net/http/cgi: replace constant map with switch statement X-Git-Tag: go1.15beta1~293 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=844b410922b2ea7d22b651c536178e79696749f6;p=gostls13.git net/http/cgi: replace constant map with switch statement The switch statement can be statically optimized by the compiler, whereas similarly optimizing the map index expression would require additional compiler analysis to detect the map is never mutated. Updates #10848. Change-Id: I2fc70d4a34dc545677b99f218b51023c7891bbbd Reviewed-on: https://go-review.googlesource.com/c/go/+/231041 Run-TryBot: Matthew Dempsky Reviewed-by: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/net/http/cgi/host.go b/src/net/http/cgi/host.go index 58e9f7132a..215bb83a39 100644 --- a/src/net/http/cgi/host.go +++ b/src/net/http/cgi/host.go @@ -32,16 +32,23 @@ import ( var trailingPort = regexp.MustCompile(`:([0-9]+)$`) -var osDefaultInheritEnv = map[string][]string{ - "darwin": {"DYLD_LIBRARY_PATH"}, - "freebsd": {"LD_LIBRARY_PATH"}, - "hpux": {"LD_LIBRARY_PATH", "SHLIB_PATH"}, - "irix": {"LD_LIBRARY_PATH", "LD_LIBRARYN32_PATH", "LD_LIBRARY64_PATH"}, - "linux": {"LD_LIBRARY_PATH"}, - "openbsd": {"LD_LIBRARY_PATH"}, - "solaris": {"LD_LIBRARY_PATH", "LD_LIBRARY_PATH_32", "LD_LIBRARY_PATH_64"}, - "windows": {"SystemRoot", "COMSPEC", "PATHEXT", "WINDIR"}, -} +var osDefaultInheritEnv = func() []string { + switch runtime.GOOS { + case "darwin": + return []string{"DYLD_LIBRARY_PATH"} + case "linux", "freebsd", "openbsd": + return []string{"LD_LIBRARY_PATH"} + case "hpux": + return []string{"LD_LIBRARY_PATH", "SHLIB_PATH"} + case "irix": + return []string{"LD_LIBRARY_PATH", "LD_LIBRARYN32_PATH", "LD_LIBRARY64_PATH"} + case "solaris": + return []string{"LD_LIBRARY_PATH", "LD_LIBRARY_PATH_32", "LD_LIBRARY_PATH_64"} + case "windows": + return []string{"SystemRoot", "COMSPEC", "PATHEXT", "WINDIR"} + } + return nil +}() // Handler runs an executable in a subprocess with a CGI environment. type Handler struct { @@ -183,7 +190,7 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } } - for _, e := range osDefaultInheritEnv[runtime.GOOS] { + for _, e := range osDefaultInheritEnv { if v := os.Getenv(e); v != "" { env = append(env, e+"="+v) }