Skip to content

[macOS] One host copy per physical-page backing instead of per title address (16-18 GB Macs) #554

Backlog

Description

@pragyaangaur

The problem

On 16 to 18 GB Macs the world is memory bound, and the biggest avoidable cost is duplicate host copies of the title's physical pages.

GTA VI asks for its physical pages as one section of about 6.1 GiB (XMemAllocatePhysicalPages) and maps them into its address space 64 KiB at a time, each page at about three addresses. A measurement on Series S found 165,000 view pieces, which are 14.5 GB of addresses over 4.7 GB of pages. Under Wine the CPU side is fine, because every view of a section shows the same pages. VIctory keys its host copies (shadow::find, buffer copies and loose windows) by title address, so a page shown at three addresses gets up to three copies, and each is refreshed, compared and copied back on its own.

On an 18 GB M3 Pro running Series S under D3DMetal, free memory is about 80 MB from 30 s into the world, the compressor holds 5 to 7 GB, swap grows and wired memory is 10 to 12 GB.

What does not work (measured)

  • Zero copy of the whole pool on D3DMetal (VICTORY_GPU_POOLS=1 and VICTORY_ALIAS_BUFFERS=1) loses. GPU-visible memory is wired on Apple silicon, so all 4.7 GB of physical pages become unswappable, untouched pages included (wired 14.5 GB against 10.4 GB at the same point). D3DMetal cannot wrap existing memory, so "zero copy, wired only where used" is not possible there.
  • Shaders must never write title memory in place. The title keeps CPU objects in its physical pages, and a translated byte or half store is a read-merge-write over a whole word. The copy path's write-back of changed bytes (Shadow::gpu_writes) has to stay.

Proposal: one host copy per backing, not per address

Key copies of physical-page memory by (section, offset) instead of title address, so every alias of a page binds the same host copy at its offset.

  • The runtime already knows every view's section and offset (victory-hle xmem.rs: Physical::views, Slice, Section::whole). The GPU side hears of maps and unmaps (title_memory_remapped, title_memory_aliased).
  • A copy's source for refreshes would be the runtime's whole view of the section (Section::whole), which never moves, instead of whichever title address asked first.
  • Write tracking: a title write through any alias must dirty the backing copy. watch already follows aliases (title_memory_aliased, the shown table), and the copy's changed pages would key on the backing page.
  • A range over several views whose pages are not consecutive in the section still needs a contiguous GPU range. Copy those as today at first, and count how often it happens (about half of view-memory ranges in a census on a native layer: 4,252 from one run, 3,789 from several places).

Expected: up to about 3 times fewer bytes copied, refreshed and compared for view memory, fewer wired host buffers, and only GPU-used ranges copied.

How to measure

  • A 5 s vm_stat sampler (free, wired, compressor, swap) during a run.
  • VIctory's own line host copies of title memory: N alive X MB (copystat), before and after.

A first step that pays on its own

A census of host copies by size, age and last use, plus how many title addresses share a backing page.

Text findings and numbers only, please. No game files, shaders, dumps, logs with game data or recordings.

Activity

  1. hackgods commented on Nov 27, 2026

    @hackgods
    Contributor

    Some measurements that bear on keying copies by backing page. They are from synthetic tests under Wine with D3DMetal on an M5 Pro (macOS 27), with no game data. The tests are on our fork (crates/victory-gpu/src/tiles.rs, branch perf/inplace-tiles).

    Reserved buffers with tiles mapped onto heaps (CreateReservedResource + UpdateTileMappings) work under D3DMetal, and they would give each alias range a contiguous GPU range over one backing copy:

    • Tiles from two heaps, in any order, back one contiguous range, and a copy through it reads each tile's own bytes. That would also cover the ranges your census found over views that are not consecutive in the section, about half of them.
    • Reserved buffers of 8 GiB work, with a tile mapped past 6 GiB.
    • A list recorded before an UpdateTileMappings on the queue, and executed after it, reads the new tile. So the mapping is taken at execution, as on the console, and a remap needs no re-recording. Mapping a range again six times in a row read the right tile each time.
    • Unmapped (null) tiles read zeros.

    Cost of the tile mapping:

    • One tile per call: about 0.7 µs.
    • 4,096 tiles in one call: 0.47 ms (about 0.11 µs a tile).
    • A copy plus fence wait went from 232 µs to 245 µs with a tile update queued before it, so about 12 µs of queue latency.

    Coherence of one page at several GPU addresses, within one list. The test fills a 64 KiB page with zeros and writes 7s into its first half through one address. After a UAV barrier, a second dispatch reads them through another address and writes them plus one into the second half. That half is copied out in a submission of its own and checked word by word, 8,192 words per case:

    written through read through barrier wrong words
    tile 0 tile 0 (control) all UAVs 0
    tile 0 tile 0 (control) the one resource 0
    tile 0 tile 1, same heap tile all UAVs 0
    tile 0 tile 1, same heap tile the one resource 0
    tile 0 tile 1, next submission none 0
    tile 0 the heap's own placed buffer all UAVs 0
    the heap's own placed buffer tile 1 all UAVs 0

    So under D3DMetal, each alias address could map as tiles onto one backing copy without losing coherence between aliases inside a submission, at least for compute work separated by barriers. Reads with no barrier inside one dispatch were not tested.

    A side finding. A CopyBufferRegion in the same list after compute writes is not ordered behind them by a UAV barrier alone:

    • With only the UAV barrier, 6,800 of 8,192 words were wrong (the copy saw the page before the dispatches finished).
    • With a UAV → COPY_SOURCE transition before the copy, 0 were wrong.

    D3D12 requires the transition anyway. But any host path that copies out of a buffer just written by shaders in the same list, with only UAV barriers between, would read stale data under D3DMetal.

    On never writing title memory in place: agreed, and we have made our in-place experiments read-only.

    • GPU work bound to write a range lets it go to a copy with the changed-bytes write-back. The page table names no in-place memory.
    • For reference, the translator lowers buffer_store_byte/short as two dword atomics (clear the field, then set it). Other bytes of the dword are not rewritten. But the field reads 0 between the two, and a CPU write to the same byte in between is OR-merged, so the copy path's merge is needed.
    • global_store_byte/short are refused by the translator.

    🤖 Generated with Claude Code

    The count of pages the title shows at more than one address will follow from a game run.

    Measured and written with Claude Code (Claude Opus 5.5).

  2. Stefa008 commented on Nov 27, 2026

    @Stefa008
    Contributor

    Some numbers from a 16 GB M4 (MacBook Pro, Series S, 1280x720 output, CrossOver Wine + D3DMetal 4.0 beta 3). It looks like the same picture you describe.

    Build: main a31fcca1 plus halfwave50's mesh row merge (now #578), with VICTORY_RAM_LOW=0.15 VICTORY_RAM_FREE=0.20 VICTORY_SMALL_LOOSE_WINDOWS=1 VICTORY_ALIAS_BUFFERS=0 VICTORY_EVICT_HEAP_UAV=1 VICTORY_SPILL_SHADERS=1 VICTORY_CENSUS_FAST=1. Prison cell, full scene (~4,550 draws), three runs back to back:

    run frame GPU fps
    default 820–830 ms 565–625 ms 1.2
    VICTORY_ATOMIC_LOCKS=0 790–836 ms 627–649 ms 1.2–1.3
    VICTORY_MERGE_MESH_ROWS=0 800–890 ms 634–661 ms 1.1–1.2

    In all three, free memory stays at 16–21% and swap sits at 5–5.8 GB once the world loads. So the GPU time isn't the atomic locks (the same with them off) or the mesh layout. The GPU costs about 130–140 ms per 1,000 draws here, where 24 GB Macs report far less (#577), so the memory pressure looks like the likely cause, as in your 18 GB numbers.

    Happy to test a branch of this on 16 GB when there is one.

    AI-assisted, prepared with Claude Code.

  3. maxdp66 commented on Nov 27, 2026

    @maxdp66

    More numbers from a 16 GB Mac (M1 Pro), same picture as above, plus one thing I could not explain: a constant 5.2 GB of native heap.

    Setup: M1 Pro, 16 GB, macOS 27.2, D3DMetal 4.0 beta 3, --console series-s, 1280x720 (default), host FidelityFX 3.1.6 via VICTORY_FIDELITYFX_DIR, VICTORY_PATCH_MASKED=all VICTORY_FAULT_TRACKING=1 VICTORY_NO_HANG_WATCHDOG=1 VICTORY_EVICT_HEAP_UAV=1 VICTORY_SPILL_SHADERS=1 VICTORY_RAM_LOW=0.20 VICTORY_RAM_FREE=0.26 VICTORY_STEP_OVER=pool VICTORY_AMBIENT=1.

    Build

    main 8baba18 (the world draws lit there), release build unless noted. Pre-build: 260 refused containers, 55 compute pipelines skipped, 13 refused by the host.

    Speed and memory

    0.3 to 0.8 fps in the world (1.3 to 3 s a frame) in a release build. The process sits at 17 to 18 GB with 9 to 11 GB of swap, so most of the CPU-side time is paging:

    footprint category GB
    IOAccelerator (graphics) 5.7 to 6.8
    Malloc Medium 5.2 (see below)
    WINE_RESERVE 2.8
    Owned physical footprint (unmapped) (graphics) 1.9 to 2.4

    Per frame in a diagnostics build (copystat, one interval in the world):

    protecting 545 ms (2,295 protect calls for 3,981 pages)
    guard handler 235 ms (1,480 pages opened)
    moves 468 ms for 111 MB (slow: swapped pages)
    marked copy-back 260 ms (679 pages, 25 MB)
    heap texture uploads 255 ms
    heap slots the title copied 110 ms (446k slots)
    tracked pages written 1,166, of them 625 the frame before too

    Host buffer copies held 2.35 GB (title buffers 0.92 GB, loose windows 1.43 GB with the 256 KiB windows).

    What I tried (all on 16 GB)

    Change Result
    VICTORY_HEAP_WORKER=1 VICTORY_TABLE_MEMO=1 VICTORY_CENSUS_FAST=1 Heap texture uploads 254 → 44 ms a frame, but the worker's staging added 285 MB, swap reached 10.8 GB, protecting rose to 887 ms, and the GPU ran out of memory 44 s into the world (kIOGPUCommandBufferCallbackErrorOutOfMemory, then "a new queue does not run it either")
    VICTORY_ALIAS_BUFFERS=1 Aliased the five big pools (about 3 GB), but the footprint stayed 18 GB, copies stayed 200 to 270 MB a frame (loose windows), and GPU time rose from 0.6–0.8 s to 2.4–2.7 s a frame
    64 KiB loose windows (local override of SMALL_WINDOW) + VICTORY_IDLE_RELEASE_S=20 No measurable change in footprint, swap or frame time
    A fresh command allocator at every HostList reset (local experiment) No change: 1,782 D3DMetal allocators alive, native heap unchanged

    Malloc Medium is 5.2 GB in every run

    Across all the runs above it stayed between 5.17 and 5.22 GB, while the host copies varied from 2.4 to 5.2 GB. A local diagnostics count of every upload, readback and write-back custom buffer VIctory creates (by call site) came to about 3 GB, so the 5.2 GB is not ours. heap shows about 2 million non-object allocations (7.1 GB), 44,000 of them 64 KB or larger (6.1 GB). D3DMetal's shaders.cache for victory.exe (6.6 GB) is memory-mapped, not on the heap. I could not attribute it further:

    • MallocStackLogging=lite and MallocStackLogging=malloc both crash Wine at start (Unhandled page fault on read access to 000000000011D7B0, right after low_guards), so allocation stacks are not available this way.
    • Attaching lldb to the game stalled for minutes, even with symbol preloading off.

    If anyone knows what D3DMetal keeps on the heap at this size, that would be the next thing to cut on 16 GB.

    Observation: unattended runs stall at loading

    Runs started while nobody was at the Mac (release and diagnostics, with my changes and on unmodified upstream) stopped on the loading artwork about 2 minutes in, at 0.8 fps with 11 draws a frame and about 1.1 s of "preparing" a frame. The same builds passed that point when I was at the Mac. A --cache pre-build run while away also paused for about 16 minutes. My guess is macOS throttling (App Nap or display sleep), not confirmed.

  4. ottco-dev commented on Nov 27, 2026

    @ottco-dev
    Contributor

    @Stefa008 the branch you asked about is up now: ottco-dev/VIctory-macOS-halfwave50 cherrypicker-ottco (cd65104).

    It is halfwave50's main-mac (cbf96d2) with upstream #381 #415 #416 #436 #452 #456 #495 #499 #530 #533 #535 #551 #567 #571–#576 #579 #580 #582 #583 picked on top, plus a few mac: commits (one Instant::now per copy instead of per range, one periodic waitable timer for the held-work tick, a multiply hasher for Batch's address maps, diagnostics counters).

    On a 24 GB M5 Pro (Series X, CrossOver Wine, D3DMetal 4.0b3) it runs the prison cell at ~180–187 ms a frame (5.3–5.5 fps) with:

    VICTORY_OUTPUT=1920x1080 VICTORY_RENDER_SCALE=0.5 VICTORY_CENSUS_BATCH=1 VICTORY_GPU_BUDGET_MB=4500
    VICTORY_RAM_LOW=0.12 VICTORY_RAM_FREE=0.16 VICTORY_ALIAS_BUFFERS=0 VICTORY_SMALL_LOOSE_WINDOWS=1
    VICTORY_EVICT_HEAP_UAV=1 VICTORY_SPILL_SHADERS=1 VICTORY_STEP_OVER=pool VICTORY_NO_HANG_WATCHDOG=1
    

    (everything else as tools/macos/play-main-mac.sh sets it). Measured per step in #505's follow-up. I have not run it on 16 GB; with 16 GB the RAM marks above are probably too low, the 0.15/0.20 you use is a better start.

    AI use: Claude Code (Claude Opus 5.5) built and benchmarked the branch and wrote this comment.

  5. maxdp66 commented on Nov 27, 2026

    @maxdp66

    Follow-up to my comment above: the constant 5.2 GB of native heap is D3DMetal's own per-exe shader cache, which it reads into memory when the device is created. On a 16 GB Mac that was about a third of the process.

    Measurements (M1 Pro 16 GB, main d1609d9, D3DMetal 4.0 beta 3, Series S)

    footprint sampled every 3 to 5 s from launch:

    Malloc Medium at "host D3D12 device ready" 16 s after launch ~90 s after launch
    D3DMetal cache $(getconf DARWIN_USER_CACHE_DIR)d3dm/victory.exe as it was (6.7 GB) ~4.8 GB (1.8 → 3.3 → 4.8 GB in the 15 s before the device was ready) 5.27 GB 5.28 GB
    Same folder renamed aside (nothing else changed) 9 MB 117 MB 291 MB

    With the folder renamed, the device was also ready after about 6 s instead of about 15 s.

    Our cache was 6.7 GB because victory translate --cache --pipelines compiles every pipeline in the shader pack (28,164 here) into that cache. The game itself started 2,614 pipelines early and made or loaded about 2,500 in the world.

    A normal run on a cache the game filled by itself (260 MB after one session): footprint 17–18 GB → 12 GB, Malloc Medium 5.2 GB → 0.6 GB, no GPU out-of-memory. The first trip into the world compiles what is missing (1,509 made and 981 loaded, finished about 4 minutes after the world appeared); frame times on heavy frames were in the same range as with the full cache.

    So on 16 to 32 GB Macs, pre-building the whole pack with --pipelines costs about as much memory as it saves compile time. It may be worth a note in docs/backend.md (which recommends translate --cache --pipelines) that D3DMetal keeps its whole per-exe cache in memory, and perhaps a pre-build of only the pipelines a run actually used (VICTORY_PIPELINE_LOG already lists them).

    Smaller findings from the same runs

    • Where host copies of title memory live. A diagnostics-only ballast of 1 GB of 64 KiB shared_buffers (custom heap, write-back) went into IOAccelerator (graphics), not the malloc heap. So the copies [macOS] One host copy per physical-page backing instead of per title address (16-18 GB Macs) #554 is about count against GPU memory.
    • Texture heaps keep partly empty space. About 12 minutes into the world: 64 MiB heaps had 637 MB free out of 1,811 MB reserved. A local switch for 16 MiB heaps changed nothing (968 MB free out of 1,745 MB; GPU total 7,054 vs 7,145 MB). Only fully empty heaps are released, so moving the survivors out of mostly empty heaps would be needed to get that space back.
    • What swap costs. The same prison-window scene (about 4,600 draws) took 5.5 to 12.5 s a frame while the Mac swapped about 190 MB/s each way, and 2.0 to 2.9 s a frame at 20 to 30 MB/s, with the same 12 GB footprint (the rest of the Mac's memory differed between the runs). Fitting in memory is worth 2 to 4x here.
    • For the record: of 12 random large heap blocks from a memory graph, the direct holder was a dispatch_data_t for 8, small 48-byte nodes for 3 and a D3DMetal command list for 1; which fits the cache explanation better than command lists.
  6. Stefa008 commented on Nov 27, 2026

    @Stefa008
    Contributor

    Tried @ottco-dev's cherrypicker-ottco (cd65104) on the 16 GB M4 (MacBook Pro, Series S, 1280x720 output, CrossOver Wine + D3DMetal 4.0 beta 3). On top of play-main-mac.sh I keep VICTORY_ALIAS_BUFFERS=0 VICTORY_RAM_LOW=0.15 VICTORY_RAM_FREE=0.20 VICTORY_SMALL_LOOSE_WINDOWS=1 VICTORY_EVICT_HEAP_UAV=1 VICTORY_SPILL_SHADERS=1, because aliasing ran this Mac out of GPU memory before.

    Big step up from main here. Prison yard/cell, numbers taken a few minutes after world entry (the first minutes are still loading the scene and look 3–5x worse):

    fps frame GPU
    main a31fcca1 (my earlier comment) 1.2 820–830 ms 565–625 ms
    cherrypicker-ottco 2.5–2.8 350–395 ms 135–150 ms
    cherrypicker-ottco + #534 2.3–3.2 310–430 ms 137–162 ms

    One thing worth picking: the branch doesn't have #534 yet, and on 16 GB that shows. Rust heap in the frame report grows from about 1.4 GB at world entry to 3.6 GB 7 minutes later. The allocation trace points at the descriptor::VIEWS list doubling (34 → 272 → 544 → 1088 MB blocks). Swap fills up and fps drops over time. With #534 ported by hand (it doesn't apply cleanly, since descriptor.rs differs, but only record, forget, view_at and views_current_into need the decode), it stays flat: 1.17 GB → 1.27 GB over the same 7 minutes. Its tests pass on the branch.

    Also tried #596 (VICTORY_HOT_PAGES=1) on this branch. Measured after the scene settled, it's neutral here: 2.5–2.8 fps, about the same guard handler and lock wait times.

    What's left per frame: preparing 135–200 ms (host copies ~250 MB/frame take 85–160 of it), GPU ~150 ms, copy-back ~35 ms. Swap still sits around 5.5 GB, but that's video memory (~6.9 GB) plus loose windows (~1.4 GB), not growth.

    AI-assisted, prepared with Claude Code.

  7. Stefa008 commented on Nov 27, 2026

    @Stefa008
    Contributor

    Small follow-up on cherrypicker-ottco on the 16 GB M4 (Series S, 1280x720, D3DMetal).

    The ~265 draws/frame the frame report shows as "dropped without a host pipeline" in the prison cell weren't missing shaders. They're the BVH builder's dispatches: all 53 of its pipelines are made (0 refused), but pipeline::builder::opens holds it back because play-main-mac.sh turns on VICTORY_HALVES_LOCAL / VICTORY_HALVES_SHARED_LDS, so it never runs on a Mac with that script. The diagnostics drop list doesn't name them either, since they have a host pipeline and no reason gets recorded.

    I added a switch locally to let it run anyway (VICTORY_BVH_HALVES=1, off by default) to see if the RTX 4070 hang shows up on Apple GPUs:

    dropped/frame GPU fps
    builder held back ~265 160–170 ms 3.3–3.4
    builder runs as halves 0 190–220 ms 2.8–3.1

    No hang in ~10 minutes in the world, no device-removed errors. Main doesn't have the halves gate at all, so this matches what main does on Macs. Maybe the gate could be keyed on the host (Apple vs the 4070 case) rather than on the halves switches?

    AI-assisted, prepared with Claude Code.

  8. pragyaangaur commented on Nov 28, 2026

    @pragyaangaur
    OwnerAuthor

    Thank you Stefa008, this is a real finding: pipeline::builder::opens holds the BVH builder back whenever VICTORY_HALVES_LOCAL or VICTORY_HALVES_SHARED_LDS is on, so a Mac that runs play-main-mac.sh never runs it, and the dropped draws are not missing shaders. #591 (merged today) lets the builder translate on a 32-lane host, so the hold-back is now the only thing in the way. Please send your VICTORY_BVH_HALVES=1 switch as a small PR, off by default, with a line in docs/macos.md, and say in the PR what the frame looked like with it. 4TheDoctor's notes on #410 and #442 say the builder can break the picture when it does run, so the switch should stay opt-in.

  9. Stefa008 commented on Nov 28, 2026

    @Stefa008
    Contributor

    Thanks! Went to make the PR against main and it turns out there's nothing to send: main never had the halves hold-back, that's only in the main-mac / cherrypicker-ottco fork. On main #591 already added its own opt-in, VICTORY_BVH_BUILDER=1, and its notes say why it stays off (the builder's lists read title memory with no host copy, the cell goes dark, macOS kills submissions). So VICTORY_BVH_HALVES would just be a second switch for the same thing.

    On the fork it's still useful, so I'll offer it there instead (off by default, same as now).

    Numbers on the fork with it on, for the record (16 GB M4, Series S, 1280x720, flights + VICTORY_SHADER_OPT=64): cell 3.7–3.9 fps, GPU 114–119 ms, and the builder costs nothing measurable there (3.79 without it vs 3.86 with). Moving around it's worse: walking out of the cell into the corridor drops to 1.6–2.0 fps with host copies at 330–380 MB a frame, and textures are wrong for the first few minutes.

    AI-assisted, prepared with Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions