18.4 C
Tuesday, July 9, 2024

New methods of optimizing stability in Jetpack Compose

The brand new sturdy skipping mode for controlling class stability in Jetpack Compose modifications the way to optimize recompositions in your app. On this weblog put up, we’ll cowl what instances it solves for you and what must be manually managed. We’ll additionally cowl frequent questions you’ve had, equivalent to whether or not remembering lambda capabilities continues to be wanted, if kotlinx immutable collections are wanted, and even the way to stabilize all of your area mannequin courses. When you’re unsure what stability is, see our documentation to study the ideas.

There are a number of the reason why the Compose compiler would possibly deal with a category as unstable:

  • It’s a mutable class. For instance, it comprises a mutable property (not backed by snapshot state).
  • It’s a category outlined in a Gradle module that doesn’t use Compose (doesn’t have a dependency on the Compose compiler).
  • It’s a category that comprises an unstable property (instability nesting).

Let’s contemplate the next class:

information class Subscription(          // class is unstable
val id: Int, // steady
val planName: String, // steady
val renewalOn: LocalDate // unstable

The id and identify properties are steady, as a result of they’re of a primitive sort, which is immutable. Nevertheless, the renewalOn property is unstable, as a result of java.time.LocalDate comes from the Java normal library, which doesn’t have a dependency on the Compose compiler. Due to that, the entire Subscription class is unstable.

Contemplate the next instance with a state property utilizing the Subscription class, which is handed to a SubscriptionComposable:

// create in a state holder (for instance, ViewModel)
var state by mutableStateOf(Subscription(
id = 1,
planName = "30 days",
renewalOn = LocalDate.now().plusDays(30)

enjoyable SubscriptionComposable(enter: Subscription) {
// at all times recomposed regardless if enter modified or not

Traditionally, a composable with the enter parameter of this unstable class wouldn’t be decided as skippable, and it will at all times be recomposed regardless if the inputs modified or not.

Jetpack Compose compiler 1.5.4 and better comes with an choice to allow sturdy skipping mode, which at all times generates the skipping logic whatever the stability of the enter parameters. This mode permits composables with unstable courses to be skipped. You possibly can learn extra about sturdy skipping mode and the way to allow it in our documentation or within the weblog put up by Ben Trengrove.

Sturdy skipping mode has two methods of figuring out if the enter parameter modified from the earlier composition:

  • If the category is steady, it makes use of the structural equality (.equals()).
  • If the category is unstable, it makes use of the referential equality (===).

After you allow sturdy skipping mode in your challenge, composables that use the unstable Subscription class received’t recompose if the occasion is identical as within the earlier composition.

So let’s say you’ve the SubscriptionComposable utilized in a unique composable Display screen that takes a parameter inputText. If that inputText parameter modifications and the subscription parameter doesn’t, the SubscriptionComposable doesn’t recompose and is skipped:

enjoyable Display screen(inputText: String, subscription: Subscription) {
Textual content(inputText)

// It is skipped when subscription parameter did not change

However let’s say you’ve a perform renewSubscription that updates the state variable with the present day to maintain monitor of newest day when a change occurred:

enjoyable renewSubscription() {
state = state.copy(renewalOn = LocalDate.now().plusDays(30))

The copy perform creates a new occasion of the category with the identical structural properties (if it happens throughout the identical day), which implies that the SubscriptionComposable would recompose once more, as a result of sturdy skipping mode compares unstable courses with referential equality (===) and duplicate is creating a brand new occasion of our subscription. Although the date is identical, as a result of referential equality is getting used, the Subscription composable continues to be recomposed.

If you wish to forestall the SubscriptionComposable from recomposing when the structural information doesn’t change (equals() returns the identical consequence), it’s worthwhile to manually mark the Subscription class as steady.

On this case, it’s a easy repair by annotating the category with @Immutable, as a result of the category represented right here can’t be mutated:

-data class Subscription( // unstable
+information class Subscription( // steady
val id: Int, // steady
val planName: String, // steady
val renewalOn: LocalDate // unstable

On this instance, when the renewSubscription is named, the SubscriptionComposable might be skipped once more, as a result of now it makes use of the equals() perform as a substitute of ===, which can return true in comparison with the earlier state.

When can this happen?

A practical instance of if you’ll nonetheless must annotate your courses as @Immutable is if you use entities coming from the peripherals of your system, equivalent to database entities, API entities, Firestore modifications, or others.

As a result of these entities are parsed each time from the underlying information, they create new situations each time. Due to this fact, with out the annotation, they’d recompose.

Notice: Recomposing will be sooner than calling equals() on each parameter. It is best to at all times measure the impact of your modifications when optimizing stability.

For courses that aren’t a part of your codebase, our steerage was once that the one solution to stabilize them is wrapping the category with a category that’s a part of your codebase and annotate that class as @Immutable as a substitute.

Contemplate an instance, the place you’d have a composable that instantly accepts the java.time.LocalDate parameter:

enjoyable LatestChangeOn(up to date: LocalDate) {
// current the day parameter on display

When you name the renewSubscription perform to replace the newest change, you’ll find yourself in an identical scenario as earlier than — the LatestChangeOn composable retains recomposing, regardless if it’s the identical day or not. Nevertheless, there’s no risk of annotating that class on this scenario, as a result of it’s a part of the usual library.

To repair this, you’ll be able to allow a stability configuration file, which may include courses or patterns of courses that might be thought of steady by the Compose compiler.

To allow it, add stabilityConfigurationFile to the composeCompiler configuration:

composeCompiler {

// Set path of the config file
stabilityConfigurationFile = rootProject.file("stability_config.conf")

And create the stability_config.conf file within the root folder of your challenge, by which you add the LocalDate class:

// add the immutable courses outdoors of your codebase

// alternatively you'll be able to stabilize all java.time courses with *

Stabilize your area mannequin courses

Along with courses that aren’t a part of your codebase, the soundness configuration file will be useful for stabilizing all of your information or area mannequin courses (assuming they’re immutable). This fashion, the area module is usually a Java Gradle module and doesn’t want dependency on the Compose compiler.

// stabilize all courses in mannequin package deal

Remember that annotating a mutable class with the @Immutable annotation, or including the category to the soundness configuration file, is usually a supply of bugs in your codebase, as a result of the Compose compiler is not able to verifying the contract and it’d present up as one thing is not recomposing if you assume it ought to.

One different advantage of sturdy skipping is that it “remembers” all lambdas utilized in composition, even those with unstable captures. Beforehand, lambdas that had been utilizing an unstable class, for instance a ViewModel, would possibly’ve been the reason for recomposition. One of many frequent workarounds was remembering the lambda capabilities.

So, you probably have lambdas wrapped with keep in mind in your codebase, you’ll be able to safely take away the keep in mind name, as a result of it’s carried out mechanically by the Compose compiler:

Display screen(
-removeItem = keep in mind(viewModel){ { id -> viewModel.removeItem(id) } }
+removeItem = { id -> viewModel.removeItem(id) }

The kotlinx.collections.immutable collections like ImmutableList may’ve been used prior to now to make a Listing of things steady and thus stopping a composable from recomposing. You probably have them in your codebase purely for the aim of stopping recompositions of composables with Listing parameters, you may contemplate refactoring them to a daily Listing and add java.util.Listing into the soundness configuration file.


When you try this, your composable is perhaps slower than if the Listing parameter was unstable!

Including Listing to the soundness configuration file means the Listing parameter is in contrast with the equals name, which ultimately results in calling equals on each single merchandise of that record. Within the context of a lazy record, the identical equals verify is then referred to as once more from the angle of the merchandise composable, which ends up in calculating the equals() name twice for most of the seen gadgets, and presumably needlessly for all of the gadgets that aren’t seen!

If the composable containing the Listing parameter doesn’t have many different UI parts, recomposing it may be sooner than calculating the equals() verify.

Nevertheless, there’s nobody measurement matches all method right here, so it is best to confirm your selection with benchmarks!

By enabling sturdy skipping mode in your code base, you’ll be able to cut back the necessity to manually craft courses to be steady. Remember that in some instances, they nonetheless want guide crafting, however this will now be simplified with the soundness configuration file!

We hope all of those modifications will simplify the psychological load of excited about stability in Compose.

Need extra? See our codelab on sensible efficiency drawback fixing in Compose.

The code snippets on this weblog have the next license:
// Copyright 2024 Google LLC. SPDX-License-Identifier: Apache-2.0
Latest news
Related news


Please enter your comment!
Please enter your name here