Skip to content

Commit

Permalink
net/http/cgi: replace constant map with switch statement
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mdempsky committed Apr 30, 2020
1 parent a7e5396 commit 844b410
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/net/http/cgi/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 844b410

Please sign in to comment.