Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory cache miss when Transformation reduces input bitmap's size. #357

Merged
merged 11 commits into from
Apr 9, 2020

Conversation

colinrtwhite
Copy link
Member

@colinrtwhite colinrtwhite commented Apr 9, 2020

This fixes a bug where a request could miss the memory cache if it uses a transformation that decreases the output bitmap's height (e.g. CircleCropTransformation). It's reproducible on master if you add a CircleCropTransformation in ImageListAdapter.

This requires a broader refactor to convert the memory cache's key from String to a custom object so we can hold onto the request's resolved size. Overall, I've been planning to move away from using a String cache key for a while as it has gotchas (like Parameters having to always order its entries the same way irrespective of insertion order).

Added regression tests to guard against this going forward.


/** Remove all values from this cache. */
fun clearMemory()

/** @see ComponentCallbacks2.onTrimMemory */
fun trimMemory(level: Int)

/** Cache key for [MemoryCache] and [WeakMemoryCache]. */
class Key {
Copy link
Member Author

@colinrtwhite colinrtwhite Apr 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No data class, unfortunately, since we need to be able to control when size is null using the constructors.

@colinrtwhite colinrtwhite assigned Jawnnypoo and unassigned Jawnnypoo Apr 9, 2020
@colinrtwhite colinrtwhite requested a review from Jawnnypoo April 9, 2020 10:03
@@ -62,14 +85,14 @@ class Parameters private constructor(

class Builder {

private val map: SortedMap<String, Entry>
private val map: MutableMap<String, Entry>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this sorted before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jawnnypoo The iteration order had to be consistent irrespective of insertion order so the cache key would match up. If we didn't sort by keys, the parameters would produce two difference cache keys. E.g.

val parameters = Parameters.Builder()
    .set("key1", "first_value")
    .set("key2", "second_value")
    .build()
// would produce the string cache key "key1=first_value#key2=second_value"

val parameters = Parameters.Builder()
    .set("key2", "second_value")
    .set("key1", "first_value")
    .build()
// would produce the string cache key "key2=second_value#key1=first_value"

One of the gotchas of using a string cache key. Now order doesn't matter since Parameters are compared using Map.equals!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhhh gotcha. Thanks for clarifying!

Copy link
Member

@Jawnnypoo Jawnnypoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Glad to see this is fixed!

@colinrtwhite colinrtwhite merged commit b58385d into master Apr 9, 2020
@colinrtwhite colinrtwhite deleted the colin/memory_cache_key_object branch April 9, 2020 20:05
colinrtwhite added a commit that referenced this pull request Oct 5, 2022
#357)

* Fix memory cache miss when Transformation reduces input bitmap's size.

* Update API.

* Re-add toString().

* Add regression test.

* Update API.

* cache -> cached.

* Remove signature.

* Re-arrange params.

* isCachedDrawableValid -> isCachedValueValid.

* Update equals.

* Fix style.
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.

2 participants