Skip to content

Resolve CPU-accessible block sizing, bounded caching and pressure recovery - #323

Draft
simzy39 wants to merge 4 commits into
Traverse-Research:mainfrom
simzy39:fix/growable-memblock-dedicated-threshold
Draft

simzy39 wants to merge 4 commits into
Traverse-Research:mainfrom
simzy39:fix/growable-memblock-dedicated-threshold

Conversation

@simzy39

@simzy39 simzy39 commented Nov 26, 2026 •

Copy link
Copy Markdown

Depends on #327 and #328. Fixes #322 and #329; adds the manual empty-cache controls requested in #96. The branch contains both independent correctness prerequisites so it compiles and can be tested today; rebase after they merge.

Resolve each memory type's sizing once using explicit CPU accessibility: Vulkan HOST_VISIBLE, D3D12 standard/custom CPU page properties, and Metal Shared versus Private. This describes access, not physical RAM versus VRAM. Use the resolved maximum for the shared/dedicated threshold and grow to fit the resource, counting occupied shared blocks.

Keep one bounded empty cache per type (at most min(2*minimum, maximum)), with aggregate Vulkan retention bounded by the physical heap. Expose cached_capacity(), trim() and trim_to(bytes) using cached indices without scanning historical slots. Under typed memory pressure, reuse compatible blocks, reclaim empty caches and retry, then reduce shared block sizes toward the aligned resource floor. Fatal and allocation-count failures do not cause size retries. Metal uses a conservative exact-floor attempt and commits pending residency removals.

Validation on final 693a9fb (test-only successor of measured b620867): 54 std and 10 no_std library tests; strict Clippy, rustdoc and formatting; actual Rust 1.71 std / 1.81 no_std consumers; Apple ARM/Intel cross-compilation. Six final-source native harness processes match all 60 prior normal allocation-plan cells. Historical seven-policy and downstream comparisons, raw data and reproducer: https://github.com/simzy39/winit-wgpu-repro-benchmarks/tree/main/allocator-reconciliation-20261128. Historical timings are not new final-source timings.

Limits: no Apple runtime validation; source-confirmed Metal selection/platform defects #330 and #331 remain separate. The historical integrated wgpu run has 2,184 passes and 23 failures/aborts, not a passing full native suite. Twenty-two reproduce on pristine upstream; the remaining DX12 multiview behavior depends on executable basename and remains unresolved (gfx-rs/wgpu#8645). wgpu cross-pool recovery also needs a real release of these APIs (gfx-rs/wgpu#11145); no release number or production override is proposed.

AI disclosure: an OpenAI coding agent performed the additional research, source changes, validation and this description under the contributor's explicit publication direction.

@Jasper-Bekkers Jasper-Bekkers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like some mistakes with the host/device toggle made their way in, or I'm mis-understanding what the is_host boolean should be doing.

Comment thread src/d3d12/mod.rs Outdated

mem_block.sub_allocator.free(allocation.chunk_id)?;

let is_host = self.heap_properties.Type != D3D12_HEAP_TYPE_DEFAULT;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is true, D3D12_HEAP_TYPE_READBACK might also be non host.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The revised policy distinguishes CPU accessibility from physical memory placement. Standard READBACK is CPU-readable (WRITE_BACK); GPU_UPLOAD can be GPU-local and still CPU-accessible. The allocator's custom heaps are classified by CPUPageProperty WRITE_BACK/WRITE_COMBINE instead of treating every non-DEFAULT type as host memory. GPU_UPLOAD classification also remains compatible with the oldest permitted Windows bindings. This selects the existing host size range without claiming that READBACK necessarily resides in a separate physical host-memory pool. See the explicit predicate and tests in b620867, plus the independent lifetime fix #327. D3D12 heap contracts.

Agent-assisted response, posted at the contributor's direction.

Comment thread src/metal/mod.rs Resolved
Comment thread src/vulkan/mod.rs Resolved
@simzy39

simzy39 commented Nov 27, 2026 •

Copy link
Copy Markdown
Author

@Jasper-Bekkers

Looks like some mistakes with the host/device toggle made their way in, or I'm mis-understanding what the is_host boolean should be doing.

Hi Jasper, the is_host boolean here is used to select the same AllocationSizes policy as the existing allocation path. All three checks are identical to those already present in MemoryType::allocate(), but they are problematic and you have found real issues.

The intention is to keep allocation and deallocation consistent, rather than introduce or change the host/device classification. If the existing classification needs correcting, I am happy to do that in another PR or would you like me to expand the scope of this PR and do that work here too? (I am up for it.)

@simzy39
simzy39 marked this pull request as draft November 27, 2026 12:31
@simzy39

simzy39 commented Nov 27, 2026 •

Copy link
Copy Markdown
Author

Actually, I'll expand this Pr's scope because fixing the allocators will affect whether I can simplify/change this Pr's code.

@simzy39
simzy39 force-pushed the fix/growable-memblock-dedicated-threshold branch from 179ea43 to b620867 Compare November 28, 2026 10:11
@simzy39 simzy39 changed the title Use max memblock size when deciding dedicated allocations for growable ranges Resolve CPU-accessible block sizing, bounded caching and pressure recovery Nov 28, 2026
@simzy39

simzy39 commented Nov 28, 2026

Copy link
Copy Markdown
Author

The reconciled head is693a9fb44e7d6effa20f988217524c61553f95fe. The earlier wording in this thread overstated the classification defects: CPU accessibility versus physical placement needed clarification; the constructed heap policies were largely already consistent with the existing CPU-visible size settings. The revised explicit predicates and resolved per-type sizing preserve that interpretation rather than claiming READBACK or UMA inherently means a separate physical host-memory pool.

The independently confirmed native-contract and ownership fixes are #327 and #328. #323 keeps the shared-threshold change, bounded idle retention and recoverable-pressure behavior together. #330/#331 document distinct pre-existing Metal selection/platform defects; Apple runtime validation remains outstanding. No MSRV increase is required. Six final-source native plan checks match all60 historical normal cells. The last source change strengthened only a regression test; production code matches b620867. Full raw evidence and the publication report: https://github.com/simzy39/winit-wgpu-repro-benchmarks/tree/main/allocator-reconciliation-20261128

Agent-assisted reconciliation at the contributor’s direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Growable AllocationSizes puts allocations larger than the current step (but <= max_*_memblock_size) into dedicated blocks and stalls geometric growth

2 participants