watch: interval-skip absent placeholders, coalesce VirtualProtect runs, O(dirty) re-arm #345

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

@⁠Vladimir-Human on GitHub · 2026-11-26 10:36 UTC · original
Vladimir-Human:perf/watch-guard-sparse-dirty → main · +2296 −255 · 2 files · 1 commits · open

Summary

Ports the watch::guard sparse interval-skip, placeholder-hole skip, O(dirty) re-arm, and coalesced region-guided VirtualProtect optimizations from fxceless/victory-fork#1279 (addressing fxceless/victory-fork#1238) onto AnyXboxX's third_party/victory/crates/victory-gpu/src/{watch.rs,inplace.rs} while preserving all existing AnyXboxX / Neptune concurrency and lock-free improvements (RwLock on JOURNAL/GUARDS, lock-free CLEAN/ABSENT/PARTS/WHOLE/SUBS, watch_4k/stale_mask, ask_once_quiet, quiet_back, HOT/VICTORY_GUARD_HOT, VICTORY_GUARD_OPEN_AHEAD, and sharded MAPPING).

What changed

  1. Coalesced interval tracking & placeholder-hole skip (watch.rs):

    • Replaced per-block BTreeSet<usize> sets in Guards (protected, clean, absent, aliased) with disjoint coalesced half-open intervals (BlockSet) plus placeholder_absent: BlockSet.
    • Extended the existing lock-free ABSENT table to distinguish ABSENT_NONE = 0, ABSENT_ORDINARY = 1, and ABSENT_PLACEHOLDER = 2 (with chunked absent_set_range / clean_set_range), allowing both the lock-free fast path and guard_locked to skip NtQueryVirtualMemory(MemoryRegionInformationEx)-confirmed MEM_RESERVE_PLACEHOLDER holes with 0 VirtualQuery and 0 VirtualProtect syscalls until title_memory_remapped (VICTORY_ABSENT_ONCE=0 opts out; ordinary non-placeholder MEM_RESERVE / SEC_RESERVE holes remain ABSENT_ORDINARY and still probe via VirtualQuery so silent commits are detected immediately).
    • writes() checks guarded && known_view(first * PAGE) (lock-free CLEAN/ABSENT first, then guards_read()) to avoid wasted failing GetWriteWatch calls on known views.
    • guards_read() uses O(log K) g.clean.covers(first, last); writing() uses O(log K) intersects; title_memory_written() uses count_in; title_memory_remapped() uses take_range/remove_range (removing the duplicate aliased loop).
    • Added INSTALLED: AtomicBool and write-lock re-check after read-to-write lock upgrade in guard() to prevent TOCTOU races with concurrent title_memory_remapped.
  2. O(dirty) re-arm, protected ∩ absent == ∅, coalesced region-guided VirtualProtect, & batched alias lookups (watch.rs, inplace.rs):

    • guard_locked skips contiguous clean and confirmed placeholder_absent intervals in O(log K), marks already-protected blocks (e.g. alias-guarded) clean in O(dirty) with 0 VirtualProtect calls (preserving VICTORY_GUARD_HOT), and coalesces adjacent unprotected dirty blocks into contiguous runs.
    • note_protected clears absent, placeholder_absent, and lock-free ABSENT so protected ∩ absent == ∅ holds by construction; handle_protect_failure returns exclusive end and both protect_here fallback loops advance past it.
    • protect_here, protect_alias_run, and unprotect_run_os issue one coalesced VirtualProtect per contiguous run with VirtualQuery-guided OS VM region fallback (unprotect_run_os skips info.State != MEM_COMMIT gaps without per-block failures), all guarded with checked_mul(BLOCK).
    • Added lock-batched inplace::aliases_in_range and inplace::aliases_for_blocks (keeping inplace::pages_of for stays_open and ours_through), a zero-allocation single-block unaliased fast path in open for VEH write faults, and strict lock-free CLEAN clearing before any PAGE_READWRITE transition.
  3. Unit, regression, stress, & deterministic benchmark tests (watch.rs):

    • block_set_interval_invariants
    • sparse_placeholder_gaps_are_skipped_without_probing_or_protecting
    • contiguous_runs_coalesce_guard_and_unguard_syscalls
    • aliased_views_coalesce_protect_open_and_o_dirty_rearm
    • ordinary_reserve_is_not_placeholder_and_silent_commit_is_detected
    • protected_and_absent_sets_remain_disjoint_after_protect
    • unprotect_run_os_skips_uncommitted_gap_without_per_block_failures
    • concurrent_writers_and_guard_readers_stay_coherent
    • benchmark_guard_coalesce_and_absent_skip

Measurements (--release, live Windows placeholders & aliased views)

Workload Metric Baseline After Reduction / Speedup
Sparse placeholders (192 blocks, 128 split 64 KiB holes, 60 submissions, 4 dirty/sub) VirtualProtect calls 493 125 3.9x fewer
Sparse placeholders (same) VirtualQuery calls 7,808 132 (0 steady-state) 59.2x fewer
Sparse placeholders (same) Wall-clock time 2.627 ms 0.546 ms 4.8x faster
Dual-aliased contiguous view (64 blocks x 2 views, 30 open+guard cycles) VirtualProtect calls 5,855 122 48.0x fewer
Dual-aliased contiguous view (same) Wall-clock time 3.315 ms 1.864 ms 1.8x faster

Verification

  • 20/20 consecutive parallel runs of cargo test -p victory-gpu -- watch: 24 passed; 0 failed; 0 ignored
  • cargo test -p victory-gpu --features diagnostics -- watch: 24 passed; 0 failed
  • cargo test -p victory-gpu -- inplace: 6 passed; 0 failed
  • cargo test --release -p victory-gpu -- watch::tests::benchmark_guard_coalesce_and_absent_skip --exact --nocapture: PASS
  • cargo clippy -p victory-gpu --all-targets: 0 new warnings in modified code
  • cargo run -p xtask -- policy --staged: asset policy: 2 files ok

Ported from fxceless/victory-fork#1279 (fxceless/victory-fork#1238).

**@⁠Vladimir-Human** on GitHub · 2026-11-26 10:36 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/345) `Vladimir-Human:perf/watch-guard-sparse-dirty` → `main` · +2296 −255 · 2 files · 1 commits · open ## Summary Ports the `watch::guard` sparse interval-skip, placeholder-hole skip, `O(dirty)` re-arm, and coalesced region-guided `VirtualProtect` optimizations from `fxceless/victory-fork#1279` (addressing `fxceless/victory-fork#1238`) onto `AnyXboxX`'s `third_party/victory/crates/victory-gpu/src/{watch.rs,inplace.rs}` while **preserving all existing AnyXboxX / Neptune concurrency and lock-free improvements** (`RwLock` on `JOURNAL`/`GUARDS`, lock-free `CLEAN`/`ABSENT`/`PARTS`/`WHOLE`/`SUBS`, `watch_4k`/`stale_mask`, `ask_once_quiet`, `quiet_back`, `HOT`/`VICTORY_GUARD_HOT`, `VICTORY_GUARD_OPEN_AHEAD`, and sharded `MAPPING`). ### What changed 1. **Coalesced interval tracking & placeholder-hole skip (`watch.rs`)**: - Replaced per-block `BTreeSet<usize>` sets in `Guards` (`protected`, `clean`, `absent`, `aliased`) with disjoint coalesced half-open intervals (`BlockSet`) plus `placeholder_absent: BlockSet`. - Extended the existing lock-free `ABSENT` table to distinguish `ABSENT_NONE = 0`, `ABSENT_ORDINARY = 1`, and `ABSENT_PLACEHOLDER = 2` (with chunked `absent_set_range` / `clean_set_range`), allowing both the lock-free fast path and `guard_locked` to skip `NtQueryVirtualMemory(MemoryRegionInformationEx)`-confirmed `MEM_RESERVE_PLACEHOLDER` holes with **0 `VirtualQuery` and 0 `VirtualProtect` syscalls** until `title_memory_remapped` (`VICTORY_ABSENT_ONCE=0` opts out; ordinary non-placeholder `MEM_RESERVE` / `SEC_RESERVE` holes remain `ABSENT_ORDINARY` and still probe via `VirtualQuery` so silent commits are detected immediately). - `writes()` checks `guarded && known_view(first * PAGE)` (lock-free `CLEAN`/`ABSENT` first, then `guards_read()`) to avoid wasted failing `GetWriteWatch` calls on known views. - `guards_read()` uses `O(log K)` `g.clean.covers(first, last)`; `writing()` uses `O(log K)` `intersects`; `title_memory_written()` uses `count_in`; `title_memory_remapped()` uses `take_range`/`remove_range` (removing the duplicate `aliased` loop). - Added `INSTALLED: AtomicBool` and write-lock re-check after read-to-write lock upgrade in `guard()` to prevent TOCTOU races with concurrent `title_memory_remapped`. 2. **`O(dirty)` re-arm, `protected ∩ absent == ∅`, coalesced region-guided `VirtualProtect`, & batched alias lookups (`watch.rs`, `inplace.rs`)**: - `guard_locked` skips contiguous `clean` and confirmed `placeholder_absent` intervals in `O(log K)`, marks already-`protected` blocks (e.g. alias-guarded) `clean` in `O(dirty)` with 0 `VirtualProtect` calls (preserving `VICTORY_GUARD_HOT`), and coalesces adjacent unprotected dirty blocks into contiguous runs. - `note_protected` clears `absent`, `placeholder_absent`, and lock-free `ABSENT` so `protected ∩ absent == ∅` holds by construction; `handle_protect_failure` returns exclusive `end` and both `protect_here` fallback loops advance past it. - `protect_here`, `protect_alias_run`, and `unprotect_run_os` issue one coalesced `VirtualProtect` per contiguous run with `VirtualQuery`-guided OS VM region fallback (`unprotect_run_os` skips `info.State != MEM_COMMIT` gaps without per-block failures), all guarded with `checked_mul(BLOCK)`. - Added lock-batched `inplace::aliases_in_range` and `inplace::aliases_for_blocks` (keeping `inplace::pages_of` for `stays_open` and `ours_through`), a zero-allocation single-block unaliased fast path in `open` for VEH write faults, and strict lock-free `CLEAN` clearing before any `PAGE_READWRITE` transition. 3. **Unit, regression, stress, & deterministic benchmark tests (`watch.rs`)**: - `block_set_interval_invariants` - `sparse_placeholder_gaps_are_skipped_without_probing_or_protecting` - `contiguous_runs_coalesce_guard_and_unguard_syscalls` - `aliased_views_coalesce_protect_open_and_o_dirty_rearm` - `ordinary_reserve_is_not_placeholder_and_silent_commit_is_detected` - `protected_and_absent_sets_remain_disjoint_after_protect` - `unprotect_run_os_skips_uncommitted_gap_without_per_block_failures` - `concurrent_writers_and_guard_readers_stay_coherent` - `benchmark_guard_coalesce_and_absent_skip` ### Measurements (`--release`, live Windows placeholders & aliased views) | Workload | Metric | Baseline | After | Reduction / Speedup | |---|---|---:|---:|---:| | Sparse placeholders (192 blocks, 128 split 64 KiB holes, 60 submissions, 4 dirty/sub) | `VirtualProtect` calls | 493 | 125 | **3.9x fewer** | | Sparse placeholders (same) | `VirtualQuery` calls | 7,808 | 132 (0 steady-state) | **59.2x fewer** | | Sparse placeholders (same) | Wall-clock time | 2.627 ms | 0.546 ms | **4.8x faster** | | Dual-aliased contiguous view (64 blocks x 2 views, 30 open+guard cycles) | `VirtualProtect` calls | 5,855 | 122 | **48.0x fewer** | | Dual-aliased contiguous view (same) | Wall-clock time | 3.315 ms | 1.864 ms | **1.8x faster** | ### Verification - **20/20 consecutive parallel runs** of `cargo test -p victory-gpu -- watch`: **24 passed; 0 failed; 0 ignored** - `cargo test -p victory-gpu --features diagnostics -- watch`: **24 passed; 0 failed** - `cargo test -p victory-gpu -- inplace`: **6 passed; 0 failed** - `cargo test --release -p victory-gpu -- watch::tests::benchmark_guard_coalesce_and_absent_skip --exact --nocapture`: **PASS** - `cargo clippy -p victory-gpu --all-targets`: **0 new warnings** in modified code - `cargo run -p xtask -- policy --staged`: **asset policy: 2 files ok** Ported from `fxceless/victory-fork#1279` (`fxceless/victory-fork#1238`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:03:21 +00:00
github-bot force-pushed github-bot/gh-345 from edc6eaef45 to 6f6ef4c8cf 2026-11-26 16:39:52 +00:00 Compare
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-26 16:36 UTC · original

Rebased onto latest main (ce0f90f -> 6f6ef4c, preserving upstream 715f7b3 page_writes_mode/page_writes_tests and ee8b92e inplace.rs cleanly) with safety fixes and bisect controls:

Changes

  1. Placeholder skipping OFF by default: placeholder_skip_enabled() is now OFF by default (opt-in only via VICTORY_PLACEHOLDER_SKIP=1), while retaining O(1)-per-OS-region uncommitted_end() region skipping.
  2. Concurrent remap PAGE_READWRITE restoration in title_memory_remapped: Fixed a race caught by our multi-threaded remap + write-fault stress test where guard() could protect newly mapped pages between MapViewOfFile3 and title_memory_remapped(), leaving OS PAGE_READONLY orphaned when title_memory_remapped removed blocks from g.protected. title_memory_remapped now restores PAGE_READWRITE via unprotect_run_os_with for evicted guarded runs.
  3. Zero-overhead OnceLock<bool> diagnostic bisect switches + VEH pre-warming:
    • VICTORY_PLACEHOLDER_SKIP=0|1 (default 0)
    • VICTORY_WATCH_COALESCE=0|1 (default 1; 0 uses per-block VirtualProtect)
    • VICTORY_WATCH_ALIAS_BATCH=0|1 (default 1; 0 falls back to inplace::views_between + pages_of, clearing lock-free CLEAN across aliases before opening primary blocks)
    • VICTORY_WATCH_SINGLE_LOCK=0|1 (default 1; 0 falls back to unconditional install() + guards())
    • install() pre-warms watch_coalesce() and watch_alias_batch().

Verification (on ce0f90f -> 6f6ef4c)

  • cargo xtask policy: asset policy: 214 files ok.
  • cargo clippy --no-deps -p victory-gpu --lib --tests (default & --features diagnostics): 0 warnings in watch.rs / inplace.rs.
  • VICTORY_D3D12_DEBUG=1 cargo test --manifest-path third_party/victory/Cargo.toml -p victory-gpu --lib -- --test-threads=1:
    • watch::: 29 passed; 0 failed (page_writes_tests included), inplace::: 6 passed; 0 failed.
    • Full victory-gpu --lib: default 251 passed; 3 failed (pre-existing main failures in descriptor::host & indirect); 4 ignored; --features diagnostics: 264 passed; 3 failed (same 3 pre-existing); 4 ignored.
**@⁠Vladimir-Human** on GitHub · 2026-11-26 16:36 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/345#issuecomment-7197877130) Rebased onto latest `main` (`ce0f90f` -> `6f6ef4c`, preserving upstream `715f7b3` `page_writes_mode`/`page_writes_tests` and `ee8b92e` `inplace.rs` cleanly) with safety fixes and bisect controls: ### Changes 1. **Placeholder skipping OFF by default:** `placeholder_skip_enabled()` is now OFF by default (opt-in only via `VICTORY_PLACEHOLDER_SKIP=1`), while retaining `O(1)`-per-OS-region `uncommitted_end()` region skipping. 2. **Concurrent remap `PAGE_READWRITE` restoration in `title_memory_remapped`:** Fixed a race caught by our multi-threaded remap + write-fault stress test where `guard()` could protect newly mapped pages between `MapViewOfFile3` and `title_memory_remapped()`, leaving OS `PAGE_READONLY` orphaned when `title_memory_remapped` removed blocks from `g.protected`. `title_memory_remapped` now restores `PAGE_READWRITE` via `unprotect_run_os_with` for evicted guarded runs. 3. **Zero-overhead `OnceLock<bool>` diagnostic bisect switches + VEH pre-warming:** - `VICTORY_PLACEHOLDER_SKIP=0|1` (default `0`) - `VICTORY_WATCH_COALESCE=0|1` (default `1`; `0` uses per-block `VirtualProtect`) - `VICTORY_WATCH_ALIAS_BATCH=0|1` (default `1`; `0` falls back to `inplace::views_between` + `pages_of`, clearing lock-free `CLEAN` across aliases before opening primary blocks) - `VICTORY_WATCH_SINGLE_LOCK=0|1` (default `1`; `0` falls back to unconditional `install()` + `guards()`) - `install()` pre-warms `watch_coalesce()` and `watch_alias_batch()`. ### Verification (on `ce0f90f` -> `6f6ef4c`) - `cargo xtask policy`: `asset policy: 214 files ok`. - `cargo clippy --no-deps -p victory-gpu --lib --tests` (default & `--features diagnostics`): 0 warnings in `watch.rs` / `inplace.rs`. - `VICTORY_D3D12_DEBUG=1 cargo test --manifest-path third_party/victory/Cargo.toml -p victory-gpu --lib -- --test-threads=1`: - `watch::`: `29 passed; 0 failed` (`page_writes_tests` included), `inplace::`: `6 passed; 0 failed`. - Full `victory-gpu --lib`: default `251 passed; 3 failed (pre-existing main failures in descriptor::host & indirect); 4 ignored`; `--features diagnostics`: `264 passed; 3 failed (same 3 pre-existing); 4 ignored`.
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-26 19:19 UTC · original

Noting that #409 (37d4686) replaced third_party/victory with the combined avatarmanz + cool_fox base, putting this branch in CONFLICTING.
The equivalent clean PR is open at fxceless/victory-fork#1279; happy to re-port onto 37d4686+ once #409 settles if you want it here.

**@⁠Vladimir-Human** on GitHub · 2026-11-26 19:19 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/345#issuecomment-7201453552) Noting that #409 (`37d4686`) replaced `third_party/victory` with the combined `avatarmanz + cool_fox` base, putting this branch in `CONFLICTING`. The equivalent clean PR is open at `fxceless/victory-fork#1279`; happy to re-port onto `37d4686+` once `#409` settles if you want it here.
This pull request has changes conflicting with the target branch.
  • third_party/victory/crates/victory-gpu/src/inplace.rs
  • third_party/victory/crates/victory-gpu/src/watch.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-345'
git switch 'github-bot/gh-345'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-345'
git switch 'github-bot/gh-345'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-345'
git switch 'main'
git merge --squash 'github-bot/gh-345'
git switch 'main'
git merge --ff-only 'github-bot/gh-345'
git switch 'main'
git merge 'github-bot/gh-345'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.