Skip to content

Commit

Permalink
Make translators run in parallel.
Browse files Browse the repository at this point in the history
  • Loading branch information
BarkingBad committed Sep 28, 2020
1 parent bba7977 commit e534ec0
Show file tree
Hide file tree
Showing 9 changed files with 660 additions and 523 deletions.
2 changes: 1 addition & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ dependencies {
implementation("org.jsoup:jsoup:1.12.1")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.11.1")

val coroutines_version: String by project
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version")

testImplementation(project(":core:test-api"))
testImplementation(kotlin("test-junit"))
}
Expand Down
13 changes: 8 additions & 5 deletions core/src/main/kotlin/DokkaGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.DokkaPlugin
import org.jetbrains.dokka.utilities.DokkaLogger
import org.jetbrains.dokka.utilities.report
import kotlinx.coroutines.*
import org.jetbrains.dokka.utilities.parallelMap


/**
Expand Down Expand Up @@ -73,9 +75,10 @@ class DokkaGenerator(

fun createDocumentationModels(
context: DokkaContext
) = context.configuration.sourceSets
.flatMap { sourceSet -> translateSources(sourceSet, context) }
.also { modules -> if (modules.isEmpty()) exitGenerationGracefully("Nothing to document") }
) = runBlocking(Dispatchers.Default) {
context.configuration.sourceSets.parallelMap { sourceSet -> translateSources(sourceSet, context) }.flatten()
.also { modules -> if (modules.isEmpty()) exitGenerationGracefully("Nothing to document") }
}

fun transformDocumentationModelBeforeMerge(
modulesFromPlatforms: List<DModule>,
Expand Down Expand Up @@ -133,8 +136,8 @@ class DokkaGenerator(
}
}

private fun translateSources(sourceSet: DokkaSourceSet, context: DokkaContext) =
context[CoreExtensions.sourceToDocumentableTranslator].map {
private suspend fun translateSources(sourceSet: DokkaSourceSet, context: DokkaContext) =
context[CoreExtensions.sourceToDocumentableTranslator].parallelMap {
it.invoke(sourceSet, context)
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/kotlin/plugability/LazyEvaluated.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.dokka.plugability

internal class LazyEvaluated<T : Any> private constructor(private val recipe: ((DokkaContext) -> T)? = null, private var value: T? = null) {

@Synchronized
internal fun get(context: DokkaContext): T {
if(value == null) {
value = recipe?.invoke(context)
Expand All @@ -13,4 +14,4 @@ internal class LazyEvaluated<T : Any> private constructor(private val recipe: ((
fun <T : Any> fromInstance(value: T) = LazyEvaluated(value = value)
fun <T : Any> fromRecipe(recipe: (DokkaContext) -> T) = LazyEvaluated(recipe = recipe)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import org.jetbrains.dokka.model.DModule
import org.jetbrains.dokka.plugability.DokkaContext

interface SourceToDocumentableTranslator {
fun invoke(sourceSet: DokkaSourceSet, context: DokkaContext): DModule
}
suspend fun invoke(sourceSet: DokkaSourceSet, context: DokkaContext): DModule
}
17 changes: 17 additions & 0 deletions core/src/main/kotlin/utilities/parallelCollectionOperations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jetbrains.dokka.utilities

import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope

suspend inline fun <A, B> Iterable<A>.parallelMap(crossinline f: suspend (A) -> B): List<B> = coroutineScope {
map { async { f(it) } }.awaitAll()
}

suspend inline fun <A, B> Iterable<A>.parallelMapNotNull(crossinline f: suspend (A) -> B?): List<B> = coroutineScope {
map { async { f(it) } }.awaitAll().filterNotNull()
}

suspend inline fun <A> Iterable<A>.parallelForEach(crossinline f: suspend (A) -> Unit): Unit = coroutineScope {
map { async { f(it) } }.awaitAll()
}
2 changes: 1 addition & 1 deletion plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,4 @@ fun ContentPage.sourceSets() = this.content.sourceSets
fun ContentEmbeddedResource.isImage(): Boolean {
val imageExtensions = setOf("png", "jpg", "jpeg", "gif", "bmp", "tif", "webp", "svg")
return File(address).extension.toLowerCase() in imageExtensions
}
}

Large diffs are not rendered by default.

0 comments on commit e534ec0

Please sign in to comment.