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

The code implementation has serious performance issues #124

Open
proxiti opened this issue Nov 6, 2024 · 2 comments
Open

The code implementation has serious performance issues #124

proxiti opened this issue Nov 6, 2024 · 2 comments

Comments

@proxiti
Copy link

proxiti commented Nov 6, 2024

Every rememberXXXSensorState has performance issue, here is demo:

@Composable
fun Demo() {
  val state = rememberGameRotationVectorSensorState()

  SideEffect {
    Log.e(TAG, "recompose") // infinite recomposition
  }
}

Any state will trigger infinite recomposition, which is not correct. I found the issue comes frome here:

val gameRotationVectorSensorState = remember {
        mutableStateOf(
            GameRotationVectorSensorState(...)
        )
    }

LaunchedEffect(key1 = sensorState, block = {
        gameRotationVectorSensorState.value = GameRotationVectorSensorState(...)
    }
})

return gameRotationVectorSensorState.value

It's incorrect to reassign the state value before return statement, every reassignment will trigger a recomposition even no component reference this state value.

@proxiti
Copy link
Author

proxiti commented Nov 6, 2024

Especially for high-frequency collected data, I cannot skip recomposition to optimize performance, such as using lambda expressions to pass data, which causes the phone to overheat severely.

@proxiti
Copy link
Author

proxiti commented Nov 6, 2024

A good way is to put the logic (before the return statement) in the state holder, and also put the sensor data in the state holder. Then exposed rememberXXX like this:

fun rememberGameRotationVectorSensorState(...) = remember {
  GameRotationVectorSensorState(...)
}

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

No branches or pull requests

1 participant