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

Open
opened 2026-11-25 21:33:56 +00:00 by Machad3x · 2 comments
Machad3x commented 2026-11-25 21:33:56 +00:00 (Migrated from github.com)
Contributor

Imported from Marvisak/VIctory. Originally posted by Machad3x on 2026-11-25T21:33:56Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6614197460 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/2826). Originally posted by [Machad3x](https://github.com/Machad3x) on 2026-11-25T21:33:56Z. --- ## 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.
Marvisak commented 2026-11-25 22:04:34 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T21:46:15Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7175516540 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/2826#issuecomment-7175516540). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T21:46:15Z. --- <!-- 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)_
kotekompotek commented 2026-11-26 00:03:37 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T23:52:03Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7178149032 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/2826#issuecomment-7178149032). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T23:52:03Z. --- 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.
Sign in to join this conversation.
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
Marvisak/VIctory#2824
No description provided.