From c29605d92d1999434ecc79e774281a8280ae2823 Mon Sep 17 00:00:00 2001 From: Paweł Marks Date: Wed, 22 Jan 2020 13:32:58 +0100 Subject: Cache of location provider added --- .../kotlin/resolvers/DefaultLocationProvider.kt | 30 +++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'core/src/main') diff --git a/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt b/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt index 3412d975..8e848b63 100644 --- a/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt +++ b/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt @@ -1,12 +1,12 @@ package org.jetbrains.dokka.resolvers import org.jetbrains.dokka.CoreExtensions -import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.dokka.utilities.htmlEscape import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.single +import org.jetbrains.dokka.utilities.htmlEscape +import java.util.* open class DefaultLocationProvider( private val pageGraphRoot: ModulePageNode, @@ -14,6 +14,9 @@ open class DefaultLocationProvider( ) : LocationProvider { // TODO: cache private val extension = dokkaContext.single(CoreExtensions.fileExtension) + private val pagesCache = mutableMapOf>() + private val pathCache: MutableMap> = IdentityHashMap>() + override fun resolve(node: PageNode, context: PageNode?): String = pathTo(node, context) + extension override fun resolve(dri: DRI, platforms: List, context: PageNode?): String = @@ -22,7 +25,8 @@ open class DefaultLocationProvider( ExternalLocationProvider.getLocation(dri, this.dokkaContext.configuration.passesConfigurations .filter { passConfig -> - platforms.toSet().contains(PlatformData(passConfig.moduleName, passConfig.analysisPlatform, passConfig.targets)) + platforms.toSet() + .contains(PlatformData(passConfig.moduleName, passConfig.analysisPlatform, passConfig.targets)) } // TODO: change targets to something better? .flatMap { it.externalDocumentationLinks }.distinct() ) @@ -41,7 +45,8 @@ open class DefaultLocationProvider( override fun top(): PageNode = pageGraphRoot protected open fun findInPageGraph(dri: DRI, platforms: List): PageNode? = - pageGraphRoot.dfs { it.dri == dri } + pagesCache.getOrPut(dri) { pageGraphRoot.dfs { it.dri == dri }.wrapped }.unwrapped + protected open fun pathTo(node: PageNode, context: PageNode?): String { @@ -49,14 +54,17 @@ open class DefaultLocationProvider( if (this is PackagePageNode) name else identifierToFilename(name) - fun getPath(pathNode: PageNode?, path: List = mutableListOf()): List = when (pathNode) { + fun getPathInternal(pathNode: PageNode?, path: List): List = when (pathNode) { null -> path - else -> getPath(pathNode.parent(), path + pathNode.pathName().ifEmpty { "root" }) + else -> getPathInternal(pathNode.parent(), path + pathNode.pathName().ifEmpty { "root" }) } - val contextNode = if (context?.children?.isEmpty() == true && context.parent() != null) context.parent() else context + fun getPath(pathNode: PageNode) = pathCache.getOrPut(pathNode) { getPathInternal(pathNode, emptyList()) } + + val contextNode = + if (context?.children?.isEmpty() == true && context.parent() != null) context.parent() else context val nodePath = getPath(node).reversed() - val contextPath = getPath(contextNode).reversed() + val contextPath = contextNode?.let(::getPath).orEmpty().reversed() val commonPathElements = nodePath.zip(contextPath).takeWhile { (a, b) -> a == b }.size @@ -112,4 +120,8 @@ private fun identifierToFilename(name: String): String { val escaped = name.replace('<', '-').replace('>', '-') val lowercase = escaped.replace("[A-Z]".toRegex()) { matchResult -> "-" + matchResult.value.toLowerCase() } return if (lowercase in reservedFilenames) "--$lowercase--" else lowercase -} \ No newline at end of file +} + +private class Maybe(val unwrapped: T?) + +private val T?.wrapped: Maybe get() = Maybe(this) \ No newline at end of file -- cgit