prune Guards::opened with 2x hysteresis instead of abrupt clear #627

Open
RileyW wants to merge 1 commit from RileyW/Darner:pr/guard-hysteresis into Main
First-time contributor

Not yet run in-game on this Darner branch (89 watch unit tests run on this revision; previously verified on RTX 3080 / i7-8700 in a downstream fork).

What it changes

  • The problem on main: read from code (crates/darner-gpu/src/watch.rs:4244, Guards::opened_at).

When Guards::opened reached 8,192 entries (const MANY = 1 shl 13), opened_at ran a full retain scan calling Instant::elapsed() per entry under the global GUARDS mutex, and if 8,192 entries were still live within 50 ms it abruptly called self.opened.clear(), dropping all active rest windows at once. If 8,191 entries survived, the very next fault immediately hit 8,192 again and repeated the full O(8192) scan on every fault.

This change:

  1. Adds opened_prune_at (initialized to OPENED_PRUNE_MIN = 8,192, capped at OPENED_PRUNE_MAX = 65,536) with 2x hysteresis backoff after each prune, so a live working set above 8,192 does not thrash retain on every fault or abruptly clear active rest entries.
  2. Uses at.saturating_duration_since(*opened) inside retain instead of calling Instant::elapsed() per entry, eliminating thousands of redundant clock queries under the GUARDS lock.
  3. Preserves fault-only insertion into Guards::opened and leaves default rest() (3 ms) and hot() (off) unchanged.
  4. Adds deterministic unit test opened_hysteresis_retains_live_entries_and_prunes_expired_without_abrupt_clear in crates/darner-gpu/src/watch.rs.

How it was tested

  • Tested revision: e64ed2c (based on c8258f9)
  • Commands and results:
    • cargo test -p darner-gpu --lib watch:: --locked -- --test-threads=1 --skip streamed_texture_written_through_an_alias_reaches_a_full_queue (89 passed, 0 failed)
    • cargo clippy --workspace --all-targets --locked -- -D warnings (0 warnings)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (0 warnings)
    • cargo xtask policy and cargo xtask policy --staged (ok)
  • Changes a default: yes, for all hosts (changes only Guards::opened pruning hysteresis and reuses passed timestamp; default rest/hot timing unchanged).
  • A/B result: no in-game Darner A/B table yet (eliminates O(N) per-fault retain thrashing and 8,192x Instant::elapsed() calls under GUARDS mutex during open-world streaming).
  • Gates run on that revision: both workspace clippy gates, cargo xtask policy, and darner-gpu watch:: tests.
  • Gameplay: not yet re-run in-game on this Darner branch.
  • Hardware dependence: general (CPU watch fault bookkeeping).
  • Coverage gaps: On Windows 10 22H2, streamed_texture_written_through_an_alias_reaches_a_full_queue aborts (exit code 3) because test binaries without Agility SDK exports see SM 0x65 from system d3d12.dll; all 89 other watch:: tests including the new hysteresis test passed.

Checklist

  • I ran this change in the game, or say below why it cannot be (CPU watch unit tests run; in-game Darner run pending).
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #565 (watch test isolation), #615 (alias-write notifications), pr/guard-scan (protect_here/bitmap batching; separate functions).
  • Replaces or overlaps: none found.
Not yet run in-game on this Darner branch (89 watch unit tests run on this revision; previously verified on RTX 3080 / i7-8700 in a downstream fork). ## What it changes - The problem on main: read from code (crates/darner-gpu/src/watch.rs:4244, Guards::opened_at). When Guards::opened reached 8,192 entries (const MANY = 1 shl 13), opened_at ran a full retain scan calling Instant::elapsed() per entry under the global GUARDS mutex, and if 8,192 entries were still live within 50 ms it abruptly called self.opened.clear(), dropping all active rest windows at once. If 8,191 entries survived, the very next fault immediately hit 8,192 again and repeated the full O(8192) scan on every fault. This change: 1. Adds opened_prune_at (initialized to OPENED_PRUNE_MIN = 8,192, capped at OPENED_PRUNE_MAX = 65,536) with 2x hysteresis backoff after each prune, so a live working set above 8,192 does not thrash retain on every fault or abruptly clear active rest entries. 2. Uses at.saturating_duration_since(*opened) inside retain instead of calling Instant::elapsed() per entry, eliminating thousands of redundant clock queries under the GUARDS lock. 3. Preserves fault-only insertion into Guards::opened and leaves default rest() (3 ms) and hot() (off) unchanged. 4. Adds deterministic unit test opened_hysteresis_retains_live_entries_and_prunes_expired_without_abrupt_clear in crates/darner-gpu/src/watch.rs. ## How it was tested - Tested revision: e64ed2c (based on c8258f9) - Commands and results: - cargo test -p darner-gpu --lib watch:: --locked -- --test-threads=1 --skip streamed_texture_written_through_an_alias_reaches_a_full_queue (89 passed, 0 failed) - cargo clippy --workspace --all-targets --locked -- -D warnings (0 warnings) - cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (0 warnings) - cargo xtask policy and cargo xtask policy --staged (ok) - Changes a default: yes, for all hosts (changes only Guards::opened pruning hysteresis and reuses passed timestamp; default rest/hot timing unchanged). - A/B result: no in-game Darner A/B table yet (eliminates O(N) per-fault retain thrashing and 8,192x Instant::elapsed() calls under GUARDS mutex during open-world streaming). - Gates run on that revision: both workspace clippy gates, cargo xtask policy, and darner-gpu watch:: tests. - Gameplay: not yet re-run in-game on this Darner branch. - Hardware dependence: general (CPU watch fault bookkeeping). - Coverage gaps: On Windows 10 22H2, streamed_texture_written_through_an_alias_reaches_a_full_queue aborts (exit code 3) because test binaries without Agility SDK exports see SM 0x65 from system d3d12.dll; all 89 other watch:: tests including the new hysteresis test passed. ## Checklist - [ ] I ran this change in the game, or say below why it cannot be (CPU watch unit tests run; in-game Darner run pending). - [x] This PR contains one change based on main. - [x] The diff follows docs/asset-policy.md. - [x] I recorded this PR dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #565 (watch test isolation), #615 (alias-write notifications), pr/guard-scan (protect_here/bitmap batching; separate functions). - Replaces or overlaps: none found.
TrustNo1 changed title from [pr-performance][memory] prune Guards::opened with 2x hysteresis instead of abrupt clear to prune Guards::opened with 2x hysteresis instead of abrupt clear 2026-12-06 03:25:57 +00:00
Owner

Test needed — commit e64ed2c2; what to check:

  • The problem on main: read from code (crates/darner-gpu/src/watch.rs:4244, Guards::opened_at).

When Guards::opened reached 8,192 entries (const MANY = 1 shl 13), opened_at ran a full retain scan calling Instant::elapsed() per entry under the global GUARDS mutex, and if 8,192 entries were still live within 50 ms it abruptly called self.opened.clear(), dropping all active rest windows at once. If 8,191 entries survived, the very next fault immediately hit 8,192 again and repeated the full O(8192) scan on every fault.

This change:

  1. Adds opened_prune_at (initialized to OPENED_PRUNE_MIN = 8,192, capped at OPENED_PRUNE_MAX = 65,536) with 2x hysteresis backoff after each prune, so a live working set above 8,192 does not thrash retain on every fault or abruptly clear active rest entries.
  2. Uses at.saturating_duration_since(*opened) inside retain instead of calling Instant::elapsed() per entry, eliminating thousands of redundant clock queries under the GUARDS lock.
  3. Preserves fault-only insertion into Guards::opened and leaves default rest() (3 ms) and hot() (off) unchanged.
  4. Adds deterministic unit test opened_hysteresis_retains_live_entries_and_prunes_expired_without_abrupt_clea

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `e64ed2c2`; what to check: - The problem on main: read from code (crates/darner-gpu/src/watch.rs:4244, Guards::opened_at). When Guards::opened reached 8,192 entries (const MANY = 1 shl 13), opened_at ran a full retain scan calling Instant::elapsed() per entry under the global GUARDS mutex, and if 8,192 entries were still live within 50 ms it abruptly called self.opened.clear(), dropping all active rest windows at once. If 8,191 entries survived, the very next fault immediately hit 8,192 again and repeated the full O(8192) scan on every fault. This change: 1. Adds opened_prune_at (initialized to OPENED_PRUNE_MIN = 8,192, capped at OPENED_PRUNE_MAX = 65,536) with 2x hysteresis backoff after each prune, so a live working set above 8,192 does not thrash retain on every fault or abruptly clear active rest entries. 2. Uses at.saturating_duration_since(*opened) inside retain instead of calling Instant::elapsed() per entry, eliminating thousands of redundant clock queries under the GUARDS lock. 3. Preserves fault-only insertion into Guards::opened and leaves default rest() (3 ms) and hot() (off) unchanged. 4. Adds deterministic unit test opened_hysteresis_retains_live_entries_and_prunes_expired_without_abrupt_clea Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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 'pr/guard-hysteresis:RileyW-pr/guard-hysteresis'
git switch 'RileyW-pr/guard-hysteresis'

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 'RileyW-pr/guard-hysteresis'
git switch 'RileyW-pr/guard-hysteresis'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'RileyW-pr/guard-hysteresis'
git switch 'RileyW-pr/guard-hysteresis'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'RileyW-pr/guard-hysteresis'
git switch 'Main'
git merge --squash 'RileyW-pr/guard-hysteresis'
git switch 'Main'
git merge --ff-only 'RileyW-pr/guard-hysteresis'
git switch 'Main'
git merge 'RileyW-pr/guard-hysteresis'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
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
TrustNo1/Darner!627
No description provided.