Explicit interfaces for memoized functions

This commit is contained in:
Andrey Breslav
2013-02-12 15:22:11 +04:00
parent 6e61ada816
commit b04110e3a9
8 changed files with 75 additions and 17 deletions
@@ -27,6 +27,7 @@ import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.ImportPath;
import org.jetbrains.jet.lang.resolve.TemporaryBindingTrace;
import org.jetbrains.jet.lang.resolve.lazy.descriptors.LazyClassDescriptor;
import org.jetbrains.jet.lang.resolve.lazy.storage.MemoizedFunctionToNotNull;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.scopes.ChainedScope;
@@ -41,7 +42,7 @@ import static org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager.Referen
public class ScopeProvider {
private final ResolveSession resolveSession;
private final Function<JetFile, JetScope> fileScopes;
private final MemoizedFunctionToNotNull<JetFile, JetScope> fileScopes;
private final NotNullLazyValue<JetScope> defaultImportsScope;
@@ -28,9 +28,10 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamespaceHeader;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
import org.jetbrains.jet.lang.resolve.lazy.storage.MemoizedFunctionToNullable;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
import org.jetbrains.jet.lang.resolve.name.FqName;
import java.util.Collection;
@@ -48,7 +49,7 @@ public class FileBasedDeclarationProviderFactory implements DeclarationProviderF
private final StorageManager storageManager;
private final NotNullLazyValue<Index> index;
private final Function<FqName, PackageMemberDeclarationProvider> packageDeclarationProviders;
private final MemoizedFunctionToNullable<FqName, PackageMemberDeclarationProvider> packageDeclarationProviders;
public FileBasedDeclarationProviderFactory(@NotNull StorageManager storageManager, @NotNull Collection<JetFile> files) {
this(storageManager, files, Predicates.<FqName>alwaysFalse());
@@ -25,11 +25,12 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassInfoUtil;
import org.jetbrains.jet.lang.resolve.lazy.declarations.DeclarationProvider;
import org.jetbrains.jet.lang.resolve.lazy.storage.MemoizedFunctionToNotNull;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
@@ -47,11 +48,11 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
protected final DP declarationProvider;
protected final D thisDescriptor;
private final Function<Name, List<ClassDescriptor>> classDescriptors;
private final Function<Name, List<ClassDescriptor>> objectDescriptors;
private final MemoizedFunctionToNotNull<Name, List<ClassDescriptor>> classDescriptors;
private final MemoizedFunctionToNotNull<Name, List<ClassDescriptor>> objectDescriptors;
private final Function<Name, Set<FunctionDescriptor>> functionDescriptors;
private final Function<Name, Set<VariableDescriptor>> propertyDescriptors;
private final MemoizedFunctionToNotNull<Name, Set<FunctionDescriptor>> functionDescriptors;
private final MemoizedFunctionToNotNull<Name, Set<VariableDescriptor>> propertyDescriptors;
private static class AllDescriptors {
private final Collection<DeclarationDescriptor> all = Sets.newLinkedHashSet();
@@ -24,8 +24,9 @@ import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.lazy.declarations.PackageMemberDeclarationProvider;
import org.jetbrains.jet.lang.resolve.lazy.storage.MemoizedFunctionToNullable;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
@@ -35,7 +36,7 @@ import java.util.Set;
public class LazyPackageMemberScope extends AbstractLazyMemberScope<NamespaceDescriptor, PackageMemberDeclarationProvider> {
private final Function<Name, NamespaceDescriptor> packageDescriptors;
private final MemoizedFunctionToNullable<Name, NamespaceDescriptor> packageDescriptors;
public LazyPackageMemberScope(@NotNull ResolveSession resolveSession,
@NotNull PackageMemberDeclarationProvider declarationProvider,
@@ -43,10 +43,11 @@ public class LockBasedStorageManager implements StorageManager {
@NotNull
@Override
public <K, V> Function<K, V> createMemoizedFunction(@NotNull final Function<K, V> compute, @NotNull final ReferenceKind valuesReferenceKind) {
return new Function<K, V>() {
public <K, V> MemoizedFunctionToNotNull<K, V> createMemoizedFunction(@NotNull final Function<K, V> compute, @NotNull final ReferenceKind valuesReferenceKind) {
return new MemoizedFunctionToNotNull<K, V>() {
private final ConcurrentMap<K, V> cache = createConcurrentMap(valuesReferenceKind);
@NotNull
@Override
public V fun(@NotNull final K input) {
V value = cache.get(input);
@@ -69,13 +70,14 @@ public class LockBasedStorageManager implements StorageManager {
@NotNull
@Override
public <K, V> Function<K, V> createMemoizedFunctionWithNullableValues(
public <K, V> MemoizedFunctionToNullable<K, V> createMemoizedFunctionWithNullableValues(
@NotNull final Function<K, V> compute, @NotNull final ReferenceKind valuesReferenceKind
) {
return new Function<K, V>() {
return new MemoizedFunctionToNullable<K, V>() {
private final ConcurrentMap<K, NullableLazyValue<V>> cache = createConcurrentMap(valuesReferenceKind);
@Override
@Nullable
public V fun(@NotNull final K input) {
NullableLazyValue<V> lazyValue = cache.get(input);
if (lazyValue != null) return lazyValue.compute();
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2013 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.jet.lang.resolve.lazy.storage;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
public interface MemoizedFunctionToNotNull<P, R> extends Function<P, R> {
@Override
@NotNull
R fun(P p);
}
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2013 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.jet.lang.resolve.lazy.storage;
import com.intellij.util.Function;
import org.jetbrains.annotations.Nullable;
public interface MemoizedFunctionToNullable<P, R> extends Function<P, R> {
@Override
@Nullable
R fun(P p);
}
@@ -32,9 +32,9 @@ public interface StorageManager {
* the value gets collected and then re-computed
*/
@NotNull
<K, V> Function<K, V> createMemoizedFunction(@NotNull Function<K, V> compute, @NotNull ReferenceKind valuesReferenceKind);
<K, V> MemoizedFunctionToNotNull<K, V> createMemoizedFunction(@NotNull Function<K, V> compute, @NotNull ReferenceKind valuesReferenceKind);
@NotNull
<K, V> Function<K, V> createMemoizedFunctionWithNullableValues(@NotNull Function<K, V> compute, @NotNull ReferenceKind valuesReferenceKind);
<K, V> MemoizedFunctionToNullable<K, V> createMemoizedFunctionWithNullableValues(@NotNull Function<K, V> compute, @NotNull ReferenceKind valuesReferenceKind);
@NotNull
<T> NotNullLazyValue<T> createLazyValue(@NotNull Computable<T> computable);