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

Merged
Lewandox merged 2 commits from upstream-pr/451 into main 2026-11-27 12:55:47 +00:00
Lewandox commented 2026-11-26 20:08:50 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by Lewandox on 2026-11-26T20:08:50Z.


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.
<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5516502920 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/451). Originally posted by [Lewandox](https://github.com/Lewandox) on 2026-11-26T20:08:50Z. --- ## 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.
mauroalvare commented 2026-11-27 01:13:22 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by mauroalvare on 2026-11-27T01:13:22Z.


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.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7207113122 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/451#issuecomment-7207113122). Originally posted by [mauroalvare](https://github.com/mauroalvare) on 2026-11-27T01:13:22Z. --- 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`.
Lewandox commented 2026-11-27 01:28:02 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by Lewandox on 2026-11-27T01:28:02Z.


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).
<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7207310692 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/451#issuecomment-7207310692). Originally posted by [Lewandox](https://github.com/Lewandox) on 2026-11-27T01:28:02Z. --- 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`).
mauroalvare commented 2026-11-27 01:56:18 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by mauroalvare on 2026-11-27T01:56:18Z.


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.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7207697859 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/451#issuecomment-7207697859). Originally posted by [mauroalvare](https://github.com/mauroalvare) on 2026-11-27T01:56:18Z. --- 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.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
LooperSalty/AnyXboxX!451
No description provided.