]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: rely on utf16.AppendRune
authorqmuntal <quimmuntal@gmail.com>
Mon, 22 Aug 2022 16:25:29 +0000 (18:25 +0200)
committerGopher Robot <gobot@golang.org>
Tue, 23 Aug 2022 20:36:49 +0000 (20:36 +0000)
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>

src/syscall/syscall_windows.go

index ebaf84343d13ddd3f00bf9ae81462ab0b9400807..c58d972e6198c94192dd5a3fb877480422ca8ec8 100644 (file)
@@ -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,