Brifing

R8 Makes Kotlin Coroutines 2x Faster on Android

Asynchronous operations now significantly faster on Android

Android Developers Blog2 min

1R8's New Optimization

Starting with AGP 9.2.0, R8 optimization converts Atomic*FieldUpdater calls to Unsafe variants, making operations 2-4x faster. This particularly benefits kotlinx.coroutines that use kotlinx.atomicfu library. With most Android apps adopting Kotlin as primary language, this optimization will have widespread impact across the ecosystem.

2Coroutines in Android Ecosystem

Kotlin coroutines became standard for asynchronous programming in Android. Modern frameworks like Jetpack Compose use coroutines to manage pointer events, animations and interactions. However performance analysis revealed coroutines create bottlenecks in many non-composition operations. For example, 80% of Modifier.clickable time was spent launching/canceling coroutines.

3Performance Analysis Methods

The easiest way to analyze function behavior on Android is capturing ART method traces. This tool records app execution flow showing exactly which methods are called, their order and time spent. Examining trace of empty LaunchedEffect call clearly shows three phases: initializing new coroutine, starting it, and completing it (or canceling with CancellationException).

4AtomicReferenceFieldUpdater Issue

Coroutines use lock-free tree structure enabling structured concurrency. The kotlinx.atomicfu library implements atomic operations using AtomicReferenceFieldUpdater which performs runtime reflective safety checks. Since every coroutine operation calls at least one atomic operation, this slowness significantly impacts overall performance.

5Benchmark Results

Tests on Pixel 5 showed kotlinx.atomicfu is 2.7x slower than java.util.concurrent.atomic. atomicReference.compareAndSet took 50.7 ns while atomicRef.compareAndSet required 135 ns. This proved ART's reflection checks create substantial runtime overhead that isn't optimized away by JIT or AOT compilation.

6R8's Optimization Approach

While Atomic*FieldUpdater classes support dynamic reflection-based use, they're often used in static patterns. R8 as full-program optimizing compiler can statically analyze these usages and replace them with internal Unsafe variants during compilation. This optimization is particularly effective in Android build toolchain.

7Impact of Optimization

R8 optimization makes coroutine launch/cancel operations 2x faster. This will bring noticeable performance improvements especially for apps with heavy user interactions. Jetpack Compose apps will benefit from smoother animations and touch event handling without any code changes required from developers.

8Steps for Developers

To benefit from this optimization, developers simply need to update to AGP 9.2.0 or higher. No code changes are necessary as R8 automatically optimizes Atomic*FieldUpdater calls. Apps will perform more operations with less CPU usage, improving both performance and battery efficiency.

9Ecosystem-wide Implications

This optimization marks important step in Android's Kotlin-first future. Improved coroutines performance will enable more efficient complex apps. It will accelerate Jetpack Compose adoption, continuing to revolutionize Android UI development processes with better tooling and runtime performance.

Original article: Android Developers Blog — R8 ile Kotlin Coroutines Performansı 2 Kat Arttı