loader: reuse the file read buffer when preparing title images #4770

Open
github-bot wants to merge 1 commit from github-bot/gh-4769 into main AGit
Owner

@⁠topangag on GitHub · 2026-12-01 06:47 UTC · original
topangag:pr/reuse-title-image-read-buffer → main · +33 −12 · 4 files · 1 commits · open

What it changes

victory_loader::prepare(&[u8]) cloned the full executable/DLL buffer (original.to_vec()) while callers in apps/victory/src/run.rs and crates/victory-loader/src/windows.rs already owned the Vec<u8> returned by std::fs::read and never needed original again.

  • Adds victory_loader::prepare_owned(Vec<u8>) sharing the implementation via Cow<'_, [u8]>, hashing original before patching and mutating the owned buffer in place.
  • Switches apps/victory/src/run.rs and crates/victory-loader/src/windows.rs (map_dll) to prepare_owned, and drops prepared in map_dll before recursing into map_imports so nested title DLL buffers are not kept live across recursion.
  • Adds a Windows unit test verifying in-place buffer reuse (as_ptr() unchanged), identical original_hash, and identical patched bytes against prepare.

How it was tested

  • GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): Startup/loader A/B benchmark in isolated release processes on a 114.61 MiB title executable (5 interleaved pairs; gameplay FPS unaffected).
  • Before: median Peak Working Set 251.38 MiB, Peak Private Commit 255.86 MiB, live private memory through prepare 231.77 MiB, prepare time 33.82 ms (reused=false).
  • After: median Peak Working Set 136.75 MiB (-114.63 MiB), Peak Private Commit 140.99 MiB (-114.87 MiB), live private memory 116.92 MiB (-114.85 MiB), prepare time 18.66 ms (-15.16 ms, reused=true).
  • cargo test passed for the crates it touches: yes (cargo test --locked -p victory -p victory-loader -p xtask passed locally; full Windows CI workflow — Clippy, Clippy, diagnostics build, and workspace Tests — passed on the fork against a clean CI baseline applying existing unmerged main fixes #4708 and #4143: https://github.com/topangag/VIctory-NVIDIA/actions/runs/46588428695).
  • Note on current main CI: main currently fails Clippy on unrelated F1/F3 UI lints (addressed by #4708) and intermittently fails victory-hle's a_read_by_range_waits_only_for_a_remapping_that_meets_it (addressed by #4143); this PR intentionally leaves those untouched.

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: none found
**@⁠topangag** on GitHub · 2026-12-01 06:47 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4769) `topangag:pr/reuse-title-image-read-buffer` → `main` · +33 −12 · 4 files · 1 commits · open ## What it changes `victory_loader::prepare(&[u8])` cloned the full executable/DLL buffer (`original.to_vec()`) while callers in `apps/victory/src/run.rs` and `crates/victory-loader/src/windows.rs` already owned the `Vec<u8>` returned by `std::fs::read` and never needed `original` again. - Adds `victory_loader::prepare_owned(Vec<u8>)` sharing the implementation via `Cow<'_, [u8]>`, hashing `original` before patching and mutating the owned buffer in place. - Switches `apps/victory/src/run.rs` and `crates/victory-loader/src/windows.rs` (`map_dll`) to `prepare_owned`, and drops `prepared` in `map_dll` before recursing into `map_imports` so nested title DLL buffers are not kept live across recursion. - Adds a Windows unit test verifying in-place buffer reuse (`as_ptr()` unchanged), identical `original_hash`, and identical patched bytes against `prepare`. ## How it was tested - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): Startup/loader A/B benchmark in isolated release processes on a 114.61 MiB title executable (5 interleaved pairs; gameplay FPS unaffected). - Before: median Peak Working Set `251.38 MiB`, Peak Private Commit `255.86 MiB`, live private memory through prepare `231.77 MiB`, `prepare` time `33.82 ms` (`reused=false`). - After: median Peak Working Set `136.75 MiB` (-114.63 MiB), Peak Private Commit `140.99 MiB` (-114.87 MiB), live private memory `116.92 MiB` (-114.85 MiB), `prepare` time `18.66 ms` (-15.16 ms, `reused=true`). - `cargo test` passed for the crates it touches: yes (`cargo test --locked -p victory -p victory-loader -p xtask` passed locally; full Windows `CI` workflow — `Clippy`, `Clippy, diagnostics build`, and workspace `Tests` — passed on the fork against a clean CI baseline applying existing unmerged `main` fixes #4708 and #4143: https://github.com/topangag/VIctory-NVIDIA/actions/runs/46588428695). - Note on current `main` CI: `main` currently fails `Clippy` on unrelated F1/F3 UI lints (addressed by #4708) and intermittently fails `victory-hle`'s `a_read_by_range_waits_only_for_a_remapping_that_meets_it` (addressed by #4143); this PR intentionally leaves those untouched. ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: none found
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 06:48:25 +00:00
Author
Owner

@⁠illuminatedfellow on GitHub · 2026-12-01 14:45 UTC · original

Tested on Linux, merged onto main (5fabb46e): victory-loader's tests give the same results as on main (the one failure is the Linux-only title_patches_check_before_they_write, which #4932 addresses). Since owned_image_reuses_the_read_buffer_and_keeps_its_original_hash is Windows-only, I ran the same checks on Linux with the synthetic image from tls_directory_at_the_top_of_the_rva_space_is_prepared: prepare_owned returns the same bytes and original_hash as prepare, reuses the read buffer, and the hash is of the bytes before patching. Merges cleanly.

**@⁠illuminatedfellow** on GitHub · 2026-12-01 14:45 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4769#issuecomment-7314783002) Tested on Linux, merged onto `main` (5fabb46e): `victory-loader`'s tests give the same results as on `main` (the one failure is the Linux-only `title_patches_check_before_they_write`, which #4932 addresses). Since `owned_image_reuses_the_read_buffer_and_keeps_its_original_hash` is Windows-only, I ran the same checks on Linux with the synthetic image from `tls_directory_at_the_top_of_the_rva_space_is_prepared`: `prepare_owned` returns the same bytes and `original_hash` as `prepare`, reuses the read buffer, and the hash is of the bytes before patching. Merges cleanly.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/4770/head:github-bot/gh-4769'
git switch 'github-bot/gh-4769'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-4769'
git switch 'github-bot/gh-4769'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-4769'
git switch 'github-bot/gh-4769'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-4769'
git switch 'main'
git merge --squash 'github-bot/gh-4769'
git switch 'main'
git merge --ff-only 'github-bot/gh-4769'
git switch 'main'
git merge 'github-bot/gh-4769'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!4770
No description provided.