GPU: replace O(N^2) unmapped page lookup in Tracked::take_split with linear merge #3154

Open
reeledMarek wants to merge 1 commit from upstream-pr/3156 into main
reeledMarek commented 2026-11-26 10:27:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-26T10:27:34Z.


Summary

  • In Tracked::take_split (crates/victory-gpu/src/watch.rs), both pages (from BTreeSet::range) and unmapped (appended in whole order by protect_noting) are strictly ascending, yet the run-splitting loop called unmapped.contains(&p) (O(N)) inside an O(N) loop over pages — O(N^2) on large sparse/placeholder reservations.
  • Extract split_runs(&pages, &unmapped, from, to) using an O(N) two-pointer merge cursor over the two sorted slices.
  • Add unit test split_runs_merges_mapped_and_unmapped_in_linear_time covering interleaved mapped/unmapped pages, clamped boundaries, and a 16,384-page (1 GiB) sparse reservation.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6624614994 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3156). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-26T10:27:34Z. --- ## Summary - In `Tracked::take_split` (`crates/victory-gpu/src/watch.rs`), both `pages` (from `BTreeSet::range`) and `unmapped` (appended in `whole` order by `protect_noting`) are strictly ascending, yet the run-splitting loop called `unmapped.contains(&p)` (O(N)) inside an O(N) loop over `pages` — O(N^2) on large sparse/placeholder reservations. - Extract `split_runs(&pages, &unmapped, from, to)` using an O(N) two-pointer merge cursor over the two sorted slices. - Add unit test `split_runs_merges_mapped_and_unmapped_in_linear_time` covering interleaved mapped/unmapped pages, clamped boundaries, and a 16,384-page (1 GiB) sparse reservation.
Dukestar69 commented 2026-11-26 11:10:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T11:10:46Z.

Review: APPROVED


Verified against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib watch:: -- --test-threads=1: 16/16 passed (including split_runs_merges_mapped_and_unmapped_in_linear_time).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly replaces the O(N^2) unmapped-page lookup in Tracked::take_split with a linear merge.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6073106766 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3156#pullrequestreview-6073106766). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T11:10:46Z. **Review:** APPROVED --- Verified against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib watch:: -- --test-threads=1`: 16/16 passed (including `split_runs_merges_mapped_and_unmapped_in_linear_time`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly replaces the O(N^2) unmapped-page lookup in `Tracked::take_split` with a linear merge. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
rezioliver-collab commented 2026-11-26 18:01:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by rezioliver-collab on 2026-11-26T18:01:41Z.


The density of unmapped pages made a large difference to the cost of splitting runs on Apple M1. In the same 16,384-page address span, I varied only which listed pages were unmapped.

Unmapped page numbers Base PR
None 22.49 µs 16.76 µs
Multiples of 97 253.45 µs 25.04 µs
Even page numbers 8,358.58 µs 41.51 µs

I compared the run-splitting loop from 63bcca43 with split_runs from 467fc21c, compiled as native CPU kernels with Rust 1.98.1 at opt-level=3. Both received borrowed lists. Every nineteenth page was omitted from the input list, and the first and last byte ranges were clipped. Times are medians of 12 alternating pairs and cover splitting only.

Across spans of 256, 4,096 and 16,384 pages, both implementations returned identical copy and zero byte ranges. I also expanded each result back into page numbers and checked it against the expected mapped and unmapped pages.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7199969717 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3156#issuecomment-7199969717). Originally posted by [rezioliver-collab](https://github.com/rezioliver-collab) on 2026-11-26T18:01:41Z. --- The density of unmapped pages made a large difference to the cost of splitting runs on Apple M1. In the same 16,384-page address span, I varied only which listed pages were unmapped. | Unmapped page numbers | Base | PR | |---|---:|---:| | None | 22.49 µs | 16.76 µs | | Multiples of 97 | 253.45 µs | 25.04 µs | | Even page numbers | 8,358.58 µs | 41.51 µs | I compared the run-splitting loop from `63bcca43` with `split_runs` from `467fc21c`, compiled as native CPU kernels with Rust 1.98.1 at `opt-level=3`. Both received borrowed lists. Every nineteenth page was omitted from the input list, and the first and last byte ranges were clipped. Times are medians of 12 alternating pairs and cover splitting only. Across spans of 256, 4,096 and 16,384 pages, both implementations returned identical copy and zero byte ranges. I also expanded each result back into page numbers and checked it against the expected mapped and unmapped pages.
futurepro62 commented 2026-11-26 21:54:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-26T21:54:18Z.


NVIDIA full-suite run, to go with the reports above by Dukestar69, since none of them names a GPU run. Tested exact head 467fc21 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 867 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: split_runs_merges_mapped_and_unmapped_in_linear_time.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7204173971 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3156#issuecomment-7204173971). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-26T21:54:18Z. --- NVIDIA full-suite run, to go with the reports above by Dukestar69, since none of them names a GPU run. Tested exact head `467fc21` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **867 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `split_runs_merges_mapped_and_unmapped_in_linear_time`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3156:upstream-pr/3156'
git switch 'upstream-pr/3156'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3156'
git switch 'upstream-pr/3156'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3156'
git switch 'upstream-pr/3156'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3156'
git switch 'main'
git merge --squash 'upstream-pr/3156'
git switch 'main'
git merge --ff-only 'upstream-pr/3156'
git switch 'main'
git merge 'upstream-pr/3156'
git push origin 'main'
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
Marvisak/VIctory!3154
No description provided.