Skip to content

Commit 7143f4a

Browse files
committed
windows: manually initialize NewNTUnicodeString
The `RtlInitUnicodeString` syscall can be avoided by manually initializing the `NTUnicodeString`. The process is described in [WdmlibRtlInitUnicodeStringEx](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdmsec/nf-wdmsec-wdmlibrtlinitunicodestringex). The less syscalls, the better. It also makes the function faster: ``` goos: windows goarch: amd64 pkg: golang.org/x/sys/windows cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ NewNTUnicodeString-12 181.05n ± 15% 96.32n ± 11% -46.80% (p=0.000 n=10) ``` Change-Id: Iaf079acdcc2024cdca6b6b649a711f6be99c5b87 Reviewed-on: https://go-review.googlesource.com/c/sys/+/618175 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 23b0dab commit 7143f4a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

windows/syscall_windows.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -1685,13 +1685,16 @@ func (s NTStatus) Error() string {
16851685
// do not use NTUnicodeString, and instead UTF16PtrFromString should be used for
16861686
// the more common *uint16 string type.
16871687
func NewNTUnicodeString(s string) (*NTUnicodeString, error) {
1688-
var u NTUnicodeString
1689-
s16, err := UTF16PtrFromString(s)
1688+
s16, err := UTF16FromString(s)
16901689
if err != nil {
16911690
return nil, err
16921691
}
1693-
RtlInitUnicodeString(&u, s16)
1694-
return &u, nil
1692+
n := uint16(len(s16) * 2)
1693+
return &NTUnicodeString{
1694+
Length: n - 2, // subtract 2 bytes for the NULL terminator
1695+
MaximumLength: n,
1696+
Buffer: &s16[0],
1697+
}, nil
16951698
}
16961699

16971700
// Slice returns a uint16 slice that aliases the data in the NTUnicodeString.

0 commit comments

Comments
 (0)