mirror of
https://github.com/block/buzz.git
synced 2026-11-27 19:52:31 +00:00
6b0dfc1957fb6cb87d40b5c3bd6570aa7e8a896e
4 Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
07dfdbc60c |
perf(mobile): keep large images in the disk cache (#9906)
The mobile image disk cache never saved a file over 5 MB. A full-resolution photo posted in a channel (12.5 MB in one case) was therefore downloaded again on every cold start, and again whenever it left the in-memory image cache. This drops the per-file cap. Each download is already capped at 20 MiB, and the cache's 250 MB byte budget evicts least recently used files, so a large photo can take at most 8% of the budget before eviction handles it. Overlapping saves of one URL now wait for each other, so the second replaces the first file instead of leaving an untracked copy behind. Making large files cacheable makes three existing edge cases bigger, so this also: - **Bounds each eviction pass.** One large image saved into a cache full of small files could evict about 2,000 of them in one UI-isolate pass (100 to 300 ms of frame gaps). Each pass now deletes at most 256 files and schedules the next until the cache is back in budget. - **Publishes saves atomically.** A save writes beside the file and renames over it, so a reader during a refresh sees the old image or the new one, never part of the new file. - **Cleans up interrupted saves.** A failed write deletes its partial file, and files the index doesn't track (a save cut short by the app exiting) are deleted in the background once the cache opens (so the first image doesn't wait for the directory listing). A file left untracked within a session waits for that next open. - **Retires a disposed cache's eviction.** Switching away from a community and back reopens its cache on the same directory. The old cache's scheduled eviction pass, which more passes make likelier, could then save its stale index over the new one and drop newer saves. Disposing now stops that cache's eviction and lets its saves in progress finish, and its index drops any later write, such as a late read marking a file used. The reopened cache opens only once the old one has closed. If that takes over 3 s, the images waiting meanwhile skip the disk, and later ones use it once the close finishes. Timeline images can't use the relay's thumbnail instead: thumbnails are at most 320 px on their longer side, well below a timeline image's width on a 2x or 3x phone. ### Validation - The [disk cache journey test](https://github.com/block/buzz/blob/mobile-large-image-cache/mobile/test/shared/relay/media_disk_cache_test.dart) now loads a 12 MiB image, then shows it from disk on an offline relaunch, and a background refresh that returns a large image replaces the saved copy instead of deleting it. - The disk cache eviction test now also saves one URL twice at once and checks that only one file stays on disk. Two decode sizes of one image can save concurrently, and before this change each save created its own file. - The same test now drains a large save into a full cache over several capped passes with no later reads, deletes untracked and partial files on relaunch, and checks that concurrent reads during repeated refreshes only ever see a whole image. It also disposes a cache without waiting, as the app does, with a save and a read in progress, reopens and saves, lets the old cache's scheduled pass run, and checks that every save survives a relaunch. - A further step holds the old cache's close past a shortened wait: the reopened cache misses at first, then reads from disk once that close finishes, and a relaunch finds both caches' saves. - Removing any one of these makes the test fail: the wait for a closing cache, the open once a slow close finishes, dispose waiting for saves, the retired index's eviction and deletion checks, the sweep's 5 s margin, and close waiting for the sweep. The closed index's write checks fail the test only as a set, since each covers for the others, and the check on single deletes has no test. The atomic rename has no reliable failing test: removing it fails the refresh step in only some runs. ### Regressions - **Disk:** the budget is unchanged at 250 MB per community and account. Large photos now count against it, so fewer small images may fit before eviction, which costs at most a re-download of the least recently used ones. - **Memory:** unchanged. The cache writes bytes the app already holds after decoding, and a read loads one file, as before. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost> |
||
|
|
6017091936 |
refactor(mobile): remove the dead per-signer cache sweep (#9902)
## Summary The media cache and saved channel-directory sweeps kept, for each saved community, only the files of its current signer, and deleted the files of any other account under the same community id. That state came only from the same-relay credential replacement removed in #9896. Community ids are random, and every remaining path that rewrites a saved community's key keeps the same key (hosted reconnect refuses a different one). So no community can change signer anymore, and the per-signer cleanup is dead. This deletes it. The sweep now keeps whole community directories by id and deletes the directories of removed communities, as before. ## Changes - `MediaDiskCache.removeOrphans` takes the retained community ids and no longer sweeps signer subdirectories. - Delete `OpenChannelDirectoryStorage.removeOrphans`. Its only job was deleting other signers' files; a removed community's directory still goes with its community directory. - Delete the tests and assertions that covered signer replacement. ### Added lines - The new, shorter `MediaDiskCache.removeOrphans` body and doc comment. - The provider's retained set: community ids that have a signer, the same communities the old map kept. - Two doc comment rewordings that no longer mention replaced accounts. - The media cache test's removal check, rewritten for the new signature. ## Validation - Full mobile test suite, analyze, format and file-size checks pass. - No user-visible change, so no screenshots. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz> |
||
|
|
ff652ed06c |
perf(mobile): cache and animate custom emoji in the iOS picker (#9479)
The iOS emoji picker's custom emoji downloaded again every time the
picker opened, and animated emoji showed only their first frame.
Now each custom emoji downloads once per install and plays its
animation:
- The native picker gets image bytes from Flutter through the same path
as the rest of the app's images: the in-memory custom emoji cache, then
the persistent media disk cache, then one download through the shared
media HTTP client. Relay media is cached as immutable.
- Swift decodes every frame (no Swift-side cache) and shows the image in
a `UIImageView`, since SwiftUI `Image` draws only the first frame of an
animated `UIImage`. With Reduce Motion on, it shows the first frame,
following the setting live.
- At most four picker downloads run at once, and an emoji scrolled off
screen before its turn never starts. Emoji already saved on disk skip
the queue, so reopening a large set stays instant.
- A picker load joins any download of the same image already in flight
elsewhere in the app. A saved copy that fails to decode is evicted and
downloaded again, and only bytes that decoded are kept in memory.
- Swift decodes at most four emoji at once, including ones from cached
bytes. An emoji whose cell scrolls away gives up its place and stops
decoding between frames, and a cell that leaves the screen keeps only
its first frame.
- Each animation frame keeps its own delay. UIKit spaces animated frames
evenly, so a frame repeats for its share of the loop, and a GIF that
holds its last frame for 3 s still does.
### Why it refetched
The native picker loaded images itself with `URLSession.shared` and an
`NSCache` keyed on the whole request. Every request carried a freshly
signed media auth header, so the key never matched and nothing was
cached on disk.
### Validation
Relay media downloads for the 48 custom emoji matching "nod" (local
relay, iPhone 17 Pro simulator):
| Step | main | this PR |
| --- | --- | --- |
| First open | 48 | 48 |
| Leave and re-enter the channel, reopen | 48 | 0 |
| Relaunch the app, reopen | 48 | 0 |
| Reinstall the app, reopen | not measured | 0 |
Before (main): every reopen downloads again, and animated emoji are
still.
https://github.com/user-attachments/assets/7a43dd6a-61e0-495d-abed-6c11f7d4f965
After (this PR): reopening shows cached emoji immediately, and animated
emoji play.
https://github.com/user-attachments/assets/bf45a5c0-77ef-4501-8843-6f42e15de389
Automated app run: signed in to a local relay, opened a channel,
long-pressed a message, opened the full picker, searched, closed, left
and re-entered the channel, reopened, relaunched and reopened. Repeated
on the final head (
|
||
|
|
52cb6818fa |
perf(mobile): keep remote images on disk and show small ones as thumbnails (#9158)
## Summary The mobile app now saves remote images on disk. After a relaunch, avatars, custom emoji, message attachments and agent artwork show immediately. The app does not download them again. Small images on a relay now load only the relay's thumbnail. The app never downloads the full-size upload to show a small image. ## Why Before this change, the app kept remote images only in memory. Each cold launch downloaded every image again. On a slow connection, attachments showed a "Loading image" placeholder for a long time. Also, when a relay had no thumbnail for an image, the app downloaded the full-size upload. That upload can be up to 20 MiB, for an avatar that is a few dozen points wide. ## What changes ### Images on disk - `MediaImageProvider` reads and writes a `MediaDiskCache`. Each account in each community has its own cache, at `<root>/communities/<community id>/media/<signer pubkey>`. The cache is built on `flutter_cache_manager`, with a JSON index. - On iOS, the root is the app group's `Library/Caches` (from a new `buzz/app_group` method channel). App extensions can then read the same cache. On other platforms, the root is the app cache directory. - Each cache holds a maximum of 250 MB. When it is full, the least recently used images go first. `MediaCacheIndex` measures each file when it writes it, because `flutter_cache_manager` limits only the number of files. Every disk read marks the file used, and every write schedules eviction, including a refresh that replaces bytes the package already holds in memory (by default the package does neither for those). - The cache does not save an image that is larger than 5 MiB. The app still shows that image from memory. Thus, one large image cannot push out all the others. - Relay media URLs contain a content hash, so their bytes never change. The cache keeps them until it evicts them. For other URLs, the cache shows the saved image, and then gets a new copy in the background after one week. A maximum of two of these refreshes run at the same time. The app decodes a refreshed image before it replaces the saved copy. - When you remove a community, the app deletes its cache. When a community gets a new signing key, the app deletes the media of the old account. At launch, the app also deletes caches of communities and accounts that are no longer present. - Startup does not wait for the cache. The cache directory resolves in the background. Image reads wait for it for a maximum of 3 seconds. ### Thumbnails for small images - A small relay image loads the relay's WebP thumbnail. If the relay has no WebP thumbnail, the image loads the JPEG thumbnail. If the relay has neither, the widget shows its placeholder (initials for an avatar, an empty tile for artwork). - Native avatars drawn by UIKit (the navigation bar, the community icon on the create button, and the New DM and Add Members pickers) use the same choice, through a new `smallMediaImageProviders`. They try the same thumbnails, fall back the same way, and show initials when neither thumbnail exists. Before, they downloaded the full-size upload and only decoded it small. - Larger layouts and the full-screen viewer load the full-size upload, as before. ### Agent artwork - Preset agent artwork (the tray, flight, video placeholder, avatar hero and the avatar editor's blurred backdrop) now loads through `MediaImage`, so it uses the disk cache too. - The create-agent and empty-bestie pages warm the same `MediaImageProvider` that their widgets read, through a new `mediaImageProviderFor(ref, url)`. - No code in `mobile/lib` uses `Image.network` or `NetworkImage` now. ## Demo A cold relaunch (the app is stopped, then started) into a synthetic "QA demo" Solo chat that holds a few generated images. Shown at half speed. Before: the attachment shows a "Loading image" placeholder while it downloads. https://github.com/user-attachments/assets/107f43a3-63d0-40c3-bd80-4ea9d7897a97 After: the attachment shows with the chat, from disk. https://github.com/user-attachments/assets/d3985bb2-838c-4efa-93cb-44cef51f677d ## Tests - `test/shared/relay/media_disk_cache_test.dart` follows one journey through the real provider and a real cache in a temporary directory. The first launch downloads and saves. An offline relaunch shows the full-size image, its thumbnail and an external image from disk, and the oversized image goes back to the network. Eight days later, only the external image refreshes. Removing the community deletes its directory. - A second test checks the order of eviction when the cache is full, that a file read from disk is evicted after files that were not, and that replacing a file's bytes after eviction last ran evicts again when the cache goes over budget. - `community_provider_test` checks that launch deletes orphan media directories, that a new signing key deletes only the old account's media, and that removing a community deletes its media. - `avatar_image_test` checks that a missing WebP thumbnail falls back to the JPEG thumbnail, and that an avatar with no thumbnail shows its initials. - `native_avatar_image_test` checks the same for native avatars: a WebP 404 paints the JPEG thumbnail, the full-size upload is never requested, a relay with no thumbnail paints initials and is not asked again, and any other failure stops at the initials. - `flutter analyze` and the full mobile test suite pass. - Automated app runs on an iPhone 17 Pro simulator (iOS 27): the channel list, a DM, Home, the Agents grid and an agent details sheet show their images. After a relaunch, the images came from disk and the app wrote no new files. In the "QA demo" chat, a new image from the app showed immediately. - Automated run of the native-thumbnail fix on an iPhone 17 Pro simulator (iOS 27): the native New DM picker shows photo avatars, and the cache gained only relay `.thumb.webp` entries for them, no full-size uploads. After a cold relaunch, the picker showed the same avatars within about 1.5 seconds, and the cache directory gained no files. The avatar editor backdrop was not exercised in the app. The test community has no remote agents, and the backdrop tests cover only emoji and picked-image selections. ## Before merge - [x] Automated simulator run of this head: avatars and agent artwork show their thumbnails, and a cold relaunch shows images from disk. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Tom Brow <tomb@squareup.com> Co-authored-by: Tom Brow <tomb@squareup.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost> |