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

fix(memory): lazy VirtualAlloc commit on Windows + mmap overcommit on macOS/Linux#728
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
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