Dive Deep into the Sustainability of Jetpack Compose
Jetpack Compose has Smart-Recomposition, a system that helps in reengineering efficiently. It is basically designed on the basis of position memorization, and the stability of composable also has a big impact. In this article, we will learn about the stability of Jetpack Compose.
First, what is “consistency” in Compose? Steady state obeys three conditions.
- When a value has changed, it should be notified to the composable. (inducing recombination)
- If two instances are in the same position, then the equals of both instances should always return the same result.
- All public sectors should also be stable.
If these three conditions are met, Compose considers the field to be in a steady state, and discards composable if the field’s value has not changed. This is called the skippable state. Skippable bypasses recompilation unless there is a value change notification, thanks to the condition that composable will be notified when that field’s value changes.
Basically, primitive types and functional types are considered static because they cannot be changed by definition.
The annotation that creates this steady state is
@StableMarker, and Write uses two types of steady state. (both are using annotations
@Immutable Annotation, indicates that all public fields are immutable since they are created, and can be applied to a class. Since the value does not change after creation, the first rule of consistency is “when a value has changed, it must be reported to the composable.” is ignored. It is a stronger promise than Kotlin
val keywords. In the case of
valThe instance is the same but the value inside can change.
@Immutable does not allow it. it can be useful for
data class where all the fields are
val And there are no extra getters.
@Stable, represents a variable state whose value can change, with slightly different roles depending on what is applied to it. If applied to either type, the StableMarker role is taken without any additional roles. If applied to a function or property, it will have a role in addition to StableMarker. For the same input, it always produces the same output (a pure function), and in the case of a function, it promises that all arguments are also steady state. If the input is the same, then the rearrangement will be discarded as the output will also be the same.
A fixture is a warning to the irreversible state of the system. Is
texts logic in
TextWithImmutableList immutable in the code below?
Definition itself is immutable because List is not of type
MutableList spread out
ListA mutable list can be passed as an immutable type.
texts The arguments to the above function are not assumed to be in a steady state and cannot be omitted. For making
texts constant, we have to tell the compiler that
List is irreversible.
The first way is to make a wrapper for
List using the
@Immutable permanent marker.
ImmutableListWrapper itself is immutable, so the compiler assumes
texts the argument to be stable, and
TextWithImmutableList Composable becomes skippable.
Another way is Kotlins. to use
ImmutableCollections, It is a Kotlin add-on library that is not built into Kotlin. It was created and maintained by JetBrains and is currently in alpha, but Compose uses it in its stability system.
texts The argument is obtained using
ImmutableCollectionsIt is considered irreversible, and
TextWithImmutableList Composables become skippable.
Also, all stability is promoted. In the code below, since the return type
SungbinLand is in a steady state,
provideSungbinLand The function is assumed to be in a steady state even if there is no staticmarker in the function.
Let’s take StableMarker from here
SungbinLandImpl in the above code.
due to this reason,
provideSungbinLand The function is considered volatile because the StableMarker annotation has been removed from the return type
provideSungbinLand Celebration. look at the
SungbinLandDisplay composable, type
data logic is an unstable position, but
SungbinLandImpl is used to pass a value to the class
provideSungbinLand Used as the default value of a function
data The argument is in a steady state. So, this composable becomes skippable. Although
data The argument is still in a shaky state.
@StableMarker If used incorrectly, the compose result may differ from what the compiler expects, and a runtime error may occur. Also, when used properly, it can be quite difficult to always maintain it according to the rules. Therefore, there is a system in Compose that can infer that it is a stable type in addition to the types that were originally recognized as stable.
Compose’s stability estimation system only applies to orbitals, and
@StabilityInferred comments on All Classes used in Write at compile time.
For example, in the above code,
Name The class is considered static because all the fields are immutable and of static type.
What about normal classes?
NameWithGeneric because the class does not know the type
T is not refined. In this case, Compose calculates the compiler’s bitmask
T during compilation and then passes the corresponding value as an argument to
StabilityInferred, Thus, stability can be inferred from the value of that argument.
However, since only
value The field is mutable, there is no guarantee that the rule “when a value has changed, it must be notified to the composable” is followed, so
NameWithGeneric class is considered volatile, even though
T The type is stable.
So far, we have looked at Compose’s stability system. Thanks for reading.
[View in Korean]
#Optimize #reconfiguration #Jetpack #Compose #Stability #System #Jisungbin #August