This document describes the effort to migrate simple, synchronous mapLatest usages in SystemUI Kotlin Flow pipelines to a new, lighter-weight alternative called mapDirect.
This migration aims to reduce coroutine cancellation overhead, simplify pipeline execution, and prevent subtle race conditions caused by unnecessary cancellations.
mapLatestIn SystemUI, mapLatest is frequently used to transform state in Flow pipelines. While mapLatest is useful when the transformation is asynchronous (e.g., database access or network requests), it is often used for simple, synchronous transformations (e.g., mapping a data class to a UI model, or extracting a field).
Using mapLatest for synchronous operations introduces several drawbacks:
mapLatest cancels the active coroutine block of the previous emission. If emissions are rapid, this causes constant coroutine cancellation and rescheduling.mapLatest can leave the system in an inconsistent state.mapDirectTo address this, we introduced mapDirect in com.android.systemui.util.kotlin.Flow.kt:
/** * Aids the transition from `mapLatest` to `map` when the lambda is a non-suspend method. */ public inline fun <T, R> Flow<T>.mapDirect(noinline transform: (value: T) -> R): Flow<R> { return this.conflate().map(transform) }
mapLatest, mapDirect uses conflate() to ensure that if the downstream collector is slow, older intermediate values are dropped.mapLatest, mapDirect uses standard map, meaning the transformation runs synchronously and is never cancelled mid-execution. This eliminates coroutine switching and cancellation overhead.mapDirectFlagGuardedTo ensure a safe migration, we also introduced a flag-guarded helper:
/** * Standard mapLatest but migrated to mapDirect when the flag is enabled. * Aids the transition from `mapLatest` to `mapDirect` with a feature flag. */ @OptIn(ExperimentalCoroutinesApi::class) public inline fun <T, R> Flow<T>.mapDirectFlagGuarded( noinline transform: (value: T) -> R ): Flow<R> { return if (Flags.mapDirectMigration()) { this.mapDirect(transform) } else { this.mapLatest(transform) } }
This allows us to migrate pipelines in place and verify performance/correctness before fully flipping the switch.
We have targeted key high-frequency pipelines for this migration:
MobileConnectionsRepositoryImpl.kt: Migrating mobile state mapping pipelines.ConfigurationInteractor.kt: Migrating global configuration and resource mapping pipelines.DemoMobileConnectionsRepository.kt: Migrating demo-mode mobile state pipelines.ClockInteractor.kt: Migrating clock-related flow pipelines.ClockRepositorySwitcher.kt: Migrating active clock tracking pipeline.DisplayTypeRepository.kt: Migrating display type mapping pipeline.KeyguardRepository.kt: Migrating keyguard state pipelines.ScreenAwareAmbientLightModeMonitorRepository.kt: Migrating ambient light mode pipelines.When writing Flow pipelines in SystemUI:
map: If the transformation is synchronous and you do not need conflation (you want to process every single emission).mapDirect: If the transformation is synchronous but you do need conflation (you only care about the latest state).mapLatest: ONLY if the transformation block contains suspend functions (e.g., calling an async repository or delaying) and you explicitly want to cancel the operation if a newer value arrives.To scale this migration and prevent new inefficient usages from being introduced, we plan to implement a custom Android Lint rule.
The linter will analyze Kotlin files in SystemUI and perform the following checks:
mapLatest Calls: Find all occurrences of .mapLatest { ... }.delay, calling a suspend fun)..map { ... } if the pipeline does not require conflation..mapDirect { ... } (or .mapDirectFlagGuarded during the migration phase) if conflation is intended.SystemUI-lint module.mapLatest calls from being merged.