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
This commit is contained in:
Sergey Igushkin
2021-12-23 19:57:55 +04:00
committed by Space
parent 723ef8f1fb
commit cb9dfc8e01
@@ -217,9 +217,17 @@ internal fun <T> 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() }
}
}