fix(victory-gpu): track exact upload block spans and move zeroing/alloc outside ARENAS lock #451

Open
github-bot wants to merge 2 commits from github-bot/gh-451 into main AGit
Owner

@⁠Lewandox on GitHub · 2026-11-26 20:08 UTC · original
Lewandox:fix/victory-gpu-upload-block-bounds-and-lock → main · +165 −33 · 1 files · 2 commits · open

Summary

Fixes two issues in victory-gpu/src/upload.rs (used by list::allocate_upload and memory::named_in_upload for ray-tracing top-level structure builds):

  1. Exact allocation boundaries in upload::block(address) (fixes cross-list bleed / over-scan):

    • Previously, Arena stored only taken: Vec<bool>, so upload::block(address) counted all consecutive true blocks starting at (address - arena.at) / BLOCK.
    • When multiple command lists held adjacent blocks in the same 4 MiB arena, memory::named_in_upload scanned across unrelated command lists' upload buffers (risking concurrent data races with other recording threads and wasting CPU scanning up to 4 MiB instead of the caller's own block). And when address fell inside a later block of a multi-block allocation, block() returned a truncated tail instead of the allocation's true start.
    • Each arena slot now records its owning allocation span (first, count) (Slot::FREE when free), so upload::block(address) returns the exact (start, len) of the owning allocation in O(1).
  2. Reduced ARENAS mutex contention and unnecessary MEM_WRITE_WATCH dirtying in Upload::new(len):

    • Tracks Arena::free to skip full arenas in O(1).
    • Moves write_bytes zeroing of reused blocks outside the global ARENAS lock.
    • Moves VirtualAlloc2 (reserve) and shadow::register outside the ARENAS lock (eliminating lock nesting with shadow's global table and stalls on parallel recording threads).
    • Skips redundant write_bytes(0) on freshly VirtualAlloc2(MEM_COMMIT) arenas (Windows guarantees zero-initialized pages), avoiding prematurely dirtying untouched pages in MEM_WRITE_WATCH.

Verification

  • cargo test -p victory-gpu --lib upload::tests (default and --features diagnostics)
  • New regression test upload::tests::block_returns_exact_allocation_bounds_without_bleeding_into_neighbors verifies adjacent allocations, multi-block queries from non-zero offsets, and clean release.
**@⁠Lewandox** on GitHub · 2026-11-26 20:08 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/451) `Lewandox:fix/victory-gpu-upload-block-bounds-and-lock` → `main` · +165 −33 · 1 files · 2 commits · open ## Summary Fixes two issues in `victory-gpu/src/upload.rs` (used by `list::allocate_upload` and `memory::named_in_upload` for ray-tracing top-level structure builds): 1. **Exact allocation boundaries in `upload::block(address)` (fixes cross-list bleed / over-scan)**: - Previously, `Arena` stored only `taken: Vec<bool>`, so `upload::block(address)` counted all consecutive `true` blocks starting at `(address - arena.at) / BLOCK`. - When multiple command lists held adjacent blocks in the same 4 MiB arena, `memory::named_in_upload` scanned across unrelated command lists' upload buffers (risking concurrent data races with other recording threads and wasting CPU scanning up to 4 MiB instead of the caller's own block). And when `address` fell inside a later block of a multi-block allocation, `block()` returned a truncated tail instead of the allocation's true start. - Each arena slot now records its owning allocation span `(first, count)` (`Slot::FREE` when free), so `upload::block(address)` returns the exact `(start, len)` of the owning allocation in O(1). 2. **Reduced `ARENAS` mutex contention and unnecessary `MEM_WRITE_WATCH` dirtying in `Upload::new(len)`**: - Tracks `Arena::free` to skip full arenas in O(1). - Moves `write_bytes` zeroing of reused blocks **outside** the global `ARENAS` lock. - Moves `VirtualAlloc2` (`reserve`) and `shadow::register` **outside** the `ARENAS` lock (eliminating lock nesting with `shadow`'s global table and stalls on parallel recording threads). - Skips redundant `write_bytes(0)` on freshly `VirtualAlloc2(MEM_COMMIT)` arenas (Windows guarantees zero-initialized pages), avoiding prematurely dirtying untouched pages in `MEM_WRITE_WATCH`. ## Verification - `cargo test -p victory-gpu --lib upload::tests` (default and `--features diagnostics`) - New regression test `upload::tests::block_returns_exact_allocation_bounds_without_bleeding_into_neighbors` verifies adjacent allocations, multi-block queries from non-zero offsets, and clean release.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 20:13:45 +00:00
Author
Owner

@⁠mauroalvare on GitHub · 2026-11-27 01:13 UTC · original

Independent Windows x64 release verification at ef0113b6585249f8e5fbe7652f9319d7dfa42ba7 and parent 37d4686711bbb04c2cc3825f3f13bbff7c140fb0.

  • The three upload::tests passed at the head, both with default features and with --features diagnostics.
  • On the parent with only the new block_returns_exact_allocation_bounds_without_bleeding_into_neighbors test copied over, the first assertion returned 256 KiB for a 64 KiB allocation.
  • The head increases committed upload arena memory on a cold parallel start. With sixteen simultaneous 128-byte requests kept live, the parent used one 4 MiB arena in all eight fresh processes. The head used 8, 9, 11, 14, 14, 15, 16 and 16 arenas (32–64 MiB committed). Arena reservations persist for the life of the process. The observed increase is consistent with several callers finding an empty pool before any caller publishes its new arena.

Reproduce the cold case by appending this test to third_party/victory/crates/victory-gpu/src/upload.rs on each revision and running the command in a fresh process for each sample.

#[cfg(test)]
mod rt_cold_arena_profile {
    use super::*;
    use std::sync::Barrier;

    #[test]
    fn cold_parallel_uploads() {
        const WORKERS: usize = 16;
        let start = Barrier::new(WORKERS);
        let live = Barrier::new(WORKERS);
        std::thread::scope(|scope| {
            for _ in 0..WORKERS {
                scope.spawn(|| {
                    start.wait();
                    let upload = Upload::new(128).expect("low upload");
                    live.wait();
                    drop(upload);
                });
            }
        });
        let arenas = ARENAS.lock().unwrap().len();
        println!("cold_parallel_uploads workers={WORKERS} arenas={arenas} arena_commit_bytes={}", arenas * ARENA);
    }
}
$env:VICTORY_UPLOAD_LOW = '1'
cargo test --manifest-path third_party/victory/Cargo.toml --offline --locked --release -p victory-gpu --lib upload::rt_cold_arena_profile::cold_parallel_uploads -- --exact --nocapture --test-threads=1

Sharing a pending arena reservation among simultaneous misses would retain the exact-span fix while limiting committed memory growth. Builder frame timings would need a fixture that exercises memory::named_in_upload.

**@⁠mauroalvare** on GitHub · 2026-11-27 01:13 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/451#issuecomment-7207113122) Independent Windows x64 release verification at `ef0113b6585249f8e5fbe7652f9319d7dfa42ba7` and parent `37d4686711bbb04c2cc3825f3f13bbff7c140fb0`. - The three `upload::tests` passed at the head, both with default features and with `--features diagnostics`. - On the parent with only the new `block_returns_exact_allocation_bounds_without_bleeding_into_neighbors` test copied over, the first assertion returned 256 KiB for a 64 KiB allocation. - The head increases committed upload arena memory on a cold parallel start. With sixteen simultaneous 128-byte requests kept live, the parent used one 4 MiB arena in all eight fresh processes. The head used 8, 9, 11, 14, 14, 15, 16 and 16 arenas (32–64 MiB committed). Arena reservations persist for the life of the process. The observed increase is consistent with several callers finding an empty pool before any caller publishes its new arena. Reproduce the cold case by appending this test to `third_party/victory/crates/victory-gpu/src/upload.rs` on each revision and running the command in a fresh process for each sample. ```rust #[cfg(test)] mod rt_cold_arena_profile { use super::*; use std::sync::Barrier; #[test] fn cold_parallel_uploads() { const WORKERS: usize = 16; let start = Barrier::new(WORKERS); let live = Barrier::new(WORKERS); std::thread::scope(|scope| { for _ in 0..WORKERS { scope.spawn(|| { start.wait(); let upload = Upload::new(128).expect("low upload"); live.wait(); drop(upload); }); } }); let arenas = ARENAS.lock().unwrap().len(); println!("cold_parallel_uploads workers={WORKERS} arenas={arenas} arena_commit_bytes={}", arenas * ARENA); } } ``` ```powershell $env:VICTORY_UPLOAD_LOW = '1' cargo test --manifest-path third_party/victory/Cargo.toml --offline --locked --release -p victory-gpu --lib upload::rt_cold_arena_profile::cold_parallel_uploads -- --exact --nocapture --test-threads=1 ``` Sharing a pending arena reservation among simultaneous misses would retain the exact-span fix while limiting committed memory growth. Builder frame timings would need a fixture that exercises `memory::named_in_upload`.
Author
Owner

@⁠Lewandox on GitHub · 2026-11-27 01:28 UTC · original

Great catch — thank you for the independent release verification and the cold-start repro!

Pushed a follow-up commit adding a dedicated slow-path ARENA_GROW mutex with a double-checked ARENAS re-probe before calling reserve():

  • Fast-path reuses still never touch ARENA_GROW and still zero outside ARENAS.
  • VirtualAlloc2 and shadow::register stay outside ARENAS, while simultaneous cold-start misses now serialize growth and share one 4 MiB arena.
  • Added upload::tests::cold_parallel_uploads_share_a_single_arena (fresh subprocess, 16 concurrent threads on a barrier, asserting ARENAS.len() == 1); all 4 upload::tests pass locally (4 passed, 0 failed).
**@⁠Lewandox** on GitHub · 2026-11-27 01:28 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/451#issuecomment-7207310692) Great catch — thank you for the independent release verification and the cold-start repro! Pushed a follow-up commit adding a dedicated slow-path `ARENA_GROW` mutex with a double-checked `ARENAS` re-probe before calling `reserve()`: - Fast-path reuses still never touch `ARENA_GROW` and still zero outside `ARENAS`. - `VirtualAlloc2` and `shadow::register` stay outside `ARENAS`, while simultaneous cold-start misses now serialize growth and share one 4 MiB arena. - Added `upload::tests::cold_parallel_uploads_share_a_single_arena` (fresh subprocess, 16 concurrent threads on a barrier, asserting `ARENAS.len() == 1`); all 4 `upload::tests` pass locally (`4 passed, 0 failed`).
Author
Owner

@⁠mauroalvare on GitHub · 2026-11-27 01:56 UTC · original

Retested the follow-up c9aadb7684e86f2f8e3afb4258d3a701027cce8b on Windows x64, Rust 1.98.1, release.

  • The four upstream upload::tests passed with the default features and with --features diagnostics.
  • The original independent cold-start fixture used one 4 MiB arena in all eight fresh processes, with the same sixteen simultaneous 128-byte requests kept live. The earlier ef0113b build used 8–16 arenas. This reproduces the correction with the original workload.
$env:VICTORY_UPLOAD_LOW = '1'
cargo test --manifest-path third_party/victory/Cargo.toml --offline --locked --release -p victory-gpu --lib upload::tests:: -- --test-threads=1
cargo test --manifest-path third_party/victory/Cargo.toml --offline --locked --release -p victory-gpu --lib --features diagnostics upload::tests:: -- --test-threads=1
cargo test --manifest-path third_party/victory/Cargo.toml --offline --locked --release -p victory-gpu --lib upload::rt_cold_arena_profile::cold_parallel_uploads -- --exact --nocapture --test-threads=1

The last command uses the test in my earlier comment and was run eight times in fresh processes.

**@⁠mauroalvare** on GitHub · 2026-11-27 01:56 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/451#issuecomment-7207697859) Retested the follow-up `c9aadb7684e86f2f8e3afb4258d3a701027cce8b` on Windows x64, Rust 1.98.1, release. - The four upstream `upload::tests` passed with the default features and with `--features diagnostics`. - The original independent cold-start fixture used one 4 MiB arena in all eight fresh processes, with the same sixteen simultaneous 128-byte requests kept live. The earlier `ef0113b` build used 8–16 arenas. This reproduces the correction with the original workload. ```powershell $env:VICTORY_UPLOAD_LOW = '1' cargo test --manifest-path third_party/victory/Cargo.toml --offline --locked --release -p victory-gpu --lib upload::tests:: -- --test-threads=1 cargo test --manifest-path third_party/victory/Cargo.toml --offline --locked --release -p victory-gpu --lib --features diagnostics upload::tests:: -- --test-threads=1 cargo test --manifest-path third_party/victory/Cargo.toml --offline --locked --release -p victory-gpu --lib upload::rt_cold_arena_profile::cold_parallel_uploads -- --exact --nocapture --test-threads=1 ``` The last command uses the test in my earlier comment and was run eight times in fresh processes.
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/451/head:github-bot/gh-451'
git switch 'github-bot/gh-451'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-451'
git switch 'github-bot/gh-451'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-451'
git switch 'github-bot/gh-451'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-451'
git switch 'main'
git merge --squash 'github-bot/gh-451'
git switch 'main'
git merge --ff-only 'github-bot/gh-451'
git switch 'main'
git merge 'github-bot/gh-451'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.