From b1f28aa75e45f3cef75aac0f1eb00b9de59526b6 Mon Sep 17 00:00:00 2001 From: Uwais Ashraf Date: Fri, 3 Jan 2025 20:11:06 +0000 Subject: [PATCH] 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 --- .../android/quickstep/task/thumbnail/TaskThumbnailView.kt | 4 +++- .../com/android/quickstep/task/util/TaskOverlayHelper.kt | 4 +++- .../com/android/quickstep/views/RecentsViewModelHelper.kt | 6 ++++-- .../android/launcher3/util/coroutines/DispatcherProvider.kt | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/quickstep/src/com/android/quickstep/task/thumbnail/TaskThumbnailView.kt b/quickstep/src/com/android/quickstep/task/thumbnail/TaskThumbnailView.kt index a952617f00..b040723a3f 100644 --- a/quickstep/src/com/android/quickstep/task/thumbnail/TaskThumbnailView.kt +++ b/quickstep/src/com/android/quickstep/task/thumbnail/TaskThumbnailView.kt @@ -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) diff --git a/quickstep/src/com/android/quickstep/task/util/TaskOverlayHelper.kt b/quickstep/src/com/android/quickstep/task/util/TaskOverlayHelper.kt index 0f61b95942..677875cef9 100644 --- a/quickstep/src/com/android/quickstep/task/util/TaskOverlayHelper.kt +++ b/quickstep/src/com/android/quickstep/task/util/TaskOverlayHelper.kt @@ -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, diff --git a/quickstep/src/com/android/quickstep/views/RecentsViewModelHelper.kt b/quickstep/src/com/android/quickstep/views/RecentsViewModelHelper.kt index 87771c6c37..d92c4d06fd 100644 --- a/quickstep/src/com/android/quickstep/views/RecentsViewModelHelper.kt +++ b/quickstep/src/com/android/quickstep/views/RecentsViewModelHelper.kt @@ -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) + } } } } diff --git a/src/com/android/launcher3/util/coroutines/DispatcherProvider.kt b/src/com/android/launcher3/util/coroutines/DispatcherProvider.kt index 887753556c..1f01b07922 100644 --- a/src/com/android/launcher3/util/coroutines/DispatcherProvider.kt +++ b/src/com/android/launcher3/util/coroutines/DispatcherProvider.kt @@ -33,7 +33,7 @@ object ProductionDispatchers : DispatcherProvider { override val default: CoroutineDispatcher = Dispatchers.Default override val background: CoroutineDispatcher = bgDispatcher - override val main: CoroutineDispatcher = Dispatchers.Main + override val main: CoroutineDispatcher = Dispatchers.Main.immediate override val unconfined: CoroutineDispatcher = Dispatchers.Unconfined }