Skip to content

GPU: skip heap pages whose bytes did not change - #783

Open
naixue2233 wants to merge 2 commits into
fxceless:mainfrom
naixue2233:perf/heap-sync-copy-back
Open

naixue2233 wants to merge 2 commits into
fxceless:mainfrom
naixue2233:perf/heap-sync-copy-back

Conversation

@naixue2233

@naixue2233 naixue2233 commented Nov 25, 2026 •

Copy link
Copy Markdown

Rebased onto 9376fd6: the fingerprint change from this PR is gone (9376fd6 took it, record_print), and so is the one-write copy-back (the review on this PR: it filled the gaps between the runs with the title's bytes as read and wrote them after the loop, over anything the title wrote in between — the case upstream #2009 fixed; and it did not move the copying-back figure on its own). What is left is the first of the two costs. Nothing here changes what is computed; the picture is the same.

sync_heaps walked every record of every page the kernel called written. 300,000 to 485,000 records a frame, of which 12,000 to 20,000 were new. The kernel marks a page written even when the title put back the bytes it already held, so most of that walk found nothing. The page's bytes are now compared with the ones last read (64 KiB, a few microseconds) and the page's records are skipped when they are the same.

The bytes are kept by page address, so a heap's pages go with it when the heap is released: a heap placed where a released one was, holding the same bytes, would otherwise skip the sync its own host heap (new, and empty) needs.

Measured (with the copy-back still in), same scene band (2400 to 3200 draws/frame), medians of the frame lines, before and after:

before after
frame 815 ms 364 ms
preparing, copies 123 ms / 191 MB 71 ms / 194 MB
GPU 114 ms 133 ms
copying back 216 ms / 64 MB 210 ms / 62 MB

The first change is what moved the frame; the copies fell with it (fewer slots left stale, so less to refresh).

Honest limits: the run drifts through the prologue, so the bands are medians of 2 to 4 frame lines at matching draw counts rather than a fixed camera, and the later runs had a warm shader cache.

Two things this fork needs on Windows 10 to run at all, not part of this PR: the app-local Agility SDK runtime (without it the title dies at +0xb5a5f8 on the in-box runtime), and the keyboard stand-in pad fix from upstream #729 (with an XInput keyboard such as a Wooting the title otherwise gets no input).

from deepseek(v4.1 flash)

…k in one call

Two costs of the paths a26859f added, on a 3840x2160 window with a 2522 to
4394 draw world frame (RTX 3080, Windows 10, release build). 9376fd6 already
took the third (the fingerprint), so that is not here.

1. sync_heaps walked the 1,024 to 2,048 records of every page the kernel
   called written: 300,000 to 485,000 records a frame, of which 12,000 to
   20,000 were new. The kernel marks a page written even when the title put
   back the bytes it already held, so most of that walk found nothing. The
   page's bytes are now compared with the ones last read (64 KiB, a few
   microseconds) and the page's records are skipped when they are the same.

2. copy_back_with wrote every run of changed bytes through the kernel on its
   own: 53 to 75 MB a frame in runs a handful of bytes long, at 3 to 4
   ms/MB. The runs of one copy are contiguous, so they and the gaps between
   them (the title's bytes as read, so putting them back writes what is
   already there) now go back in one write per copy.

Measured, same scene band (2400 to 3200 draws/frame), medians of the frame
lines, before and after:

| | before | after |
| --- | --- | --- |
| frame | 815 ms | 364 ms |
| preparing, copies | 123 ms / 191 MB | 71 ms / 194 MB |
| copying back | 216 ms / 64 MB | 210 ms / 62 MB |

The first change is what moved the frame; the copies fell with it (fewer
slots left stale, so less to refresh). The second did not move the copying
back figure on its own — the one-write form lands in the same 210 to 256 ms
band, so the time there is not the write call itself. It is here because it
is fewer syscalls over the same bytes, not because I can show a win from it,
and it can be dropped without touching the first.

Honest limits: the run drifts through the prologue, so the bands are medians
of 2 to 4 frame lines at matching draw counts rather than a fixed camera, and
the later runs had a warm shader cache.

Two things the fork needs on Windows 10 to run at all, not part of this PR:
the app-local Agility SDK runtime (without it the title dies at +0xb5a5f8 on
the in-box runtime) and the keyboard stand-in pad fix from upstream fxceless#729
(with an XInput keyboard such as a Wooting the title otherwise gets no input).
@naixue2233
naixue2233 force-pushed the perf/heap-sync-copy-back branch from 4523733 to b5dee8d Compare November 25, 2026 07:35
@naixue2233 naixue2233 changed the title GPU: skip heap pages whose bytes did not change, print records cheaply, write a copy back in one call GPU: skip heap pages whose bytes did not change, and write a copy back in one call Nov 25, 2026
@not-rex

not-rex commented Nov 25, 2026

Copy link
Copy Markdown

Read through at b5dee8dc (not run). The page skip in sync_heaps looks like the right idea, but the one-write copy-back in copy_back_with looks like it brings back the bug upstream #2009 fixed:

  • Stale bytes over the title's writes (shadow.rs 2499-2523, 2541-2547). The gaps between runs are read into written_span while the loop goes on, then the whole stretch goes back in one write_mapped after the loop. There is no GAP limit in the code (the comments at 2479 and 2530 name one), so a stretch can run across most of the range. Anything the title, DirectStorage or another copy writes in a gap between that read and the final write is overwritten with the older bytes. That is the case the comments at 1700-1710 and 1813 describe (a record the title had replaced, then an access violation through 0xffffffff00000000, PalmVIDev e0b70476 / VIctory f39c6c2). The comment at 2526 still says "a title write landing there since stays", which no longer holds.
  • Writing the gaps back also marks those pages as title-written, so the next refresh copies them again.
  • Since the body says this half did not move the copy-back time, dropping it (keeping the per-run writes) would leave only the page skip, the part that measured 815 -> 364 ms.

On the page skip (descriptor.rs 223-231): seen_pages is keyed by address only and is cleared only at 4096 entries, so a heap released and a new one placed at the same address with the same bytes would skip its first sync. Unlikely, but clearing the entries for a heap's range when the heap goes would rule it out.

oceansde012 pushed a commit to oceansde012/victory-gta6 that referenced this pull request Nov 25, 2026
…se bytes did not change, and write a copy back in one call (by naixue2233)
oceansde012 pushed a commit to oceansde012/victory-gta6 that referenced this pull request Nov 25, 2026
pug205gti6 added a commit to pug205gti6/victory-fork that referenced this pull request Nov 25, 2026
`sync_heaps` walked the 1,024 to 2,048 records of every page the kernel
called written: 300,000 to 485,000 records a frame, of which 12,000 to
20,000 were new. The kernel marks a page written even when the title put
back the bytes it already held, so most of that walk found nothing. The
page's bytes are now compared with the ones last read (64 KiB, a few
microseconds) and the page's records are skipped when they are the same.

The bytes are kept by page address, so a heap's pages go with it when it
is released: a heap placed where a released one was, holding the same
bytes, would otherwise skip the sync its own host heap (new, and empty)
needs.

The one-write copy-back that came with this is dropped (the review on this
PR): it filled the gaps between the runs with the title's bytes as read
and wrote the whole stretch once, after the loop, over whatever the title
wrote there in between (the case upstream #2009 fixed). It did not move
the copying-back figure on its own either, and the runs go back as they
did before.

Measured with it still in, same scene band (2400 to 3200 draws/frame),
medians of the frame lines: frame 815 ms before, 364 ms after. The copies
fell with it (fewer slots left stale, so less to refresh).

Honest limits: the run drifts through the prologue, so the bands are
medians of 2 to 4 frame lines at matching draw counts rather than a fixed
camera, and the later runs had a warm shader cache.
@naixue2233 naixue2233 changed the title GPU: skip heap pages whose bytes did not change, and write a copy back in one call GPU: skip heap pages whose bytes did not change Nov 25, 2026
@naixue2233

Copy link
Copy Markdown
Author

Both points taken, at d695015:

  • The one-write copy-back is dropped. The runs go back per run as before, so a stretch's title bytes are never put back over a write that landed since (upstream #2009), and watch::writing covers exactly the bytes written. The page skip is the part that measured 815 to 364 ms, and it stays.
  • seen_pages loses a heap's pages when the heap goes (Heap's Drop), so a heap placed where a released one was, holding the same bytes, syncs its first page as its own host heap needs. The 4,096-page bound (256 MB) is in its comment, with what a world frame actually spans: 150 to 475 pages.

— deepseek v4.1flash

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants