From fa2577dda8e1f9b5a93c6440cd5e34cb9d7cf1dd Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Fri, 17 Sep 2021 09:58:36 +0200 Subject: [PATCH] [Commonizer] Improve performance of deserialization ^KT-48288 --- .../kotlin/commonizer/cir/CirAnnotation.kt | 24 +++++++------------ .../CirTreePackageDeserializer.kt | 11 ++++----- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/native/commonizer/src/org/jetbrains/kotlin/commonizer/cir/CirAnnotation.kt b/native/commonizer/src/org/jetbrains/kotlin/commonizer/cir/CirAnnotation.kt index eeec33d28de..80187f61102 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/commonizer/cir/CirAnnotation.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/commonizer/cir/CirAnnotation.kt @@ -36,28 +36,20 @@ private data class CirAnnotationInternedImpl( override val constantValueArguments: Map, override val annotationValueArguments: Map ) : CirAnnotation { - // See also org.jetbrains.kotlin.types.KotlinType.cachedHashCode - private var cachedHashCode = 0 - - private fun computeHashCode() = hashCode(type) + private var hashCode = hashCode(type) .appendHashCode(constantValueArguments) .appendHashCode(annotationValueArguments) - override fun hashCode(): Int { - var currentHashCode = cachedHashCode - if (currentHashCode != 0) return currentHashCode - currentHashCode = computeHashCode() - cachedHashCode = currentHashCode - return currentHashCode - } + override fun hashCode(): Int = hashCode override fun equals(other: Any?): Boolean { if (other === this) return true - - return other is CirAnnotation - && type == other.type - && constantValueArguments == other.constantValueArguments - && annotationValueArguments == other.annotationValueArguments + if (other is CirAnnotationInternedImpl && other.hashCode != this.hashCode) return false + if (other !is CirAnnotation) return false + if (other.type != this.type) return false + if (other.constantValueArguments != this.constantValueArguments) return false + if (other.annotationValueArguments != this.annotationValueArguments) return false + return true } } diff --git a/native/commonizer/src/org/jetbrains/kotlin/commonizer/tree/deserializer/CirTreePackageDeserializer.kt b/native/commonizer/src/org/jetbrains/kotlin/commonizer/tree/deserializer/CirTreePackageDeserializer.kt index 254bc22611c..8d7dc32f55b 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/commonizer/tree/deserializer/CirTreePackageDeserializer.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/commonizer/tree/deserializer/CirTreePackageDeserializer.kt @@ -24,20 +24,17 @@ internal class CirTreePackageDeserializer( ): CirTreePackage { val pkg = CirPackage.create(packageName) - val properties = fragments.asSequence().mapNotNull { it.pkg } + val properties = fragments.mapNotNull { it.pkg } .flatMap { it.properties } .mapNotNull { property -> propertyDeserializer(property, null, typeResolver) } - .toList() - val functions = fragments.asSequence().mapNotNull { it.pkg } + val functions = fragments.mapNotNull { it.pkg } .flatMap { it.functions } .mapNotNull { function -> functionDeserializer(function, null, typeResolver) } - .toList() - val typeAliases = fragments.asSequence().mapNotNull { it.pkg } + val typeAliases = fragments.mapNotNull { it.pkg } .flatMap { it.typeAliases } - .mapNotNull { typeAlias -> typeAliasDeserializer(typeAlias, pkg.packageName, typeResolver) } - .toList() + .map { typeAlias -> typeAliasDeserializer(typeAlias, pkg.packageName, typeResolver) } val classes = ClassesToProcess() .apply { fragments.forEach { fragment -> addClassesFromFragment(fragment) } }