Cache AsyncImage results#65
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes the correctness gap flagged when
AsyncImagelanded: every re-keyedAsyncImagedropped its bitmap and re-fetched over the network — the spinner flashed on every URL change, scroll-back, or re-navigation.Fix
A process-wide LRU cache of decoded bitmaps, keyed by URL (
ImageCache, access-orderedLinkedHashMapcapped at 64 entries,synchronizedMapsince fetches run onDispatchers.IO).RenderAsyncImagenow seeds its state from the cache synchronously — a URL seen before renders on the first frame with no spinner — and theLaunchedEffectearly-returns on a hit, so only a genuine miss touches the network. Successful fetches populate the cache; failures aren't cached (so a transient failure can retry).Lives in
commonMainalongsideImageFetch(both JVM targets), so Android and the desktop rig share it.Verification
Kotlin-only change, so no
swift testcoverage — verified on-device by counting real network fetches. Temporarily logged each call tofetchImageBytes(which only runs on a cache miss), then toggled the demo's "Load the other image" button A→B→A→B→A→B (6 times):The instrumentation was removed before committing (the diff is just the cache and the two-line
RenderAsyncImagechange).Scope
In-memory only — no disk cache, so it doesn't survive a process restart (fine for a session; a real app would add a disk tier). Cache is unbounded in byte size, bounded only by entry count (64).