authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2024-07-13 17:29:18-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2024-07-13 18:37:49-07:00
logd48251d0f0d2de2f618afe65db3ba425b332d5c4
tree6e68a4f307c7a8ec048a8bc8f5dacc04f6979341
parent1a62cfffa79917d930b77f2598b1bfc93efeede5

ArgIteratorWindows.init: Take `[]const u16` slice instead of multi-item pointer

Now that we use the PEB to get the precise length of the command line string, there's no need for a multi-item pointer/sliceTo call. This provides a minor speedup: Benchmark 1 (153 runs): benchargv-before.exe measurement mean ± σ min … max outliers delta wall_time 32.7ms ± 429us 32.1ms … 36.9ms 1 ( 1%) 0% peak_rss 6.49MB ± 5.62KB 6.46MB … 6.49MB 14 ( 9%) 0% Benchmark 2 (157 runs): benchargv-after.exe measurement mean ± σ min … max outliers delta wall_time 31.9ms ± 236us 31.4ms … 32.7ms 4 ( 3%) ⚡- 2.4% ± 0.2% peak_rss 6.49MB ± 4.77KB 6.46MB … 6.49MB 14 ( 9%) + 0.0% ± 0.0%

1 files changed, 6 insertions(+), 7 deletions(-)

lib/std/process.zig+6-7
......@@ -664,7 +664,7 @@ pub const ArgIteratorWasi = struct {
664664pub const ArgIteratorWindows = struct {
665665 allocator: Allocator,
666666 /// Encoded as WTF-16 LE.
667 cmd_line: [:0]const u16,
667 cmd_line: []const u16,
668668 index: usize = 0,
669669 /// Owned by the iterator. Long enough to hold contiguous NUL-terminated slices
670670 /// of each argument encoded as WTF-8.
......@@ -678,9 +678,8 @@ pub const ArgIteratorWindows = struct {
678678 ///
679679 /// The iterator stores and uses `cmd_line_w`, so its memory must be valid for
680680 /// at least as long as the returned ArgIteratorWindows.
681 pub fn init(allocator: Allocator, cmd_line_w: [*:0]const u16) InitError!ArgIteratorWindows {
682 const cmd_line = mem.sliceTo(cmd_line_w, 0);
683 const wtf8_len = unicode.calcWtf8Len(cmd_line);
681 pub fn init(allocator: Allocator, cmd_line_w: []const u16) InitError!ArgIteratorWindows {
682 const wtf8_len = unicode.calcWtf8Len(cmd_line_w);
684683
685684 // This buffer must be large enough to contain contiguous NUL-terminated slices
686685 // of each argument.
......@@ -694,7 +693,7 @@ pub const ArgIteratorWindows = struct {
694693
695694 return .{
696695 .allocator = allocator,
697 .cmd_line = cmd_line,
696 .cmd_line = cmd_line_w,
698697 .buffer = buffer,
699698 };
700699 }
......@@ -1151,8 +1150,8 @@ pub const ArgIterator = struct {
11511150 }
11521151 if (native_os == .windows) {
11531152 const cmd_line = std.os.windows.peb().ProcessParameters.CommandLine;
1154 const cmd_line_w = cmd_line.Buffer.?[0 .. cmd_line.Length / 2 :0];
1155 return ArgIterator{ .inner = try InnerType.init(allocator, cmd_line_w.ptr) };
1153 const cmd_line_w = cmd_line.Buffer.?[0 .. cmd_line.Length / 2];
1154 return ArgIterator{ .inner = try InnerType.init(allocator, cmd_line_w) };
11561155 }
11571156
11581157 return ArgIterator{ .inner = InnerType.init() };