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 4ebe97370e5..9fe484a670a 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 @@ -109,39 +109,32 @@ class JvmDependenciesIndexImpl(_roots: List): JvmDependenciesIndex { request: SearchRequest, handler: (VirtualFile, JavaRoot.RootType) -> HandleResult ): T? { - - // default to searching with given parameters - fun doSearch() = doSearch(request, handler) - // make a decision based on information saved from last class search - if (request !is FindClassRequest || lastClassSearch == null) { - return doSearch() + if (request !is FindClassRequest || lastClassSearch?.first?.classId != request.classId) { + return doSearch(request, handler) } val (cachedRequest, cachedResult) = lastClassSearch!! - if (cachedRequest.classId != request.classId) { - return doSearch() - } - - when (cachedResult) { + return when (cachedResult) { is SearchResult.NotFound -> { val limitedRootTypes = request.acceptedRootTypes.toHashSet() limitedRootTypes.removeAll(cachedRequest.acceptedRootTypes) if (limitedRootTypes.isEmpty()) { - return null + null } else { - return doSearch(FindClassRequest(request.classId, limitedRootTypes), handler) + doSearch(FindClassRequest(request.classId, limitedRootTypes), handler) } } is SearchResult.Found -> { if (cachedRequest.acceptedRootTypes == request.acceptedRootTypes) { - return handler(cachedResult.packageDirectory, cachedResult.root.type).result + handler(cachedResult.packageDirectory, cachedResult.root.type).result + } + else { + doSearch(request, handler) } } } - - return doSearch() } private fun doSearch(request: SearchRequest, handler: (VirtualFile, JavaRoot.RootType) -> HandleResult): T? { @@ -183,9 +176,9 @@ class JvmDependenciesIndexImpl(_roots: List): JvmDependenciesIndex { val cachesLastIndex = caches.lastIndex for (cacheIndex in 0..cachesLastIndex) { val reverseCacheIndex = cachesLastIndex - cacheIndex - val cache = caches[reverseCacheIndex] - for (i in 0..cache.rootIndices.size() - 1) { - val rootIndex = cache.rootIndices[i] + val cacheRootIndices = caches[reverseCacheIndex].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, packagesPath, reverseCacheIndex, caches) ?: continue @@ -195,7 +188,7 @@ class JvmDependenciesIndexImpl(_roots: List): JvmDependenciesIndex { return found(directoryInRoot, root, result) } } - processedRootsUpTo = cache.rootIndices.lastOrNull() ?: processedRootsUpTo + processedRootsUpTo = if (cacheRootIndices.isEmpty) processedRootsUpTo else cacheRootIndices.get(cacheRootIndices.size() - 1) } return notFound() @@ -241,7 +234,7 @@ class JvmDependenciesIndexImpl(_roots: List): JvmDependenciesIndex { } private fun cachesPath(path: List): List { - val caches = ArrayList() + val caches = ArrayList(path.size + 1) caches.add(rootCache) var currentCache = rootCache for (subPackageName in path) { @@ -266,13 +259,9 @@ class JvmDependenciesIndexImpl(_roots: List): JvmDependenciesIndex { val acceptedRootTypes: Set } - private interface SearchResult { - class Found(val packageDirectory: VirtualFile, val root: JavaRoot) : SearchResult + private sealed class SearchResult { + class Found(val packageDirectory: VirtualFile, val root: JavaRoot) : SearchResult() - object NotFound : SearchResult - } - - private companion object { - fun IntArrayList.lastOrNull() = if (isEmpty) null else get(size() - 1) + object NotFound : SearchResult() } }