]> Cypherpunks repositories - gostls13.git/commitdiff
net/http/cgi: replace constant map with switch statement
authorMatthew Dempsky <mdempsky@google.com>
Wed, 29 Apr 2020 23:45:05 +0000 (16:45 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 30 Apr 2020 00:13:38 +0000 (00:13 +0000)
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 <mdempsky@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/http/cgi/host.go

index 58e9f7132a86cbffdf569b196a0ae38049f9e045..215bb83a39f501b30f54c5e17ee34e6cfeff6e91 100644 (file)
@@ -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)
                }