victory-gpu: look a submission's refreshes up by pointer, not by scanning (4-13x on the merge) #1556

Closed
github-bot wants to merge 1 commit from gh-pr/1556/head into gh-pr/1556/base
Owner

@⁠pes7 on GitHub · 2026-11-24 19:09 UTC · original
pes7:perf/batch-refresh-index → main · +72 −7 · 1 files · 1 commits · closed without merging

A submission's refreshes are merged with a linear scan per refresh, so a frame's preparation is quadratic in the number of buffers. An index makes it constant: 4.4x at 250 buffers, 13x at 1000, and 33 000 refreshes per submission is what #400 measures.

Batch::add keeps one entry per shadow buffer and folds each new range into it. It found that entry with refreshes.iter_mut().find(|(s, ..)| Arc::ptr_eq(s, shadow)) - a scan over every buffer already seen in the submission - and it is called once per Step::Refresh, which #400 counts at 33-35 k per world frame.

What changes

Batch keeps HashMap<usize, usize> from Arc::as_ptr to the entry's position. Lookup is a hash of one word instead of a scan; the entry still holds a clone of the Arc, so the allocation the key names stays alive until the entry is drained, and addresses cannot be reused under it. flush() clears the index with the vector.

Measured

New #[ignore]d test in the batch module, stand-in buffers of one word each (the comparison is a pointer either way, so the shape is the real one), 33 000 refreshes over the same 50/250/1000 buffers, best of 7:

buffers per submission scan index ratio
50 0.15 ms 0.20 ms 0.8x
250 0.91 ms 0.21 ms 4.4x
1 000 3.18 ms 0.24 ms 13.1x

Honest about the small end: below about 75 buffers the scan is cheaper than a hash, by 0.05 ms. Above it the scan grows linearly and the index does not. #400 and #783 report a world submission refreshing tens of thousands of ranges over buffers that include "several views into one 174 MB block, among others", so if a submission touches more than ~75 distinct buffers this is 0.7-2.9 ms of every frame's preparation; if it touches fewer, it costs 0.05 ms. A one-line counter of batch.refreshes.len() in the copystat line would settle which - happy to add it if you want the number before deciding.

Run it with:

cargo test --release -p victory-gpu --lib -- --ignored --nocapture refresh_merge

Correctness

The merge result is identical: same entry, same min/max fold, entries still drained in insertion order. cargo test --workspace --locked, both clippy configurations with -D warnings, cargo xtask policy.

No game data used.

**@⁠pes7** on GitHub · 2026-11-24 19:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/1556) `pes7:perf/batch-refresh-index` → `main` · +72 −7 · 1 files · 1 commits · closed without merging **A submission's refreshes are merged with a linear scan per refresh, so a frame's preparation is quadratic in the number of buffers.** An index makes it constant: 4.4x at 250 buffers, 13x at 1000, and 33 000 refreshes per submission is what #400 measures. `Batch::add` keeps one entry per shadow buffer and folds each new range into it. It found that entry with `refreshes.iter_mut().find(|(s, ..)| Arc::ptr_eq(s, shadow))` - a scan over every buffer already seen in the submission - and it is called once per `Step::Refresh`, which #400 counts at 33-35 k per world frame. ## What changes `Batch` keeps `HashMap<usize, usize>` from `Arc::as_ptr` to the entry's position. Lookup is a hash of one word instead of a scan; the entry still holds a clone of the `Arc`, so the allocation the key names stays alive until the entry is drained, and addresses cannot be reused under it. `flush()` clears the index with the vector. ## Measured New `#[ignore]`d test in the `batch` module, stand-in buffers of one word each (the comparison is a pointer either way, so the shape is the real one), 33 000 refreshes over the same 50/250/1000 buffers, best of 7: | buffers per submission | scan | index | ratio | |---:|---:|---:|---:| | 50 | 0.15 ms | 0.20 ms | **0.8x** | | 250 | 0.91 ms | 0.21 ms | **4.4x** | | 1 000 | 3.18 ms | 0.24 ms | **13.1x** | Honest about the small end: below about 75 buffers the scan is cheaper than a hash, by 0.05 ms. Above it the scan grows linearly and the index does not. #400 and #783 report a world submission refreshing tens of thousands of ranges over buffers that include "several views into one 174 MB block, among others", so if a submission touches more than ~75 distinct buffers this is 0.7-2.9 ms of every frame's preparation; if it touches fewer, it costs 0.05 ms. A one-line counter of `batch.refreshes.len()` in the `copystat` line would settle which - happy to add it if you want the number before deciding. Run it with: ``` cargo test --release -p victory-gpu --lib -- --ignored --nocapture refresh_merge ``` ## Correctness The merge result is identical: same entry, same `min`/`max` fold, entries still drained in insertion order. `cargo test --workspace --locked`, both clippy configurations with `-D warnings`, `cargo xtask policy`. No game data used.
`Batch::add` keeps one entry per shadow buffer and folds each new range
into it, but it found that entry with a linear `Arc::ptr_eq` scan over
every buffer already seen in the submission. It runs once per
`Step::Refresh`, which #400 counts at 33-35 k per world frame, so the
preparation was quadratic in the number of buffers a submission touches.

Keep a `HashMap<usize, usize>` from `Arc::as_ptr` to the entry's position
instead. The entry still holds a clone of the `Arc`, so the allocation the
key names stays alive until the entry is drained and the address cannot be
reused under it; `flush()` clears the index with the vector.

New `#[ignore]`d test in the `batch` module, stand-in buffers of one word
each (the comparison is a pointer either way), 33 000 refreshes over the
same 50/250/1000 buffers, best of 7:

  buffers    scan     index    ratio
       50  0.15 ms  0.20 ms     0.8x
      250  0.91 ms  0.21 ms     4.4x
     1000  3.18 ms  0.24 ms    13.1x

Below about 75 buffers the scan wins by 0.05 ms; above it the scan grows
linearly and the index does not. The merge result is identical - same
entry, same min/max fold, drained in insertion order.

cargo test --workspace --locked, both clippy configurations with
-D warnings, cargo xtask policy.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:36:20 +00:00
github-bot closed this pull request 2026-11-26 04:36:20 +00:00
Author
Owner

@⁠pes7 on GitHub · 2026-11-24 19:32 UTC · original

Note for whoever reviews this against #776: both touch Batch in this file. #776's branch changes title_written/the watch sweep; this changes add and flush. Different lines, same struct - whichever lands second needs a trivial rebase, and I am happy to do it for this side.

They also stack: #776 cuts the number of askings, this cuts the per-refresh merge. Neither changes the other's numbers.

**@⁠pes7** on GitHub · 2026-11-24 19:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1556#issuecomment-7142251544) Note for whoever reviews this against #776: both touch `Batch` in this file. #776's branch changes `title_written`/the watch sweep; this changes `add` and `flush`. Different lines, same struct - whichever lands second needs a trivial rebase, and I am happy to do it for this side. They also stack: #776 cuts the number of askings, this cuts the per-refresh merge. Neither changes the other's numbers.
Author
Owner

@⁠pes7 on GitHub · 2026-11-24 21:54 UTC · original

Closing this as superseded by main: Batch::add no longer scans. Upstream now keeps pending (where each copy's refresh is in refreshes, by Arc::as_ptr) and current (the range each copy was brought up to date to since the last title write), so both the quadratic lookup and the repeated-range dedupe this PR proposed are already there:

let (key, end) = (std::sync::Arc::as_ptr(shadow) as usize, address + len);
if self.current.get(&key).is_some_and(|&(_, a, b)| a <= address && end <= b) {
    return;
}
match self.pending.get(&key) { .. }

For the record, so the measurement is not lost: on a synthetic world-sized batch (250 buffers, then 1 000) the merge went from 4.4x to 13x faster than the scan, and the same numbers would have applied to a Batch like the one this was written against.

Thanks to whoever landed it - it is the same fix, done there first. Nothing else of mine depends on this PR; #1656 (the copy-back side) is being rebased on top of the new Batch.

**@⁠pes7** on GitHub · 2026-11-24 21:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/1556#issuecomment-7145550138) Closing this as superseded by `main`: `Batch::add` no longer scans. Upstream now keeps `pending` (where each copy's refresh is in `refreshes`, by `Arc::as_ptr`) and `current` (the range each copy was brought up to date to since the last title write), so both the quadratic lookup *and* the repeated-range dedupe this PR proposed are already there: ```rust let (key, end) = (std::sync::Arc::as_ptr(shadow) as usize, address + len); if self.current.get(&key).is_some_and(|&(_, a, b)| a <= address && end <= b) { return; } match self.pending.get(&key) { .. } ``` For the record, so the measurement is not lost: on a synthetic world-sized batch (250 buffers, then 1 000) the merge went from 4.4x to 13x faster than the scan, and the same numbers would have applied to a `Batch` like the one this was written against. Thanks to whoever landed it - it is the same fix, done there first. Nothing else of mine depends on this PR; #1656 (the copy-back side) is being rebased on top of the new `Batch`.

Pull request closed

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/Marvisak--VIctory!1556
No description provided.