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

Change rememberRetain not to retain the value of removed node #1794

Open
wants to merge 28 commits into
base: main
Choose a base branch
from

Conversation

vulpeszerda
Copy link
Contributor

@vulpeszerda vulpeszerda commented Nov 13, 2024

As mentioned in #1783, I propose modifying the behavior of rememberRetained to improve consistency.

Changes to rememberRetained Behavior

The following explanation is based on the code sample below.

@Composable
private fun ConditionalRetainContent(registry: RetainedStateRegistry) {
  CompositionLocalProvider(LocalRetainedStateRegistry provides registry) {
    var showContent by remember { mutableStateOf(false) }
    Column {
      Button(modifier = Modifier.testTag(TAG_BUTTON_HIDE), onClick = { showContent = false }) {
        Text(text = "Hide content")
      }
      Button(modifier = Modifier.testTag(TAG_BUTTON_SHOW), onClick = { showContent = true }) {
        Text(text = "Show content")
      }
      if (showContent) {
        var count by rememberRetained { mutableIntStateOf(0) }
        Button(modifier = Modifier.testTag(TAG_BUTTON_INC), onClick = { count += 1 }) {
          Text(text = "Increment")
        }
        Text(modifier = Modifier.testTag(TAG_RETAINED_1), text = count.toString())
      }
    }
  }
}

Current Behavior

Before saveAll is called on the registry

  • rememberRetained added when the condition is true will be removed when the condition changes to false.
  • When the condition changes back to true and the same rememberRetained is called, the previous value is not retained.

Test code for this behavior

After saveAll is called on the registry

  • rememberRetained added when the condition is true will be removed when the condition changes to false.
  • Then, saveAll is called on the registry.
  • When the condition changes back to true and the same rememberRetained is called, the previous value is retained.

Test code for this behavior

Challenges with the Current Behavior

  • The timing of the saveAll call on the RetainedStateRegistry is unknown from the perspective of lower-level content, making retention depend on whether saveAll was called.
  • In the case of remember / rememberSaveable, the node doesn’t retain the previous value when it’s removed and added back, making it easy to expect rememberRetained to behave similarly.

Due to these challenges, I propose modifying rememberRetained to behave like remember / rememberSaveable, so that rememberRetained does not retain values when it is hidden and reshown in the compose node based on a condition.

Internal Implementation Changes

Overall, I drew inspiration from the implementation of SaveableStateHolder and rememberSaveable for these modifications.

The following changes were made to achieve this goal:

1. Modify RetainableSaveableHolder to always unregister values from the RetainedStateRegistry when onForgotten is called.

In the previous implementation, if rememberRetained was removed from the node and onForgotten was called, values were not unregistered from the registry if canRetain was true. As a result, all values were retained in the registry regardless of whether rememberRetained was present in the node when saveAll was called.

Now, values are unregistered from the registry when onForgotten is called, so only values present in the composition node are retained when saveAll is called .

2. Change the timing of saveAll in RetainedStateRegistry

Previously, saveAll was called on the RetainedStateRegistry within RetainableSaveableHolder when onForgotten was invoked. However, due to the change in 1, onForgotten of all child rememberRetained nodes is called before onForgotten of rememberRetained { RetainedStateRegistry() }, which would result in no values being retained.

Therefore, I referenced the SaveableStateHolder implementation to create a separate DisposableEffect to run saveAll, as shown below:

val parentRegistry = LocalRetainedStateRegistry.current
val registry = rememberRetained(key) { RetainedStateRegistry() }

CompositionLocalProvider(LocalRetainedStateRegistry provides registry) {
    // child content
    ... 
}

DisposableEffect(key, registry) {
    onDispose { 
        registry.saveAll()
        parentRegistry.saveValue(key)
    }
}

By structuring it this way, saveAll can be called on the RetainedStateRegistry before the child content's dispose stage. Thus, even if onForgotten is called on the child content's rememberRetained and unregisters the value, it will still be retained.

Compared to the existing implementation, this change needs to be applied to all cases where the RetainedStateRegistry is redefined in a nested way. To facilitate this process, I made the RetainedStateProvider RetainedStateHolder function public and modified NavigableCircuitContent and PausableState to use it. (24d8547 ff654b4 )

3. Change the timing of saveAll in AndroidContinuity

For the same reason as in 2, continuityRetainedStateRegistry also needs to call saveAll using DisposableEffect after declaring the child content.

Example

val registry = continuityRetainedStateRegistry()

CompositionLocalProvider(LocalRetainedStateRegistry provides registry) {
    // child content
    ... 
}

DisposableEffect(registry) {
    onDispose { 
        registry.saveAll()
    }
}

However, in Android, continuityRegistry should always call saveAll when onStop is called. Since calling saveAll multiple times won’t cause issues, I modified it to call saveAll conveniently upon onStop or disposal.

@Composable
public fun continuityRetainedStateRegistry(
  key: String = Continuity.KEY,
  factory: ViewModelProvider.Factory = ContinuityViewModel.Factory,
  canRetainChecker: CanRetainChecker = LocalCanRetainChecker.current ?: rememberCanRetainChecker(),
): RetainedStateRegistry {
  @Suppress("ComposeViewModelInjection")
  val vm = viewModel<ContinuityViewModel>(key = key, factory = factory)

  LifecycleStartEffect(vm) {
    onStopOrDispose {
      if (canRetainChecker.canRetain(vm)) {
        vm.saveAll()
      }
    }
  }

  LaunchedEffect(vm) {
    withFrameNanos {}
    // This resumes after the just-composed frame completes drawing. Any unclaimed values at this
    // point can be assumed to be no longer used
    vm.forgetUnclaimedValues()
  }

  return vm
}

Changes to Test Code

Change 1

In RetainedTest.kt, parts where nestedRegistry is declared now use RetainedStateProvider RetainedStateHolder, as saveAll must be manually called.
affec25

Change 2

In NestedRetainWithPushAndPop and NestedRetainWithPushAndPopAndCannotRetain, the tests assume the same value is retained regardless of the showNestedContent value, so I set the same key in RetainedStateProvider to retain the values.

However, since these assumptions may change with this PR, it may need verification to ensure correctness.
affec25

Change 3

To test ImpressionEffect, I modified the function attempting to recreate it. Previously, the condition was first set to false to remove the child content and then saveAll was performed. In the modified behavior, saveAll is performed first and then the child content is removed.

465f98f

Additional Test Cases for the Reported Issue

I added test cases to cover the issue reported, which show which tests failed with the previous implementation and how they succeed with the modified implementation.

019c8cc, 5b279d4


When I initially reported this issue, I may have been somewhat aggressive due to the unexpected behavior. I apologize if it came across that way. Through working on this modification, I had the chance to explore the internal structure of rememberRetained and have come to appreciate how robust and well-designed many parts of the circuit library are. I have great respect for the efforts of the main contributors.

After considering various approaches, I believe this change to the behavior of rememberRetained is the right direction. However, since the implementation of rememberRetained is a core part of circuit, there may be differing perspectives on this.

While having this PR approved would be ideal, if there are differing views, I hope we can use this as a foundation for further discussion.

Copy link

Thanks for the contribution! Unfortunately we can't verify the commit author(s): roy.tk <r***@k***.com>. One possible solution is to add that email to your GitHub account. Alternatively you can change your commits to another email and force push the change. After getting your commits associated with your GitHub account, sign the Salesforce Inc. Contributor License Agreement and this Pull Request will be revalidated.

@vulpeszerda vulpeszerda force-pushed the remember-retained-redesigned branch from 50dc04e to affec25 Compare November 13, 2024 05:22
@vulpeszerda

This comment was marked as outdated.

@vulpeszerda

This comment was marked as outdated.

@ZacSweers
Copy link
Collaborator

Please be patient and don't tag maintainers, we will get to it when we get to it!

Copy link
Contributor

@alexvanyo alexvanyo left a comment

Choose a reason for hiding this comment

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

I agree with the goal of having rememberRetained match in behavior with rememberSaveable and remember: if one of those leaves composition in an if, the state is wiped, and I would expect the same for rememberRetained.

One thing not clear right now to me still after running some tests: if there is the if (...) { rememberRetained() } inside the RetainedStateProvider, I think by the same logic, state should also be lost.

However, in my tests, the state is still preserved in that case.

Maybe the LocalCanRetainChecker inside RetainedStateProvider shouldn't be set to the static CanRetainChecker.Always?

@@ -40,7 +39,7 @@ internal class ContinuityViewModel : ViewModel(), RetainedStateRegistry {
}

override fun onCleared() {
delegate.retained.clear()
delegate.forgetUnclaimedValues()
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the difference in behavior with this change?

Copy link
Contributor Author

@vulpeszerda vulpeszerda Nov 22, 2024

Choose a reason for hiding this comment

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

I intended to notify onForgotten for all retained values that implment RememberObserver.

@vulpeszerda
Copy link
Contributor Author

vulpeszerda commented Nov 22, 2024

@alexvanyo
Thank you for the review.

I agree with this

One thing not clear right now to me still after running some tests: if there is the if (...) { rememberRetained() } inside the RetainedStateProvider, I think by the same logic, state should also be lost.

However, I couldn't reproduce the test case you mentioned. (I tested with this)
Could you please share the exact test case?

One thing not clear right now to me still after running some tests: if there is the if (...) { rememberRetained() } inside the RetainedStateProvider, I think by the same logic, state should also be lost.

However, in my tests, the state is still preserved in that case.

Copy link
Contributor

@alexvanyo alexvanyo left a comment

Choose a reason for hiding this comment

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

The RetainedStateHolder API looks very similar to #1168 as its now mimicking the SaveableStateHolder API exactly, so curious to hear from Zac as to how that exploration went previously.

@ZacSweers
Copy link
Collaborator

The RetainedStateHolder API looks very similar to #1168 as its now mimicking the SaveableStateHolder API exactly, so curious to hear from Zac as to how that exploration went previously.

Still want it! It essentially stalled because I hadn't picked it back up again. If that's something that naturally can fall out of this work then that'd be dope 👌

@vulpeszerda
Copy link
Contributor Author

Is there any additional work I need to do to get my changes reviewed?

@ZacSweers
Copy link
Collaborator

Sorry I think we were waiting for a resolution on my last comment?

Still want it! It essentially stalled because I hadn't picked it back up again. If that's something that naturally can fall out of this work then that'd be dope 👌

Is that something that falls out of this PR? Should it?

@vulpeszerda
Copy link
Contributor Author

Ah, I thought contributors were busy with reviews since it’s the end of the year.

I’ll analyze issue #1168 in more detail and review whether replacing it with the RetainedStateHolder I added in my work would resolve it. Also, since we’re on the topic, it would be great to include the test cases added in #1168 here as well.

I’ll work on that.

@ZacSweers
Copy link
Collaborator

Sounds good!

override fun RetainedStateProvider(key: String, content: @Composable (() -> Unit)) {
CompositionLocalProvider(LocalRetainedStateRegistry provides this) {
val canRetainChecker = LocalCanRetainChecker.current ?: CanRetainChecker.Always
val childRegistry = rememberRetained(key = key) { RetainedStateRegistry() }
Copy link
Contributor

Choose a reason for hiding this comment

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

This is minor, but I think specifying the key here as the key to rememberRetained has the downside of polluting the key space with a risk of conflicts: if you had a RetainedStateProvider in the RetainedStateRegistry scope with overlapping key sets, then this rememberRetained could conflict.

I think a safer way to do this is to add the composite key hash, either explicitly, or do what SaveableStateHolder does and doi it implicitly by adding a ReusableContent(key) around this whole content.

Copy link
Contributor Author

@vulpeszerda vulpeszerda Jan 11, 2025

Choose a reason for hiding this comment

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

@alexvanyo yes right.
I confused that rememberRetained would add composite key hash as prefix even though the key provided.
Adding composite key hash as prefix would be easy fix. But adding ReusableContent(key) would be more proper fix for considering other content (non circuit related) reuse.
I haven’t had much free time, so I haven’t been able to work on what Zac mentioned earlier yet. I’ll try to incorporate this as well tomorrow and next week.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexvanyo,

After much consideration and testing, I’ve decided to keep the current implementation that passes rememberRetained(key = key) here. There are several reasons for this:

1. RetainedStateHolder usage

RetainedStateHolder constructs its own RetainedStateRegistry and uses it only once to store a child registry, which makes conflicts highly unlikely. The only potential conflict scenario would be if holder.RetainedStateProvider is used in multiple places, as shown below.

val holder = rememberRetainedStateHolder()
holder.RetainedStateProvider("key1") { ... }
holder.RetainedStateProvider("key1") { ... }

2. Handling removeState

When removeState is called and the holder.RetainedStateProvider for the corresponding key is already disposed (meaning that child registry remains retained in the parent registry), the parent registry needs to remove that retained child registry. However, if we rely on a composite key hash instead of the direct key, we cannot determine which composite key hash corresponds to the original key.

override fun removeState(key: String) {
  val canRetainChecker = canRetainCheckers[key]
  if (canRetainChecker != null) {
    canRetainChecker.shouldSave = false
  } else {
    registry.consumeValue(key) // <--- this case
  }
}

3. SaveableStateHolder consistency

Internally, SaveableStateHolder also uses the key argument to distinguish between different data sets.


For these reasons, I will continue passing rememberRetained(key = key) as is.
However, since I appreciate the usability of ReusableContent in SaveableStateHolder, I’ve updated that part accordingly in 16cf579.

@vulpeszerda
Copy link
Contributor Author

I’ve cherry-picked the RetainedStateHolderTest from #1168 and integrated it into this branch. The tests pass with the RetainedStateHolder implementation in this PR, and everything appears to be working fine.

To use the existing circuit’s RetainedStateRegistry without significantly modifying its implementation, I had to implement RetainedStateHolder differently than the one in previous PR.

From my understanding, the issue addressed in #1168 is resolved by using this RetainedStateHolder, so I went ahead and applied those changes in commit 9714ce3.

It looks like all the issues have been resolved. If there’s anything else you’d like me to work on, please let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants