Skip to content

fix(image): reclaim dead slots on shared atlas pages and bound ImageAtlas in pages, slots, and bytes - #2124

Merged
Hona merged 1 commit into
devfrom
fix/2103-shared-page-eviction-and-bounds
Nov 25, 2026
Merged

Hona merged 1 commit into
devfrom
fix/2103-shared-page-eviction-and-bounds

Conversation

@Hona

@Hona Hona commented Nov 25, 2026

Copy link
Copy Markdown
Member

Fixes #2103

Follow-up to #2115 (which fixed empty middle shelf pages retaining their CPU/GPU buffers).

Root cause

  • Slot (crates/rune-image/src/pixels.rs) tracked no per-slot liveness (Weak<FrameInner>, touched, shown, or last-drawn frame); ImageAtlas::end_frame and ImageAtlas::rest retained slots solely by pages[s.page].touched / pages[s.page].shown.
  • Any dropped or unshown image sharing a shelf page with one persistent image remained in slots and starts forever (contradicting rest()'s doc), its shelf space could never be reused until the whole page emptied, and ImageAtlas had no cap in pages, slots, or decoded pixel bytes.

Changes

  • Added crates/rune-image/src/shelf.rs (Shelves + ImagePage) partitioning each 1024x1024 shelf page into vertical bands with coalesced horizontal free spans and coalescing/popping empty bands, plus shrink_to_fit_rows() to reclaim unused bottom rows without moving live slots.
  • Added per-slot liveness on Slot (Weak<FrameInner>, shown, touched, last_frame, relocate) and mid-frame resize deferred-free tracking in crates/rune-image/src/pixels.rs:
    • end_frame() immediately drops undrawn slots whose Pixels handles were dropped and returns their rects to their shelf bands, while keeping slots drawn on the current frame intact for the frame's upload/draw.
    • rest() evicts slots not shown in the last completed frame (or whose handles were dropped) per slot—including on page 0 and shared pages—cleans their starts entries, shrinks remaining shelf pages to their occupied rows, and empties pages left with zero slots.
  • Bounded ImageAtlas retention across frames to 16 pages (IMAGE_ATLAS_MAX_PAGES), 64 MiB of decoded pixels (IMAGE_ATLAS_MAX_BYTES), and 4,096 slots (IMAGE_ATLAS_MAX_SLOTS) with LRU eviction of unshown entries and next-frame relocation of sparse overflow pages above IMAGE_ATLAS_MAX_PAGES.

Before / after

Scenario Before After
1 persistent 64x64 + 4 dropped 256x256/frame × 40 frames (after frame 39 end_frame) 2 pages, 20 slots, 8,388,608 B 1 page, 5 slots, 4,194,304 B
Same scenario after rest() 2 pages, 20 slots, 8,388,608 B 1 page, 1 slot, 524,288 B
20 persistent 64x64 icons interleaved with 20 bursts of 3×512x512 dropped thumbs (after rest()) 20 pages, 80 slots, 83,886,080 B 1 page, 20 slots, 524,288 B
Atlas retention bound Unbounded 16 pages / 64 MiB (67,108,864 B) / 4,096 slots

Verification

  • Added 7 unit tests in crates/rune-image/src/atlas_tests.rs (40-frame shared-page repro failed on dev with 2 pages / 20 slots / 8,388,608 B and passes with fix; variable-size band coalescing; 20 persistent icons + thumbnail bursts; rest() per-slot + animation starts cleanup; filling past page, byte, and slot bounds with re-upload of returning evicted images; ImageCache counts; mid-frame resize deferred reclamation) and 1 GPU test in crates/rune/src/render/image_tests.rs (gpu_shared_page_eviction_rest_shrink_and_reupload_draw_exact_pixels).
  • Ran cargo test -p rune-image (91 passed, 9 doctests passed), cargo test -p rune -p rune-mermaid -p rune-kit --features rune-kit/markdown,rune-kit/mermaid render::image_tests, sixty_ (pixels_streaming: 0 allocations), render::upload::tests, 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.

@Hona
Hona merged commit 037775c into dev Nov 25, 2026
1 check passed
@Hona
Hona deleted the fix/2103-shared-page-eviction-and-bounds branch November 25, 2026 05:04
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.

ImageAtlas retains empty <= 1024x1024 shelf pages when higher pages are active and never evicts dead Slots on shared pages

1 participant