aboutsummaryrefslogtreecommitdiff
path: root/src/syscall
diff options
context:
space:
mode:
authorqmuntal <quimmuntal@gmail.com>2022-08-22 18:25:29 +0200
committerGopher Robot <gobot@golang.org>2022-08-23 20:36:49 +0000
commitd88560afd32d18d8c9c3b31c41ae9877ca292e1d (patch)
treedbb9bd37458e61e7feb4877a79216edd43cf3934 /src/syscall
parentfc0d423789e3a2e1f84801be14ac657d41c115e4 (diff)
downloadgo-d88560afd32d18d8c9c3b31c41ae9877ca292e1d.tar.xz
syscall: rely on utf16.AppendRune
Using utf16.AppendRune instead of utf16.Encode safe a bunch of allocations across the board, as many higher level functions use it to call Windows syscalls, for example to `os` package: name old alloc/op new alloc/op delta Readdirname-12 15.6kB ± 0% 15.6kB ± 0% +0.26% (p=0.008 n=5+5) Readdir-12 29.4kB ± 0% 29.4kB ± 0% +0.14% (p=0.008 n=5+5) ReadDir-12 29.4kB ± 0% 29.4kB ± 0% +0.14% (p=0.016 n=4+5) StatDot-12 552B ± 0% 560B ± 0% +1.45% (p=0.008 n=5+5) StatFile-12 512B ± 0% 336B ± 0% -34.38% (p=0.008 n=5+5) StatDir-12 432B ± 0% 288B ± 0% -33.33% (p=0.008 n=5+5) LstatDot-12 552B ± 0% 560B ± 0% +1.45% (p=0.008 n=5+5) LstatFile-12 512B ± 0% 336B ± 0% -34.38% (p=0.008 n=5+5) LstatDir-12 432B ± 0% 288B ± 0% -33.33% (p=0.008 n=5+5) StatFile-12 4.00 ± 0% 2.00 ± 0% -50.00% (p=0.008 n=5+5) StatDir-12 4.00 ± 0% 2.00 ± 0% -50.00% (p=0.008 n=5+5) LstatFile-12 4.00 ± 0% 2.00 ± 0% -50.00% (p=0.008 n=5+5) LstatDir-12 4.00 ± 0% 2.00 ± 0% -50.00% (p=0.008 n=5+5) Updates #51786 Change-Id: I0a088cf1a96e9c304da9311bb3895b70443c1637 Reviewed-on: https://go-review.googlesource.com/c/go/+/425054 Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/syscall')
-rw-r--r--src/syscall/syscall_windows.go8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/syscall/syscall_windows.go b/src/syscall/syscall_windows.go
index ebaf84343d..c58d972e61 100644
--- a/src/syscall/syscall_windows.go
+++ b/src/syscall/syscall_windows.go
@@ -43,7 +43,13 @@ func UTF16FromString(s string) ([]uint16, error) {
if bytealg.IndexByteString(s, 0) != -1 {
return nil, EINVAL
}
- return utf16.Encode([]rune(s + "\x00")), nil
+ // In the worst case all characters require two uint16.
+ // Also account for the terminating NULL character.
+ buf := make([]uint16, 0, len(s)*2+1)
+ for _, r := range s {
+ buf = utf16.AppendRune(buf, r)
+ }
+ return utf16.AppendRune(buf, '\x00'), nil
}
// UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s,