Repository navigation
Description
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
Pixels::setreplacesframe.rgbaand drops the previousVec: pinned pixels.rs#L95-L106, currentdevpixels.rs#L109-L120.Frameownsrgba: Vec<u8>with no spare: pixels.rs#L15-L21 /dev#L29-L35.Pixels::rgba(doc-hidden) clones the whole buffer, so it can't be used to recycle either:dev#L147-L149.
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
Hona commentedon Nov 24, 2026
Added
Pixels::replaceondevin #1907 (commit1f6dd178b).This follows Rust's
std::mem::replace/Option::replaceidiom and implements the first option you proposed (Pixels::replace -> Vec<u8>), keepingPixels::setunchanged by delegating to it and holding no idle spare buffer insidePixels.replacereturns an emptyVecon the first frame, when replacing decoded mipmaps, or while an in-flight atlas copy orwith_rgbaborrow still holds the oldArc(which drops once the reader finishes). Ifset_withis 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-allocationGlobalAlloctest over 60 frames incrates/rune/tests/pixels_streaming.rson Windows, Linux aarch64 compile check, andwasm32compile checks (cargo check-wasm,cargo check-webgl). Not run: a live desktop window or real emulator stream.