https://kotlinlang.org logo
#compose
Title
# compose
j

Ji Sungbin

05/23/2022, 4:49 PM
Every Layout composable emits a ReusableComposeNode. But what does Reusable mean here? Below is the code of ReusableComposeNode.
Copy code
@Composable inline fun <T : Any, reified E : Applier<*>> ReusableComposeNode(
    noinline factory: () -> T,
    update: @DisallowComposableCalls Updater<T>.() -> Unit
) {
    if (currentComposer.applier !is E) invalidApplier()
    currentComposer.startReusableNode()
    if (currentComposer.inserting) {
        currentComposer.createNode { factory() }
    } else {
        currentComposer.useNode()
    }
    currentComposer.disableReusing()
    Updater<T>(currentComposer).update()
    currentComposer.enableReusing()
    currentComposer.endNode()
}
I noticed with this code that the only difference between ReusableComposeNode and ComposeNode is the way the groups are created. The KDoc of startReusableNode(), which is the group creation method used in ReusableComposeNode, is written as follows.
Start a group that tracks a the code that will create or update a node
that is generated as part of the tree implied by the composition. A
reusable node can be reused in a reusable group even if the group key
is changed.
I don’t know what reused means here. Does this mean that the node is reused as measured? (normal nodes will start with the measure phase. but does the reused node skip the measured phase?)
a

Alexandre Elias [G]

05/23/2022, 11:39 PM
We introduced it for use in LazyLists. Lazy node creation when possible is a recomposition of a previously scrolled-off node of the LazyList. Like any recomposition, it will skip the measure phase if all the inputs of measure are provably identical, otherwise it will remeasure.
Using the git blame layer on cs.android.com, I tracked down the patches where it was originally introduced: [1] and [2]
j

Ji Sungbin

05/24/2022, 2:16 AM
really thanks. l’ll check it.
https://kotlinlang.slack.com/archives/CJLTWPH7S/p1653163311872229 Can you help me with this too? I really haven’t figured this out yet. It is difficult for me to understand the many internal code by myself.
a

Andrey Kulikov

05/24/2022, 12:23 PM

https://youtu.be/1ANt65eoNhQ?t=1227

this can explain the reusing logic
j

Ji Sungbin

05/24/2022, 12:32 PM
I’ve already seen that video, but I’ll watch it again! Thank you so much.
91 Views