Lazy views of title pages (switch, off by default)

VICTORY_LAZY_VIEWS=1: the title's own mapping calls (XMemMapPhysicalPages,
and the scatter form a page at a time) note the view in the books and
tell the GPU side of it as before, and make its addresses a placeholder of
their own (a split; a VirtualQuery to make sure), but map the host view
only when something touches it. A view no one touched is let go with no
system call, and mapped again at the same addresses with none. The title
streams into its pool one call a page (365,000 calls, 25.7 s in 4 minutes
of run x1440-streamlog; 846 ms of 1.7 s on a cutscene's critical path,
timeline tl.120), DirectStorage fills the pages through views of its own,
and the page-mapping copies read the pool from the books alone.

A lazy view is made real (xmem::materialize, the books' lock only) by:
- any fault in it, the title's or ours (direct reads and writes, our safe
  copies and puts): the GPU side's fault handler, first in the process,
  asks before anything else and makes the access again
  (victory_gpu::use_lazy_views);
- the GPU side before what that handler does not see: reads and writes
  through the kernel (host::read, write_mapped, copy_mapped's title
  destinations), kernel writes the runtime announces (kernel_writing: file
  reads, DirectStorage memory destinations), guarding a range
  (watch::writes), and asking Windows whether memory is committed
  (texture::committed);
- the title's XMemVirtualQuery, XMemVirtualAlloc at an address, and
  VirtualFree, which then see a view as before.

Lock order: the remapping lock (victory_gpu::title_memory_remapping) is now
taken before the views' lock in every mapping call, so a reader that holds
the reading lock and faults into a lazy view gets the views' lock while a
mapping call waits for its lock (the old order, views first, would wait
for good). Page-mapping copies and side-by-side mapping map as before and
let lazy views go as their own. The streaming log's M line gets three more
fields with the switch: lazy views noted, let go untouched, made real.

Tests: lazy_views_are_mapped_when_touched (a read faults and maps it with
the pages' bytes; a write lands in the pages; untouched ones go without a
host view; a lazy view cut in two stays lazy at its ends; XMemVirtualQuery
maps it), a_reader_in_place_faults_into_a_lazy_view_while_a_mapping_waits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
readmet3xt-labandClaude Opus 5.5 committed 2026-11-26 19:27:11 +05:30
1 parent dfe9acca03
commit f12996ca37
5 files changed
+408 -15

No files matched your search

+7 -1
View File
@@ -8287,7 +8287,9 @@ pub(crate) fn copy_mapped(dst: *mut u8, src: *const u8, len: usize, title_dst: b
return;
}
if title_dst {
// The kernel writes the destination: it must not be guarded.
// The kernel writes the destination: it must not be guarded, and it
// must be a mapping of the host's.
crate::watch::materialize(dst as usize, len);
crate::watch::writing(dst as usize, len);
}
#[cfg(feature = "diagnostics")]
@@ -8440,6 +8442,9 @@ pub(crate) fn read_there(src: *const u8, dst: &mut [u8]) -> usize {
fn read(src: *const u8, dst: *mut u8, len: usize) -> bool {
use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
// (The kernel's read does not fault into the handler that makes lazily
// mapped title memory real.)
crate::watch::materialize(src as usize, len);
if crate::safecopy::on() {
// SAFETY: the destination is mapped writable memory of `len` bytes;
// a source page that is not there fails the copy.
@@ -8481,6 +8486,7 @@ fn direct_writes() -> bool {
pub(crate) fn write_mapped(dst: *mut u8, src: *const u8, len: usize) {
use windows_sys::Win32::System::Diagnostics::Debug::WriteProcessMemory;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
crate::watch::materialize(dst as usize, len);
let direct = direct_writes();
let since = crate::watch::changes();
let write = |at: usize, n: usize| {