Skip to content

Commit c978c6f

Browse files
committed
Parallelize resolution in KotlinLibraryResolverImpl.
^KT-82978
1 parent f32be28 commit c978c6f

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

compiler/util-io/src/org/jetbrains/kotlin/konan/file/ZipFileSystemAccessor.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,24 @@ class ZipFileSystemCacheableAccessor(private val cacheLimit: Int) : ZipFileSyste
3232
}
3333

3434
override fun <T> withZipFileSystem(zipFile: File, action: (FileSystem) -> T): T {
35-
val fileSystem = openedFileSystems.getOrPut(zipFile) { zipFile.zipFileSystem() }
35+
val fileSystem = synchronized(openedFileSystems) {
36+
openedFileSystems.getOrPut(zipFile) { zipFile.zipFileSystem() }
37+
}
3638
return action(fileSystem)
3739
}
3840

3941
fun reset() {
4042
var lastException: Exception? = null
41-
for (fileSystem in openedFileSystems.values) {
42-
try {
43-
fileSystem.close()
44-
} catch (e: Exception) {
45-
lastException = e
43+
synchronized(openedFileSystems) {
44+
for (fileSystem in openedFileSystems.values) {
45+
try {
46+
fileSystem.close()
47+
} catch (e: Exception) {
48+
lastException = e
49+
}
4650
}
51+
openedFileSystems.clear()
4752
}
48-
openedFileSystems.clear()
4953

5054
lastException?.let {
5155
throw it

compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/resolver/impl/KotlinLibraryResolverImpl.kt

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ class KotlinLibraryResolverImpl<L : KotlinLibrary> internal constructor(
5656
): List<KotlinLibrary> {
5757

5858
val userProvidedLibraries = unresolvedLibraries.asSequence()
59-
.mapNotNull { searchPathResolver.resolve(it) }
6059
.toList()
60+
.parallelStream()
61+
.map { searchPathResolver.resolve(it) }
62+
.filter { it != null }
63+
.map { it!! }
64+
.collect(java.util.stream.Collectors.toList())
6165

6266
val defaultLibraries = searchPathResolver.defaultLinks(noStdLib, noDefaultLibs, noEndorsedLibs)
6367

@@ -126,28 +130,31 @@ class KotlinLibraryResolverImpl<L : KotlinLibrary> internal constructor(
126130
// constructed from the very beginning as well.
127131
val result = KotlinLibraryResolverResultImpl(rootLibraries)
128132

129-
val cache = mutableMapOf<Any, KotlinResolvedLibrary>()
133+
val cache = java.util.concurrent.ConcurrentHashMap<Any, KotlinResolvedLibrary>()
130134
cache.putAll(rootLibraries.map { it.library.libraryFile.fileKey to it })
131135

132-
val processingQueue = ArrayDeque(rootLibraries)
133-
while(processingQueue.isNotEmpty()) {
134-
val currentLibrary = processingQueue.removeFirst()
135-
currentLibrary.library.unresolvedDependencies(resolveManifestDependenciesLenient)
136-
.forEach { unresolvedDependency ->
137-
if (!searchPathResolver.isProvidedByDefault(unresolvedDependency)) {
138-
searchPathResolver.resolve(unresolvedDependency)?.let { resolvedDependencyLibrary ->
139-
val fileKey = resolvedDependencyLibrary.libraryFile.fileKey
140-
if (fileKey in cache) {
141-
currentLibrary.addDependency(cache[fileKey]!!)
142-
} else {
143-
val newlyResolved = KotlinResolvedLibraryImpl(resolvedDependencyLibrary)
144-
cache[fileKey] = newlyResolved
145-
currentLibrary.addDependency(newlyResolved)
146-
processingQueue.add(newlyResolved)
136+
var currentLevel: List<KotlinResolvedLibraryImpl> = rootLibraries
137+
while (currentLevel.isNotEmpty()) {
138+
val nextLevel = java.util.concurrent.ConcurrentLinkedQueue<KotlinResolvedLibraryImpl>()
139+
140+
currentLevel.parallelStream().forEach { currentLibrary ->
141+
currentLibrary.library.unresolvedDependencies(resolveManifestDependenciesLenient)
142+
.parallelStream()
143+
.forEach { unresolvedDependency ->
144+
if (!searchPathResolver.isProvidedByDefault(unresolvedDependency)) {
145+
searchPathResolver.resolve(unresolvedDependency)?.let { resolvedDependencyLibrary ->
146+
val fileKey = resolvedDependencyLibrary.libraryFile.fileKey
147+
val resolvedLibrary = cache.computeIfAbsent(fileKey) {
148+
val newLib = KotlinResolvedLibraryImpl(resolvedDependencyLibrary)
149+
nextLevel.add(newLib)
150+
newLib
151+
}
152+
currentLibrary.addDependency(resolvedLibrary)
147153
}
148154
}
149155
}
150-
}
156+
}
157+
currentLevel = nextLevel.toList()
151158
}
152159
return result
153160
}

compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/resolver/impl/KotlinResolvedLibraryImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.jetbrains.kotlin.library.metadata.resolver.KotlinResolvedLibrary
88

99
class KotlinResolvedLibraryImpl(override val library: KotlinLibrary) : KotlinResolvedLibrary {
1010

11-
private val _resolvedDependencies = mutableListOf<KotlinResolvedLibrary>()
11+
private val _resolvedDependencies = java.util.Collections.synchronizedList(mutableListOf<KotlinResolvedLibrary>())
1212
private val _emptyPackages by lazy { parseModuleHeader(library.metadata.moduleHeaderData).emptyPackageList }
1313

1414
override val resolvedDependencies: List<KotlinResolvedLibrary>

compiler/util-klib/src/org/jetbrains/kotlin/library/SearchPathResolver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ abstract class KotlinLibrarySearchPathResolver<L : KotlinLibrary>(
169169
// Store already resolved libraries.
170170
private inner class ResolvedLibrary(val library: L?)
171171

172-
private val resolvedLibraries = HashMap<UnresolvedLibrary, ResolvedLibrary>()
172+
private val resolvedLibraries = java.util.concurrent.ConcurrentHashMap<UnresolvedLibrary, ResolvedLibrary>()
173173

174174
private fun resolveOrNull(unresolved: UnresolvedLibrary): L? {
175175
return resolvedLibraries.getOrPut(unresolved) {

0 commit comments

Comments
 (0)