[Gradle, JS] Optimize JS tasks serialized size

Tasks are serialized by Gradle's configurations cache. Each referenced object by task is also serialized. Reduce task's size by narrowing referenced objects.
#KT-45294 In Progress
This commit is contained in:
Alexander Likhachev
2021-03-05 23:13:23 +03:00
parent 072492386d
commit 14f6e5db6d
14 changed files with 71 additions and 43 deletions
@@ -107,7 +107,7 @@ internal class DukatCompilationResolverPlugin(
}
DukatExecutor(
nodeJs,
nodeJs.versions,
DtsResolver(npmProject).getAllDts(externalNpmDependencies),
externalsOutputFormat,
npmProject,
@@ -6,11 +6,11 @@
package org.jetbrains.kotlin.gradle.targets.js.dukat
import org.gradle.internal.service.ServiceRegistry
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.NpmVersions
import org.jetbrains.kotlin.gradle.targets.js.npm.NpmProject
class DukatExecutor(
val nodeJs: NodeJsRootExtension,
val npmVersions: NpmVersions,
val typeDefinitions: List<DtsResolver.Dts>,
val externalsOutputFormat: ExternalsOutputFormat,
val npmProject: NpmProject,
@@ -23,7 +23,7 @@ class DukatExecutor(
}
val versionFile = npmProject.externalsDirRoot.resolve("version.txt")
val version = DukatCompilationResolverPlugin.VERSION + ", " + nodeJs.versions.dukat.version
val version = DukatCompilationResolverPlugin.VERSION + ", " + npmVersions.dukat.version
val prevVersion = if (versionFile.exists()) versionFile.readText() else null
val inputsFile = npmProject.externalsDirRoot.resolve("inputs.txt")
@@ -19,8 +19,11 @@ abstract class DukatTask(
override val compilation: KotlinJsCompilation
) : DefaultTask(), RequiresNpmDependencies {
@get:Internal
@Transient
protected val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
private val resolutionManager = nodeJs.npmResolutionManager
@get:Internal
val compilationName by lazy {
compilation.disambiguatedName
@@ -31,8 +34,9 @@ abstract class DukatTask(
get() = true
@get:Internal
override val requiredNpmDependencies: Set<RequiredKotlinJsDependency>
get() = setOf(nodeJs.versions.dukat)
override val requiredNpmDependencies: Set<RequiredKotlinJsDependency> by lazy {
setOf(nodeJs.versions.dukat)
}
/**
* [ExternalsOutputFormat] what to generate, sources or binaries
@@ -43,11 +47,10 @@ abstract class DukatTask(
private val projectPath = project.path
private val compilationResolution
get() =
nodeJs.npmResolutionManager.requireInstalled(
services,
logger
)[projectPath][compilationName]
get() = resolutionManager.requireInstalled(
services,
logger
)[projectPath][compilationName]
@get:Internal
val dts: List<DtsResolver.Dts>
@@ -91,7 +94,7 @@ abstract class DukatTask(
@TaskAction
open fun run() {
nodeJs.npmResolutionManager.checkRequiredDependencies(this, services, logger, projectPath)
resolutionManager.checkRequiredDependencies(this, services, logger, projectPath)
destinationDir.deleteRecursively()
@@ -22,6 +22,10 @@ constructor(
private val npmProject = compilation.npmProject
private val versions by lazy {
nodeJs.versions
}
@get:OutputDirectory
override val destinationDir: File by lazy {
npmProject.externalsDir
@@ -29,7 +33,7 @@ constructor(
private val executor
get() = DukatExecutor(
nodeJs,
versions,
dts,
externalsOutputFormat,
npmProject,
@@ -21,6 +21,7 @@ constructor(
@Internal
override val compilation: KotlinJsCompilation
) : AbstractExecTask<NodeJsExec>(NodeJsExec::class.java), RequiresNpmDependencies {
@Transient
@get:Internal
lateinit var nodeJs: NodeJsRootExtension
@@ -48,12 +49,13 @@ constructor(
get() = true
@get:Internal
override val requiredNpmDependencies: Set<RequiredKotlinJsDependency>
get() = mutableSetOf<RequiredKotlinJsDependency>().also {
override val requiredNpmDependencies: Set<RequiredKotlinJsDependency> by lazy {
mutableSetOf<RequiredKotlinJsDependency>().also {
if (sourceMapStackTraces) {
it.add(nodeJs.versions.sourceMapSupport)
}
}
}
override fun exec() {
val newArgs = mutableListOf<String>()
@@ -14,10 +14,12 @@ import java.net.URI
@CacheableTask
open class NodeJsSetupTask : DefaultTask() {
@Transient
private val settings = NodeJsRootPlugin.apply(project.rootProject)
private val env by lazy { settings.requireConfigured() }
private val fs = FileSystemOperationsCompat(project)
private val archiveOperations = ArchiveOperationsCompat(project)
private val shouldDownload = settings.download
val ivyDependency: String
@Input get() = env.ivyDependency
@@ -63,7 +65,7 @@ open class NodeJsSetupTask : DefaultTask() {
init {
@Suppress("LeakingThis")
onlyIf {
settings.download && !File(env.nodeExecutable).isFile
shouldDownload && !File(env.nodeExecutable).isFile
}
}
@@ -31,12 +31,12 @@ open class NpmProject(@Transient val compilation: KotlinJsCompilation) {
buildNpmProjectName()
}
val nodeJs by lazy {
NodeJsRootPlugin.apply(project.rootProject)
}
@Transient
val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
val dir: File
get() = nodeJs.projectPackagesDir.resolve(name)
val dir: File by lazy {
nodeJs.projectPackagesDir.resolve(name)
}
val target: KotlinJsTargetDsl
get() = compilation.target as KotlinJsTargetDsl
@@ -77,8 +77,9 @@ open class NpmProject(@Transient val compilation: KotlinJsCompilation) {
internal val modules = NpmProjectModules(dir)
private val rootNodeModules: NpmProjectModules?
get() = NpmProjectModules(nodeJs.rootPackageDir)
private val nodeExecutable by lazy {
nodeJs.requireConfigured().nodeExecutable
}
fun useTool(
exec: ExecSpec,
@@ -87,7 +88,7 @@ open class NpmProject(@Transient val compilation: KotlinJsCompilation) {
args: List<String>
) {
exec.workingDir = dir
exec.executable = nodeJs.requireConfigured().nodeExecutable
exec.executable = nodeExecutable
exec.args = nodeArgs + require(tool) + args
}
@@ -24,16 +24,19 @@ constructor(
private val compilation: KotlinJsCompilation
) : DefaultTask() {
private val npmProject = compilation.npmProject
@Transient
private val nodeJs = npmProject.nodeJs
private val resolutionManager = nodeJs.npmResolutionManager
private val compilationName = compilation.disambiguatedName
private val projectPath = project.path
private val compilationResolution
get() = nodeJs.npmResolutionManager.requireInstalled(
services,
logger
)[projectPath][compilationName]
get() = resolutionManager.requireInstalled(
services,
logger
)[projectPath][compilationName]
private val packageJsonHandlers = compilation.packageJsonHandlers
@@ -26,8 +26,9 @@ open class KotlinNpmInstallTask : DefaultTask() {
}
}
@Transient
private val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
private val resolutionManager get() = nodeJs.npmResolutionManager
private val resolutionManager = nodeJs.npmResolutionManager
@Input
val args: MutableList<String> = mutableListOf()
@@ -47,12 +48,14 @@ open class KotlinNpmInstallTask : DefaultTask() {
// avoid using node_modules as output directory, as it is significantly slows down build
@get:OutputFile
val nodeModulesState: File
get() = nodeJs.rootNodeModulesStateFile
val nodeModulesState: File by lazy {
nodeJs.rootNodeModulesStateFile
}
@get:OutputFile
val yarnLock: File
get() = nodeJs.rootPackageDir.resolve("yarn.lock")
val yarnLock: File by lazy {
nodeJs.rootPackageDir.resolve("yarn.lock")
}
@TaskAction
fun resolve() {
@@ -18,18 +18,22 @@ import org.jetbrains.kotlin.gradle.targets.js.npm.resolver.KotlinCompilationNpmR
import org.jetbrains.kotlin.gradle.targets.js.npm.resolver.PACKAGE_JSON_UMBRELLA_TASK_NAME
import org.jetbrains.kotlin.gradle.tasks.dependsOn
import org.jetbrains.kotlin.gradle.tasks.registerTask
import org.jetbrains.kotlin.gradle.utils.getValue
import java.io.File
open class KotlinPackageJsonTask : DefaultTask() {
init {
onlyIf {
nodeJs.npmResolutionManager.isConfiguringState()
npmResolutionManager.isConfiguringState()
}
}
@Transient
private lateinit var nodeJs: NodeJsRootExtension
private val npmResolutionManager by project.provider { nodeJs.npmResolutionManager }
@Transient
private lateinit var compilation: KotlinJsCompilation
@@ -41,7 +45,7 @@ open class KotlinPackageJsonTask : DefaultTask() {
val projectPath = project.path
private val compilationResolver
get() = nodeJs.npmResolutionManager.resolver[projectPath][compilationDisambiguatedName]
get() = npmResolutionManager.resolver[projectPath][compilationDisambiguatedName]
private val producer: KotlinCompilationNpmResolver.PackageJsonProducer
get() = compilationResolver.packageJsonProducer
@@ -25,12 +25,14 @@ open class RootPackageJsonTask : DefaultTask() {
}
}
@Transient
private val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
private val resolutionManager get() = nodeJs.npmResolutionManager
private val resolutionManager = nodeJs.npmResolutionManager
@get:OutputFile
val rootPackageJson: File
get() = nodeJs.rootPackageDir.resolve(NpmProject.PACKAGE_JSON)
val rootPackageJson: File by lazy {
nodeJs.rootPackageDir.resolve(NpmProject.PACKAGE_JSON)
}
@TaskAction
fun resolve() {
@@ -49,7 +49,9 @@ class KotlinKarma(
@Transient
private val project: Project = compilation.target.project
private val npmProject = compilation.npmProject
@Transient
private val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
private val nodeRootPackageDir by lazy { nodeJs.rootPackageDir }
private val versions = nodeJs.versions
private val config: KarmaConfig = KarmaConfig()
@@ -87,7 +89,7 @@ class KotlinKarma(
devtool = null,
export = false,
progressReporter = true,
progressReporterPathFilter = nodeJs.rootPackageDir.absolutePath,
progressReporterPathFilter = nodeRootPackageDir.absolutePath,
webpackMajorVersion = webpackMajorVersion
)
@@ -27,8 +27,7 @@ class KotlinMocha(@Transient override val compilation: KotlinJsCompilation, priv
@Transient
private val project: Project = compilation.target.project
private val npmProject = compilation.npmProject
private val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
private val versions = nodeJs.versions
private val versions = NodeJsRootPlugin.apply(project.rootProject).versions
private val isTeamCity by lazy {
if (isConfigurationCacheAvailable(project.gradle)) {
project.providers.gradleProperty(TC_PROJECT_PROPERTY).forUseAtConfigurationTime().isPresent
@@ -39,8 +39,11 @@ constructor(
@Transient
override val compilation: KotlinJsCompilation
) : DefaultTask(), RequiresNpmDependencies {
@Transient
private val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
private val versions = nodeJs.versions
private val resolutionManager = nodeJs.npmResolutionManager
private val rootPackageDir by lazy { nodeJs.rootPackageDir }
private val npmProject = compilation.npmProject
@@ -260,7 +263,7 @@ constructor(
@TaskAction
fun doExecute() {
nodeJs.npmResolutionManager.checkRequiredDependencies(task = this, services = services, logger = logger, projectPath = projectPath)
resolutionManager.checkRequiredDependencies(task = this, services = services, logger = logger, projectPath = projectPath)
val runner = createRunner()
@@ -279,7 +282,7 @@ constructor(
runner.copy(
config = runner.config.copy(
progressReporter = true,
progressReporterPathFilter = nodeJs.rootPackageDir.absolutePath
progressReporterPathFilter = rootPackageDir.absolutePath
)
).execute(services)
}