Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f78b9b6c authored by Omar Miatello's avatar Omar Miatello
Browse files

MM: remove derived state for currentSpringState.

The performance of the unstable tests are up to 45% less.

Test: MotionValueBenchmark (run on AS, results go/mm-microbenchmarks)
Test: atest MotionValueTest
Bug: 404975090
Flag: com.android.systemui.scene_container
Change-Id: Iaad7d6137b40ba4972353a198fe9c1c2a505b5ca
parent 2127943f
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -312,7 +312,22 @@ private class ObservableComputations(
    override val currentSegment by derivedStateOf { computeCurrentSegment() }
    override val currentGuaranteeState by derivedStateOf { computeCurrentGuaranteeState() }
    override val currentAnimation by derivedStateOf { computeCurrentAnimation() }
    override val currentSpringState by derivedStateOf { computeCurrentSpringState() }

    private var memoizedSpringState: SpringState = SpringState.AtRest
    private var memoizedAnimation: DiscontinuityAnimation? = null
    private var memoizedTimeNanos: Long = Long.MIN_VALUE
    override val currentSpringState: SpringState
        get() {
            val animation = currentAnimation
            val timeNanos = currentAnimationTimeNanos
            return if (memoizedAnimation == animation && memoizedTimeNanos == timeNanos) {
                memoizedSpringState
            } else {
                memoizedAnimation = animation
                memoizedTimeNanos = timeNanos
                computeCurrentSpringState(animation, timeNanos).also { memoizedSpringState = it }
            }
        }

    suspend fun keepRunning(continueRunning: () -> Boolean) {
        check(!isActive) { "MotionValue($label) is already running" }
+3 −3
Original line number Diff line number Diff line
@@ -403,11 +403,11 @@ internal interface ComputeAnimation : ComputeGuaranteeState {
internal interface ComputeSpringState : ComputeAnimation {
    val currentAnimation: DiscontinuityAnimation

    fun computeCurrentSpringState(): SpringState {
        with(currentAnimation) {
    fun computeCurrentSpringState(animation: DiscontinuityAnimation, timeNanos: Long): SpringState {
        with(animation) {
            if (isAtRest) return SpringState.AtRest

            val nanosSinceAnimationStart = currentAnimationTimeNanos - springStartTimeNanos
            val nanosSinceAnimationStart = timeNanos - springStartTimeNanos
            val updatedSpringState =
                springStartState.calculateUpdatedState(nanosSinceAnimationStart, springParameters)

+4 −2
Original line number Diff line number Diff line
@@ -209,7 +209,8 @@ private class ImperativeComputations(
    override var currentSegment: SegmentData = computeCurrentSegment()
    override var currentGuaranteeState: GuaranteeState = computeCurrentGuaranteeState()
    override var currentAnimation: DiscontinuityAnimation = computeCurrentAnimation()
    override var currentSpringState: SpringState = computeCurrentSpringState()
    override var currentSpringState: SpringState =
        computeCurrentSpringState(currentAnimation, currentAnimationTimeNanos)

    // ---- Lifecycle ------------------------------------------------------------------------------

@@ -281,7 +282,8 @@ private class ImperativeComputations(
            currentSegment = computeCurrentSegment()
            currentGuaranteeState = computeCurrentGuaranteeState()
            currentAnimation = computeCurrentAnimation()
            currentSpringState = computeCurrentSpringState()
            currentSpringState =
                computeCurrentSpringState(currentAnimation, currentAnimationTimeNanos)
        }

        debugInspector?.run {