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

Open
github-bot wants to merge 1 commit from github-bot/gh-3156 into main AGit
Owner

@⁠reeledMarek on GitHub · 2026-11-26 10:27 UTC · original
reeledMarek:fix/gpu-watch-take-split-linear-merge → main · +43 −10 · 1 files · 1 commits · open

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.
**@⁠reeledMarek** on GitHub · 2026-11-26 10:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/3156) `reeledMarek:fix/gpu-watch-take-split-linear-merge` → `main` · +43 −10 · 1 files · 1 commits · open ## 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:08:57 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 11:10 UTC · original · approved these changes

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).

**@⁠Dukestar69** on GitHub · 2026-11-26 11:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/3156#pullrequestreview-6073106766) · **approved these changes** 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).
Author
Owner

@⁠rezioliver-collab on GitHub · 2026-11-26 18:01 UTC · original

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.

**@⁠rezioliver-collab** on GitHub · 2026-11-26 18:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/3156#issuecomment-7199969717) 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.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3156/head:github-bot/gh-3156'
git switch 'github-bot/gh-3156'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3156'
git switch 'github-bot/gh-3156'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3156'
git switch 'github-bot/gh-3156'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3156'
git switch 'main'
git merge --squash 'github-bot/gh-3156'
git switch 'main'
git merge --ff-only 'github-bot/gh-3156'
git switch 'main'
git merge 'github-bot/gh-3156'
git push origin 'main'
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!3156
No description provided.