Skip to content

Commit

Permalink
Fix memory leak in Update effects (#325)
Browse files Browse the repository at this point in the history
If there are animations on the screen, after some time we will encounter performance degradation, and an increased memory consumption.

This fix is similar to the other fix in the past (https://android-review.googlesource.com/c/platform/frameworks/support/+/1398690). We have to remember the lambda instead of creating it each time.

Fixes JetBrains/compose-multiplatform#2455
Fixes JetBrains/compose-multiplatform#1969

The tests are not very deterministic, but it seems better to have them than not to have. The second test fails before the fix, and passes after the fix.
We will see if they will be stable on our CI or not. If they will be flaky, we will tune them, or remove them.
  • Loading branch information
igordmn committed Dec 5, 2022
1 parent 447a33d commit ade9c44
Show file tree
Hide file tree
Showing 4 changed files with 723 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ internal fun UpdateEffect(update: () -> Unit) {
}
snapshotObserver.start()

lateinit var sendUpdate: (Unit) -> Unit
fun performUpdate() {
snapshotObserver.observeReads(
Unit,
onValueChangedForScope = { tasks.trySend(::performUpdate) }
onValueChangedForScope = sendUpdate,
) {
currentUpdate()
}
}
sendUpdate = { tasks.trySend(::performUpdate) }

performUpdate()

Expand Down
Loading

0 comments on commit ade9c44

Please sign in to comment.