Skip to content

Commit 5351bcf

Browse files
committed
syscall: simplify and optimize environment block creation on Windows
createEnvBlock currently allocates multiple times: at least one to convert the slice of strings into a NULL separated slice of bytes, and then again to encode it as UTF-16. The logic to do so is also quite complex. This CL simplifies the logic by allocating only once by encoding the slice of strings into UTF-16 directly using utf16.AppendRune. goos: windows goarch: amd64 pkg: syscall cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ CreateEnvBlock-12 37.92µ ± 24% 21.36µ ± 8% -43.66% (p=0.000 n=10) │ old.txt │ new.txt │ │ B/op │ B/op vs base │ CreateEnvBlock-12 109.12Ki ± 0% 26.62Ki ± 0% -75.60% (p=0.000 n=10) │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ CreateEnvBlock-12 4.000 ± 0% 1.000 ± 0% -75.00% (p=0.000 n=10) Change-Id: If35f62c3926b486d5253a9ae23a33b979b2f02c2 Reviewed-on: https://go-review.googlesource.com/c/go/+/531355 Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Quim Muntal <[email protected]> TryBot-Result: Gopher Robot <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent 6e866fe commit 5351bcf

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/syscall/exec_windows.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ func makeCmdLine(args []string) string {
118118
// terminated strings followed by a nil.
119119
// Last bytes are two UCS-2 NULs, or four NUL bytes.
120120
// If any string contains a NUL, it returns (nil, EINVAL).
121-
func createEnvBlock(envv []string) (*uint16, error) {
121+
func createEnvBlock(envv []string) ([]uint16, error) {
122122
if len(envv) == 0 {
123-
return &utf16.Encode([]rune("\x00\x00"))[0], nil
123+
return utf16.Encode([]rune("\x00\x00")), nil
124124
}
125-
length := 0
125+
var length int
126126
for _, s := range envv {
127127
if bytealg.IndexByteString(s, 0) != -1 {
128128
return nil, EINVAL
@@ -131,17 +131,15 @@ func createEnvBlock(envv []string) (*uint16, error) {
131131
}
132132
length += 1
133133

134-
b := make([]byte, length)
135-
i := 0
134+
b := make([]uint16, 0, length)
136135
for _, s := range envv {
137-
l := len(s)
138-
copy(b[i:i+l], []byte(s))
139-
copy(b[i+l:i+l+1], []byte{0})
140-
i = i + l + 1
136+
for _, c := range s {
137+
b = utf16.AppendRune(b, c)
138+
}
139+
b = utf16.AppendRune(b, 0)
141140
}
142-
copy(b[i:i+1], []byte{0})
143-
144-
return &utf16.Encode([]rune(string(b)))[0], nil
141+
b = utf16.AppendRune(b, 0)
142+
return b, nil
145143
}
146144

147145
func CloseOnExec(fd Handle) {
@@ -387,9 +385,9 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle
387385
pi := new(ProcessInformation)
388386
flags := sys.CreationFlags | CREATE_UNICODE_ENVIRONMENT | _EXTENDED_STARTUPINFO_PRESENT
389387
if sys.Token != 0 {
390-
err = CreateProcessAsUser(sys.Token, argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, willInheritHandles, flags, envBlock, dirp, &si.StartupInfo, pi)
388+
err = CreateProcessAsUser(sys.Token, argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, willInheritHandles, flags, &envBlock[0], dirp, &si.StartupInfo, pi)
391389
} else {
392-
err = CreateProcess(argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, willInheritHandles, flags, envBlock, dirp, &si.StartupInfo, pi)
390+
err = CreateProcess(argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, willInheritHandles, flags, &envBlock[0], dirp, &si.StartupInfo, pi)
393391
}
394392
if err != nil {
395393
return 0, 0, err

0 commit comments

Comments
 (0)