Skip to content

fix(image): free empty shelf page buffers in ImageAtlas::end_frame when higher pages remain active - #2115

Merged
Hona merged 1 commit into
devfrom
fix/2103-empty-shelf-pages
Nov 25, 2026
Merged

Hona merged 1 commit into
devfrom
fix/2103-empty-shelf-pages

Conversation

@Hona

@Hona Hona commented Nov 25, 2026

Copy link
Copy Markdown
Member

Refs #2103

Root cause

  • In ImageAtlas::end_frame (crates/rune-image/src/pixels.rs), a page left with zero slots only cleared page.data = Vec::new() when page.width > IMAGE_ATLAS_SIZE || page.height > IMAGE_ATLAS_SIZE.
  • When an empty <= 1024x1024 shelf page sat below a higher-indexed active page, self.pages.truncate(keep) could not remove it, leaving up to 4 MiB of CPU Vec<u8> allocated and keeping its 4 MiB GPU wgpu::Texture alive in Renderer::upload_images (crates/rune/src/render/upload.rs) until rest().

Change

  • Free page.data = Vec::new() in ImageAtlas::end_frame for every page left with zero slots (regardless of dimensions). ImageAtlas::allocate already re-initialises empty pages on demand via ImagePage::new(w, h), and Renderer::upload_images drops the GPU texture whenever page.data.is_empty().
  • Skip empty freed pages in ImageAtlas::invalidate so dirty is not left Some on a page upload_images skips.

Before / after (3 pages: 4x512 on p0, 4x512 on p1, 1x64 on p2; next frame draws only p2)

Metric Before After
pages.len() 3 3
Page 1 CPU buffer 4,194,304 B 0 B
Total atlas CPU bytes 8,912,896 B 4,718,592 B
Page 1 GPU texture after upload_images Some(1024x1024) (4 MiB) None (0 B)

Verification

  • Regression tests added: empty_middle_shelf_page_frees_buffer_and_reuses_cleanly (crates/rune-image/src/pixels_tests.rs, failed without fix holding 4,194,304 B, passes with fix) and gpu_empty_middle_shelf_page_drops_texture_on_upload (crates/rune/src/render/image_tests.rs).
  • Ran cargo test -p rune-image (85 passed, 9 doctests passed), cargo test -p rune -p rune-mermaid -p rune-kit --features rune-kit/markdown,rune-kit/mermaid render::image_tests (12 passed), macOS cargo test -p rune-image pixels and cargo clippy -p rune-image -p rune --lib --bins --tests, Linux cargo check -p rune -p rune-kit --tests, and scripts/file-size.sh.

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.

1 participant