[Commonizer] Apply interner to avoid duplicated FqName objects
This commit is contained in:
+2
-1
@@ -6,12 +6,13 @@
|
||||
package org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.constants.*
|
||||
|
||||
class CirAnnotation(original: AnnotationDescriptor) {
|
||||
val fqName: FqName = original.fqName ?: error("Annotation with no FQ name: ${original::class.java}, $original")
|
||||
val fqName: FqName = original.fqName?.intern() ?: error("Annotation with no FQ name: ${original::class.java}, $original")
|
||||
val allValueArguments: Map<Name, ConstantValue<*>> = original.allValueArguments
|
||||
|
||||
init {
|
||||
|
||||
+2
-1
@@ -6,6 +6,7 @@
|
||||
package org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
@@ -65,7 +66,7 @@ class CirClassImpl(original: ClassDescriptor) : CirClass {
|
||||
override val annotations = original.annotations.map(::CirAnnotation)
|
||||
override val name = original.name
|
||||
override val typeParameters = original.declaredTypeParameters.map(::CirTypeParameterImpl)
|
||||
override val companion = original.companionObjectDescriptor?.fqNameSafe
|
||||
override val companion = original.companionObjectDescriptor?.fqNameSafe?.intern()
|
||||
override val kind = original.kind
|
||||
override val modality = original.modality
|
||||
override val visibility = original.visibility
|
||||
|
||||
+3
-2
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir.CirSimpleTypeKi
|
||||
import org.jetbrains.kotlin.descriptors.commonizer.utils.declarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.commonizer.utils.fqName
|
||||
import org.jetbrains.kotlin.descriptors.commonizer.utils.fqNameWithTypeParameters
|
||||
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.types.*
|
||||
|
||||
@@ -52,7 +53,7 @@ class CirSimpleType(original: SimpleType) : CirType() {
|
||||
|
||||
annotations = abbreviation.annotations.map(::CirAnnotation)
|
||||
kind = CirSimpleTypeKind.determineKind(abbreviation.declarationDescriptor)
|
||||
fqName = abbreviation.fqName
|
||||
fqName = abbreviation.fqNameInterned
|
||||
arguments = abbreviation.arguments.map(::CirTypeProjection)
|
||||
isMarkedNullable = abbreviation.isMarkedNullable
|
||||
isDefinitelyNotNullType = abbreviation.isDefinitelyNotNullType
|
||||
@@ -85,7 +86,7 @@ enum class CirSimpleTypeKind {
|
||||
}
|
||||
|
||||
data class CirTypeConstructorId(val fqName: FqName, val numberOfTypeParameters: Int) {
|
||||
constructor(type: SimpleType) : this(type.fqName, type.constructor.parameters.size)
|
||||
constructor(type: SimpleType) : this(type.fqNameInterned, type.constructor.parameters.size)
|
||||
}
|
||||
|
||||
class CirTypeProjection(original: TypeProjection) {
|
||||
|
||||
+3
-2
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.descriptors.commonizer.core.*
|
||||
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir.CirRootNode.ClassifiersCacheImpl
|
||||
import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroup
|
||||
import org.jetbrains.kotlin.descriptors.commonizer.utils.firstNonNull
|
||||
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
@@ -100,7 +101,7 @@ internal fun buildClassNode(
|
||||
recursionMarker = CirClassRecursionMarker,
|
||||
nodeProducer = ::CirClassNode
|
||||
).also { node ->
|
||||
classes.firstNonNull().fqNameSafe.let { fqName ->
|
||||
classes.firstNonNull().fqNameSafe.intern().let { fqName ->
|
||||
node.fqName = fqName
|
||||
cacheRW.classes.putSafe(fqName, node)
|
||||
}
|
||||
@@ -132,7 +133,7 @@ internal fun buildTypeAliasNode(
|
||||
recursionMarker = CirClassRecursionMarker,
|
||||
nodeProducer = ::CirTypeAliasNode
|
||||
).also { node ->
|
||||
typeAliases.firstNonNull().fqNameSafe.let { fqName ->
|
||||
typeAliases.firstNonNull().fqNameSafe.intern().let { fqName ->
|
||||
node.fqName = fqName
|
||||
cacheRW.typeAliases.putSafe(fqName, node)
|
||||
}
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ internal fun mergeModules(
|
||||
|
||||
modules.forEachIndexed { index, module ->
|
||||
module?.collectNonEmptyPackageMemberScopes { packageFqName, memberScope ->
|
||||
packageMemberScopesMap[packageFqName][index] = memberScope
|
||||
packageMemberScopesMap[packageFqName.intern()][index] = memberScope
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ package org.jetbrains.kotlin.descriptors.commonizer.utils
|
||||
|
||||
import java.util.WeakHashMap
|
||||
|
||||
internal class Interner<T : Any> {
|
||||
internal class NonThreadSafeInterner<T : Any> {
|
||||
private val pool = WeakHashMap<T, T>()
|
||||
|
||||
fun intern(value: T): T = pool.computeIfAbsent(value) { value }
|
||||
@@ -17,8 +17,10 @@ import kotlin.collections.HashSet
|
||||
internal inline val KotlinType.declarationDescriptor: ClassifierDescriptor
|
||||
get() = (constructor.declarationDescriptor ?: error("No declaration descriptor found for $constructor"))
|
||||
|
||||
internal inline val KotlinType.fqName: FqName
|
||||
get() = declarationDescriptor.fqNameSafe
|
||||
internal inline val KotlinType.fqNameInterned: FqName
|
||||
get() = declarationDescriptor.fqNameSafe.intern()
|
||||
|
||||
internal fun FqName.intern(): FqName = fqNameInterner.intern(this)
|
||||
|
||||
internal val KotlinType.fqNameWithTypeParameters: String
|
||||
get() {
|
||||
@@ -27,7 +29,7 @@ internal val KotlinType.fqNameWithTypeParameters: String
|
||||
}
|
||||
|
||||
private fun StringBuilder.buildFqNameWithTypeParameters(type: KotlinType, exploredTypeParameters: MutableSet<KotlinType>) {
|
||||
append(type.fqName)
|
||||
append(type.fqNameInterned)
|
||||
|
||||
val typeParameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(type)
|
||||
if (typeParameterDescriptor != null) {
|
||||
@@ -70,4 +72,6 @@ private fun StringBuilder.buildFqNameWithTypeParameters(type: KotlinType, explor
|
||||
}
|
||||
|
||||
// dedicated to hold unique entries of "fqNameWithTypeParameters"
|
||||
private val stringInterner = Interner<String>()
|
||||
private val stringInterner = NonThreadSafeInterner<String>()
|
||||
|
||||
private val fqNameInterner = NonThreadSafeInterner<FqName>()
|
||||
|
||||
+2
-2
@@ -426,8 +426,8 @@ internal class ComparingDeclarationsVisitor(
|
||||
context.nextLevel("Unwrapped/unabbreviated type annotations")
|
||||
)
|
||||
|
||||
val expectedFqName = expectedUnwrapped.fqName
|
||||
val actualFqName = actualUnwrapped.fqName
|
||||
val expectedFqName = expectedUnwrapped.fqNameInterned
|
||||
val actualFqName = actualUnwrapped.fqNameInterned
|
||||
|
||||
context.assertEquals(expectedFqName, actualFqName, "type FQN")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user