Add WeakMemoryCache implementation.#295
Merged
Merged
Conversation
33066f5 to
2f7ee78
Compare
colinrtwhite
commented
Mar 1, 2020
| * Completely clear this image loader's memory cache and bitmap pool. | ||
| */ | ||
| @MainThread | ||
| fun clearMemory() |
Member
Author
There was a problem hiding this comment.
These functions have always called through to data structures that aren't thread safe, however now I'm making this assumption explicit. We could support calling these from any thread, however it would slightly slow down every image request due to synchronization so let's optimize for the common case.
You can technically still call these methods off the main thread, however it's not 100% safe due to race conditions.
colinrtwhite
commented
Mar 1, 2020
| * | ||
| * Default: true | ||
| */ | ||
| fun trackWeakReferences(enable: Boolean) = apply { |
Member
Author
There was a problem hiding this comment.
Definitely open to better name suggestions for this.
colinrtwhite
commented
Mar 1, 2020
| private var size: Size? = null | ||
|
|
||
| @MainThread | ||
| suspend inline fun size(cached: BitmapDrawable? = null): Size = scope.run { |
Member
Author
There was a problem hiding this comment.
Pretty sure this inline isn't actually being inlined because var size is private.
23f09b2 to
24e5be4
Compare
colinrtwhite
added a commit
that referenced
this pull request
Oct 5, 2022
* Add WeakMemoryCache implementation. * Extensions. * Implicit return types. * Rename method. * Test. * Undo. * First pass implementation. * Optimize + document. * Add a basic test. * Add more tests. * Move test functions out of main binary. * Support enabling/disabling the weak memory cache. * Rename values. * Fix ktlint. * Specify return type. * Refactor. * Test multiple API levels. * Fix tests. * Fix tests. * Really fix tests. * Add MemoryCache tests. * Fix naming clash. * Fix tests. * Edit tests. * Not null. * More coverage. * Remove unused function. * Formatting. * Documentation. * Documentation.
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.
This adds a
WeakMemoryCacheimplementation, which holds weak references to bitmaps.The plan is
MemoryCacheholds strong references and when they're evicted, pass the bitmap to theWeakMemoryCachewhere it will hold a weak reference until either the bitmap is no longer referenced or it is added to theBitmapPool.The use case for this is now you can rely on an
ImageLoaderhaving a value in its memory cache if the image still has a strong reference outside of it (i.e. anImageView). I'd argue this is much more intuitive - basically if it's on screen, it's cached. This should make shared element transitions easier to implement and will increase the overall cache hit rate of the memory cache (at the slight cost of extra overhead to manage the weak references).The behaviour can be enabled/disabled with
ImageLoaderBuilder.trackWeakReferences. Planning to enable this by default since it should be a fairly safe optimization.