Use Dispatchers.Main.immediate to run code quickly that runs on main.

immediate dispatcher schedules code on main (post{...}) if the current coroutine context is not main. However, if the coroutine context is main it will run synchronously without scheduling the work.

This removes all the continuations that come from initialisation of flow collectors.

Bug: 387511656
Flag: com.android.launcher3.enable_refactor_task_thumbnail
Test: Checking perfetto traces shows that this reduces the number of continuations on main by ~120 for 30 apps
Change-Id: I6c5ffba893e470c25d81f044f83cf43cb80588b8
This commit is contained in:
Uwais Ashraf
2025-01-06 16:06:26 +00:00
parent bdc352f645
commit b1f28aa75e
4 changed files with 11 additions and 5 deletions
@@ -89,7 +89,9 @@ class TaskThumbnailView : FrameLayout, ViewPool.Reusable {
override fun onAttachedToWindow() {
super.onAttachedToWindow()
viewAttachedScope =
CoroutineScope(SupervisorJob() + Dispatchers.Main + CoroutineName("TaskThumbnailView"))
CoroutineScope(
SupervisorJob() + Dispatchers.Main.immediate + CoroutineName("TaskThumbnailView")
)
viewData = RecentsDependencies.get(this)
updateViewDataValues()
viewModel = RecentsDependencies.get(this)
@@ -67,7 +67,9 @@ class TaskOverlayHelper(val task: Task, val overlay: TaskOverlayFactory.TaskOver
fun init() {
overlayInitializedScope =
CoroutineScope(SupervisorJob() + Dispatchers.Main + CoroutineName("TaskOverlayHelper"))
CoroutineScope(
SupervisorJob() + Dispatchers.Main.immediate + CoroutineName("TaskOverlayHelper")
)
viewModel =
TaskOverlayViewModel(
task = task,
@@ -44,10 +44,12 @@ class RecentsViewModelHelper(
// Update recentsViewModel and apply the thumbnailOverride ASAP, before waiting inside
// viewAttachedScope.
recentsViewModel.setRunningTaskShowScreenshot(true)
recentsCoroutineScope.launch(dispatcherProvider.main) {
recentsCoroutineScope.launch(dispatcherProvider.background) {
recentsViewModel.waitForRunningTaskShowScreenshotToUpdate()
recentsViewModel.waitForThumbnailsToUpdate(updatedThumbnails)
withContext(Dispatchers.Main) { ViewUtils.postFrameDrawn(taskView, onFinishRunnable) }
withContext(Dispatchers.Main.immediate) {
ViewUtils.postFrameDrawn(taskView, onFinishRunnable)
}
}
}
}