heap_lazy = 1 commits the game's heap a 1 MiB chunk at a time on first use (Windows, opt-in)

SetHeapSize takes the game's whole heap at boot (2.1 GiB on 1.4.3) and Windows charges all of it against the commit
limit, although a sky-island run touched about two thirds. With TOTK_HEAP_LAZY=1 the heap stays reserved: the first
read or write of a chunk faults and memory.c heap_fault commits it; read faults reach the hook only in this mode
(plat_mem_fault_reads). A byte per chunk (lazy_map) answers without asking the OS, so frozen-region write faults pay
nothing for it, and only clear chunks are committed, never pages a freeze made read-only. mem_thaw, which file reads,
save writes and Vulkan imports call before the kernel or driver touches guest memory, commits its range's chunks
first; ipc.c and svc.c check readability by the layout; vk_mem_host_room counts the uncommitted heap as taken. Default
off: the heap path is unchanged unless set.

Sky island, 120 s, this laptop (Iris Xe): peak commit 6345-6536 MiB on dev, 5736-5821 MiB with heap_lazy = 1; about
600 first-use faults, 8 ms in all.
This commit is contained in:
Ruan Shannon committed 2026-11-22 04:49:58 -08:00
1 parent afae80c908
commit 6370c7c0b6
11 files changed
+142 -8

No files matched your search

+2
View File
@@ -985,6 +985,8 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)) {
fault_install();
}
void plat_mem_fault_reads(int on) { (void)on; }
void plat_crash_hook(void (*fn)(const PlatCrash *c)) {
crash_fn = fn;
fault_install();
+3
View File
@@ -220,6 +220,9 @@ uint64_t plat_mem_written(uint64_t addr, uint64_t n, uint64_t *pages, uint64_t m
// A write that faulted on a read-only page at addr goes to fn first; fn returns 1 when it made the page writable and
// the write should run again, 0 to let the fault go on (to the crash handler). One hook per process.
void plat_mem_fault_hook(int (*fn)(uint64_t addr));
// Read faults go to the hook too (memory.c heap_fault, TOTK_HEAP_LAZY): Windows only, before the game starts; a no-op
// on Linux, which commits on first use by itself.
void plat_mem_fault_reads(int on);
// Starts this program again with the same arguments and ends this process with the new one's exit code (Windows), or
// replaces this process with it (Linux). Environment changes made before the call are passed on.
_Noreturn void plat_reexec(void);
+10 -2
View File
@@ -721,10 +721,16 @@ uint64_t plat_mem_written(uint64_t addr, uint64_t n, uint64_t *pages, uint64_t m
static int (*fault_hook)(uint64_t addr);
// A write access violation (ExceptionInformation[0] == 1) goes to the hook; registered first, before the crash handler.
static ULONG_PTR fault_kinds = 1; // highest ExceptionInformation[0] the hook takes: 1 writes, 0 also reads
// A write access violation (ExceptionInformation[0] == 1) goes to the hook, and a read (0) too once
// plat_mem_fault_reads asked for it; registered first, before the crash handler. Writes thaw frozen regions; with
// TOTK_HEAP_LAZY reads and writes both commit heap pages on first use (memory.c, heap_fault). An execute fault (8) is
// never the hook's.
static LONG CALLBACK fault_veh(EXCEPTION_POINTERS *ep) {
const EXCEPTION_RECORD *er = ep->ExceptionRecord;
if (er->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || er->NumberParameters < 2 || er->ExceptionInformation[0] != 1)
ULONG_PTR kind = er->NumberParameters >= 2 ? er->ExceptionInformation[0] : 8;
if (er->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || kind > 1 || (kind == 0 && fault_kinds != 0))
return EXCEPTION_CONTINUE_SEARCH;
return fault_hook((uint64_t)er->ExceptionInformation[1]) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
}
@@ -734,6 +740,8 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)) {
AddVectoredExceptionHandler(1, fault_veh);
}
void plat_mem_fault_reads(int on) { fault_kinds = on ? 0 : 1; }
static void (*crash_fn)(const PlatCrash *c);
// The faulting thread's general registers, for the report to tie the faulting instruction to its operands.