Add heap_lazy (off by default): on Windows the game's heap is committed where it is first used.

Windows charges every committed page to the commit limit whether it is used or not, and the game sizes its whole heap up front: in Castle Town after Hyrule Field the heap held 6.8 GiB of commit of which 3.4 GiB had ever been used. With heap_lazy = 1 a change of the heap's size only moves its end, an access to a reserved heap page commits the reserved runs of the 64 KiB around it in the access-fault hook and runs again, and the paths that do not fault commit their range first: mem_thaw (file reads, the host-pointer import), mem_kernel_access (file writes, the frame-rate fix's read) and mem_host_readable (the host's pointer checks). A page another path committed is left as it is, so a frozen, probed or armed page keeps its protection. With trace = perf a line per window gives the faults, the bytes committed and the time they took.

The contributor's runs on a 16 GB PC: 4.67 instead of 6.77 GiB of heap committed and 29.3 instead of 32.9 GiB of process commit in Castle Town, no "system commit low", 12,858 faults in 440 ms over four minutes, the longest 41 ms. With the setting off nothing is hooked and the heap is committed as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-10 23:04:40 -05:00
parent 73abd6577f
commit dda12051f5
10 changed files with 133 additions and 15 deletions

View File

@@ -1152,8 +1152,10 @@ static uint32_t write_file(uint64_t h, int64_t offset, const void *buffer, uint6
if (file->save) save_touched();
for (uint64_t done = 0; done < size;) {
uint32_t chunk = size - done > 0x40000000 ? 0x40000000 : (uint32_t)(size - done);
// NX2NAVI_WB_PROBE: the kernel reads the buffer, and a probed page would fail the write instead of faulting
// NX2NAVI_WB_PROBE: the kernel reads the buffer, and a probed page would fail the write instead of faulting;
// so would a heap page NX2NAVI_HEAP_LAZY has not committed yet
if (mem_probing()) mem_thaw((uintptr_t)buffer + done, chunk);
mem_kernel_access((uintptr_t)buffer + done, chunk);
int64_t put = plat_file_pwrite(f, (const uint8_t *)buffer + done, chunk, (uint64_t)offset + done);
if (put <= 0) rt_fatal("fs: host save write failure");
done += (uint64_t)put;