From 68239f3814275d6fcef7edc2948276c8edfde739 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Mon, 16 Nov 2020 16:32:33 +0500 Subject: [PATCH] [caches] Optimized away some calls to Files.exists --- .../kotlin/backend/konan/CachedLibraries.kt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CachedLibraries.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CachedLibraries.kt index ebfe168967a..52a28d85f39 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CachedLibraries.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CachedLibraries.kt @@ -28,19 +28,24 @@ class CachedLibraries( } } + private val cacheDirsContents = mutableMapOf>() + private fun selectCache(library: KotlinLibrary, cacheDir: File): Cache? { // See Linker.renameOutput why is it ok to have an empty cache directory. - if (cacheDir.listFilesOrEmpty.isEmpty()) return null + val cacheDirContents = cacheDirsContents.getOrPut(cacheDir.absolutePath) { + cacheDir.listFilesOrEmpty.map { it.absolutePath }.toSet() + } + if (cacheDirContents.isEmpty()) return null val baseName = getCachedLibraryName(library) val dynamicFile = cacheDir.child(getArtifactName(baseName, CompilerOutputKind.DYNAMIC_CACHE)) val staticFile = cacheDir.child(getArtifactName(baseName, CompilerOutputKind.STATIC_CACHE)) - if (dynamicFile.exists && staticFile.exists) + if (dynamicFile.absolutePath in cacheDirContents && staticFile.absolutePath in cacheDirContents) error("Both dynamic and static caches files cannot be in the same directory." + " Library: ${library.libraryName}, path to cache: ${cacheDir.absolutePath}") return when { - dynamicFile.exists -> Cache(Cache.Kind.DYNAMIC, dynamicFile.absolutePath) - staticFile.exists -> Cache(Cache.Kind.STATIC, staticFile.absolutePath) + dynamicFile.absolutePath in cacheDirContents -> Cache(Cache.Kind.DYNAMIC, dynamicFile.absolutePath) + staticFile.absolutePath in cacheDirContents -> Cache(Cache.Kind.STATIC, staticFile.absolutePath) else -> error("No cache found for library ${library.libraryName} at ${cacheDir.absolutePath}") } }