From 099f6c67ccfb271ce143b49c2e4e310c496c1410 Mon Sep 17 00:00:00 2001 From: vmishenev Date: Wed, 20 Sep 2023 22:46:36 +0300 Subject: [PATCH] Make using of the compiler single-thread #3151 --- .../kotlin/generation/SingleModuleGeneration.kt | 15 ++++++++++++--- ...DefaultDescriptorToDocumentableTranslator.kt | 17 ++++++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/plugins/base/src/main/kotlin/generation/SingleModuleGeneration.kt b/plugins/base/src/main/kotlin/generation/SingleModuleGeneration.kt index 602173f42f..0ae6c2659f 100644 --- a/plugins/base/src/main/kotlin/generation/SingleModuleGeneration.kt +++ b/plugins/base/src/main/kotlin/generation/SingleModuleGeneration.kt @@ -4,7 +4,9 @@ package org.jetbrains.dokka.base.generation +import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.newSingleThreadContext import kotlinx.coroutines.runBlocking import org.jetbrains.dokka.CoreExtensions import org.jetbrains.dokka.DokkaConfiguration @@ -61,11 +63,18 @@ public class SingleModuleGeneration(private val context: DokkaContext) : Generat override val generationName: String = "documentation for ${context.configuration.moduleName}" - public fun createDocumentationModels(): List = runBlocking(Dispatchers.Default) { - context.configuration.sourceSets.parallelMap { sourceSet -> translateSources(sourceSet, context) }.flatten() - .also { modules -> if (modules.isEmpty()) exitGenerationGracefully("Nothing to document") } + /** + * Implementation note: it runs in a separated single thread due to existing support of coroutines (see #2936) + */ + @OptIn(DelicateCoroutinesApi::class) + public fun createDocumentationModels(): List = newSingleThreadContext("Generating documentable model").use { coroutineContext -> // see https://github.com/Kotlin/dokka/issues/3151 + runBlocking(coroutineContext) { + context.configuration.sourceSets.parallelMap { sourceSet -> translateSources(sourceSet, context) }.flatten() + .also { modules -> if (modules.isEmpty()) exitGenerationGracefully("Nothing to document") } + } } + public fun transformDocumentationModelBeforeMerge(modulesFromPlatforms: List): List { return context.plugin() .query { preMergeDocumentableTransformer } diff --git a/subprojects/analysis-kotlin-descriptors/compiler/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/descriptors/compiler/translator/DefaultDescriptorToDocumentableTranslator.kt b/subprojects/analysis-kotlin-descriptors/compiler/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/descriptors/compiler/translator/DefaultDescriptorToDocumentableTranslator.kt index 02e9545bce..f20514cf0c 100644 --- a/subprojects/analysis-kotlin-descriptors/compiler/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/descriptors/compiler/translator/DefaultDescriptorToDocumentableTranslator.kt +++ b/subprojects/analysis-kotlin-descriptors/compiler/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/descriptors/compiler/translator/DefaultDescriptorToDocumentableTranslator.kt @@ -7,10 +7,7 @@ package org.jetbrains.dokka.analysis.kotlin.descriptors.compiler.translator import com.intellij.psi.PsiElement import com.intellij.psi.PsiNamedElement import com.intellij.psi.util.PsiLiteralUtil.* -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.async -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.* import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet import org.jetbrains.dokka.analysis.java.JavaAnalysisPlugin import org.jetbrains.dokka.analysis.java.parsers.JavadocParser @@ -124,6 +121,10 @@ internal class DefaultDescriptorToDocumentableTranslator( } } + /** + * Implementation note: it runs in a separated single thread due to existing support of coroutines (see #2936) + */ + @OptIn(DelicateCoroutinesApi::class) override fun translateClassDescriptor(descriptor: ClassDescriptor, sourceSet: DokkaSourceSet): DClasslike { val driInfo = DRI.from(descriptor.parents.first()).withEmptyInfo() @@ -132,9 +133,11 @@ internal class DefaultDescriptorToDocumentableTranslator( docCommentFinder = context.plugin().docCommentFinder ) - return runBlocking(Dispatchers.Default) { - DokkaDescriptorVisitor(sourceSet, kdocFinder, kotlinAnalysis[sourceSet], context.logger, javadocParser) - .visitClassDescriptor(descriptor, driInfo) + return newSingleThreadContext("Generating documentable model of classlike").use { coroutineContext -> // see https://github.com/Kotlin/dokka/issues/3151 + runBlocking(coroutineContext) { + DokkaDescriptorVisitor(sourceSet, kdocFinder, kotlinAnalysis[sourceSet], context.logger, javadocParser) + .visitClassDescriptor(descriptor, driInfo) + } } } }