Skip to content

perf: bound image pixel caches, scale thumbnails, and unload closed/background sessions - #1352

Merged
Hona merged 6 commits into
devfrom
fix/image-and-session-ram
Nov 26, 2026
Merged

Hona merged 6 commits into
devfrom
fix/image-and-session-ram

Conversation

@Hona

@Hona Hona commented Nov 26, 2026

Copy link
Copy Markdown
Member

Why

Live profiling of opencode-desktop.exe (strobe rec_198110, 1,781 MB commit / 1,517 MB private working set) showed 1,165 MB (65.4%) held by 347 live decoded rune_image::Mip CPU buffers (including 92× 1366×1278 screenshots = 805 MB) because:

  • Timeline thumbnails decoded at full native resolution + full mip chains (ImageCache::load, 5.33 B/px steady, 9.33–12.33 B/px peak).
  • MediaStore, MdStore, and plugin Images had no decoded pixel or encoded byte LRU caps.
  • MediaStore::rest() kept every image pinned while its tab stayed open, rest_pixels() required tabs.is_empty(), parked server halves were never rested, and active animations (e.g. Thinking spinner) blocked App::on_rest indefinitely.
  • Page workers held separate ImageCaches, re-serialized full JSON pages with serde_json::to_vec solely to measure byte length, and parked with cross-thread mimalloc frees uncollected.
flowchart LR
  A[Attachment / Markdown / Ref] -->|load_scaled at display size| B[Shared ImageCache]
  B --> C[MediaStore 48 MiB LRU / MdStore 32 MiB LRU]
  C -->|Scroll past / Page in| D[Demote oldest decoded Image -> keep header size + bytes <= 24 MiB]
  C -->|Click thumbnail| E[Lightbox: Full-res on demand -> dropped on close / tab switch]
  C -->|Tab switch / close / 30s quiet sweep / on_rest| F[Unload closed session + trim active/parked servers + mi_collect]
Loading

Changes

Image Decoding, Scaling & Peak RAM

  • Scaled thumbnails + on-demand full-res viewer (crates/session-view/src/images/cache.rs, mod.rs, viewer.rs, composer/attachments.rs, timeline/attachments.rs): Decode composer chips (160 pt), user thumbs (264 pt), gallery tiles (480 pt), markdown images (720 pt), and favicons (32 pt) with rune::ImageCache::load_scaled (keeping animations for small GIF/WebP/APNG within target px). Lightbox Viewing uses an explicit Resolution state machine (Decoding { full, fallback } -> Ready) to decode full resolution on demand and release it immediately on close or tab switch.
  • Byte-budgeted LRU caches (crates/session-view/src/images/cache.rs, mod.rs, markdown.rs): Cap MediaStore decoded pixels at 48 MiB active / 12 MiB rest and media: encoded bytes at 24 MiB; cap MdStore at 32 MiB active / 8 MiB rest and 24 MiB encoded bytes. Unify Media fetcher and RefSlot into explicit state machines.
  • Bounded concurrent image reads & limits (crates/session-view/src/images/markdown.rs, menu.rs, crates/opencode-plugin-runtime/src/images/, crates/project-icon/): Cap concurrent markdown and plugin image fetch/decode workers to 4 (replacing unbounded task::spawn), hash plugin data: URL keys, bound plugin source images on rest, enforce image::Limits on copy-to-PNG and server project icons, and pre-downsample large project icons before converting to 16 B/px f32 RGBA.
  • Shared ImageCache & zero-copy page/file pipelines (apps/desktop/src/session/pages/mod.rs, worker.rs, boot.rs, features/files/src/poll.rs): Share MediaStore::image_cache() across session-open, session-prefetch, and the timeline; remove the temporary serde_json::to_vec(value) serialization in pages::read, drop value before prepare_images, and move Body::Image bytes without cloning.

Session Unloading, Lifecycle & Allocator

  • Immediate tab-close/switch unloading & quiet periodic sweep (apps/desktop/src/shell/memory_reclaim.rs, tabs/actions.rs, session/meta/open.rs, tick.rs, kernel.rs): Unload closed sessions immediately on close_tab, trim background pixels on tab switch, and run an explicit MemorySweep state machine every 30 s (returning without dirtying clean Tracked cells or disrupting on_rest) so active spinners never starve reclamation.
  • Multi-server rest & auxiliary cache pruning (apps/desktop/src/shell/memory_reclaim.rs, servers/runtime.rs, windows/mod.rs, windows/peer.rs, plugin_host/mod.rs, features/files/src/lib.rs, shell/panels.rs): Rest both active and parked server halves using per-server tab visibility, preserve parked Pages CacheRefs in multi-window retain_user_messages, prune TurnDiffs, ContextWindows, pages::{histories, gaps, bridges}, Ends/Recall, inbox_payload_store, closed plugin Server:Resource entries, and release closed file tabs via SessionFiles::close_file.
  • Non-image peaks & mimalloc worker purging (apps/desktop/src/lib.rs, pages/worker.rs, crates/opencode-kit/src/client.rs, model/tool.rs, crates/browser-pane/src/transport.rs): Tune mimalloc 3.3 (purge_delay = 250 ms, page_reclaim_on_free = 1), collect worker thread heaps before parking, shrink SSE (opencode-kit) and browser sidecar (browser-pane) line buffers after large frames, and drop redundant metadata["output"] once opencode-kit.output is stored.

@Hona
Hona marked this pull request as ready for review November 26, 2026 21:20
@Hona
Hona merged commit f0256d1 into dev Nov 26, 2026
7 checks passed
@Hona
Hona deleted the fix/image-and-session-ram branch November 26, 2026 23:06
@Hona Hona mentioned this pull request Nov 29, 2026
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