From dd144a19881d94b2060e3596057199c71137cad3 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Mon, 28 Sep 2026 18:00:39 -0700 Subject: [PATCH] Threaded.createFileMap: Update comments to reflect Windows prefetch efficacy findings With a test case that (1) calls createFileMap on a 250MiB text file, and (2) reads every byte in the mapped region looking for `\n`, hard page faults were eliminated in the `populate = true` case. `rammap -Et` is used to clear the relevant Windows cache. ``` > rammap -Et > bench-no-populate.exe .{ .page = 1090, .hard = 0 } // initial count .{ .page = 1112, .hard = 0 } // after createFileMap .{ .page = 1114, .hard = 0 } // after 1s sleep 4194304 newlines in the file .{ .page = 66779, .hard = 255 } > rammap -Et > bench-populate.exe .{ .page = 1093, .hard = 0 } // initial count .{ .page = 1242, .hard = 0 } // after createFileMap .{ .page = 1244, .hard = 0 } // after 1s sleep 4194304 newlines in the file .{ .page = 66782, .hard = 1 } ``` More details are in https://codeberg.org/ziglang/zig/pulls/36964 --- lib/std/Io/Threaded.zig | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig index 1095778cefc253851b223fe707a400c5f6832982..485624f2d8efaff1d95dfc33387d76482bb1bd0a 100644 --- a/lib/std/Io/Threaded.zig +++ b/lib/std/Io/Threaded.zig @@ -19089,7 +19089,7 @@ fn createFileMap( page, .{ // Required when calling NtCreateSection with a file handle, otherwise - // this call will fail with INVALID_PARAMETER. The higher level + // this call will fail with INVALID_PARAMETER_6. The higher level // `CreateFileMapping` function will set this bit for you when it // calls NtCreateSection. .COMMIT = true, @@ -19140,11 +19140,13 @@ fn createFileMap( .TO_WORKING_SET = false, }, }; - // It appears that this is effectively a no-op, as it doesn't decrease page/hard faults. + // There has been some doubt about the efficacy of this function: // https://github.com/microsoft/Windows-Dev-Performance/issues/108 // - // We still call it, though, to signal the intention of doing something useful. - // It doesn't appear to make anything *worse*, so there's no harm in doing so. + // However, through testing it has been shown that, if the mapped file is not + // in the cache, calling this function can turn what might otherwise be hard + // faults into soft faults. Either way, it's still good to call it in order to + // accurately reflect our intent. switch (windows.ntdll.NtSetInformationVirtualMemory( windows.current_process, .Prefetch, -- 2.54.0