From 48b14562907929264e2bd643e3f95909c6926258 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Tue, 31 Jan 2023 14:38:07 +0100 Subject: [PATCH] [CLI] create `ArgumentParseErrors` instances on request spare memory for projects with big number of modules currently 500kb for IJ monorepo of mostly empty errors --- .../arguments/parseCommandLineArguments.kt | 22 +++++++++---------- .../preprocessCommandLineArguments.kt | 8 +++---- plugins/kapt3/kapt3-cli/src/KaptCli.kt | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/parseCommandLineArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/parseCommandLineArguments.kt index c4d80b2e1c7..81d7f8f6fb1 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/parseCommandLineArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/parseCommandLineArguments.kt @@ -65,7 +65,7 @@ data class ArgumentParseErrors( // Parses arguments into the passed [result] object. Errors related to the parsing will be collected into [CommonToolArguments.errors]. fun parseCommandLineArguments(args: List, result: A, overrideArguments: Boolean = false) { - val errors = result.errors ?: ArgumentParseErrors().also { result.errors = it } + val errors = lazy { result.errors ?: ArgumentParseErrors().also { result.errors = it } } val preprocessed = preprocessCommandLineArguments(args, errors) parsePreprocessedCommandLineArguments(preprocessed, result, errors, overrideArguments) } @@ -81,7 +81,7 @@ fun parseCommandLineArgumentsFromEnvironment(arguments private fun parsePreprocessedCommandLineArguments( args: List, result: A, - errors: ArgumentParseErrors, + errors: Lazy, overrideArguments: Boolean ) { data class ArgumentField(val property: KMutableProperty1, val argument: Argument) @@ -103,13 +103,13 @@ private fun parsePreprocessedCommandLineArguments( val deprecatedName = argument.deprecatedName if (deprecatedName.isNotEmpty() && (deprecatedName == arg || arg.startsWith("$deprecatedName="))) { - errors.deprecatedArguments[deprecatedName] = argument.value + errors.value.deprecatedArguments[deprecatedName] = argument.value return true } if (argument.value == arg) { if (argument.isAdvanced && property.returnType.classifier != Boolean::class) { - errors.extraArgumentsPassedInObsoleteForm.add(arg) + errors.value.extraArgumentsPassedInObsoleteForm.add(arg) } return true } @@ -140,9 +140,9 @@ private fun parsePreprocessedCommandLineArguments( val parser = matchingParsers.firstOrNull() if (parser == null) { - errors.unknownExtraFlags += arg + errors.value.unknownExtraFlags += arg } else { - val newInternalArgument = parser.parseInternalArgument(arg, errors) ?: continue + val newInternalArgument = parser.parseInternalArgument(arg, errors.value) ?: continue // Manual language feature setting overrides the previous value of the same feature setting, if it exists. internalArguments.removeIf { (it as? ManualLanguageFeatureSetting)?.languageFeature == @@ -157,8 +157,8 @@ private fun parsePreprocessedCommandLineArguments( val argumentField = properties.firstOrNull { it.matches(arg) } if (argumentField == null) { when { - arg.startsWith(ADVANCED_ARGUMENT_PREFIX) -> errors.unknownExtraFlags.add(arg) - arg.startsWith("-") -> errors.unknownArgs.add(arg) + arg.startsWith(ADVANCED_ARGUMENT_PREFIX) -> errors.value.unknownExtraFlags.add(arg) + arg.startsWith("-") -> errors.value.unknownArgs.add(arg) else -> freeArgs.add(arg) } continue @@ -172,7 +172,7 @@ private fun parsePreprocessedCommandLineArguments( when (arg.substring(argument.value.length + 1)) { "true" -> true "false" -> false - else -> true.also { errors.booleanArgumentWithValue = arg } + else -> true.also { errors.value.booleanArgumentWithValue = arg } } } else true } @@ -183,7 +183,7 @@ private fun parsePreprocessedCommandLineArguments( arg.substring(argument.deprecatedName.length + 1) } i == args.size -> { - errors.argumentWithoutValue = arg + errors.value.argumentWithoutValue = arg break@loop } else -> { @@ -194,7 +194,7 @@ private fun parsePreprocessedCommandLineArguments( if ((argumentField.property.returnType.classifier as? KClass<*>)?.java?.isArray == false && !visitedArgs.add(argument.value) && value is String && property.get(result) != value ) { - errors.duplicateArguments[argument.value] = value + errors.value.duplicateArguments[argument.value] = value } updateField(property, result, value, argument.delimiter, overrideArguments) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/preprocessCommandLineArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/preprocessCommandLineArguments.kt index e258ef09705..ffb5a00c9f7 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/preprocessCommandLineArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/preprocessCommandLineArguments.kt @@ -23,14 +23,14 @@ private const val BACKSLASH = '\\' * This is done prior to *any* arguments parsing, and result of preprocessing * will be used instead of actual passed arguments. */ -fun preprocessCommandLineArguments(args: List, errors: ArgumentParseErrors): List = +fun preprocessCommandLineArguments(args: List, errors: Lazy): List = args.flatMap { arg -> if (arg.isArgfileArgument) { - File(arg.argfilePath).expand(errors) + File(arg.argfilePath).expand(errors.value) } else if (arg.isDeprecatedArgfileArgument) { - errors.deprecatedArguments[EXPERIMENTAL_ARGFILE_ARGUMENT] = ARGFILE_ARGUMENT + errors.value.deprecatedArguments[EXPERIMENTAL_ARGFILE_ARGUMENT] = ARGFILE_ARGUMENT - File(arg.deprecatedArgfilePath).expand(errors) + File(arg.deprecatedArgfilePath).expand(errors.value) } else { listOf(arg) } diff --git a/plugins/kapt3/kapt3-cli/src/KaptCli.kt b/plugins/kapt3/kapt3-cli/src/KaptCli.kt index 04f4a23a4bf..82800b297eb 100644 --- a/plugins/kapt3/kapt3-cli/src/KaptCli.kt +++ b/plugins/kapt3/kapt3-cli/src/KaptCli.kt @@ -39,7 +39,7 @@ fun main(args: Array) { @TestOnly internal fun transformArgs(args: List, messageCollector: MessageCollector, isTest: Boolean): List { val parseErrors = ArgumentParseErrors() - val kotlincTransformed = preprocessCommandLineArguments(args, parseErrors) + val kotlincTransformed = preprocessCommandLineArguments(args, lazy { parseErrors }) val errorMessage = validateArguments(parseErrors) if (errorMessage != null) {