From 30ab11575dbc52e85f4f555022fc25b8e90506a0 Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Mon, 3 Jun 2019 10:20:26 +0200 Subject: [PATCH] Jvm Ir. Optimization: avoid 'IrClass.functions' call if it not necessary --- .../jvm/lower/CollectionStubMethodLowering.kt | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/CollectionStubMethodLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/CollectionStubMethodLowering.kt index 025284ca243..c6ac2b83562 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/CollectionStubMethodLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/CollectionStubMethodLowering.kt @@ -44,6 +44,7 @@ private class CollectionStubMethodLowering(val context: JvmBackendContext) : Cla } val methodStubsToGenerate = generateRelevantStubMethods(irClass) + if (methodStubsToGenerate.isEmpty()) return // We don't need to generate stub for existing methods, but for FAKE_OVERRIDE methods with ABSTRACT modality, // it means an abstract function in superclass that is not implemented yet, @@ -140,11 +141,22 @@ private class CollectionStubMethodLowering(val context: JvmBackendContext) : Cla return mutableClass.typeParameters.map { it.symbol }.zip(readOnlyClassTypeArguments).toMap() } - private data class StubsForCollectionClass( + private inner class StubsForCollectionClass( val readOnlyClass: IrClassSymbol, - val mutableClass: IrClassSymbol, - val mutableOnlyMethods: Collection - ) + val mutableClass: IrClassSymbol + ) { + val mutableOnlyMethods: Collection by lazy { + val readOnlyMethodSignatures = readOnlyClass.functions.map { it.owner.toSignature() }.toHashSet() + mutableClass.functions + .map { it.owner } + .filter { it.toSignature() !in readOnlyMethodSignatures } + .toHashSet() + } + + operator fun component1() = readOnlyClass + operator fun component2() = mutableClass + operator fun component3() = mutableOnlyMethods + } private val preComputedStubs: Collection by lazy { with(context.ir.symbols) { @@ -158,12 +170,7 @@ private class CollectionStubMethodLowering(val context: JvmBackendContext) : Cla iterator to mutableIterator, listIterator to mutableListIterator ).map { (readOnlyClass, mutableClass) -> - val readOnlyMethodSignatures = readOnlyClass.functions.map { it.owner.toSignature() }.toHashSet() - val mutableMethods = mutableClass.functions - .map { it.owner } - .filter { it.toSignature() !in readOnlyMethodSignatures } - .toHashSet() - StubsForCollectionClass(readOnlyClass, mutableClass, mutableMethods) + StubsForCollectionClass(readOnlyClass, mutableClass) } } }