Skip to content

Expose GPU memory policy and allocation block sizing #80

Done

Description

@0rdep

Request

Would you consider exposing the GPU memory allocation policy through App / AppSpec, while keeping Rune's current default?

wgpu already provides MemoryHints::{Performance, MemoryUsage, Manual}. Rune currently selects Performance inside Gpu::device_on, so a downstream app must patch Rune to choose another policy.

This is a request for configurability, not for making small allocation blocks the default for every Rune app.

Downstream use case

I'm building Brazap, a Rust WhatsApp desktop client using Rune and whatsapp-rust. Its main window has a chat sidebar, message history, and image/video/voice previews. It is intended to stay open during the day, so a low idle memory footprint matters. The ordinary idle view needs relatively little GPU resource memory compared with a game or a graphics-heavy application.

We currently carry a small local patch to select MemoryHints::Manual with a 4–8 MiB allocation-block range. A supported API would let the app make this tradeoff without modifying Rune's device creation.

Measurements motivating the request

Approximate snapshots from the same idle 1180 × 800 Brazap window, with full-pane blur disabled and the same integrated GPU / DX12 backend selected for all three runs:

Memory policy Live GPU allocations Reserved GPU memory
Performance 9.6 MiB 192 MiB
MemoryUsage 9.8 MiB 64 MiB
Manual, 4–8 MiB blocks 9.6 MiB 24 MiB

These values come from wgpu::Device::generate_allocator_report() (total_allocated_bytes and total_reserved_bytes). They are allocator measurements, not process RAM or total VRAM figures, and should not be added to those totals. These were separate development-build launches, not a controlled latency/throughput benchmark; they demonstrate the allocation tradeoff rather than establish an optimal block size.

Environment: Windows build 26200 / 25H2, AMD Radeon integrated graphics alongside an RX 9070 XT, Rune 1213159f9ec300c7a309c67a89176f61a0b47753, wgpu 30.0.1, Rust 1.99.1, x86_64-pc-windows-gnu, development profile with debug information disabled. Only Windows was measured.

Possible API

For example, an illustrative builder method forwarding wgpu's existing type, or a Rune-owned equivalent:

App::new(spec)
    .gpu_memory_hints(wgpu::MemoryHints::Manual {
        suballocated_device_memory_block_size:
            (4 * 1024 * 1024)..(8 * 1024 * 1024),
    })
    .try_run()?;

The exact API/name is flexible. Useful behavior would be:

  • Preserve Performance when the application supplies no preference.
  • Allow MemoryUsage and a custom allocation-block range.
  • Define how the choice applies to prewarmed/shared devices, so an existing device does not silently bypass the requested policy. A startup-only option would be sufficient.
  • Document which backends honor or ignore the preference, including the native Metal path. No claim of equal savings across platforms.
  • Make clear that the block range is not an image/video size limit: larger resources can still use dedicated allocations. Smaller blocks can trade allocation overhead for lower reservation.

Related: #46 addresses Windows GPU selection, which also affected Brazap's process memory. That was a separate experiment; its effects are excluded from the table above. This request is specifically about exposing the allocation policy.

Activity

  1. godismyjudge95 commented on Nov 6, 2026

    @godismyjudge95

    Data from a second app for this request, on Linux with lavapipe. dev now asks for MemoryHints::MemoryUsage (render.rs#L477). That helps, but a screenshot app that draws a few screen-size images still keeps several 32 MiB blocks after the images are gone. A Manual block range would let us trade that.

    Setup

    Ubuntu 26.04, GNOME 50 Wayland, VM with 7 vCPUs and no GPU (Mesa lavapipe), screen 2880x1696 at scale 2. Rune 7ead7d63; render.rs, render/layers.rs and pixels.rs are the same on dev d03349e2. On lavapipe, device memory is process memory (memfd:allocation fd mappings), so it shows in PSS. On a GPU with its own VRAM this would not be process RAM. We did not measure that case.

    What we see

    One probe run, main window hidden at the end (per-mapping PSS from smaps):

    step device memory blocks (MiB size / MB resident)
    idle, main window shown 38.4 MB 32/20.8, 16/13.1, 8/4.0, 4/0.5
    after 3 region captures, 1 full-screen capture, hide 155.0 MB 32/25.5, 32/32.0, 32/30.8, 32/28.3, 32/20.8, 16/13.1, 8/4.0, 4/0.5

    Each capture shows one 3024x1964 RGBA image (23.8 MB). The block sizes double 4, 8, 16, 32 MiB and stop at 32. That matches the host-visible range wgpu-hal 30.0.1 gives MemoryUsage (4 to 32 MiB, AllocationSizes::from_memory_hints), so lavapipe's memory types are host-visible (inference). gpu-allocator 0.28 frees a general block only when it is empty and is not the last one (vulkan/mod.rs lines 672-673). One small live resource keeps a whole block, and lavapipe keeps every page that was touched. We did not trace which resource keeps each block.

    Effect on the whole process, against the same app on our previous toolkit, same harness, 5 alternating runs per side (median MB PSS):

    metric previous toolkit Rune A/A noise
    idle, main window shown 226.0 243.0 1.0
    after 10 captures 246.8 291.2 2.9
    idle, hidden to tray 220.5 288.3 1.4

    These numbers are after we removed our own extra copies of the frozen frame. Our smaps probes put the rest of the gap in device memory; the heap is about equal.

    One source of short-lived large allocations: a fade on a window-size element makes an Rgba16Float "isolated group" target (render/layers.rs#L415-L420). For our 2240x1536 bucket that is 27.5 MB per fade (seen with a gdb breakpoint on create_texture). When we removed that fade, the full benchmark did not change (after 10 captures 335.1 to 335.7 MB, p = 0.42), so this is churn, not the cause.

    Ask

    The API in this issue, with Manual allowed. Inference from the allocator code, not measured: with Manual { 4..8 MiB }, a 23.8 MB image texture is larger than the block, so it gets its own allocation that is freed with the texture. That is the behaviour a screenshot app wants.

    Done when: an app can choose Performance, MemoryUsage or Manual with a block range at startup, the choice reaches every device that draws its windows and overlays (prewarmed ones too), and a test reads it back from generate_allocator_report or the device descriptor.


    Posted via Claude

  2. godismyjudge95 commented on Nov 6, 2026

    @godismyjudge95

    Related, filed since our comment: #142 (a hidden Wayland overlay keeps its image atlas pages and last frame, which is part of what fills these blocks in our numbers) and #148 (no read access to Pixels, so apps keep a second copy of each image).


    Posted via Claude

  3. purian23 commented on Nov 12, 2026

    @purian23

    A data point for making this settable. With MemoryHints::MemoryUsage, blocks double in size and a block is freed only when it's empty. One small long-lived allocation pinned a closed popup's 16 MiB block for the life of the device.

    With fixed 4 MiB blocks (MemoryHints::Manual), our bar went from 33.1 to 28.9 MB of GPU memory, and opening a popup cost +5.9 MB instead of +18. +1 for a settable block range.

  4. Hona commented on Nov 24, 2026

    @Hona
    Member

    Startup GPU suballocation policy configuration is now on dev in #1903 (eb588e335).

    API: rune::GpuMemoryPolicy (MemoryUsage default, Performance, Manual { block_size: Range<u64> }), App::gpu_memory_policy, and rune::set_gpu_memory_policy (call before prewarm_gpu, warm_gpu, or warm_gpu_cache). Changing the policy restarts an in-flight macOS prewarm or discards a parked Windows/Linux warm-up device so windows and overlays never silently reuse a device created under the old policy.

    rune::App::new(spec)
        .gpu_memory_policy(rune::GpuMemoryPolicy::Manual {
            block_size: (4 * 1024 * 1024)..(8 * 1024 * 1024),
        })
        .run();

    This follows wgpu::MemoryHints for allocation behaviour (honored by D3D12 and Vulkan; ignored by Rune's native Metal backend, wgpu Metal, and web; resources larger than a Manual block range still succeed via dedicated allocations) and Rune's GpuBackend / App::gpu_backend startup pattern. Compared to the proposal in the issue, we used a Rune-owned enum instead of exposing wgpu::MemoryHints directly (to keep wgpu out of rune's public API) and kept current dev's MemoryUsage default rather than Performance. If any part of that shape doesn't fit your use case, please let us know.

    Verified: Windows D3D12 offscreen allocator-report tests (default 8 MiB, Manual 4..8 MiB with a 10 MiB dedicated allocation, Performance 128 MiB, and parked-GPU discard), macOS arm64 unit/prewarm tests, Linux aarch64 compile check, and wasm32 (WebGPU + WebGL2) compile checks. Not verified: live Vulkan hardware/lavapipe allocator measurements, iOS compile, or an interactive desktop window. Thanks for the detailed measurements!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions