Skip to content

The desktop app keeps at most 32 MB of a chat's pictures in memory, decoding them as their rows are drawn - #194

Merged
pitis merged 2 commits into
mainfrom
perf/desktop-picture-budget
Nov 14, 2026
Merged

pitis merged 2 commits into
mainfrom
perf/desktop-picture-budget

Conversation

@pitis

@pitis pitis commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

What changed

  • Pictures are decoded when their rows are drawn, not when the chat's details load. Files::look, called only for rows the timeline builds (on screen plus the list's 400 px overdraw), records each drawn picture and queues the ones with nothing decoded. The row callback then starts a single worker that decodes them one at a time, the most recently drawn first, skipping rows already scrolled past.
  • At most 32 MB of decoded pictures are kept (PICTURE_BUDGET, the Mac app's Thumbnails budget, about 20 photos). Past it, the least recently drawn are let go through cx.drop_image, which frees their GPU copies as well. A row drawn again decodes its picture again.
  • Recordings' lengths are read as before; leaving a chat clears everything as before.

Why

The desktop app kept every decoded picture of the open chat until another chat opened: up to 1.6 MB each, about 160 MB for 100 photos, plus the GPU copies once scrolled through.

Measured

scrolling_through_many_photos_keeps_their_pictures_within_a_budget scrolls a row at a time through 150 photos, each decoded at full size (640×640, 1.6 MB), with four on screen.

  • With the budget: the decoded pictures held never go past 32 MB, every photo on screen shows, and a photo let go is asked for again when its row comes back.
  • Without eviction (the old behaviour, checked by disabling it): 245.8 MB held, and the test fails.

Testing

  • cargo test -p hush-desktop: 108 passed. clippy -D warnings and cargo fmt --check are clean.
  • Not run against a real account: the desktop app isn't linked on this Mac. The draw-time loading path (row callback → deferred load_media → client.file_bytes) is checked by reading only. The snapshot demo inserts its pictures directly, so it doesn't go through that path.
  • The budget goes soft for what's on screen: pictures drawn in the latest frame are never let go, so an album bigger than 32 MB keeps all its pictures while it shows. Decoding runs one picture at a time (see the review comment).

Closes #185

… keeps at most 32 MB of them

Every picture of the open chat was decoded when its details loaded and kept until another chat
opened: about 160 MB for 100 photos, plus their GPU copies. Now a picture is decoded when its
row is drawn (on screen or within the list's overdraw), and past 32 MB, the Mac app's budget,
the least recently drawn are let go, GPU copies included; a row drawn again decodes it again.
…t a time skipping rows scrolled past, and the tests cover scrolling back, dropped copies and real drawing
@pitis

pitis commented Nov 14, 2026

Copy link
Copy Markdown
Owner Author

Review (/pr-review-toolkit:review-pr: code-reviewer, pr-test-analyzer) and fixes, in 03fb001

Critical (both reviewers): an album row bigger than the budget decoded forever. A message's files are one row, so an album of 21+ square photos (or ~28 at 4:3) drew more than 32 MB at once. Each decode then evicted a picture on screen, which was asked for again, decoded again, and so on, with the pictures flickering.
Fixed:

  • Hush::render counts frames (Files::new_frame), and each draw records its frame.
  • decoded never evicts a picture drawn in the latest frame, so the budget stretches while an oversized album shows.
  • The test now draws a 30-photo album over three frames and checks that nothing is asked for again. It fails without the guard (p209 is wanted again).

Important: scrolling started many decodes at once (a deferred load_media per frame, each its own task, with full-size decodes before shrinking). That's a transient memory peak in a PR about memory.
Fixed: a single worker, decode_pictures behind a decoding flag, decodes one picture at a time, the most recently drawn first. It skips a picture whose row wasn't drawn in the latest frame (scrolled past); drawn again, it's asked for again. The row trigger now starts only that worker, not the recordings scan.

Important (tests)

  • Recency was only tested where "least recently drawn" equals "first decoded". Fixed: the test scrolls down and back up, so FIFO eviction would evict on-screen photos and fail.
  • GPU copies being freed was untested. Fixed: the test checks that every picture let go is handed back (to drop_image): let go + kept = decoded.
  • The real draw path was untested. Fixed, partly: a headless-window test (--features snapshot) clears the decoded pictures, lets the real list draw, and checks the drawn photos are asked for. The rest of the chain (deferred worker → client.file_bytes) needs a client and wasn't run.

Suggestions, fixed:

  • A decoded, failed or not-downloaded picture asks for nothing (only_a_downloaded_picture_with_none_decoded_is_asked_for).
  • The window test checks that rows that go take their drawn entries with them.

Declined: moving the "still loading?" check into Files::decoded. It stays in the worker, where the chat switch is known; clear_files empties thumbs, so a late decode is dropped there.

Checks: cargo test -p hush-desktop --features snapshot: 144 passed (109 without the feature). clippy -D warnings clean both ways; cargo fmt clean.

Measured (model): 150 photos at 1.6 MB, scrolled down and back. The picture cache stays at or under 32 MB, against 245.8 MB without eviction.
Not measured: process memory in the running app. The desktop app isn't linked on this Mac.

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.

The desktop app keeps every picture of the open chat decoded in memory

1 participant