GPU: marked pages could go back on several threads (copy-back 184 to 124 ms here) #2826

Open
opened 2026-11-26 05:15:13 +00:00 by github-bot · 2 comments
Owner

@⁠Machad3x on GitHub · 2026-11-25 21:33 UTC · original

What

After a submission, shadow::copy_back_marks writes the marked pages back one after another on the submitting thread. In a world scene here that is about 2,000 pages of 64 KiB a frame at 55 to 80 µs each (two reads, a compare, writes, a protection change).

Change tried

Group the pages by address (two buffers covering the same page stay on one thread, in their old order) and take the groups on up to six scoped threads, the submitting one among them; under 16 pages it stays serial.

fn for_each_by_page(mut pages: Vec<(Arc<Shadow>, usize)>, f: impl Fn(&Arc<Shadow>, usize) + Sync) {
    pages.sort_by_key(|&(_, page)| page);
    let threads = (pages.len() / 8).min(6);
    if threads < 2 {
        for (copy, page) in &pages {
            f(copy, *page);
        }
        return;
    }
    let groups: Vec<&[(Arc<Shadow>, usize)]> = pages.chunk_by(|a, b| a.1 == b.1).collect();
    let next = AtomicUsize::new(0);
    let work = || {
        while let Some(group) = groups.get(next.fetch_add(1, Ordering::Relaxed)) {
            for (copy, page) in *group {
                f(copy, *page);
            }
        }
    };
    std::thread::scope(|scope| {
        for _ in 1..threads {
            scope.spawn(work);
        }
        work();
    });
}

The final loop of copy_back_marks calls it with the withheld check and copy.copy_back_marked(page) in the closure.

Measured

Ryzen 7 5700X, RTX 3080 Ti, Windows 11, on a stack of open pull requests (including #2120, #2130 and #2280) merged with main 67e8ae1, not on main alone. One 7-minute run each, same scene, medians:

frame copy-back marked pages
serial 777 ms 184 ms 166 ms
threads 690 ms 124 ms 111 ms

victory-gpu tests pass, with a new one writing 96 marked pages of two overlapping buffers back. No device removal in this and later runs. The gain per thread is well under linear; the threads are made anew each submission, so a kept pool may do better.

Code, with the test: github.com/Machad3x/VIctory-nvidia-3080ti@7229f41

Written with Claude Code; tested locally.

**@⁠Machad3x** on GitHub · 2026-11-25 21:33 UTC · [original](https://github.com/Marvisak/VIctory/issues/2826) ## What After a submission, `shadow::copy_back_marks` writes the marked pages back one after another on the submitting thread. In a world scene here that is about 2,000 pages of 64 KiB a frame at 55 to 80 µs each (two reads, a compare, writes, a protection change). ## Change tried Group the pages by address (two buffers covering the same page stay on one thread, in their old order) and take the groups on up to six scoped threads, the submitting one among them; under 16 pages it stays serial. ```rust fn for_each_by_page(mut pages: Vec<(Arc<Shadow>, usize)>, f: impl Fn(&Arc<Shadow>, usize) + Sync) { pages.sort_by_key(|&(_, page)| page); let threads = (pages.len() / 8).min(6); if threads < 2 { for (copy, page) in &pages { f(copy, *page); } return; } let groups: Vec<&[(Arc<Shadow>, usize)]> = pages.chunk_by(|a, b| a.1 == b.1).collect(); let next = AtomicUsize::new(0); let work = || { while let Some(group) = groups.get(next.fetch_add(1, Ordering::Relaxed)) { for (copy, page) in *group { f(copy, *page); } } }; std::thread::scope(|scope| { for _ in 1..threads { scope.spawn(work); } work(); }); } ``` The final loop of `copy_back_marks` calls it with the withheld check and `copy.copy_back_marked(page)` in the closure. ## Measured Ryzen 7 5700X, RTX 3080 Ti, Windows 11, **on a stack of open pull requests (including #2120, #2130 and #2280) merged with main 67e8ae1, not on `main` alone**. One 7-minute run each, same scene, medians: | | frame | copy-back | marked pages | |---|---|---|---| | serial | 777 ms | 184 ms | 166 ms | | threads | 690 ms | 124 ms | 111 ms | `victory-gpu` tests pass, with a new one writing 96 marked pages of two overlapping buffers back. No device removal in this and later runs. The gain per thread is well under linear; the threads are made anew each submission, so a kept pool may do better. Code, with the test: https://github.com/Machad3x/VIctory-nvidia-3080ti/commit/7229f41 Written with Claude Code; tested locally.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:15:13 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 21:46 UTC · original · edited 2026-11-26 00:44 UTC

Automated triage for the maintainers.

Performance change with a patch. On main, the final loop of copy_back_marks in crates/victory-gpu/src/shadow.rs calls Shadow::copy_back_marked once per page, serially, on the submitting thread, as described. The change is now open as pull request #2914 (ported to current main, original authorship kept). #2051 does the same for refreshes, and #2280/#2409 make each page's copy-back cheaper.

The numbers above were measured on a stack of open PRs (#2120, #2130, #2280); a measurement on main alone, which #2914 notes is still open, would help the maintainers judge the gain.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 21:46 UTC · [original](https://github.com/Marvisak/VIctory/issues/2826#issuecomment-7175516540) · edited 2026-11-26 00:44 UTC <!-- victory-issue-triage v1 verdict=tracked seen=2026-11-25T23:52:03Z at=2026-11-26T01:04:00Z --> Automated triage for the maintainers. Performance change with a patch. On `main`, the final loop of `copy_back_marks` in `crates/victory-gpu/src/shadow.rs` calls `Shadow::copy_back_marked` once per page, serially, on the submitting thread, as described. The change is now open as pull request #2914 (ported to current `main`, original authorship kept). #2051 does the same for refreshes, and #2280/#2409 make each page's copy-back cheaper. The numbers above were measured on a stack of open PRs (#2120, #2130, #2280); a measurement on `main` alone, which #2914 notes is still open, would help the maintainers judge the gain. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 23:52 UTC · original

Opened as #2914 — the 7229f41 change ported to current main verbatim (keeping your authorship, minus the fork doc page). The 96-page two-copy test passes on a GTX 1050; the main-alone measurement the triage asked for is still open — noted in the PR.

**@⁠kotekompotek** on GitHub · 2026-11-25 23:52 UTC · [original](https://github.com/Marvisak/VIctory/issues/2826#issuecomment-7178149032) Opened as #2914 — the `7229f41` change ported to current main verbatim (keeping your authorship, minus the fork doc page). The 96-page two-copy test passes on a GTX 1050; the main-alone measurement the triage asked for is still open — noted in the PR.
This discussion has been locked. Commenting is limited to contributors.
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#2826
No description provided.