From cb9dfc8e01756fd7f73f6d1c2ad2a906c25a1610 Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Thu, 23 Dec 2021 19:57:55 +0400 Subject: [PATCH] KT-50509: Optimize the performance of `whenEvaluated { ... }` wrapper The Gradle API `pluginManager.withPlugin(id) { ... }` turned out to have a significant performance footprint due to operations on underlying immutable collections. We can replace calls to `withPlugin` with checks of `hasPlugin` running on each applied plugin. Issue #KT-50509 Verification pending --- .../gradle/plugin/KotlinMultiplatformPlugin.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt index 776f4e37e2f..1cafc264230 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt @@ -217,9 +217,17 @@ internal fun Project.whenEvaluated(fn: Project.() -> T) { /* Make sure that all afterEvaluate blocks from the AndroidPlugin get scheduled first */ val isDispatched = AtomicBoolean(false) - androidPluginIds.forEach { androidPluginId -> - pluginManager.withPlugin(androidPluginId) { - if (!isDispatched.getAndSet(true)) { + + fun isAndroidPluginApplied(): Boolean = + androidPluginIds.any { pluginManager.hasPlugin(it) } + + if (isAndroidPluginApplied()) { + if (!isDispatched.getAndSet(true)) { + afterEvaluate { fn() } + } + } else { + plugins.all { + if (!isDispatched.get() && isAndroidPluginApplied() && !isDispatched.getAndSet(true)) { afterEvaluate { fn() } } }