From: qmuntal Date: Mon, 22 Aug 2022 16:25:29 +0000 (+0200) Subject: syscall: rely on utf16.AppendRune X-Git-Tag: go1.20rc1~1445 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=d88560afd32d18d8c9c3b31c41ae9877ca292e1d;p=gostls13.git 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 Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor TryBot-Result: Gopher Robot Reviewed-by: David Chase Auto-Submit: Ian Lance Taylor --- 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,