From 0dab39b6e374d674b9b2b9862eef56a73b01f24e Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Wed, 27 Oct 2021 18:25:11 +0300 Subject: [PATCH] [FIR] Get rid of unnecessary allocations --- .../FirRepeatableAnnotationChecker.kt | 3 ++- .../cfa/FirPropertyInitializationAnalyzer.kt | 2 +- .../PropertyInitializationInfoCollector.kt | 9 ++++--- .../analysis/checkers/FirUnderscoreHelpers.kt | 4 ++- .../FirPropertyTypeParametersChecker.kt | 2 +- .../FirExpressionAnnotationChecker.kt | 5 +++- .../expression/FirUnderscoreChecker.kt | 6 +++-- .../collectors/AbstractDiagnosticCollector.kt | 26 ++++++++++++------- .../JavaClassMembersEnhancementScope.kt | 14 +++++----- 9 files changed, 46 insertions(+), 25 deletions(-) diff --git a/compiler/fir/checkers/checkers.jvm/src/org/jetbrains/kotlin/fir/analysis/jvm/checkers/declaration/FirRepeatableAnnotationChecker.kt b/compiler/fir/checkers/checkers.jvm/src/org/jetbrains/kotlin/fir/analysis/jvm/checkers/declaration/FirRepeatableAnnotationChecker.kt index 4b2b8bf323a..fee3c58a854 100644 --- a/compiler/fir/checkers/checkers.jvm/src/org/jetbrains/kotlin/fir/analysis/jvm/checkers/declaration/FirRepeatableAnnotationChecker.kt +++ b/compiler/fir/checkers/checkers.jvm/src/org/jetbrains/kotlin/fir/analysis/jvm/checkers/declaration/FirRepeatableAnnotationChecker.kt @@ -40,10 +40,11 @@ object FirRepeatableAnnotationChecker : FirBasicDeclarationChecker() { private val REPEATABLE_ANNOTATION_CONTAINER_NAME = Name.identifier(JvmAbi.REPEATABLE_ANNOTATION_CONTAINER_NAME) override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) { + val annotations = declaration.annotations + if (annotations.isEmpty()) return val annotationsMap = hashMapOf>() val session = context.session - val annotations = declaration.annotations for (annotation in annotations) { val classId = annotation.classId ?: continue val annotationClassId = annotation.toAnnotationClassId() ?: continue diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirPropertyInitializationAnalyzer.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirPropertyInitializationAnalyzer.kt index 9865bd5af1c..9f287ee7ab8 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirPropertyInitializationAnalyzer.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirPropertyInitializationAnalyzer.kt @@ -40,7 +40,7 @@ object FirPropertyInitializationAnalyzer : AbstractFirPropertyInitializationChec symbolFir == null || symbolFir.initializer == null && symbolFir.delegate == null } - val localProperties = properties.filter { it.fir.initializer == null && it.fir.delegate == null }.toSet() + val localProperties = properties.filterTo(mutableSetOf()) { it.fir.initializer == null && it.fir.delegate == null } val reporterVisitor = PropertyReporter(localData, localProperties, capturedWrites, reporter, context) graph.traverse(TraverseDirection.Forward, reporterVisitor) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PropertyInitializationInfoCollector.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PropertyInitializationInfoCollector.kt index af4b74b9c39..9dd36780e4d 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PropertyInitializationInfoCollector.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PropertyInitializationInfoCollector.kt @@ -21,9 +21,12 @@ class PropertyInitializationInfoCollector( node: CFGNode<*>, data: Collection> ): PathAwarePropertyInitializationInfo { - if (data.isEmpty()) return PathAwarePropertyInitializationInfo.EMPTY - return data.map { (label, info) -> info.applyLabel(node, label) } - .reduce(PathAwarePropertyInitializationInfo::merge) + var result: PathAwarePropertyInitializationInfo? = null + for ((label, info) in data) { + val resultItem = info.applyLabel(node, label) + result = result?.merge(resultItem) ?: resultItem + } + return result ?: PathAwarePropertyInitializationInfo.EMPTY } override fun visitVariableAssignmentNode( diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirUnderscoreHelpers.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirUnderscoreHelpers.kt index 57d55ad3474..1de17431ad8 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirUnderscoreHelpers.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirUnderscoreHelpers.kt @@ -19,7 +19,9 @@ fun checkUnderscoreDiagnostics( reporter: DiagnosticReporter, isExpression: Boolean ) { - if (source != null && (source.kind is KtRealSourceElementKind || source.kind is KtFakeSourceElementKind.ReferenceInAtomicQualifiedAccess)) { + val sourceKind = source?.kind ?: return + + if (sourceKind is KtRealSourceElementKind || sourceKind is KtFakeSourceElementKind.ReferenceInAtomicQualifiedAccess) { with(SourceNavigator.forSource(source)) { if (source.getRawIdentifier()?.isUnderscore == true) { reporter.reportOn( diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPropertyTypeParametersChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPropertyTypeParametersChecker.kt index a3cc86486fe..92dda4ceea9 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPropertyTypeParametersChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPropertyTypeParametersChecker.kt @@ -18,7 +18,7 @@ import org.jetbrains.kotlin.fir.types.type object FirPropertyTypeParametersChecker : FirPropertyChecker() { override fun check(declaration: FirProperty, context: CheckerContext, reporter: DiagnosticReporter) { - val boundsByName = declaration.typeParameters.map { it.name to it.bounds }.toMap() + val boundsByName = declaration.typeParameters.associate { it.name to it.bounds } val usedTypes = HashSet() fun collectAllTypes(type: ConeKotlinType) { if (usedTypes.add(type)) { diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExpressionAnnotationChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExpressionAnnotationChecker.kt index ff5b7800e6b..1eb46108644 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExpressionAnnotationChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExpressionAnnotationChecker.kt @@ -32,9 +32,12 @@ object FirExpressionAnnotationChecker : FirBasicExpressionChecker() { expression is FirBlock && expression.source?.kind == KtFakeSourceElementKind.DesugaredForLoop ) return + val annotations = expression.annotations + if (annotations.isEmpty()) return + val annotationsMap = hashMapOf>() - for (annotation in expression.annotations) { + for (annotation in annotations) { val useSiteTarget = annotation.useSiteTarget ?: expression.getDefaultUseSiteTarget(annotation, context) val existingTargetsForAnnotation = annotationsMap.getOrPut(annotation.annotationTypeRef.coneType) { arrayListOf() } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUnderscoreChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUnderscoreChecker.kt index 8b4b6686ca9..73be9769dc3 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUnderscoreChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUnderscoreChecker.kt @@ -29,8 +29,10 @@ object FirUnderscoreChecker : FirBasicExpressionChecker() { checkResolvedToUnderscoreNamedCatchParameter(expression, context, reporter) } is FirResolvedQualifier -> { - for (reservedUnderscoreDiagnostic in expression.nonFatalDiagnostics.filterIsInstance()) { - reporter.reportOn(reservedUnderscoreDiagnostic.source, FirErrors.UNDERSCORE_USAGE_WITHOUT_BACKTICKS, context) + for (reservedUnderscoreDiagnostic in expression.nonFatalDiagnostics) { + if (reservedUnderscoreDiagnostic is ConeUnderscoreUsageWithoutBackticks) { + reporter.reportOn(reservedUnderscoreDiagnostic.source, FirErrors.UNDERSCORE_USAGE_WITHOUT_BACKTICKS, context) + } } } } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/AbstractDiagnosticCollector.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/AbstractDiagnosticCollector.kt index 3314f97d19f..7ab8a4c40ba 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/AbstractDiagnosticCollector.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/AbstractDiagnosticCollector.kt @@ -41,16 +41,24 @@ abstract class AbstractDiagnosticCollector( private val SUPPRESS_NAMES_NAME = Name.identifier("names") fun getDiagnosticsSuppressedForContainer(annotationContainer: FirAnnotationContainer): List? { - val annotations = annotationContainer.annotations.filter { - val type = it.annotationTypeRef.coneType as? ConeClassLikeType ?: return@filter false - type.lookupTag.classId == StandardClassIds.Annotations.Suppress - } - if (annotations.isEmpty()) return null - return annotations.flatMap { annotationCall -> - annotationCall.findArgumentByName(SUPPRESS_NAMES_NAME)?.unwrapVarargValue()?.mapNotNull { - (it as? FirConstExpression<*>)?.value as? String? - } ?: emptyList() + var result: MutableList? = null + + for (annotation in annotationContainer.annotations) { + val type = annotation.annotationTypeRef.coneType as? ConeClassLikeType ?: continue + if (type.lookupTag.classId != StandardClassIds.Annotations.Suppress) continue + val argumentValues = annotation.findArgumentByName(SUPPRESS_NAMES_NAME)?.unwrapVarargValue() ?: continue + + for (argumentValue in argumentValues) { + val value = (argumentValue as? FirConstExpression<*>)?.value as? String ?: continue + + if (result == null) { + result = mutableListOf() + } + result.add(value) + } } + + return result } } } diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassMembersEnhancementScope.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassMembersEnhancementScope.kt index ff7e7d66c7e..a0fb2bdb350 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassMembersEnhancementScope.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassMembersEnhancementScope.kt @@ -24,7 +24,7 @@ class JavaClassMembersEnhancementScope( private val overriddenFunctions = mutableMapOf>() private val overriddenProperties = mutableMapOf>() - private val overrideBindCache = mutableMapOf?, List>>>() + private val overrideBindCache = mutableMapOf?, List>>() private val signatureEnhancement = FirSignatureEnhancement(owner.fir, session) { overriddenMembers(name) } @@ -66,12 +66,14 @@ class JavaClassMembersEnhancementScope( private fun FirCallableDeclaration.overriddenMembers(name: Name): List { val backMap = overrideBindCache.getOrPut(name) { - useSiteMemberScope - .overrideByBase - .toList() - .groupBy({ (_, key) -> key }, { (value) -> value }) + val result = mutableMapOf?, MutableList>() + for ((key, value) in useSiteMemberScope.overrideByBase) { + val resultItem = result.getOrPut(value) { mutableListOf() } + resultItem.add(key.fir) + } + result } - return backMap[this.symbol]?.map { it.fir } ?: emptyList() + return backMap[this.symbol] ?: emptyList() } override fun processClassifiersByNameWithSubstitution(name: Name, processor: (FirClassifierSymbol<*>, ConeSubstitutor) -> Unit) {