Skip to content

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

Merged
colinrtwhite merged 11 commits into
masterfrom
colin/memory_cache_key_object
Apr 9, 2020
Merged

Fix memory cache miss when Transformation reduces input bitmap's size.#357
colinrtwhite merged 11 commits into
masterfrom
colin/memory_cache_key_object

Conversation

@colinrtwhite
Copy link
Copy Markdown
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.

fun trimMemory(level: Int)

/** Cache key for [MemoryCache] and [WeakMemoryCache]. */
class Key {
Copy link
Copy Markdown
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
class Builder {

private val map: SortedMap<String, Entry>
private val map: MutableMap<String, Entry>
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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