Skip to content

Commit

Permalink
refactor(npm): Remove unused parallelization constructs
Browse files Browse the repository at this point in the history
The code looks like it would run things in parallel, but it executes
`getRemotePackageDetails()` strictly sequential. The pull request which
had introduced these constructs [1] also introduced the caching. So, the
speed-up observed back then was probably solely due to the added cache.
Remove the parallelization constructs to not mislead the reader and to
simplify.

[1]: #6009

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Oct 22, 2024
1 parent 6bb98df commit 41c90e0
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions plugins/package-managers/node/src/main/kotlin/Npm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ package org.ossreviewtoolkit.plugins.packagemanagers.node
import java.io.File
import java.util.concurrent.ConcurrentHashMap

import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext

import org.apache.logging.log4j.kotlin.logger

import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
Expand Down Expand Up @@ -120,7 +115,7 @@ open class Npm(

private val graphBuilder by lazy { DependencyGraphBuilder(NpmDependencyHandler(this)) }

private val npmViewCache = ConcurrentHashMap<String, Deferred<PackageJson>>()
private val npmViewCache = mutableMapOf<String, PackageJson>()

protected open fun hasLockfile(projectDir: File) = NodePackageManager.NPM.hasLockfile(projectDir)

Expand Down Expand Up @@ -249,7 +244,7 @@ open class Npm(
* Construct a [Package] by parsing its _package.json_ file and - if applicable - querying additional
* content via the `npm view` command. The result is a [Pair] with the raw identifier and the new package.
*/
internal suspend fun parsePackage(workingDir: File, packageJsonFile: File): Package {
internal fun parsePackage(workingDir: File, packageJsonFile: File): Package {
val packageDir = packageJsonFile.parentFile

logger.debug { "Found a 'package.json' file in '$packageDir'." }
Expand Down Expand Up @@ -289,7 +284,7 @@ open class Npm(

if (hasIncompleteData) {
runCatching {
getRemotePackageDetailsAsync(workingDir, "$rawName@$version").await()
getRemotePackageDetails(workingDir, "$rawName@$version")
}.onSuccess { details ->
if (description.isEmpty()) description = details.description.orEmpty()
if (homepageUrl.isEmpty()) homepageUrl = details.homepage.orEmpty()
Expand Down Expand Up @@ -342,20 +337,12 @@ open class Npm(
return module
}

private suspend fun getRemotePackageDetailsAsync(workingDir: File, packageName: String): Deferred<PackageJson> =
withContext(Dispatchers.IO) {
npmViewCache.getOrPut(packageName) {
async {
getRemotePackageDetails(workingDir, packageName)
}
}
protected open fun getRemotePackageDetails(workingDir: File, packageName: String): PackageJson =
npmViewCache.getOrPut(packageName) {
val process = run(workingDir, "info", "--json", packageName)
parsePackageJson(process.stdout)
}

protected open fun getRemotePackageDetails(workingDir: File, packageName: String): PackageJson {
val process = run(workingDir, "info", "--json", packageName)
return parsePackageJson(process.stdout)
}

/** Cache for submodules identified by its moduleDir absolutePath */
private val submodulesCache = ConcurrentHashMap<String, Set<File>>()

Expand Down

0 comments on commit 41c90e0

Please sign in to comment.