From d85884426ed41367a697b4c5b58a67647e4edb5a Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 26 Apr 2016 19:50:45 +0300 Subject: [PATCH] Minor optimization of lookup tracker records --- .../CompilerCallbackServicesFacadeServer.kt | 2 +- .../synthetic/JavaSyntheticPropertiesScope.kt | 2 +- .../descriptors/AbstractLazyMemberScope.kt | 12 +++---- .../lazy/descriptors/LazyClassMemberScope.kt | 6 +++- .../descriptors/LazyPackageMemberScope.kt | 11 ++++++- .../descriptors/LazyScriptClassMemberScope.kt | 5 +++ .../kotlin/util/lookupTrackerUtil.kt | 29 +++++++++++++--- .../incremental/components/LookupLocation.kt | 2 +- .../org/jetbrains/kotlin/incremental/utils.kt | 33 +++++++++---------- .../DeserializedClassDescriptor.kt | 2 +- 10 files changed, 71 insertions(+), 33 deletions(-) diff --git a/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/CompilerCallbackServicesFacadeServer.kt b/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/CompilerCallbackServicesFacadeServer.kt index 7f1cf121923..ee668b980f6 100644 --- a/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/CompilerCallbackServicesFacadeServer.kt +++ b/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/CompilerCallbackServicesFacadeServer.kt @@ -77,7 +77,7 @@ class CompilerCallbackServicesFacadeServer( } } - private val lookupTracker_isDoNothing: Boolean = incrementalCompilationComponents != null && incrementalCompilationComponents.getLookupTracker() == LookupTracker.DO_NOTHING + private val lookupTracker_isDoNothing: Boolean = incrementalCompilationComponents?.getLookupTracker() === LookupTracker.DO_NOTHING override fun lookupTracker_isDoNothing(): Boolean = lookupTracker_isDoNothing diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt index b8038e21291..2a98c65ed3b 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt @@ -94,7 +94,7 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l private fun syntheticPropertyInClassNotCached(ownerClass: ClassDescriptor, name: Name): SyntheticPropertyHolder { fun result(descriptor: PropertyDescriptor?, getterNames: List, setterName: Name? = null): SyntheticPropertyHolder { - if (lookupTracker == LookupTracker.DO_NOTHING) { + if (lookupTracker === LookupTracker.DO_NOTHING) { return if (descriptor == null) SyntheticPropertyHolder.EMPTY else SyntheticPropertyHolder(descriptor, emptyList()) } 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 907e67dc95a..76fee0d62a5 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 @@ -17,9 +17,11 @@ package org.jetbrains.kotlin.resolve.lazy.descriptors import com.google.common.collect.Sets -import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor import org.jetbrains.kotlin.incremental.components.LookupLocation -import org.jetbrains.kotlin.incremental.record import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.BindingTrace @@ -166,6 +168,8 @@ protected constructor( return result.toReadOnlyList() } + abstract fun recordLookup(name: Name, from: LookupLocation) + // Do not change this, override in concrete subclasses: // it is very easy to compromise laziness of this class, and fail all the debugging // a generic implementation can't do this properly @@ -180,8 +184,4 @@ protected constructor( p.popIndent() p.println("}") } - - private fun recordLookup(name: Name, from: LookupLocation) { - c.lookupTracker.record(from, thisDescriptor, name) - } } 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 d4f2ead11d9..8dc98033df7 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 @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticSink import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.incremental.components.NoLookupLocation +import org.jetbrains.kotlin.incremental.record import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.KtDeclaration @@ -38,7 +39,6 @@ import org.jetbrains.kotlin.resolve.lazy.declarations.ClassMemberDeclarationProv import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.MemberScope -import org.jetbrains.kotlin.resolve.VarianceCheckerCore import org.jetbrains.kotlin.storage.NotNullLazyValue import org.jetbrains.kotlin.storage.NullableLazyValue import org.jetbrains.kotlin.types.DeferredType @@ -316,6 +316,10 @@ open class LazyClassMemberScope( descriptor.returnType = DeferredType.create(c.storageManager, trace, { thisDescriptor.getDefaultType() }) } + override fun recordLookup(name: Name, from: LookupLocation) { + c.lookupTracker.record(from, thisDescriptor, name) + } + // Do not add details here, they may compromise the laziness during debugging override fun toString() = "lazy scope for class ${thisDescriptor.name}" diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyPackageMemberScope.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyPackageMemberScope.kt index 537c1b0b9c7..60b949c6139 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyPackageMemberScope.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyPackageMemberScope.kt @@ -16,8 +16,13 @@ package org.jetbrains.kotlin.resolve.lazy.descriptors -import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor +import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.incremental.components.NoLookupLocation +import org.jetbrains.kotlin.incremental.record import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.resolve.lazy.ResolveSession @@ -45,6 +50,10 @@ class LazyPackageMemberScope( // No extra properties } + override fun recordLookup(name: Name, from: LookupLocation) { + c.lookupTracker.record(from, thisDescriptor, name) + } + // Do not add details here, they may compromise the laziness during debugging override fun toString() = "lazy scope for package " + thisDescriptor.name } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt index 67b396b60d6..89c549c1a3e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.incremental.components.NoLookupLocation +import org.jetbrains.kotlin.incremental.record import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.BindingTrace import org.jetbrains.kotlin.resolve.lazy.ResolveSession @@ -109,4 +110,8 @@ class LazyScriptClassMemberScope( propertyDescriptor.initialize(null, null) return propertyDescriptor } + + override fun recordLookup(name: Name, from: LookupLocation) { + c.lookupTracker.record(from, thisDescriptor, name) + } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/util/lookupTrackerUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/util/lookupTrackerUtil.kt index 3aae9aca834..191b1e8b90f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/util/lookupTrackerUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/util/lookupTrackerUtil.kt @@ -1,8 +1,25 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.jetbrains.kotlin.util +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor import org.jetbrains.kotlin.incremental.KotlinLookupLocation import org.jetbrains.kotlin.incremental.components.LookupTracker -import org.jetbrains.kotlin.incremental.getScopeKind import org.jetbrains.kotlin.incremental.record import org.jetbrains.kotlin.psi.KtExpression import org.jetbrains.kotlin.resolve.DescriptorUtils @@ -17,8 +34,13 @@ fun LookupTracker.record(expression: KtExpression, type: KotlinType) { // Scope descriptor is function descriptor only when type is local // Lookups for local types are not needed since all usages are compiled with the type - if (getScopeKind(scopeDescriptor) != null && !DescriptorUtils.isLocal(typeDescriptor)) { - record(KotlinLookupLocation(expression), scopeDescriptor, typeDescriptor.name) + when { + scopeDescriptor is PackageFragmentDescriptor && !DescriptorUtils.isLocal(typeDescriptor) -> { + record(KotlinLookupLocation(expression), scopeDescriptor, typeDescriptor.name) + } + scopeDescriptor is ClassDescriptor && !DescriptorUtils.isLocal(typeDescriptor) -> { + record(KotlinLookupLocation(expression), scopeDescriptor, typeDescriptor.name) + } } for (typeArgument in type.arguments) { @@ -27,4 +49,3 @@ fun LookupTracker.record(expression: KtExpression, type: KotlinType) { } } } - diff --git a/core/descriptors/src/org/jetbrains/kotlin/incremental/components/LookupLocation.kt b/core/descriptors/src/org/jetbrains/kotlin/incremental/components/LookupLocation.kt index 0969c32bf01..a4daebdb4f1 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/incremental/components/LookupLocation.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/incremental/components/LookupLocation.kt @@ -60,5 +60,5 @@ enum class NoLookupLocation : LookupLocation { WHEN_FIND_BY_FQNAME, WHEN_GET_COMPANION_OBJECT; - override val location: LocationInfo? = null + override val location: LocationInfo? get() = null } diff --git a/core/descriptors/src/org/jetbrains/kotlin/incremental/utils.kt b/core/descriptors/src/org/jetbrains/kotlin/incremental/utils.kt index 80c8bfaebf4..b7f5c8776e2 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/incremental/utils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/incremental/utils.kt @@ -16,28 +16,27 @@ package org.jetbrains.kotlin.incremental -import org.jetbrains.kotlin.descriptors.ClassifierDescriptor -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor -import org.jetbrains.kotlin.incremental.components.* +import org.jetbrains.kotlin.incremental.components.LookupLocation +import org.jetbrains.kotlin.incremental.components.LookupTracker +import org.jetbrains.kotlin.incremental.components.Position +import org.jetbrains.kotlin.incremental.components.ScopeKind import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe +import org.jetbrains.kotlin.resolve.DescriptorUtils -fun LookupTracker.record(from: LookupLocation, scopeOwner: DeclarationDescriptor, name: Name) { - if (this == LookupTracker.DO_NOTHING || from is NoLookupLocation) return +// These methods are called many times, please pay attention to performance here +fun LookupTracker.record(from: LookupLocation, scopeOwner: ClassDescriptor, name: Name) { + if (this === LookupTracker.DO_NOTHING) return val location = from.location ?: return - - val scopeKind = getScopeKind(scopeOwner) ?: - throw AssertionError("Unexpected containing declaration type: ${scopeOwner.javaClass}") - val position = if (requiresPosition) location.position else Position.NO_POSITION - - record(location.filePath, position, scopeOwner.fqNameUnsafe.asString(), scopeKind, name.asString()) + record(location.filePath, position, DescriptorUtils.getFqName(scopeOwner).asString(), ScopeKind.CLASSIFIER, name.asString()) } -fun getScopeKind(scopeOwner: DeclarationDescriptor) = when (scopeOwner) { - is ClassifierDescriptor -> ScopeKind.CLASSIFIER - is PackageFragmentDescriptor -> ScopeKind.PACKAGE - else -> null -} \ No newline at end of file +fun LookupTracker.record(from: LookupLocation, scopeOwner: PackageFragmentDescriptor, name: Name) { + if (this === LookupTracker.DO_NOTHING) return + val location = from.location ?: return + val position = if (requiresPosition) location.position else Position.NO_POSITION + record(location.filePath, position, scopeOwner.fqName.asString(), ScopeKind.PACKAGE, name.asString()) +} diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedClassDescriptor.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedClassDescriptor.kt index d158e483011..2bf98228f07 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedClassDescriptor.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedClassDescriptor.kt @@ -271,7 +271,7 @@ class DeserializedClassDescriptor( } private fun recordLookup(name: Name, from: LookupLocation) { - c.components.lookupTracker.record(from, c.containingDeclaration, name) + c.components.lookupTracker.record(from, classDescriptor, name) } }