One Off the Slack: Why So Slow?

Aleks E asked:

Does anyone else notice that Compose takes some time to “warm up” when used in Android Views? In our project we’re relying on Android interop to integrate Compose into existing fragments, and first screen open produces a lot of skipped frames/lag, but consequent launches are a lot more smooth

As it turns out, this is not strictly a Compose issue. But Compose is big, which means that on first install, Compose is slow, as Google’s Adam Powell points out:

Yes, we’re addressing this on multiple fronts as we speak. The root issue is that after initial installation, on modern versions of ART the app runs in interpreted mode briefly before the JIT compiler kicks in. After a few days of collecting profiling data, the device will create a fully ahead of time compiled version of the app code that it will use from there on. What you’re seeing is that brief period of time where compose is running in interpreted mode.

Adam suggests R8 for improving matters:

…if you aren’t already running r8 on your release builds, it performs a lot of whole-program optimizations that help apps run faster during these interpreted periods. Definitely test against r8-optimized release builds too.

The Compose team is also working on the problem:

On the compose side we’re looking at a few things: we’re doing internal optimizations in compose-runtime and compose-ui to make the code friendlier to the interpreter, we’re investigating additional code generation optimizations in the compose compiler plugin, and additional optimizations that r8 can perform on the kinds of code that Compose uses and incentivizes writing

So, if your Compose app seems slow when you first launch it from the IDE… give it some time. 😁

Read the original thread in the kotlinlang Slack workspace. Not a member? Join that Slack workspace here!