From 9fa2f25972156f8046985806d6114bf82e085bf7 Mon Sep 17 00:00:00 2001 From: Maxim Shafirov Date: Fri, 12 Nov 2010 17:42:53 +0300 Subject: [PATCH] Lighter 'CachedValue' for storing class's method hierarchies. --- .../psi/impl/PsiSuperMethodImplUtil.java | 37 ++++---------- .../StructureDependentNotNullLazyKey.java | 51 +++++++++++++++++++ 2 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 platform/lang-api/src/com/intellij/psi/util/StructureDependentNotNullLazyKey.java diff --git a/java/java-impl/src/com/intellij/psi/impl/PsiSuperMethodImplUtil.java b/java/java-impl/src/com/intellij/psi/impl/PsiSuperMethodImplUtil.java index 0db41dc1a9eb..e66ad380b1c9 100644 --- a/java/java-impl/src/com/intellij/psi/impl/PsiSuperMethodImplUtil.java +++ b/java/java-impl/src/com/intellij/psi/impl/PsiSuperMethodImplUtil.java @@ -15,14 +15,13 @@ */ package com.intellij.psi.impl; -import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Pair; -import com.intellij.openapi.util.UserDataHolderEx; import com.intellij.psi.*; import com.intellij.psi.impl.source.HierarchicalMethodSignatureImpl; import com.intellij.psi.search.searches.DeepestSuperMethodsSearch; import com.intellij.psi.search.searches.SuperMethodsSearch; import com.intellij.psi.util.*; +import com.intellij.util.NotNullFunction; import com.intellij.util.SmartList; import gnu.trove.THashMap; import gnu.trove.THashSet; @@ -33,7 +32,13 @@ import org.jetbrains.annotations.Nullable; import java.util.*; public class PsiSuperMethodImplUtil { - private static final Key>> SIGNATURES_KEY = Key.create("MAP_KEY"); + private static final StructureDependentNotNullLazyKey, PsiClass> SIGNATURES_KEY = StructureDependentNotNullLazyKey.create("SIGNATURES_KEY", new NotNullFunction>() { + @NotNull + @Override + public Map fun(PsiClass dom) { + return buildMethodHierarchy(dom, PsiSubstitutor.EMPTY, true, new THashSet(), false); + } + }); private PsiSuperMethodImplUtil() { } @@ -298,30 +303,6 @@ public class PsiSuperMethodImplUtil { } private static Map getSignaturesMap(final PsiClass aClass) { - CachedValue> value = aClass.getUserData(SIGNATURES_KEY); - if (value == null) { - BySignaturesCachedValueProvider provider = new BySignaturesCachedValueProvider(aClass); - UserDataHolderEx dataHolder = (UserDataHolderEx)aClass; - value = dataHolder.putUserDataIfAbsent(SIGNATURES_KEY, - CachedValuesManager.getManager(aClass.getProject()).createCachedValue(provider, false)); - } - - return value.getValue(); - } - - private static class BySignaturesCachedValueProvider implements CachedValueProvider> { - private final PsiClass myClass; - - private BySignaturesCachedValueProvider(final PsiClass aClass) { - myClass = aClass; - } - - public Result> compute() { - Map result = buildMethodHierarchy(myClass, PsiSubstitutor.EMPTY, true, new THashSet(), false); - assert result != null; - - - return new Result>(result, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); - } + return SIGNATURES_KEY.getValue(aClass); } } diff --git a/platform/lang-api/src/com/intellij/psi/util/StructureDependentNotNullLazyKey.java b/platform/lang-api/src/com/intellij/psi/util/StructureDependentNotNullLazyKey.java new file mode 100644 index 000000000000..1131d7eb44fa --- /dev/null +++ b/platform/lang-api/src/com/intellij/psi/util/StructureDependentNotNullLazyKey.java @@ -0,0 +1,51 @@ +/* + * Copyright 2000-2010 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. + */ + +/* + * @author max + */ +package com.intellij.psi.util; + +import com.intellij.openapi.util.Key; +import com.intellij.openapi.util.Pair; +import com.intellij.psi.PsiElement; +import com.intellij.util.NotNullFunction; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; + +public class StructureDependentNotNullLazyKey extends Key> { + private final NotNullFunction myFunction; + + private StructureDependentNotNullLazyKey(@NonNls String name, final NotNullFunction function) { + super(name); + myFunction = function; + } + + @NotNull + public final T getValue(H h) { + Pair data = h.getUserData(this); + final long count = h.getManager().getModificationTracker().getJavaStructureModificationCount(); + if (data == null || data.getFirst() != count) { + h.putUserData(this, data = new Pair(count, myFunction.fun(h))); + } + + return data.getSecond(); + } + + public static StructureDependentNotNullLazyKey create(@NonNls String name, final NotNullFunction function) { + return new StructureDependentNotNullLazyKey(name, function); + } +}