-
Notifications
You must be signed in to change notification settings - Fork 61
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
REPLAY-1943: Refactor caches from singletons to class instances #1564
REPLAY-1943: Refactor caches from singletons to class instances #1564
Conversation
f58d3ee
to
d6a4547
Compare
...oid-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/SessionReplayPrivacy.kt
Outdated
Show resolved
Hide resolved
...src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/base64/Base64LRUCache.kt
Outdated
Show resolved
Hide resolved
...lay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/base64/BitmapPool.kt
Outdated
Show resolved
Hide resolved
...lay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/base64/BitmapPool.kt
Outdated
Show resolved
Hide resolved
...src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/base64/Base64LRUCache.kt
Show resolved
Hide resolved
...lay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/base64/BitmapPool.kt
Outdated
Show resolved
Hide resolved
c0a9969
to
227afe9
Compare
93329d5
to
29f2c89
Compare
...c/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/base64/Base64Serializer.kt
Outdated
Show resolved
Hide resolved
@@ -49,17 +49,30 @@ internal class BitmapPoolTest { | |||
|
|||
@BeforeEach | |||
fun setup(forge: Forge) { | |||
internalCache = LruCache(MAX_CACHE_MEMORY_SIZE_BYTES) | |||
testedCache.setBackingCache(internalCache) | |||
internalCache = object : LruCache<String, Bitmap>(MAX_CACHE_MEMORY_SIZE_BYTES) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the purpose of providing an implementation here and not a Mock ? I see that you are not doing anything in the entryRemoved
method ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a slightly more accurate representation of the lrucache we're using. The default implementation calculates size by number of objects, and here we're calculating size by bitmap allocation the same as in the class. Regarding mocking, we discussed before to use the actual lrucache and not a mock in order to make use of the cache in the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but if you provide this custom implementation in your tests this means that you are altering the class functionality ? This would be acceptable if you want to mock other methods in order to isolate one test case but if you do this for the whole test - suite how is this going to be relevant anymore ? You say here: It's a slightly more accurate representation of the lrucache we're using
. Would this mean that you are changing your internal logic ?
Codecov Report
@@ Coverage Diff @@
## feature/base64-jmoskovich #1564 +/- ##
=============================================================
- Coverage 83.32% 83.19% -0.14%
=============================================================
Files 443 444 +1
Lines 15070 15095 +25
Branches 2272 2279 +7
=============================================================
Hits 12557 12557
- Misses 1923 1941 +18
- Partials 590 597 +7
|
227afe9
to
8002635
Compare
29f2c89
to
97f7fd3
Compare
val bitmapPool = BitmapPool() | ||
val base64LRUCache = Base64LRUCache() | ||
|
||
val builder = Base64Serializer.Builder() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Builder methods should be chainable, it's the pattern we've been using in the SDK.
E.g. Configuration.kt allows a use case such as this SampleApplication.kt .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm modifying the builder in order to pass the dependencies in the builder constructor, so the setters won't exist anymore
) | ||
|
||
private companion object { | ||
private const val BITMAP_OPERATION_FAILED = "operation failed for bitmap pool" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a highly generic and non actionable message, could we improve it to give more context of what failed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the stacktrace to the log to give additional context
|
||
internal companion object { | ||
@Suppress("MagicNumber") | ||
internal val MAX_CACHE_MEMORY_SIZE_BYTES = 4 * 1024 * 1024 // 4MB |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curiosity: Did we do any study into the optimal cache size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is more of a starting point to improve upon later. All images are downscaled to have a maximum size of around 15kb, so we can calculate for 4mb a capacity of around 270 cached images - which is more than enough even for an app with only images in a grid. Working out the optimal cache size would be quite complicated, given the different use cases of customers - apps with lots of images vs apps with few.
We could think about implementing a cache that autoadjusts its size based upon recorded performance from previous runs (record the maximum percentage utilization and adjust the size of the cache accordingly) but this will add some complexity.
97f7fd3
to
1d39248
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nicely done !!
c7b5c2f
to
0f66105
Compare
0f66105
to
be9a3a8
Compare
be9a3a8
to
5be1cc0
Compare
What does this PR do?
Refactor the base64 drawable cache and the bitmap pool to be class instances instead of singletons.
Motivation
Make things more straightforward to test and remove mutable variables
Additional Notes
Anything else we should know when reviewing?
Review checklist (to be filled by reviewers)