[FIR] Get rid of unnecessary allocations

This commit is contained in:
Ivan Kochurkin
2021-10-27 18:25:11 +03:00
committed by teamcity
parent c9ad2b3bf9
commit 0dab39b6e3
9 changed files with 46 additions and 25 deletions
@@ -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<ConeKotlinType, MutableList<AnnotationUseSiteTarget?>>()
val session = context.session
val annotations = declaration.annotations
for (annotation in annotations) {
val classId = annotation.classId ?: continue
val annotationClassId = annotation.toAnnotationClassId() ?: continue
@@ -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)
@@ -21,9 +21,12 @@ class PropertyInitializationInfoCollector(
node: CFGNode<*>,
data: Collection<Pair<EdgeLabel, PathAwarePropertyInitializationInfo>>
): 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(
@@ -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(
@@ -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<ConeKotlinType>()
fun collectAllTypes(type: ConeKotlinType) {
if (usedTypes.add(type)) {
@@ -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<ConeKotlinType, MutableList<AnnotationUseSiteTarget?>>()
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() }
@@ -29,8 +29,10 @@ object FirUnderscoreChecker : FirBasicExpressionChecker() {
checkResolvedToUnderscoreNamedCatchParameter(expression, context, reporter)
}
is FirResolvedQualifier -> {
for (reservedUnderscoreDiagnostic in expression.nonFatalDiagnostics.filterIsInstance<ConeUnderscoreUsageWithoutBackticks>()) {
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)
}
}
}
}
@@ -41,16 +41,24 @@ abstract class AbstractDiagnosticCollector(
private val SUPPRESS_NAMES_NAME = Name.identifier("names")
fun getDiagnosticsSuppressedForContainer(annotationContainer: FirAnnotationContainer): List<String>? {
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<String>? = 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
}
}
}
@@ -24,7 +24,7 @@ class JavaClassMembersEnhancementScope(
private val overriddenFunctions = mutableMapOf<FirNamedFunctionSymbol, Collection<FirNamedFunctionSymbol>>()
private val overriddenProperties = mutableMapOf<FirPropertySymbol, Collection<FirPropertySymbol>>()
private val overrideBindCache = mutableMapOf<Name, Map<FirCallableSymbol<*>?, List<FirCallableSymbol<*>>>>()
private val overrideBindCache = mutableMapOf<Name, Map<FirCallableSymbol<*>?, List<FirCallableDeclaration>>>()
private val signatureEnhancement = FirSignatureEnhancement(owner.fir, session) {
overriddenMembers(name)
}
@@ -66,12 +66,14 @@ class JavaClassMembersEnhancementScope(
private fun FirCallableDeclaration.overriddenMembers(name: Name): List<FirCallableDeclaration> {
val backMap = overrideBindCache.getOrPut(name) {
useSiteMemberScope
.overrideByBase
.toList()
.groupBy({ (_, key) -> key }, { (value) -> value })
val result = mutableMapOf<FirCallableSymbol<*>?, MutableList<FirCallableDeclaration>>()
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) {