Skip to content

fix(memory): lazy VirtualAlloc commit on Windows + mmap overcommit on macOS/Linux - #728

Merged
JeanPhilippeKernel merged 6 commits into
developfrom
fix/arena-windows-lazy-commit
Sep 1, 2026
Merged

JeanPhilippeKernel merged 6 commits into
developfrom
fix/arena-windows-lazy-commit

Conversation

@JeanPhilippeKernel

@JeanPhilippeKernel JeanPhilippeKernel commented Sep 1, 2026

Copy link
Copy Markdown
Owner

Summary

  • Root cause (Windows): CreateSubArena called Allocate(size) which triggered VirtualAlloc(MEM_COMMIT) for the full sub-arena budget upfront. By the time UIContext was allocated, earlier sub-arenas (ImportPipeline 3.5 GB + AssetManager 1 GB + VulkanDevice 1 GB) had already consumed pagefile quota and the UIContext commit returned NULL.
  • Fix (Windows): Sub-arenas now set m_committed_size = 0 and commit pages lazily in ArenaAllocateRaw. Sub-arena m_memory is page-aligned (m_mem_page_size) so VirtualAlloc(MEM_COMMIT) boundaries never cross into adjacent sub-arenas' pages.
  • Fix (macOS/Linux): Replace mmap(PROT_NONE) + per-allocation mprotect with mmap(PROT_READ|PROT_WRITE) + m_committed_size = size. The OS uses overcommit — physical pages are backed on first write only. Eliminates every mprotect syscall from the allocation hot path. The mprotect branches in ArenaAllocateRaw and Resize are removed entirely.

Test plan

  • ArenaSubArenaLifecycle — asserts m_committed_size = 0 on Windows, = size on macOS/Linux; allocation into sub-arena still works
  • ArenaSubArenaMultipleLargeSubArenas — regression: 3 x 1 MB sub-arenas from a 4 MB parent all succeed and have non-overlapping ranges
  • ArenaSubArenaPageAlignedOnWindows — Windows only: sub-arena m_memory is a multiple of dwPageSize
  • Full test suite on macOS: no regressions in existing allocator, VFS, logging, asset registry tests
  • Manual: engine starts on Windows without UIContext nullptr crash

… macOS/Linux

Windows: CreateSubArena no longer calls Allocate(size) which triggered
VirtualAlloc(MEM_COMMIT) for the full sub-arena budget upfront. With
ImportPipeline (3.5 GB) + AssetManager (1 GB) + VulkanDevice (1 GB)
committed before UIContext, pagefile quota was exhausted and the
UIContext VirtualAlloc returned NULL. Sub-arenas now start with
m_committed_size = 0 and commit pages lazily in ArenaAllocateRaw.
Sub-arena m_memory is aligned to m_mem_page_size (4 KB) so commit
boundaries never cross into adjacent sub-arenas' pages.

macOS/Linux: Replace mmap(PROT_NONE) + per-allocation mprotect with
mmap(PROT_READ|PROT_WRITE) + m_committed_size = size. The OS uses
overcommit — physical pages are only backed on first write. This
eliminates every mprotect syscall from the allocation hot path.
Sub-arenas inherit m_committed_size = size (parent mmap covers their
range). The mprotect branches in ArenaAllocateRaw and Resize are
removed entirely for macOS/Linux.

Tests: updated ArenaSubArenaLifecycle to assert platform-specific
m_committed_size; added ArenaSubArenaMultipleLargeSubArenas (regression
for the UIContext Windows bug) and ArenaSubArenaPageAlignedOnWindows.
@JeanPhilippeKernel JeanPhilippeKernel added this to the Stable Core (1.0.0) milestone Sep 1, 2026
@JeanPhilippeKernel JeanPhilippeKernel added area-linux Work on Linux system area-window Work on Window system area-macOS Work on macOS system labels Sep 1, 2026
@JeanPhilippeKernel JeanPhilippeKernel self-assigned this Sep 1, 2026
@JeanPhilippeKernel JeanPhilippeKernel moved this to In Progress in ZEngine Board Sep 1, 2026
@JeanPhilippeKernel
JeanPhilippeKernel merged commit e94edde into develop Sep 1, 2026
33 of 34 checks passed
@JeanPhilippeKernel
JeanPhilippeKernel deleted the fix/arena-windows-lazy-commit branch September 1, 2026 23:26
@github-project-automation github-project-automation Bot moved this from In Progress to Done in ZEngine Board Sep 1, 2026
JeanPhilippeKernel added a commit that referenced this pull request Sep 3, 2026
…mpleted/

Verified every doc's completion claim against the actual codebase — file
existence, key symbols, checklist items — rather than trusting the Status
line alone. Two categories of finding:

Checklist-hygiene gaps (real implementation, boxes just never ticked):
render-resource-manager.md, vfs-ticket2/4/5. Ticked every item after
confirming the referenced file/symbol/test exists. render-resource-manager.md
also got a naming-divergence note — the doc's proposed GPUResource.h/
GPUBuffer/GPUImage shipped as GpuAllocator.h's BufferView/BufferImage
instead; functionally identical, different names.

Genuine correctness gap: fly-camera-redesign.md claimed 'Implemented' with
every one of its own checklist items unchecked, and describes an entirely
different architecture (FlyCameraInput/FlyCameraState/EditorCameraController)
than what's actually in FlyCameraController.h (CamState enum,
SetViewportRect self-gating). Corrected the status to flag this and pointed
at the real design so a future reader isn't misled.

memory-allocator-audit.md — added a scope note: its 13 bugs (#497/#531) are
genuinely all fixed, but later, unrelated allocator bugs were found and
fixed independently this cycle (#680-683, #697, #728, #731) — pointed to the
wiki's Memory Management page for the current picture.

Moved to ZEngine/docs/completed/ (verified, no open items): asset-manager.md,
memory-allocator-audit.md, vfs-design.md, vfs-ticket2/3/4/5/6,
gpu-allocator-rearchitecture.md, render-resource-manager.md,
system-scheduler.md, ui-system.md.

Left in place — genuinely partial: tlsf-allocator-integration.md (Phase 3
blocked), logging-policy.md (real outstanding verification/benchmark tasks),
fly-camera-redesign.md (needs a content rewrite, not just a status fix), and
everything already marked Design/Planning/In Progress/Partially implemented.

Cross-checked every backtick-quoted reference to the 12 moved filenames
across the rest of the docs tree — all are informal textual mentions, not
markdown hyperlinks, so nothing broke.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-linux Work on Linux system area-macOS Work on macOS system area-window Work on Window system

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant