d3d12-gpu: cut a texture from the fullest block that has room, so a sparse block can empty #3451

Closed
leonida/mandy
KelboLive wants to merge 1 commit from KelboLive/mandy:d3d12/pool-fullest-first into main

Pull request closed

Please reopen this pull request to perform a merge.

First-time contributor

What/why

#3261: texture-pool blocks only grow while driving. The finding for D3D12-POOL-PACK-FULLEST-BLOCK reproduced a cause without the game: with two ripe 64 MiB blocks (8 MiB and 48 MiB live, both with room for a new 8 MiB piece), the allocator took the first block with room — the sparse one. Its original piece then came back to a block that was not empty, so the pool kept two blocks even with reclaim(…, 0, true). Reversing only the two entries sent the piece to the fuller block, and the sparse block then dropped when its own piece rested.

Change

  • Pool::take_or_need: among ripe blocks that fit, choose the one with the fewest free bytes (fullest) instead of the first. Ties keep the lowest index; a block that does not fit is never chosen, so the choice is exactly a candidate of the old loop.
  • Ranges::fits makes the same test take makes without taking; Ranges::bytes is the free total. Both are small and used only in the choice.
  • The unripe fallback (blocks made ahead) is unchanged: it still takes the oldest, since ripeness there is the safety property.

Test

a_piece_goes_to_the_fullest_block_so_the_sparse_one_can_empty builds the finding's exact case (two blocks, 8 and 48 MiB live) and asserts the new piece lands in the fuller block, then that the sparse block is let go when its own piece rests and no spare is kept.

  • Red with the old first-fit loop: the piece lands in block 0 (the sparse one) and the pool stays at two blocks.
  • Green here.
  • cargo test -p leonida-gpu --lib -- --test-threads=1: 756 passed, 0 failed, 60 ignored.
  • cargo clippy -p leonida-gpu --lib: no warning in pool.rs; check_no_game_data, git diff --check: clean.

Not done here

No game run. What a run should show: with the title driving, pool.rs blocks of textures rises more slowly and empty blocks are let go on the existing reclaim tick; the #3261 census lines are the measure. This is one case of that issue, not a fix for the whole growth (staging/other pools are separate).

Item: D3D12-POOL-PACK-FULLEST-BLOCK, claimed in #3312. Depends on: none.
AI-assisted: DeepSeek V4.1 Flash (OpenCode); a second read-only pass reviewed the test and the choice.

## What/why #3261: texture-pool blocks only grow while driving. The finding for `D3D12-POOL-PACK-FULLEST-BLOCK` reproduced a cause without the game: with two ripe 64 MiB blocks (8 MiB and 48 MiB live, both with room for a new 8 MiB piece), the allocator took the **first** block with room — the sparse one. Its original piece then came back to a block that was not empty, so the pool kept two blocks even with `reclaim(…, 0, true)`. Reversing only the two entries sent the piece to the fuller block, and the sparse block then dropped when its own piece rested. ## Change - `Pool::take_or_need`: among ripe blocks that fit, choose the one with the **fewest free bytes** (fullest) instead of the first. Ties keep the lowest index; a block that does not fit is never chosen, so the choice is exactly a candidate of the old loop. - `Ranges::fits` makes the same test `take` makes without taking; `Ranges::bytes` is the free total. Both are small and used only in the choice. - The unripe fallback (blocks made ahead) is unchanged: it still takes the oldest, since ripeness there is the safety property. ## Test `a_piece_goes_to_the_fullest_block_so_the_sparse_one_can_empty` builds the finding's exact case (two blocks, 8 and 48 MiB live) and asserts the new piece lands in the fuller block, then that the sparse block is let go when its own piece rests and no spare is kept. - **Red** with the old first-fit loop: the piece lands in block 0 (the sparse one) and the pool stays at two blocks. - **Green** here. - `cargo test -p leonida-gpu --lib -- --test-threads=1`: **756 passed, 0 failed, 60 ignored**. - `cargo clippy -p leonida-gpu --lib`: no warning in `pool.rs`; `check_no_game_data`, `git diff --check`: clean. ## Not done here No game run. What a run should show: with the title driving, `pool.rs blocks of textures` rises more slowly and empty blocks are let go on the existing reclaim tick; the #3261 census lines are the measure. This is one case of that issue, not a fix for the whole growth (staging/other pools are separate). Item: `D3D12-POOL-PACK-FULLEST-BLOCK`, claimed in #3312. Depends on: none. AI-assisted: DeepSeek V4.1 Flash (OpenCode); a second read-only pass reviewed the test and the choice.
Author
First-time contributor

Closing as a duplicate of #3302, which already implements this item (D3D12-POOL-PACK-FULLEST-BLOCK) with the two things an independent review found missing here:

  • it ranks by live bytes, so a block whose pieces are only resting (returned, not yet given back into the free map) is not mistaken for a full one and re-pinned — the failure mode this PR would have introduced;
  • it caches the free total and uses find/take_at, so the per-allocation cost is O(1) rather than summing every range of every candidate.

It also carries the draining-block test. My synthetic test did reproduce the same first-fit red (piece.block == 0 on main), so the diagnosis is confirmed independently; there is no need for two fixes for one issue.

Closing without merging.

Closing as a **duplicate of #3302**, which already implements this item (`D3D12-POOL-PACK-FULLEST-BLOCK`) with the two things an independent review found missing here: - it ranks by **live** bytes, so a block whose pieces are only *resting* (returned, not yet given back into the free map) is not mistaken for a full one and re-pinned — the failure mode this PR would have introduced; - it caches the free total and uses `find`/`take_at`, so the per-allocation cost is O(1) rather than summing every range of every candidate. It also carries the draining-block test. My synthetic test did reproduce the same first-fit red (`piece.block == 0` on `main`), so the diagnosis is confirmed independently; there is no need for two fixes for one issue. Closing without merging.
KelboLive closed this pull request 2026-12-04 12:55:39 +00:00
Sign in to join this conversation.
No description provided.