Optimize memory footprint for case of empty annotations in types

It seems that most of the simple types don't have any annotations,
so for that case it's worth removing the field from SimpleTypeImpl

Also introduce a NotNullSimpleType being used when replacing nullability
This commit is contained in:
Denis Zharkov
2017-07-18 12:25:58 +03:00
parent f8a12a0ec3
commit f454858bbb
@@ -45,7 +45,7 @@ object KotlinTypeFactory {
return constructor.declarationDescriptor!!.defaultType return constructor.declarationDescriptor!!.defaultType
} }
return SimpleTypeImpl(annotations, constructor, arguments, nullable, computeMemberScope(constructor, arguments)) return simpleType(annotations, constructor, arguments, nullable, computeMemberScope(constructor, arguments))
} }
@JvmStatic @JvmStatic
@@ -55,14 +55,21 @@ object KotlinTypeFactory {
arguments: List<TypeProjection>, arguments: List<TypeProjection>,
nullable: Boolean, nullable: Boolean,
memberScope: MemberScope memberScope: MemberScope
): SimpleType = SimpleTypeImpl(annotations, constructor, arguments, nullable, memberScope) ): SimpleType =
SimpleTypeImpl(constructor, arguments, nullable, memberScope)
.let {
if (annotations.isEmpty())
it
else
AnnotatedSimpleType(it, annotations)
}
@JvmStatic @JvmStatic
fun simpleNotNullType( fun simpleNotNullType(
annotations: Annotations, annotations: Annotations,
descriptor: ClassDescriptor, descriptor: ClassDescriptor,
arguments: List<TypeProjection> arguments: List<TypeProjection>
): SimpleType = SimpleTypeImpl(annotations, descriptor.typeConstructor, arguments, false, descriptor.getMemberScope(arguments)) ): SimpleType = simpleType(annotations, descriptor.typeConstructor, arguments, false, descriptor.getMemberScope(arguments))
@JvmStatic @JvmStatic
fun simpleType( fun simpleType(
@@ -82,17 +89,18 @@ object KotlinTypeFactory {
} }
private class SimpleTypeImpl( private class SimpleTypeImpl(
override val annotations: Annotations,
override val constructor: TypeConstructor, override val constructor: TypeConstructor,
override val arguments: List<TypeProjection>, override val arguments: List<TypeProjection>,
override val isMarkedNullable: Boolean, override val isMarkedNullable: Boolean,
override val memberScope: MemberScope override val memberScope: MemberScope
) : SimpleType() { ) : SimpleType() {
override val annotations: Annotations get() = Annotations.EMPTY
override fun replaceAnnotations(newAnnotations: Annotations) = override fun replaceAnnotations(newAnnotations: Annotations) =
if (newAnnotations === annotations) if (newAnnotations.isEmpty())
this this
else else
SimpleTypeImpl(newAnnotations, constructor, arguments, isMarkedNullable, memberScope) AnnotatedSimpleType(this, newAnnotations)
override fun makeNullableAsSpecified(newNullability: Boolean) = override fun makeNullableAsSpecified(newNullability: Boolean) =
if (newNullability == isMarkedNullable) if (newNullability == isMarkedNullable)
@@ -100,7 +108,7 @@ private class SimpleTypeImpl(
else if (newNullability) else if (newNullability)
NullableSimpleType(this) NullableSimpleType(this)
else else
SimpleTypeImpl(annotations, constructor, arguments, newNullability, memberScope) NotNullSimpleType(this)
init { init {
if (memberScope is ErrorUtils.ErrorScope) { if (memberScope is ErrorUtils.ErrorScope) {
@@ -109,18 +117,30 @@ private class SimpleTypeImpl(
} }
} }
private class NullableSimpleType(override val delegate: SimpleType) : DelegatingSimpleType() { abstract class DelegatingSimpleTypeImpl(override val delegate: SimpleType) : DelegatingSimpleType() {
override val isMarkedNullable: Boolean
get() = true
override fun replaceAnnotations(newAnnotations: Annotations) = override fun replaceAnnotations(newAnnotations: Annotations) =
if (newAnnotations !== delegate.annotations) if (newAnnotations !== annotations)
NullableSimpleType(delegate.replaceAnnotations(newAnnotations)) AnnotatedSimpleType(this, newAnnotations)
else else
this this
override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType { override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType {
if (newNullability) return this if (newNullability == isMarkedNullable) return this
return delegate.makeNullableAsSpecified(newNullability) return delegate.makeNullableAsSpecified(newNullability).replaceAnnotations(annotations)
} }
} }
private class AnnotatedSimpleType(
delegate: SimpleType,
override val annotations: Annotations
) : DelegatingSimpleTypeImpl(delegate)
private class NullableSimpleType(delegate: SimpleType) : DelegatingSimpleTypeImpl(delegate) {
override val isMarkedNullable: Boolean
get() = true
}
private class NotNullSimpleType(delegate: SimpleType) : DelegatingSimpleTypeImpl(delegate) {
override val isMarkedNullable: Boolean
get() = false
}