Skip to content

Commit 3a90bfb

Browse files
committed
cmd/dist, cmd/compile: eliminate mergeEnvLists copies
This is now handled by os/exec. Updates #12868 Change-Id: Ic21a6ff76a9b9517437ff1acf3a9195f9604bb45 Reviewed-on: https://go-review.googlesource.com/37698 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 9d29be4 commit 3a90bfb

File tree

2 files changed

+8
-47
lines changed

2 files changed

+8
-47
lines changed

src/cmd/compile/internal/gc/asm_test.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (ats *asmTests) compileToAsm(t *testing.T, dir string) string {
143143
func (ats *asmTests) runGo(t *testing.T, args ...string) string {
144144
var stdout, stderr bytes.Buffer
145145
cmd := exec.Command(testenv.GoToolPath(t), args...)
146-
cmd.Env = mergeEnvLists([]string{"GOARCH=" + ats.arch, "GOOS=" + ats.os}, os.Environ())
146+
cmd.Env = append(os.Environ(), "GOARCH="+ats.arch, "GOOS="+ats.os)
147147
cmd.Stdout = &stdout
148148
cmd.Stderr = &stderr
149149

@@ -790,25 +790,6 @@ var linuxARM64Tests = []*asmTest{
790790
},
791791
}
792792

793-
// mergeEnvLists merges the two environment lists such that
794-
// variables with the same name in "in" replace those in "out".
795-
// This always returns a newly allocated slice.
796-
func mergeEnvLists(in, out []string) []string {
797-
out = append([]string(nil), out...)
798-
NextVar:
799-
for _, inkv := range in {
800-
k := strings.SplitAfterN(inkv, "=", 2)[0]
801-
for i, outkv := range out {
802-
if strings.HasPrefix(outkv, k) {
803-
out[i] = inkv
804-
continue NextVar
805-
}
806-
}
807-
out = append(out, inkv)
808-
}
809-
return out
810-
}
811-
812793
// TestLineNumber checks to make sure the generated assembly has line numbers
813794
// see issue #16214
814795
func TestLineNumber(t *testing.T) {

src/cmd/dist/test.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ func (t *tester) registerTests() {
426426
cmd := t.addCmd(dt, "src", "go", "test", "-short", t.timeout(300), t.tags(), "runtime", "-cpu=1,2,4")
427427
// We set GOMAXPROCS=2 in addition to -cpu=1,2,4 in order to test runtime bootstrap code,
428428
// creation of first goroutines and first garbage collections in the parallel setting.
429-
cmd.Env = mergeEnvLists([]string{"GOMAXPROCS=2"}, os.Environ())
429+
cmd.Env = append(os.Environ(), "GOMAXPROCS=2")
430430
return nil
431431
},
432432
})
@@ -807,18 +807,17 @@ func (t *tester) registerHostTest(name, heading, dir, pkg string) {
807807
}
808808

809809
func (t *tester) runHostTest(dir, pkg string) error {
810-
env := mergeEnvLists([]string{"GOARCH=" + t.gohostarch, "GOOS=" + t.gohostos}, os.Environ())
811810
defer os.Remove(filepath.Join(t.goroot, dir, "test.test"))
812811
cmd := t.dirCmd(dir, "go", "test", t.tags(), "-c", "-o", "test.test", pkg)
813-
cmd.Env = env
812+
cmd.Env = append(os.Environ(), "GOARCH="+t.gohostarch, "GOOS="+t.gohostos)
814813
if err := cmd.Run(); err != nil {
815814
return err
816815
}
817816
return t.dirCmd(dir, "./test.test").Run()
818817
}
819818

820819
func (t *tester) cgoTest(dt *distTest) error {
821-
env := mergeEnvLists([]string{"GOTRACEBACK=2"}, os.Environ())
820+
env := append(os.Environ(), "GOTRACEBACK=2")
822821

823822
cmd := t.addCmd(dt, "misc/cgo/test", "go", "test", t.tags(), "-ldflags", "-linkmode=auto", t.runFlag(""))
824823
cmd.Env = env
@@ -1053,12 +1052,12 @@ func (t *tester) cgoTestSO(dt *distTest, testpath string) error {
10531052
if t.goos == "darwin" {
10541053
s = "DYLD_LIBRARY_PATH"
10551054
}
1056-
cmd.Env = mergeEnvLists([]string{s + "=."}, os.Environ())
1055+
cmd.Env = append(os.Environ(), s+"=.")
10571056

10581057
// On FreeBSD 64-bit architectures, the 32-bit linker looks for
10591058
// different environment variables.
10601059
if t.goos == "freebsd" && t.gohostarch == "386" {
1061-
cmd.Env = mergeEnvLists([]string{"LD_32_LIBRARY_PATH=."}, cmd.Env)
1060+
cmd.Env = append(cmd.Env, "LD_32_LIBRARY_PATH=.")
10621061
}
10631062
}
10641063
return cmd.Run()
@@ -1097,9 +1096,8 @@ func (t *tester) raceTest(dt *distTest) error {
10971096
// TODO(iant): Figure out how to catch this.
10981097
// t.addCmd(dt, "src", "go", "test", "-race", "-run=TestParallelTest", "cmd/go")
10991098
if t.cgoEnabled {
1100-
env := mergeEnvLists([]string{"GOTRACEBACK=2"}, os.Environ())
11011099
cmd := t.addCmd(dt, "misc/cgo/test", "go", "test", "-race", "-short", t.runFlag(""))
1102-
cmd.Env = env
1100+
cmd.Env = append(os.Environ(), "GOTRACEBACK=2")
11031101
}
11041102
if t.extLink() {
11051103
// Test with external linking; see issue 9133.
@@ -1118,7 +1116,7 @@ func (t *tester) testDirTest(dt *distTest, shard, shards int) error {
11181116
runtest.Do(func() {
11191117
const exe = "runtest.exe" // named exe for Windows, but harmless elsewhere
11201118
cmd := t.dirCmd("test", "go", "build", "-o", exe, "run.go")
1121-
cmd.Env = mergeEnvLists([]string{"GOOS=" + t.gohostos, "GOARCH=" + t.gohostarch, "GOMAXPROCS="}, os.Environ())
1119+
cmd.Env = append(os.Environ(), "GOOS="+t.gohostos, "GOARCH="+t.gohostarch, "GOMAXPROCS=")
11221120
runtest.exe = filepath.Join(cmd.Dir, exe)
11231121
if err := cmd.Run(); err != nil {
11241122
runtest.err = err
@@ -1141,24 +1139,6 @@ func (t *tester) testDirTest(dt *distTest, shard, shards int) error {
11411139
return nil
11421140
}
11431141

1144-
// mergeEnvLists merges the two environment lists such that
1145-
// variables with the same name in "in" replace those in "out".
1146-
// out may be mutated.
1147-
func mergeEnvLists(in, out []string) []string {
1148-
NextVar:
1149-
for _, inkv := range in {
1150-
k := strings.SplitAfterN(inkv, "=", 2)[0]
1151-
for i, outkv := range out {
1152-
if strings.HasPrefix(outkv, k) {
1153-
out[i] = inkv
1154-
continue NextVar
1155-
}
1156-
}
1157-
out = append(out, inkv)
1158-
}
1159-
return out
1160-
}
1161-
11621142
// cgoPackages is the standard packages that use cgo.
11631143
var cgoPackages = []string{
11641144
"crypto/x509",

0 commit comments

Comments
 (0)