From 4841cc710753358c82f6e647473aff39225b5fce Mon Sep 17 00:00:00 2001 From: meower <59313695+a55uka@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:36:59 +0000 Subject: [PATCH] xmem: commit physical-page sections whole under Wine XMemAllocatePhysicalPages makes SEC_RESERVE sections and commits their pages one view at a time. Under Wine each commit is a wineserver request that scans every range committed so far, so wineserver ran at full load while the title's threads waited on it, and loading hardly moved. Under Wine (detected by ntdll's wine_get_version export) the sections are now SEC_COMMIT and the per-view commit is skipped. Wine backs a committed section with a sparse file, so untouched pages still cost nothing. Windows keeps SEC_RESERVE, since a busy host may be unable to commit the 6.1 GiB the title asks for in one go. Tested: Linux (Wine 11.21), i7-12700K, RTX 4070. wineserver load drops after the first seconds and the title gets to its loading screen, drawing and dispatching. --- crates/victory-hle/src/modules/xmem.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/victory-hle/src/modules/xmem.rs b/crates/victory-hle/src/modules/xmem.rs index bcd3222..0f463b7 100644 --- a/crates/victory-hle/src/modules/xmem.rs +++ b/crates/victory-hle/src/modules/xmem.rs @@ -18,8 +18,8 @@ use windows_sys::Win32::System::Memory::{ CreateFileMappingW, MEM_ADDRESS_REQUIREMENTS, MEM_COMMIT, MEM_EXTENDED_PARAMETER, MEM_PRESERVE_PLACEHOLDER, MEM_RELEASE, MEM_REPLACE_PLACEHOLDER, MEM_RESERVE, MEM_RESERVE_PLACEHOLDER, MEMORY_BASIC_INFORMATION, MEMORY_MAPPED_VIEW_ADDRESS, MapViewOfFile3, - MemExtendedParameterAddressRequirements, PAGE_NOACCESS, PAGE_READWRITE, SEC_RESERVE, - UnmapViewOfFile2, VirtualAlloc, VirtualAlloc2, VirtualFree, VirtualQueryEx, + MemExtendedParameterAddressRequirements, PAGE_NOACCESS, PAGE_READWRITE, SEC_COMMIT, + SEC_RESERVE, UnmapViewOfFile2, VirtualAlloc, VirtualAlloc2, VirtualFree, VirtualQueryEx, }; use windows_sys::Win32::System::ProcessStatus::{K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS}; use windows_sys::Win32::System::Threading::GetCurrentProcess; @@ -29,6 +29,19 @@ use crate::{HleModule, hle_export}; const MIB: usize = 1 << 20; +/// Whether we run under Wine (its ntdll exports `wine_get_version`). +pub(crate) fn on_wine() -> bool { + static WINE: LazyLock = LazyLock::new(|| { + use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress}; + // SAFETY: plain lookups in ntdll, always loaded. + unsafe { + let ntdll = GetModuleHandleA(c"ntdll.dll".as_ptr().cast()); + !ntdll.is_null() && GetProcAddress(ntdll, c"wine_get_version".as_ptr().cast()).is_some() + } + }); + *WINE +} + /// Standard memory the title has handed to the system. static TRANSFERRED: AtomicUsize = AtomicUsize::new(0); @@ -388,7 +401,11 @@ pub extern "system" fn XMemAllocatePhysicalPages( CreateFileMappingW( INVALID_HANDLE_VALUE, std::ptr::null(), - PAGE_READWRITE | SEC_RESERVE, + // Under Wine a committed section is a sparse file, free until + // touched, and committing a reserved one page by page costs a + // wineserver request each that scans every range committed so + // far: commit it whole there. + PAGE_READWRITE | if on_wine() { SEC_COMMIT } else { SEC_RESERVE }, (size as u64 >> 32) as u32, size as u32, std::ptr::null(), @@ -635,6 +652,9 @@ fn map(physical: &mut Physical, base: usize, len: usize, slice: Slice) -> bool { /// Commits the section pages a view shows (once per page, whichever view). fn commit(base: usize, len: usize) -> bool { + if on_wine() { + return true; // sections are committed whole there + } // SAFETY: `[base, base + len)` is a view of a reserved section. let done = !unsafe { VirtualAlloc(base as *const c_void, len, MEM_COMMIT, PAGE_READWRITE) }.is_null();