mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
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>