Skip to content

Pixels: set drops the previous frame's buffer, so a streamed image allocates a full frame every frame #1823

Done

Description

@kaeawc

What happens

Pixels::set(width, height, rgba: Vec<u8>) moves the new buffer into the shared Frame and drops the one it held. There is no way to get that buffer back or to write a frame into storage Pixels already owns. A producer that streams frames (a video decoder, an emulator or simulator screen) has to allocate a fresh width * height * 4 buffer for every frame, and the old one is freed under the frame lock.

For a 1080×2400 phone screen that is a new 10,368,000-byte Vec per frame. A fresh allocation that size comes straight from the OS on macOS and Linux (above the malloc mmap threshold), so every frame also page-faults its 10 MB in when the producer writes it, and the drop returns it.

Where

Unchanged between 7a707454 (Nov 20) and 55414843 (dev, Nov 23).

Measured

Our desktop app (kaeawc/simee, desktop/src/android.rs) shows an Android emulator's 1080×2400 screen through El::pixels, calling Pixels::set from a frame thread for each new frame. Reading the frame and handing it to Rune costs a fresh 10 MB Vec per frame plus about three full-frame copies (ours into the Vec, the atlas copy in ImageAtlas::place_at, the staging copy). We estimate that at roughly 6 ms of CPU per frame; we have not isolated the allocation and page faults from the copies.

On our side only (not Rune), our ring-reader bench shows the size of one such copy: catching up a burst of 8 frames takes 4.4–7.6 ms reading 83 MB, and 0.52–0.79 ms reading 10 MB when only the newest frame is read. The second number is the per-frame floor we'd like to keep, and the fresh allocation per frame sits on top of it.

Ask

A way to hand a buffer back for reuse. Either of these would do; the first is smallest and source-compatible:

impl Pixels {
    /// As `set`, and returns the buffer the previous frame held (empty if none, or if the
    /// renderer still needs it), for the producer to fill with a later frame.
    pub fn replace(&self, width: u32, height: u32, rgba: Vec<u8>) -> Vec<u8>;
}
impl Pixels {
    /// Writes the next frame into a buffer `Pixels` keeps for this purpose (reused when the
    /// size matches), then publishes it as `set` does.
    pub fn set_with(&self, width: u32, height: u32, fill: impl FnOnce(&mut [u8]));
}

replace keeps the frame lock as short as set holds it today. set_with needs two buffers so fill doesn't run under the lock that ImageAtlas::place_at takes (see the separate issue about that lock). A frame that coalesced away without being drawn can be handed back too; a stream only ever shows the newest.

Done when: a producer calling replace (or set_with) at a steady size allocates no frame buffers after the first two frames.

Activity

  1. Hona commented on Nov 24, 2026

    @Hona
    Member

    Added Pixels::replace on dev in #1907 (commit 1f6dd178b).

    use rune::Pixels;
    
    let pixels = Pixels::new();
    let mut spare = pixels.replace(2, 1, vec![255, 0, 0, 255, 0, 255, 0, 255]);
    spare.resize(8, 0);
    let recycled = pixels.replace(2, 1, spare); // returns the prior Vec for reuse

    This follows Rust's std::mem::replace / Option::replace idiom and implements the first option you proposed (Pixels::replace -> Vec<u8>), keeping Pixels::set unchanged by delegating to it and holding no idle spare buffer inside Pixels. replace returns an empty Vec on the first frame, when replacing decoded mipmaps, or while an in-flight atlas copy or with_rgba borrow still holds the old Arc (which drops once the reader finishes). If set_with is still needed for any reason, feel free to share why.

    You can see buffer reuse in examples/matched (--scene mips).

    Verified with unit tests in crates/rune-image/src/pixels_tests.rs (steady-state pointer/capacity reuse, coalesced updates, concurrent atlas copy, and empty/mips transitions) on Windows and macOS arm64, a 0-allocation GlobalAlloc test over 60 frames in crates/rune/tests/pixels_streaming.rs on Windows, Linux aarch64 compile check, and wasm32 compile checks (cargo check-wasm, cargo check-webgl). Not run: a live desktop window or real emulator stream.

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