avoid unnecessary collection copying

This commit is contained in:
Vladimir Dolzhenko
2021-11-21 23:17:31 +01:00
committed by teamcityserver
parent 42837415f0
commit 0495ba2495
2 changed files with 12 additions and 20 deletions
@@ -225,7 +225,7 @@ protected constructor(
kindFilter: DescriptorKindFilter, kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean, nameFilter: (Name) -> Boolean,
location: LookupLocation location: LookupLocation
): List<DeclarationDescriptor> { ): MutableSet<DeclarationDescriptor> {
val declarations = declarationProvider.getDeclarations(kindFilter, nameFilter) val declarations = declarationProvider.getDeclarations(kindFilter, nameFilter)
val result = LinkedHashSet<DeclarationDescriptor>(declarations.size) val result = LinkedHashSet<DeclarationDescriptor>(declarations.size)
for (declaration in declarations) { for (declaration in declarations) {
@@ -272,7 +272,7 @@ protected constructor(
else -> throw IllegalArgumentException("Unsupported declaration kind: " + declaration) else -> throw IllegalArgumentException("Unsupported declaration kind: " + declaration)
} }
} }
return result.toList() return result
} }
// Do not change this, override in concrete subclasses: // Do not change this, override in concrete subclasses:
@@ -65,14 +65,12 @@ open class LazyClassMemberScope(
} }
private fun doDescriptors(nameFilter: (Name) -> Boolean): List<DeclarationDescriptor> { private fun doDescriptors(nameFilter: (Name) -> Boolean): List<DeclarationDescriptor> {
val result = LinkedHashSet( val result = computeDescriptorsFromDeclaredElements(
computeDescriptorsFromDeclaredElements( DescriptorKindFilter.ALL,
DescriptorKindFilter.ALL, nameFilter,
nameFilter, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS
NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS
)
) )
result.addAll(computeExtraDescriptors(NoLookupLocation.FOR_ALREADY_TRACKED)) computeExtraDescriptors(result, NoLookupLocation.FOR_ALREADY_TRACKED)
return result.toList() return result.toList()
} }
@@ -81,12 +79,10 @@ open class LazyClassMemberScope(
} }
private fun doClassifierDescriptors(nameFilter: (Name) -> Boolean): List<DeclarationDescriptor> { private fun doClassifierDescriptors(nameFilter: (Name) -> Boolean): List<DeclarationDescriptor> {
val result = LinkedHashSet( val result = computeDescriptorsFromDeclaredElements(
computeDescriptorsFromDeclaredElements( DescriptorKindFilter.CLASSIFIERS,
DescriptorKindFilter.CLASSIFIERS, nameFilter,
nameFilter, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS
NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS
)
) )
addSyntheticCompanionObject(result, NoLookupLocation.FOR_ALREADY_TRACKED) addSyntheticCompanionObject(result, NoLookupLocation.FOR_ALREADY_TRACKED)
addSyntheticNestedClasses(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<DeclarationDescriptor> { protected open fun computeExtraDescriptors(result: MutableCollection<DeclarationDescriptor>, location: LookupLocation) {
val result = ArrayList<DeclarationDescriptor>()
for (supertype in supertypes) { for (supertype in supertypes) {
for (descriptor in supertype.memberScope.getContributedDescriptors()) { for (descriptor in supertype.memberScope.getContributedDescriptors()) {
if (descriptor is FunctionDescriptor) { if (descriptor is FunctionDescriptor) {
@@ -133,9 +128,6 @@ open class LazyClassMemberScope(
addSyntheticVariables(result, location) addSyntheticVariables(result, location)
addSyntheticCompanionObject(result, location) addSyntheticCompanionObject(result, location)
addSyntheticNestedClasses(result, location) addSyntheticNestedClasses(result, location)
result.trimToSize()
return result
} }
val supertypes by storageManager.createLazyValue { val supertypes by storageManager.createLazyValue {