From 0495ba2495699c6737baf508feeccf49e5dac795 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Sun, 21 Nov 2021 23:17:31 +0100 Subject: [PATCH] avoid unnecessary collection copying --- .../descriptors/AbstractLazyMemberScope.kt | 4 +-- .../lazy/descriptors/LazyClassMemberScope.kt | 28 +++++++------------ 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/AbstractLazyMemberScope.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/AbstractLazyMemberScope.kt index f38571ac7df..8b072592ad5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/AbstractLazyMemberScope.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/AbstractLazyMemberScope.kt @@ -225,7 +225,7 @@ protected constructor( kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean, location: LookupLocation - ): List { + ): MutableSet { val declarations = declarationProvider.getDeclarations(kindFilter, nameFilter) val result = LinkedHashSet(declarations.size) for (declaration in declarations) { @@ -272,7 +272,7 @@ protected constructor( else -> throw IllegalArgumentException("Unsupported declaration kind: " + declaration) } } - return result.toList() + return result } // Do not change this, override in concrete subclasses: diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassMemberScope.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassMemberScope.kt index c151943d888..fdc061185f7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassMemberScope.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassMemberScope.kt @@ -65,14 +65,12 @@ open class LazyClassMemberScope( } private fun doDescriptors(nameFilter: (Name) -> Boolean): List { - val result = LinkedHashSet( - computeDescriptorsFromDeclaredElements( - DescriptorKindFilter.ALL, - nameFilter, - NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS - ) + val result = computeDescriptorsFromDeclaredElements( + DescriptorKindFilter.ALL, + nameFilter, + NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS ) - result.addAll(computeExtraDescriptors(NoLookupLocation.FOR_ALREADY_TRACKED)) + computeExtraDescriptors(result, NoLookupLocation.FOR_ALREADY_TRACKED) return result.toList() } @@ -81,12 +79,10 @@ open class LazyClassMemberScope( } private fun doClassifierDescriptors(nameFilter: (Name) -> Boolean): List { - val result = LinkedHashSet( - computeDescriptorsFromDeclaredElements( - DescriptorKindFilter.CLASSIFIERS, - nameFilter, - NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS - ) + val result = computeDescriptorsFromDeclaredElements( + DescriptorKindFilter.CLASSIFIERS, + nameFilter, + NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS ) addSyntheticCompanionObject(result, NoLookupLocation.FOR_ALREADY_TRACKED) addSyntheticNestedClasses(result, NoLookupLocation.FOR_ALREADY_TRACKED) @@ -115,8 +111,7 @@ open class LazyClassMemberScope( } } - protected open fun computeExtraDescriptors(location: LookupLocation): Collection { - val result = ArrayList() + protected open fun computeExtraDescriptors(result: MutableCollection, location: LookupLocation) { for (supertype in supertypes) { for (descriptor in supertype.memberScope.getContributedDescriptors()) { if (descriptor is FunctionDescriptor) { @@ -133,9 +128,6 @@ open class LazyClassMemberScope( addSyntheticVariables(result, location) addSyntheticCompanionObject(result, location) addSyntheticNestedClasses(result, location) - - result.trimToSize() - return result } val supertypes by storageManager.createLazyValue {