Posted by Andrei Shikov, Senior Software Engineer, Android Toolkit and Jonathan Starup, Software Engineer, R8 Teamkotlinx.coroutines, making launching and cancelling coroutines up to 2x faster. In order to get the benefits, update your AGP to 9.2.0 or above.With the majority of Android apps adopting Kotlin as their main language of choice,
LaunchedEffect method trace visualized in the Perfetto UI
The method trace above can be separated into three parts:
Initializing a new coroutine
Starting coroutine
Completing coroutine (because it exits immediately)
Cancelling LaunchedEffect is similar to normal completion, except it also creates a CancellationException.
From the profile above, one thing that is immediately suspicious is frequent calls into java.util.concurrent.AtomicReferenceFieldUpdater (purple or green boxes with j… labels). While each call is relatively fast, the frequency is concerning; any non-negligible overhead that is spread out across multiple invocations might add up to a noticeable regression. Zooming in on a call reveals that most of the time is spent on... reflection checks?
Investigating AtomicReferenceFieldUpdater
But let's not get ahead of ourselves. AtomicReferenceFieldUpdater is actually well-optimized on JVM
Aside from that, the ART team is implementing these optimizations natively at the VM level. If your app is targeting API 37 and is running on a recent version of Android, it is possible that your device is already optimizing coroutines in a similar way. The coroutine benchmarks above observed ~15% improvement in performance after JIT updates in the recent versions of ART.
Your app will receive this optimization by default when upgrading to AGP 9.2.0 or by using R8 9.2.0 directly. For more information, see D8 dexer and R8 shrinker.
SOCIAL SHARE CARD GENERATOR