Reduce memory footprint for basic case of flexible types

The case is when we have a simple type constructor
(not array nor collection) and type is not raw:
A<T1, >..A<T1>?

Actually these types are almost equal besides of nullability,
but we create and resolve two different simple types,
they have different arguments' lists, etc.

The idea is to add NullableSimpleType subclass with delegate
to another simple type

It should help a lot both with common cases and with corner ones:
flexibles types in spark for Function22 having exponential size
because of its flexibility

 #KT-14375 Fixed
 #KT-14323 Fixed
This commit is contained in:
Denis Zharkov
2017-05-24 15:25:49 +03:00
parent 59f2ba98a6
commit 4385ce8828
4 changed files with 105 additions and 10 deletions
@@ -90,11 +90,11 @@ class JavaTypeResolver(
return computeSimpleJavaClassifierType(javaType, attr) ?: errorType()
}
fun computeBound(flexibility: JavaTypeFlexibility) =
computeSimpleJavaClassifierType(javaType, attr.withFlexibility(flexibility))
fun computeBound(flexibility: JavaTypeFlexibility, lowerResult: SimpleType? = null) =
computeSimpleJavaClassifierType(javaType, attr.withFlexibility(flexibility), lowerResult)
val lower = computeBound(FLEXIBLE_LOWER_BOUND) ?: return errorType()
val upper = computeBound(FLEXIBLE_UPPER_BOUND) ?: return errorType()
val upper = computeBound(FLEXIBLE_UPPER_BOUND, lowerResult = lower) ?: return errorType()
return if (isRaw) {
RawTypeImpl(lower, upper)
@@ -104,12 +104,21 @@ class JavaTypeResolver(
}
}
private fun computeSimpleJavaClassifierType(javaType: JavaClassifierType, attr: JavaTypeAttributes): SimpleType? {
val annotations = LazyJavaAnnotations(c, javaType)
private fun computeSimpleJavaClassifierType(
javaType: JavaClassifierType, attr: JavaTypeAttributes,
lowerResult: SimpleType? = null
): SimpleType? {
val annotations =
lowerResult?.annotations ?: LazyJavaAnnotations(c, javaType)
val constructor = computeTypeConstructor(javaType, attr) ?: return null
val arguments = computeArguments(javaType, attr, constructor)
val isNullable = attr.isNullable()
if (lowerResult?.constructor == constructor && !javaType.isRaw && isNullable) {
return lowerResult.makeNullableAsSpecified(true)
}
val arguments = computeArguments(javaType, attr, constructor)
return KotlinTypeFactory.simpleType(annotations, constructor, arguments, isNullable)
}
@@ -340,4 +349,3 @@ internal fun TypeParameterDescriptor.getErasedUpperBound(
return defaultValue()
}
@@ -89,10 +89,18 @@ private class SimpleTypeImpl(
override val memberScope: MemberScope
) : SimpleType() {
override fun replaceAnnotations(newAnnotations: Annotations) =
SimpleTypeImpl(newAnnotations, constructor, arguments, isMarkedNullable, memberScope)
if (newAnnotations === annotations)
this
else
SimpleTypeImpl(newAnnotations, constructor, arguments, isMarkedNullable, memberScope)
override fun makeNullableAsSpecified(newNullability: Boolean) =
SimpleTypeImpl(annotations, constructor, arguments, newNullability, memberScope)
if (newNullability == isMarkedNullable)
this
else if (newNullability)
NullableSimpleType(this)
else
SimpleTypeImpl(annotations, constructor, arguments, newNullability, memberScope)
init {
if (memberScope is ErrorUtils.ErrorScope) {
@@ -100,3 +108,19 @@ private class SimpleTypeImpl(
}
}
}
private class NullableSimpleType(override val delegate: SimpleType) : DelegatingSimpleType() {
override val isMarkedNullable: Boolean
get() = true
override fun replaceAnnotations(newAnnotations: Annotations) =
if (newAnnotations !== delegate.annotations)
NullableSimpleType(delegate.replaceAnnotations(newAnnotations))
else
this
override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType {
if (newNullability) return this
return delegate.makeNullableAsSpecified(newNullability)
}
}
@@ -53,6 +53,9 @@ object StrictEqualityTypeChecker {
) {
return false
}
if (a.arguments === b.arguments) return true
for (i in a.arguments.indices) {
val aArg = a.arguments[i]
val bArg = b.arguments[i]
@@ -322,6 +325,9 @@ object NewKotlinTypeChecker : KotlinTypeChecker {
capturedSubArguments: List<TypeProjection>,
superType: SimpleType
): Boolean {
if (capturedSubArguments === superType.arguments) return true
val parameters = superType.constructor.parameters
for (index in parameters.indices) {
@@ -433,4 +439,4 @@ val SimpleType.isSingleClassifierType: Boolean
(constructor.declarationDescriptor != null || this is CapturedType || this is NewCapturedType)
val SimpleType.isIntersectionType: Boolean
get() = constructor is IntersectionTypeConstructor
get() = constructor is IntersectionTypeConstructor