From 3f5b8b3f6804a0c04373c4d1e84b29c71b849ea9 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 27 Apr 2017 15:32:46 +0300 Subject: [PATCH] Refactor JvmDependenciesIndexImpl.search - use nullable type "T?" instead of HandleResult consisting of the found value and the "should continue" flag - inline all local functions because they don't add any value now --- .../cli/jvm/index/JvmDependenciesIndexImpl.kt | 79 ++++++------------- 1 file changed, 25 insertions(+), 54 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/index/JvmDependenciesIndexImpl.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/index/JvmDependenciesIndexImpl.kt index 1ba465617d7..4d8740f7412 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/index/JvmDependenciesIndexImpl.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/index/JvmDependenciesIndexImpl.kt @@ -68,38 +68,25 @@ class JvmDependenciesIndexImpl(_roots: List): JvmDependenciesIndex { Array(roots.size) { THashMap() } } - // findClassGivenDirectory MUST check whether the class with this classId exists in given package - override fun findClass( - classId: ClassId, - acceptedRootTypes: Set, - findClassGivenDirectory: (VirtualFile, JavaRoot.RootType) -> T? - ): T? { - return findClassCached(classId, acceptedRootTypes) { dir, rootType -> - val found = findClassGivenDirectory(dir, rootType) - HandleResult(found, continueSearch = found == null) - } - } - override fun traverseDirectoriesInPackage( packageFqName: FqName, acceptedRootTypes: Set, continueSearch: (VirtualFile, JavaRoot.RootType) -> Boolean ) { search(TraverseRequest(packageFqName, acceptedRootTypes)) { dir, rootType -> - HandleResult(Unit, continueSearch(dir, rootType)) + if (continueSearch(dir, rootType)) null else Unit } } - private data class HandleResult(val result: T?, val continueSearch: Boolean) - - private fun findClassCached( + // findClassGivenDirectory MUST check whether the class with this classId exists in given package + override fun findClass( classId: ClassId, acceptedRootTypes: Set, - handler: (VirtualFile, JavaRoot.RootType) -> HandleResult + findClassGivenDirectory: (VirtualFile, JavaRoot.RootType) -> T? ): T? { // make a decision based on information saved from last class search if (lastClassSearch?.first?.classId != classId) { - return search(FindClassRequest(classId, acceptedRootTypes), handler) + return search(FindClassRequest(classId, acceptedRootTypes), findClassGivenDirectory) } val (cachedRequest, cachedResult) = lastClassSearch!! @@ -110,43 +97,21 @@ class JvmDependenciesIndexImpl(_roots: List): JvmDependenciesIndex { null } else { - search(FindClassRequest(classId, limitedRootTypes), handler) + search(FindClassRequest(classId, limitedRootTypes), findClassGivenDirectory) } } is SearchResult.Found -> { if (cachedRequest.acceptedRootTypes == acceptedRootTypes) { - handler(cachedResult.packageDirectory, cachedResult.root.type).result + findClassGivenDirectory(cachedResult.packageDirectory, cachedResult.root.type) } else { - search(FindClassRequest(classId, acceptedRootTypes), handler) + search(FindClassRequest(classId, acceptedRootTypes), findClassGivenDirectory) } } } } - private fun search(request: SearchRequest, handler: (VirtualFile, JavaRoot.RootType) -> HandleResult): T? { - fun found(packageDirectory: VirtualFile, root: JavaRoot, result: T): T = result.also { - if (request is FindClassRequest) { - lastClassSearch = Pair(request, SearchResult.Found(packageDirectory, root)) - } - } - - fun notFound(): T? = null.also { - if (request is FindClassRequest) { - lastClassSearch = Pair(request, SearchResult.NotFound) - } - } - - fun handle(root: JavaRoot, targetDirInRoot: VirtualFile): T? { - if (root.type in request.acceptedRootTypes) { - val (result, shouldContinue) = handler(targetDirInRoot, root.type) - if (!shouldContinue) { - return result - } - } - return null - } - + private fun search(request: SearchRequest, handler: (VirtualFile, JavaRoot.RootType) -> T?): T? { // a list of package sub names, ["org", "jb", "kotlin"] val packagesPath = request.packageFqName.pathSegments().map { it.identifier } // a list of caches corresponding to packages, [default, "org", "org.jb", "org.jb.kotlin"] @@ -155,26 +120,32 @@ class JvmDependenciesIndexImpl(_roots: List): JvmDependenciesIndex { var processedRootsUpTo = -1 // traverse caches starting from last, which contains most specific information - // NOTE: indices manipulation instead of using caches.indices.reversed() is here for performance reasons - val cachesLastIndex = caches.lastIndex - for (cacheIndex in 0..cachesLastIndex) { - val reverseCacheIndex = cachesLastIndex - cacheIndex - val cacheRootIndices = caches[reverseCacheIndex].rootIndices + // NOTE: indices manipulation instead of using caches.reversed() is here for performance reasons + for (cacheIndex in caches.lastIndex downTo 0) { + val cacheRootIndices = caches[cacheIndex].rootIndices for (i in 0..cacheRootIndices.size() - 1) { val rootIndex = cacheRootIndices[i] if (rootIndex <= processedRootsUpTo) continue // roots with those indices have been processed by now - val directoryInRoot = travelPath(rootIndex, request.packageFqName, packagesPath, reverseCacheIndex, caches) ?: continue + val directoryInRoot = travelPath(rootIndex, request.packageFqName, packagesPath, cacheIndex, caches) ?: continue val root = roots[rootIndex] - val result = handle(root, directoryInRoot) - if (result != null) { - return found(directoryInRoot, root, result) + if (root.type in request.acceptedRootTypes) { + val result = handler(directoryInRoot, root.type) + if (result != null) { + if (request is FindClassRequest) { + lastClassSearch = Pair(request, SearchResult.Found(directoryInRoot, root)) + } + return result + } } } processedRootsUpTo = if (cacheRootIndices.isEmpty) processedRootsUpTo else cacheRootIndices.get(cacheRootIndices.size() - 1) } - return notFound() + if (request is FindClassRequest) { + lastClassSearch = Pair(request, SearchResult.NotFound) + } + return null } // try to find a target directory corresponding to package represented by packagesPath in a given root represented by index