Coder Social home page Coder Social logo

Comments (3)

m-sasha avatar m-sasha commented on May 30, 2024

This is a problem with real runs, not just tests. The measuring and layout gets into an infinite remeasure/relayout loop on every frame.

Reproduces in 1.6.x, but not in 1.5.12

from compose-multiplatform.

m-sasha avatar m-sasha commented on May 30, 2024

Reproducer:

fun main() = singleWindowApplication {
    val state = remember { mutableStateOf(1) }
    Layout(
        measurePolicy = { measurables, constraints ->
            val measurable = measurables.first()
            val placeable = measurable.measure(constraints)
            val prevValue = Snapshot.withoutReadObservation {
                state.value
            }
            state.value = prevValue+1
            layout(100, 100) {
                state.value  // Read the state value!
                placeable.placeRelative(0, 0)
            }
        },
        content = {
            Text("Hello")
        }
    )
}

from compose-multiplatform.

m-sasha avatar m-sasha commented on May 30, 2024

What happens here is:

  1. The state is written in the measure phase, and GlobalSnapshotManager receives a write notification and schedules calling Snapshot.sendApplyNotifications(), but it's not yet called because the main thread is busy.
  2. The layout phase happens, which reads the state, marking the scope as having read it.
  3. When the rendering of the current frame (BaseComposeScene.render() ) completes, the GSM calls Snapshot.sendApplyNotifications(), the scope reports that it has read that state, so the scope is marked as invalid.
  4. The next time BaseComposeScene.render() is called, the whole thing repeats from 1.

This appears to be a bug, because the state is read after it has been written to, so it should not mark the scope as invalid.

There are several differences in Android that "cover" for this, preventing the infinite relayout:

  • Between the layout and drawing phases, Android's Choreographer flushes the tasks scheduled by the main dispatcher, which includes the GSM. This means that the scope is marked as invalid before the drawing phase.
  • Android's drawing phase (AndroidComposeView.dispatchDraw) actually calls measureAndLayout again before drawing. This causes the layout scope to be executed again, and its invalidation state is cleared.

Additionally, in CMP, we don't mark nodes only for layout. Our Owner.onRequestRelayout implementation actually marks the node as needing to be measured. So even if we add Snapshot.sendApplyNotifications() and doLayout() between layout and draw, doLayout() will end up executing both measure and layout on the node, bringing us back to square one.

from compose-multiplatform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.