Skip to content

Hush idles under its 100 MB budget: chats' pictures are decoded small, and the window lets go of spare drawables - #296

Merged
pitis merged 3 commits into
mainfrom
perf/idle-memory
Nov 29, 2026
Merged

pitis merged 3 commits into
mainfrom
perf/idle-memory

Conversation

@pitis

@pitis pitis commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Hush idled at ~190 MB on the owner's Mac against a 100 MB budget. Two things held most of it, and both are fixed here.

What changed

Chats' pictures are decoded at the size of their largest drawing (files::avatar, load_pictures). The chat list handed each picture to GPUI as an encoded Image, and GPUI decodes those whole and keeps them for good. A phone's 640×640 picture took 1.6 MB of pixels, plus the same again for its GPU copy. The chat list isn't virtualized, so every chat with a picture paid this, on screen or not. Now each picture is decoded off the window's thread so its shorter side is at most 128 px: the largest one drawn (Group Info's, 64pt) at 2×, the scale the bubbles and stickers already use. That's 64 KB per picture, plus a 64 KB GPU copy. The shorter side is rounded up, so it is never a pixel short. A picture wider than 4:1 has its longer side capped at 512 px, so it draws a little soft; the circle crops it anyway. An animated picture shows its first frame, as the Mac app did, so a GIF picture no longer animates. A picture that doesn't decode shows its initials and is logged ("a chat's picture isn't shown", with the file's name, as the core logs it). image_format and its test go away, since the decoder now decides what counts as a picture.

macOS: the window lets go of its spare drawables (app::drawables). GPUI's Metal layer keeps the window-sized drawables that a run of frames took, and keeps them while the window lives, hidden too. After the "Connecting…" spinner at launch, vmmap counted five "CAMetalLayer Display Drawable" surfaces, though GPUI asks for at most three: 2000×1442 BGRA, 11 MB each, at the default window size, and more for a larger window. Changing the layer's drawable size empties that pool, and setting it straight back leaves the frame on screen as it is. Hush does this at two moments:

  • When another app comes to the front: one second later, whatever the window still draws. Behind another app it only draws a caret blinking or someone typing, and that needs only a drawable or two.
  • After each minute's redraw of the times: behind another app, one second later; in front, once the window has drawn nothing for a second. A window that keeps drawing (scrolling) keeps its drawables until the next minute.

If GPUI's view ever stops having a Metal layer, this says so once in the log.

This reaches into GPUI's layer from outside, so it carries a shortcut: comment: drop it once GPUI trims its own idle drawables. On Windows and Linux it does nothing.

Measured

All numbers come from the bare release binary (cargo build --release -p hush-desktop) with HUSH_DATA_DIR set to a throwaway folder. Memory is footprint (Activity Monitor's Memory, the same as top -stats mem and scripts/perf.sh), sampled every second or so after the window settled. Window: the default 1000×720 at 2×.

For a realistic dataset without an account, I used a scratch harness that isn't committed. It makes 60 chats, each with its own 640×640 JPEG picture (120 KB on average), and loads them the way load_pictures keeps them: before, an encoded Image; after, files::avatar. It also starts out "Connecting…" for 2 s and then goes online, as a linked launch does. "Another app in front" means the harness deactivated Hush (NSApp.deactivate).

Idle Before After
Unlinked (link screen), window in front 91–97 MB 93–96 MB, until the next minute
Unlinked, another app in front 91–95 MB 60–63 MB
60 chats with pictures, window in front 329–333 MB 105–109 MB, then 71–74 MB from the next minute
60 chats with pictures, another app in front 329–332 MB 62–65 MB

Where the 60-chat "before" went: 147 MB of malloc (the decoded pictures) and 126 MB of GPU memory (their copies). Then 56 MB of IOSurfaces (5 drawables) and about 20 MB of everything else.

Checked and left as they were

  • The call engine already starts lazily. spawn_call_router spawns its thread on the first call message, and only while linked. Incoming offers still go through ensure_engine, and the existing router tests cover them. At rest it costs nothing, so nothing changed there.
  • The core's 64 MB thread stack is virtual. All stacks together come to 0.6–0.9 MB resident.
  • SQLCipher's page caches are small: presage's 3 × 512 KB and the projection's 2 MB default, about 1 MB in use at idle (sqlite3MemMalloc). Not worth shrinking.
  • Message pictures were already capped at 32 MB, with their GPU copies dropped on eviction (The desktop app keeps at most 32 MB of a chat's pictures in memory, decoding them as their rows are drawn #194).
  • Malloc: with MallocStackLogging the floor's heap is about 14 MB allocated, about 30 MB with 60 chats, and nothing in it is outsized. The biggest items are GPUI's atlas, the element arena and the window: a few MB each.

What's left above 100 MB

Nothing at steady idle. It goes over only for a while, and only on macOS:

  • For up to a minute after an animation, with the window in front (the launch's "Connecting…", someone typing): about 105–109 MB in the 60-chat case. These are up to four spare drawables, which go at the next minute. They aren't let go mid-use: a window in front may be scrolling, and an emptied pool means new drawables then.
  • With a chat open, the window in front keeps its spare drawables until another app comes to the front. The composer's caret blinks every 500 ms, with no idle timeout in gpui-base, so the window never goes a second without a frame. Behind another app the caret keeps toggling too, by my reading of gpui-base's BlinkCursor (not measured): it hides the caret but still redraws the window twice a second. That redrawing was already there and isn't changed here. It keeps one or two drawables, not five. Letting the caret stop blinking after a while (CursorBlink::with_idle_timeout) would end both, but it changes how the caret looks, so that's a call for the owner.
  • While anything animates, and on the first frame after a quiet spell, about 98 MB more of GPU memory (footprint's "Owned physical footprint (unmapped) (graphics)"), for as long as frames are drawn. That is also the startup peak of ~195 MB. It's GPUI's renderer, it was there before, and it goes when drawing stops.
  • Pictures still scale with the number of chats. Every picture is decoded and drawn, because the list isn't virtualized: 128 KB per chat with a picture (CPU plus GPU), so 300 such chats is about 38 MB. Virtualizing the list would bound this; that's not done here.
  • The drawables scale with the window: one drawable at width × height × 4 bytes at 2×. A larger window keeps a larger one.

Tests

  • chats::window_tests::chat_pictures_are_held_no_larger_than_their_largest_drawing: a 640×640 picture read through a real core is held at 128×128, and GPUI never decoded it whole. It failed before the change ("GPUI decoded the whole picture").
  • chats::window_tests::a_chat_picture_that_doesnt_decode_stays_unread_and_one_that_didnt_read_is_asked_again: guards the rewritten failure paths of load_pictures. It passes on the old code too, since the behaviour is kept.
  • files::tests::a_chats_picture_is_decoded_to_fill_its_circle: square, wide, tall, rounded-up, small and very wide pictures, plus a still-encrypted file that isn't one.
  • drawables::window_tests (3): a window lets go a second after the frame its asker drew, not before; a window in front that keeps drawing keeps its drawables; a window behind another app lets go though it keeps drawing. Two of them failed before the review fix.
  • drawables::tests::letting_go_keeps_the_drawables_size: emptying a real CAMetalLayer's pool leaves its drawable size as it was.
  • Gate: make check passes on 3ba1450 (origin/main merged in): cargo fmt --check, both clippy -D warnings runs, and cargo test --features hush-desktop/snapshot (402 + 553 passed, 4 ignored).
  • Snapshots (--snapshot) looked at: typing-light/typing-dark. Bogdan's picture in the chat list (40pt) and the timeline's gutter (28pt) is sharp and fills its circle.

Closes #276

…indow lets go of spare drawables

Chats' pictures went to GPUI as encoded images, which it decodes whole and keeps for good: a
phone's 640x640 picture held 1.6 MB, and as much again on the GPU, for every chat in the list.
They are now decoded off the window's thread with their shorter side at most 128 px (Group Info's
64pt at 2x), 64 KB each.

On macOS GPUI's Metal layer keeps every window-sized drawable a run of frames needed (five after
the launch's Connecting spinner, 11 MB each at the default size), hidden or not. A second after the
window last drew, once another app is in use and after each minute's redraw, a drawable size set
and set back empties that pool; the frame shown stays.

Measured with 60 chats with pictures: 330 MB idle before, 62-72 MB after.
…r drew, and a picture that doesn't decode is logged

Review fixes. The quiet check raced the frame its own trigger draws (the redraw after going
behind another app, the minute's redraw): whether the drawables went came down to timer lateness.
A window behind another app now lets go a second after, whatever it still draws (a blinking
caret); one in front waits once more for the frame just asked for, then lets go if quiet. The
last frame's time is on the executor's clock, which window tests drive.

A chat picture that doesn't decode is logged by its file's name, as GPUI's loader did before. The
shorter side is rounded up, a missing Metal layer is said once, and comments say what the code does.
# Conflicts:
#	core/Cargo.lock
#	core/desktop/Cargo.toml
@pitis

pitis commented Nov 29, 2026

Copy link
Copy Markdown
Owner Author

Review (pr-review-toolkit:review-pr on git diff origin/main...HEAD)

Four reviewers ran: code, tests, silent failures and comments. No Critical findings. Everything below is fixed in 4a1e087 unless marked declined.

Important: fixed

  1. The quiet check raced its own trigger's frame (code, tests and comments reviewers). let_go_soon started its one-second timer just before the frame its trigger causes: the redraw after going behind another app, or the minute's redraw. So whether the drawables went came down to how late the timer fired. The fix:
    • A window behind another app now lets go a second later, whatever it still draws. Its caret keeps toggling, so it would never be quiet.
    • A window in front waits once more for the frame just asked for, then lets go only if it drew nothing for a second.
    • drawn_at uses the executor's clock, so window tests can drive it. Three new window tests in drawables.rs cover this; two of them failed on the old code.
    • Measured on screen afterwards: the window in front went from 105 MB to 71–74 MB at the minute, and after going behind it stayed at one drawable.
  2. A chat picture that doesn't decode was no longer logged (silent failures). GPUI's asset loader used to log it. It now logs a chat's picture isn't shown with the error and the file's name, as the core logs it.
  3. Comments that didn't match the code (comments):
    • The SAFETY reason.
    • The drawn_at and let_go_soon docs.
    • "five" against GPUI's three: it now says vmmap counted five though GPUI asks for three.
    • "keeps 4:1 of it sharp".
    • The test's "as small as they are drawn": the test is renamed, and it now says "largest drawing".
    • The Cargo.toml comment.
    • The minute-tick comment, which now names the blinking caret.
    • The non-macOS let_go is now a shortcut:.
    • avatar() now notes that a size over 64pt must raise files::AVATAR_PIXELS.
  4. load_pictures's rewritten failure paths were untested (tests). A new window test checks that a picture that doesn't decode stays none, and that one that didn't read is asked for again.

Suggestions: fixed

  1. Rounding (tests): the longer side is now rounded up, so the shorter side never falls a pixel short of 128. A 1000×700 picture now gives 183×128; a test case covers it.
  2. A Metal layer that disappears in a GPUI bump would undo this silently (silent failures, tests). It now logs a warning once.

Declined

  • Decode at 3× for displays above 2× (code). Every picture in files.rs is decoded for 2×: the bubbles' MAX_PIXELS and the stickers. Avatars follow that. Raising them all is a change of its own.
  • A device-backed test that the pool really empties (tests). It would rely on CoreAnimation's undocumented reuse order and could be flaky. The effect was measured with vmmap and footprint instead (table in the PR body).
  • A positive control for "GPUI never decoded it whole" (tests). The assertion failed on the old code, so Ana's row does draw the picture; it's kept as it is.

Noted for the owner (product call, not changed)

  • With a chat open and the window in front, the composer's caret blinks every 500 ms with no idle timeout. The window never goes a second without a frame, so it keeps its spare drawables until another app comes to the front.
  • Behind another app, gpui-base's BlinkCursor hides the caret but, by my reading of it (not measured), still redraws twice a second. That was there before this PR.
  • An idle timeout (CursorBlink::with_idle_timeout in the theme) would end both, but it changes how the caret behaves.

Gate on the merged head: make check passes on 3ba1450 (origin/main merged in): cargo fmt --check, both clippy -D warnings runs, and cargo test --features hush-desktop/snapshot (402 + 553 passed, 4 ignored).

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.

Hush stays under its 100 MB idle memory budget

1 participant