From f454858bbb58f4127f87f2e020ee315929e3c808 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Tue, 18 Jul 2017 12:25:58 +0300 Subject: [PATCH] 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 --- .../kotlin/types/KotlinTypeFactory.kt | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt index 0d68f304b61..adc7f85fb55 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt @@ -45,7 +45,7 @@ object KotlinTypeFactory { return constructor.declarationDescriptor!!.defaultType } - return SimpleTypeImpl(annotations, constructor, arguments, nullable, computeMemberScope(constructor, arguments)) + return simpleType(annotations, constructor, arguments, nullable, computeMemberScope(constructor, arguments)) } @JvmStatic @@ -55,14 +55,21 @@ object KotlinTypeFactory { arguments: List, nullable: Boolean, 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 fun simpleNotNullType( annotations: Annotations, descriptor: ClassDescriptor, arguments: List - ): SimpleType = SimpleTypeImpl(annotations, descriptor.typeConstructor, arguments, false, descriptor.getMemberScope(arguments)) + ): SimpleType = simpleType(annotations, descriptor.typeConstructor, arguments, false, descriptor.getMemberScope(arguments)) @JvmStatic fun simpleType( @@ -82,17 +89,18 @@ object KotlinTypeFactory { } private class SimpleTypeImpl( - override val annotations: Annotations, override val constructor: TypeConstructor, override val arguments: List, override val isMarkedNullable: Boolean, override val memberScope: MemberScope ) : SimpleType() { + override val annotations: Annotations get() = Annotations.EMPTY + override fun replaceAnnotations(newAnnotations: Annotations) = - if (newAnnotations === annotations) + if (newAnnotations.isEmpty()) this else - SimpleTypeImpl(newAnnotations, constructor, arguments, isMarkedNullable, memberScope) + AnnotatedSimpleType(this, newAnnotations) override fun makeNullableAsSpecified(newNullability: Boolean) = if (newNullability == isMarkedNullable) @@ -100,7 +108,7 @@ private class SimpleTypeImpl( else if (newNullability) NullableSimpleType(this) else - SimpleTypeImpl(annotations, constructor, arguments, newNullability, memberScope) + NotNullSimpleType(this) init { if (memberScope is ErrorUtils.ErrorScope) { @@ -109,18 +117,30 @@ private class SimpleTypeImpl( } } -private class NullableSimpleType(override val delegate: SimpleType) : DelegatingSimpleType() { - override val isMarkedNullable: Boolean - get() = true - +abstract class DelegatingSimpleTypeImpl(override val delegate: SimpleType) : DelegatingSimpleType() { override fun replaceAnnotations(newAnnotations: Annotations) = - if (newAnnotations !== delegate.annotations) - NullableSimpleType(delegate.replaceAnnotations(newAnnotations)) + if (newAnnotations !== annotations) + AnnotatedSimpleType(this, newAnnotations) else this override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType { - if (newNullability) return this - return delegate.makeNullableAsSpecified(newNullability) + if (newNullability == isMarkedNullable) return this + 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 +}