[K/N] Optimized libraries hash computation

#KT-63081 Fixed
This commit is contained in:
Igor Chevdar
2023-11-09 11:53:41 +02:00
committed by Space Team
parent ea49a9a292
commit cc69df0de6
2 changed files with 24 additions and 5 deletions
@@ -50,6 +50,7 @@ class CacheBuilder(
private val allLibraries by lazy { konanConfig.resolvedLibraries.getFullList(TopologicalLibraryOrder) }
private val uniqueNameToLibrary by lazy { allLibraries.associateBy { it.uniqueName } }
private val uniqueNameToHash = mutableMapOf<String, ByteArray>()
private val caches = mutableMapOf<KotlinLibrary, CachedLibraries.Cache>()
private val cacheRootDirectories = mutableMapOf<KotlinLibrary, String>()
@@ -237,7 +238,8 @@ class CacheBuilder(
val libraryCacheDirectory = when {
library.isDefault -> konanConfig.systemCacheDirectory
isExternal -> CachedLibraries.computeVersionedCacheDirectory(konanConfig.autoCacheDirectory, library, uniqueNameToLibrary)
isExternal -> CachedLibraries.computeVersionedCacheDirectory(
konanConfig.autoCacheDirectory, library, uniqueNameToLibrary, uniqueNameToHash)
else -> konanConfig.incrementalCacheDirectory!!
}
val libraryCache = libraryCacheDirectory.child(
@@ -171,6 +171,7 @@ class CachedLibraries(
}
private val uniqueNameToLibrary = allLibraries.associateBy { it.uniqueName }
private val uniqueNameToHash = mutableMapOf<String, ByteArray>()
private val allCaches: Map<KotlinLibrary, Cache> = allLibraries.mapNotNull { library ->
val explicitPath = explicitCaches[library]
@@ -186,7 +187,7 @@ class CachedLibraries(
}
?: autoCacheDirectory.takeIf { autoCacheableFrom.any { libraryPath.startsWith(it.absolutePath) } }
?.let {
val dir = computeVersionedCacheDirectory(it, library, uniqueNameToLibrary)
val dir = computeVersionedCacheDirectory(it, library, uniqueNameToLibrary, uniqueNameToHash)
selectCache(library, dir.child(getPerFileCachedLibraryName(library)))
?: selectCache(library, dir.child(getCachedLibraryName(library)))
}
@@ -220,12 +221,28 @@ class CachedLibraries(
fun getCachedLibraryName(library: KotlinLibrary): String = getCachedLibraryName(library.uniqueName)
fun getCachedLibraryName(libraryName: String): String = "$libraryName-cache"
fun computeVersionedCacheDirectory(baseCacheDirectory: File, library: KotlinLibrary, allLibraries: Map<String, KotlinLibrary>): File {
private fun computeLibraryHash(
library: KotlinLibrary,
librariesHashes: MutableMap<String, ByteArray>,
): ByteArray = librariesHashes.getOrPut(library.uniqueName) {
val messageDigest = MessageDigest.getInstance("SHA-256")
messageDigest.digestLibrary(library)
messageDigest.digest()
}
fun computeVersionedCacheDirectory(
baseCacheDirectory: File,
library: KotlinLibrary,
allLibraries: Map<String, KotlinLibrary>,
librariesHashes: MutableMap<String, ByteArray>,
): File {
val dependencies = library.getAllTransitiveDependencies(allLibraries)
val messageDigest = MessageDigest.getInstance("SHA-256")
messageDigest.update(compilerMarker)
messageDigest.digestLibrary(library)
dependencies.sortedBy { it.uniqueName }.forEach { messageDigest.digestLibrary(it) }
messageDigest.update(computeLibraryHash(library, librariesHashes))
dependencies.sortedBy { it.uniqueName }.forEach {
messageDigest.update(computeLibraryHash(it, librariesHashes))
}
val version = library.versions.libraryVersion ?: "unspecified"
val hashString = messageDigest.digest()