Repository navigation
Description
What happens
ImageAtlas copies each shown image into a page buffer (ImagePage::data, 4 MiB for a 1024x1024 page, or the image's own size for a larger one) and uploads the dirty rows to the page texture. The buffer stays for as long as the page lives. So every image a window shows costs three copies: the app's Pixels, the page buffer, and the texture. The glyph atlas does the same with its 2048x2048 pages.
Where
At dev de2b16de:
- pixels.rs#L214-L218 and pixels.rs#L347-L351: a page owns
data, allocated at full page size. - pixels.rs#L300-L316:
end_frameemptiesdataonly for an oversize page with no image left; a 1024 page keeps it. - render/upload.rs#L129-L151:
datais read again only for a new texture or after a lost device.
Measured
macOS 26.6 VM, Rune 7ead7d63 (these files are the same on dev), our screenshot app with its main window showing a grid of thumbnails.
- With malloc stack logging, the large resident anonymous regions at idle are three 4 MiB regions from
ImageAtlas::place(page buffers) and one 4 MiB region fromtext::atlas::Atlas::new(an overlay's glyph atlas). Together 16 of the 34 MB of untagged memory. - Over 10 hide and show cycles of the main window, the untagged memory and the private texture memory move together, in 4 MB steps (38 and 36 MB, then 42 and 40 MB, and back). One page buffer and one page texture per step.
So the CPU side of the image atlas is about as large as its GPU side. On a GPU with its own memory, the CPU copy is the part that counts as process memory.
Ask
Do not keep the page buffer after its rows are uploaded. Options, from the cheapest:
- Copy each new image's rows straight into the staging upload, and keep no page buffer.
- On a lost device or a new texture, upload again from the
Pixelsthat the view still shows (the slots know them), not from a page copy. - If a CPU copy must stay for some backend, make it opt-in.
Done when: a window that shows N MB of images in 1024 atlas pages holds no CPU page buffers after the frame that uploaded them, a lost-device test still draws the images after recovery, and the glyph atlas follows the same rule or the issue says why not.
Related: #148 (the app keeps its own copy because Pixels cannot be read back), #80 (memory policy).
Posted via Claude
Activity
heto-m commentedon Nov 8, 2026
Another data point: I show scanned A4 pages (2480×3508, ~35 MB RGBA each). With
El::pixelsthePixelscopy is also kept for as long as the image is in the view, so each page stays in RAM twice. Three pages on screen cost about 100 MB RSS more than uploading the same pages to my ownwgpu::Texturefrom aCompositorand dropping the bytes.So for this case the
Pixelscopy matters as much as the page buffer. Something like an upload-once image that only keeps GPU memory (and asks the app for the bytes again after a lost device) would let me drop the compositor.hlimouni commentedon Nov 13, 2026
Another data point, from a document viewer at
dev8e1ae9aeon macOS (M2 Pro, release, headless at 2×), with a view showing PDF pages:Pixelseach (about 1190×1684 px, 8 MB): each visible page is held as thePixelscopy, the standalone page'sImagePage::dataand the texture, so 2–3 visible pages cost about 61 MB resident. Pages scrolling in and out also create and free a standalone texture each time.Pixelswithout showing them costs little (peak 140 MB resident); showing them during a fast scroll took the peak to 444–499 MB. Most of the difference looked like upload staging:Stagingkeeps its chunks for reuse at the size of the busiest burst, and a fast scroll uploads several MB of new tiles a frame. A headless run isn't paced, so a window may peak lower; not measured.What the app could do itself was hold tiles back during a fling. That brought the same run to 229 MB. Anything from the ask above would cut the rest. One addition: a way to trim
Stagingafter a burst (when the window rests, or app-callable;give_backalready frees it) would bound the peak.kaeawc commentedon Nov 24, 2026
Another data point, for a streamed image rather than a static one: an Android emulator screen (1080×2400 RGBA, 10 MB a frame) shown through
El::pixelsand replaced withPixels::seton every new frame (kaeawc/simee,desktop/src/android.rs).An image that size gets a standalone page the size of the image (
ImagePage::new,devpixels.rs#L452-L454), so the page buffer is a byte-for-byte copy of thePixelsbuffer. Each frame then pays for it: the producer's buffer is copied into the page (place_at, #L344-L353), and the page's rows are copied into staging (render/upload.rs#L141-L168). With the producer's own copy that is about three full-frame copies and a fresh 10 MBVecper frame, which we estimate at roughly 6 ms of CPU per frame (an estimate, not a profile).Option 1 here (stage rows straight from the image, keep no page buffer) would remove one of those copies per frame for this case as well as the retained memory. For a standalone page in particular there is nothing to pack, so its rows could be staged from the
Pixelsbuffer directly.opencodejr commentedon Nov 25, 2026
Thanks for the detailed write-up and the measurements. This still holds on current
dev, though the code has moved.ImagePageis now incrates/rune-image/src/shelf.rsandImageAtlasincrates/rune-image/src/pixels.rs. Since your report, shelf pages start small (#701), and #2103/#2102 dealt with empty or rested pages. A page that still shows an image keeps its fulldatabuffer.A few things that read
datatoday and would need another source if the buffer goes away:Renderer::upload_images(crates/rune/src/render/upload.rs) uploads whole pages from it again for a new texture or after a lost device.ImagePage::shrink_to_fit_rowstruncates it, then marks the whole page dirty for upload again.crates/rune/src/render/cpu_boot.rs,Scene::new) samplesImagePage::dataand the glyph atlas'spage_datadirectly. That's the backend your option 3 would cover.Your option 1 (stage the rows from the
Pixelsframe, with the slots re-uploading from theirPixelsafter a loss) fits the follow-up comments too. In particular, a standalone page is a byte-for-byte copy of itsPixels. We'll look at making the change against the "done when" criteria you listed.opencodejr commentedon Nov 25, 2026
I looked into making this change and haven't opened a PR. It needs a few design decisions before the work can be split up, so here's what I found on current
dev:Staging::write_texture_rows(and the MetalDirectTexturehook) writes whole page-width rows. Several images share those rows on a 1024 shelf page, so uploading without the page buffer would zero out the other images. Option 1 first needs rectangle writes inrune-staging, including its Metal path.Renderer::upload_imagesuses it after a lost device or for a new texture. When a shelf page grows (ImagePage::allocate) or shrinks (shrink_to_fit_rows), the whole page is also marked dirty. Option 2 can rebuild from the slots, but a slot only holds aWeakreference to itsPixels. We'd need to decide what a slot whosePixelswas dropped draws after recovery, and how animated or mip-level slots rebuild their bytes.render/cpu_boot.rs,Scene::new) samplesImagePage::data, and the GPU's first frame uploads those same bytes at the hand-over. That's the CPU copy option 3 would make opt-in.FilledOnCpuandrefill_gpu_masksinrune-text/src/atlas/fill.rs), so it can't follow the same rule without a different path for those masks.A smaller step would be for a standalone (larger than 1024) page to hold a reference to the
Pixelsframe instead of copying it. That removes the extra copy for whole-page images and streamed frames like the emulator case, but it doesn't help 1024 shelf pages. I'm leaving this open with these notes so the approach can be agreed first.godismyjudge95 commentedon Nov 27, 2026
A current number from
devdacf5d89(macOS 26.6 VM, native Metal backend, our screenshot app, no patches). With the main window hidden in the tray, malloc stack logging finds 10.5 MB ofImagePagebuffers (rune-image/src/shelf.rs#L176) in 4 regions (ImagePage::allocate8.0 MB,ImagePage::new2.5 MB). After a region capture and a full-screen capture it is 12.0 MB in 4 regions. The GPU textures of these pages are gone at hide (#161), so only the CPU copies stay.In our case most of these pages stay because the last view of the hidden window still holds the images (#2675). If Rune drops those, the pages can go at hide before the larger change that this issue describes.
Posted via Claude
Hona commentedon Nov 28, 2026
Thank you @godismyjudge95 for the fresh Mac
mallocstack measurement ondacf5d89!Merged #2688 (
2173df2f) ondevto eliminate retainedImagePage::dataCPU shadow buffers on GPU windows and overlays:Staging::write_texture_rect(plusDirectTextureRectand Metalreplace_texture_rect/write_texture_rect_in_placefor idle Apple Silicon shared textures) so individual slots upload straight fromPixels/ decoded mips without needing a full-page CPU row buffer.ImageAtlas::set_backing(Backing::Gpu)/release_cpu_pages. GPU-first windows and overlays switch toBacking::Gpubefore frame 0 (soImagePage::newandImagePage::allocateallocate 0 bytes forpage.data), andRenderer::upload_imagesdrops any retained CPU page buffers after uploading.rest()shrinking,give_back(true)hide/wake,invalidate(), andstaging.lost()re-upload live slots directly from theirPixelshandles (Slot::live) plus any in-frame pending snapshots.cpu_boot::Scenestill borrowsImagePage::datawith zero allocation during CPU-boot (and Windows handover keeps it untilSurfaceMode::Handedcommits) and reconstructs pages on demand viaImageAtlas::read_page_rgbaif called after GPU upload.FilledOnCpu/refill_gpu_masksinrune-text/src/atlas/fill.rs), so this PR specifically removes theImagePageCPU buffers.Verified locally (Windows DX12/wgpu + wasm32 check):
rune-staging: sub-rectangle uploads with unaligned widths preserve same-row neighbors.rune-image:Backing::Gpuretains 0 bytes inImagePage::dataacross placement, growth, incremental updates,rest()shrinking,invalidate()rebuilds, and bound enforcement.rune: GPU upload drops CPU page buffers to 0 bytes while preserving same-row neighbors, shelf growth, hide/wake, lost-device recovery, and on-demandcpu_boot::Scenepainting;wasm32-unknown-unknowncompiles cleanly.Not run locally: macOS / native Metal hardware (
replaceRegionsub-rect direct path and Macmallocstack logging).Leaving this issue open pending your Mac
mallocrerun on2173df2f(or laterdev) to confirm the 10.5–12.0 MB ofImagePageallocations are gone!