FIR: Introduce smart containers in supertypes search

assuming that we may have quite many searches resulting
in a single supertypes, smart containers here reduce
allocations.
This commit is contained in:
Ilya Chernikov
2021-03-13 16:47:55 +01:00
parent 038a8af80e
commit 7ed35e5c2b
@@ -18,6 +18,8 @@ import org.jetbrains.kotlin.fir.typeContext
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.types.model.CaptureStatus
import org.jetbrains.kotlin.utils.SmartList
import org.jetbrains.kotlin.utils.SmartSet
abstract class SupertypeSupplier {
abstract fun forClass(firClass: FirClass<*>): List<ConeClassLikeType>
@@ -37,13 +39,13 @@ fun lookupSuperTypes(
supertypeSupplier: SupertypeSupplier = SupertypeSupplier.Default,
substituteTypes: Boolean = false
): List<ConeClassLikeType> {
return mutableListOf<ConeClassLikeType>().also {
klass.symbol.collectSuperTypes(it, mutableSetOf(), deep, lookupInterfaces, substituteTypes, useSiteSession, supertypeSupplier)
return SmartList<ConeClassLikeType>().also {
klass.symbol.collectSuperTypes(it, SmartSet.create(), deep, lookupInterfaces, substituteTypes, useSiteSession, supertypeSupplier)
}
}
fun FirClass<*>.isThereLoopInSupertypes(session: FirSession): Boolean {
val visitedSymbols: MutableSet<FirClassifierSymbol<*>> = mutableSetOf()
val visitedSymbols: MutableSet<FirClassifierSymbol<*>> = SmartSet.create()
val inProcess: MutableSet<FirClassifierSymbol<*>> = mutableSetOf()
var isThereLoop = false
@@ -82,8 +84,8 @@ fun lookupSuperTypes(
useSiteSession: FirSession,
supertypeSupplier: SupertypeSupplier = SupertypeSupplier.Default
): List<ConeClassLikeType> {
return mutableListOf<ConeClassLikeType>().also {
symbol.collectSuperTypes(it, mutableSetOf(), deep, lookupInterfaces, false, useSiteSession, supertypeSupplier)
return SmartList<ConeClassLikeType>().also {
symbol.collectSuperTypes(it, SmartSet.create(), deep, lookupInterfaces, false, useSiteSession, supertypeSupplier)
}
}
@@ -180,7 +182,7 @@ private fun FirClassifierSymbol<*>.collectSuperTypes(
superClassTypes.forEach {
if (it !is ConeClassErrorType) {
if (substituteSuperTypes) {
val substitutedTypes = mutableListOf<ConeClassLikeType>()
val substitutedTypes = SmartList<ConeClassLikeType>()
it.lookupTag.toSymbol(useSiteSession)?.collectSuperTypes(
substitutedTypes,
visitedSymbols,