Skip to content

Commit

Permalink
Fixed incorrect use of caching and reworked caching of expression eva…
Browse files Browse the repository at this point in the history
…luations
  • Loading branch information
m0rkeulv committed Sep 8, 2024
1 parent d1639e5 commit a8400d2
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ public AbstractHaxeNamedComponent(@NotNull ASTNode node) {
@Nullable
@NonNls
public String getName() {
return CachedValuesManager.getProjectPsiDependentCache(this, AbstractHaxeNamedComponent::_getName).getValue();
return CachedValuesManager.getProjectPsiDependentCache(this, AbstractHaxeNamedComponent::_getName);
}

private static CachedValueProvider.Result<String> _getName(AbstractHaxeNamedComponent namedComponent) {
private static String _getName(AbstractHaxeNamedComponent namedComponent) {
final HaxeComponentName name = namedComponent.getComponentName();
if (name != null) {
return new CachedValueProvider.Result<>(name.getText(), name, namedComponent);
return name.getText();
} else {
return new CachedValueProvider.Result<>(namedComponent._getNameFromSuper(), namedComponent);
return namedComponent._getNameFromSuper();
}
}
private String _getNameFromSuper() {
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/com/intellij/plugins/haxe/model/HaxeClassModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,22 +462,22 @@ private SpecificTypeReference setSpecificsConstraints(@NotNull HaxeMethodModel m
//caching implicit cast method lookup results
@NotNull
private List<HaxeMethodModel> getCastToMethods() {
return CachedValuesManager.getProjectPsiDependentCache(haxeClass, HaxeClassModel::getCastToMethodsCached).getValue();
return CachedValuesManager.getProjectPsiDependentCache(haxeClass, HaxeClassModel::getCastToMethodsCached);
}

private static CachedValueProvider.Result<List<HaxeMethodModel>> getCastToMethodsCached(@NotNull HaxeClass haxeClass) {
private static List<HaxeMethodModel> getCastToMethodsCached(@NotNull HaxeClass haxeClass) {
List<HaxeMethodModel> castToMethods = getMethodsWithMetadata(haxeClass.getModel(), "to", HaxeMeta.COMPILE_TIME, null);
return CachedValueProvider.Result.create(castToMethods, haxeClass);
return List.copyOf(castToMethods);
}

//caching implicit cast method lookup results
@NotNull
private List<HaxeMethodModel> getCastFromMethods() {
return CachedValuesManager.getProjectPsiDependentCache(haxeClass, HaxeClassModel::getCastFromMethodsCached).getValue();
return CachedValuesManager.getProjectPsiDependentCache(haxeClass, HaxeClassModel::getCastFromMethodsCached);
}
private static CachedValueProvider.Result<List<HaxeMethodModel>> getCastFromMethodsCached(@NotNull HaxeClass haxeClass) {
private static List<HaxeMethodModel> getCastFromMethodsCached(@NotNull HaxeClass haxeClass) {
List<HaxeMethodModel> castFromMethods = getMethodsWithMetadata(haxeClass.getModel(), "from", HaxeMeta.COMPILE_TIME, null);
return CachedValueProvider.Result.create(castFromMethods, haxeClass);
return List.copyOf(castFromMethods);
}

@NotNull
Expand Down Expand Up @@ -677,20 +677,20 @@ public PsiIdentifier getNamePsi() {
}
@NotNull
public List<HaxeType> getExtendsList() {
return CachedValuesManager.getProjectPsiDependentCache(haxeClass, HaxeClassModel::getHaxeExtendsListCached).getValue();
return CachedValuesManager.getProjectPsiDependentCache(haxeClass, HaxeClassModel::getHaxeExtendsListCached);
}
private static CachedValueProvider.Result<List<HaxeType>> getHaxeExtendsListCached(@NotNull HaxeClass haxeClass) {
private static List<HaxeType> getHaxeExtendsListCached(@NotNull HaxeClass haxeClass) {
List<HaxeType> list = haxeClass.getHaxeExtendsList();
return CachedValueProvider.Result.create(list, haxeClass);
return List.copyOf(list);
}

@NotNull
public List<HaxeType> getImplementsList() {
return CachedValuesManager.getProjectPsiDependentCache(haxeClass, HaxeClassModel::getHaxeImplementsListCached).getValue();
return CachedValuesManager.getProjectPsiDependentCache(haxeClass, HaxeClassModel::getHaxeImplementsListCached);
}
private static CachedValueProvider.Result<List<HaxeType>> getHaxeImplementsListCached(@NotNull HaxeClass haxeClass) {
private static List<HaxeType> getHaxeImplementsListCached(@NotNull HaxeClass haxeClass) {
List<HaxeType> list = haxeClass.getHaxeImplementsList();
return CachedValueProvider.Result.create(list, haxeClass);
return List.copyOf(list);
}

@NotNull
Expand Down Expand Up @@ -826,13 +826,13 @@ public List<HaxeGenericParamModel> getGenericParams() {

@Nullable
private HaxeGenericParam getGenericParamPsi() {
return CachedValuesManager.getProjectPsiDependentCache(haxeClass, HaxeClassModel::getGenericParamPsiCached).getValue();
return CachedValuesManager.getProjectPsiDependentCache(haxeClass, HaxeClassModel::getGenericParamPsiCached);
}

private static CachedValueProvider.Result<HaxeGenericParam> getGenericParamPsiCached(@NotNull HaxeClass haxeClass) {
private static HaxeGenericParam getGenericParamPsiCached(@NotNull HaxeClass haxeClass) {
boolean isAnonymous = haxeClass instanceof HaxeAnonymousType;
HaxeGenericParam param = isAnonymous ? getGenericParamFromParent(haxeClass) : haxeClass.getGenericParam();
return CachedValueProvider.Result.create(param, haxeClass);
return param;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public HaxeEnumValueModel getEnumValueModel() {

@Nullable
private PsiElement resolveEnumValueCached() {
return CachedValuesManager.getProjectPsiDependentCache(extractor, HaxeEnumExtractorModel::computeResolveEnumValue).getValue();
return CachedValuesManager.getProjectPsiDependentCache(extractor, HaxeEnumExtractorModel::computeResolveEnumValue);
}


Expand Down Expand Up @@ -95,22 +95,15 @@ public HaxeGenericResolver getGenericResolver() {
}

private @NotNull PsiElement[] getChildrenCached() {
return CachedValuesManager.getProjectPsiDependentCache(extractor, HaxeEnumExtractorModel::computeChildren).getValue();
return CachedValuesManager.getProjectPsiDependentCache(extractor, HaxeEnumExtractorModel::computeChildren);
}

private static CachedValueProvider.Result<PsiElement[]> computeChildren(HaxeEnumArgumentExtractor extractor) {
PsiElement[] children = extractor.getEnumExtractorArgumentList().getChildren();
PsiElement[] elements = ArrayUtils.addAll(children, extractor);
return new CachedValueProvider.Result<>(children, (Object[])elements);
private static PsiElement[] computeChildren(HaxeEnumArgumentExtractor extractor) {
return extractor.getEnumExtractorArgumentList().getChildren();
}

private static CachedValueProvider.Result<PsiElement> computeResolveEnumValue(HaxeEnumArgumentExtractor extractor) {
PsiElement resolve = extractor.getEnumValueReference().getReferenceExpression().resolve();
if (resolve != null) {
return new CachedValueProvider.Result<>(resolve, extractor, resolve);
}else {
return new CachedValueProvider.Result<>(null, extractor);
}
private static PsiElement computeResolveEnumValue(HaxeEnumArgumentExtractor extractor) {
return extractor.getEnumValueReference().getReferenceExpression().resolve();
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ protected boolean isReferencingCurrentFile(FullyQualifiedInfo info) {
}

public static HaxeExprFileModel fromFile(@NotNull HaxeFile file) {
return CachedValuesManager.getProjectPsiDependentCache(file, HaxeExprFileModel::cacheValueProvider).getValue();
return CachedValuesManager.getProjectPsiDependentCache(file, HaxeExprFileModel::cacheValueProvider);
}

private static CachedValueProvider.Result<HaxeExprFileModel> cacheValueProvider(@NotNull HaxeFile file) {
return CachedValueProvider.Result.create(new HaxeExprFileModel(file),
ModificationTracker.EVER_CHANGED,
ProjectRootModificationTracker.getInstance(file.getProject()));
private static HaxeExprFileModel cacheValueProvider(@NotNull HaxeFile file) {
return new HaxeExprFileModel(file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,12 @@ private List<PsiElement> getChildren() {
}
@NotNull
private static List<PsiElement> getChildren(HaxeFile file) {
return CachedValuesManager.getProjectPsiDependentCache(file, HaxeFileModel::_getChildren).getValue();
return CachedValuesManager.getProjectPsiDependentCache(file, HaxeFileModel::_getChildren);
}

private static CachedValueProvider.Result<List<PsiElement>> _getChildren(HaxeFile file) {
private static List<PsiElement> _getChildren(HaxeFile file) {
PsiElement[] children = file.getChildren();
List<PsiElement> list = Arrays.asList(children);
return new CachedValueProvider.Result<>(list, file);
return Arrays.asList(children);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private boolean isOverriddenPublicMethod() {
@Override
@Nullable
public HaxeClassModel getDeclaringClass() {
return CachedValuesManager.getProjectPsiDependentCache(getMemberPsi(), HaxeMemberModel::_getDeclaringClass).getValue();
return CachedValuesManager.getProjectPsiDependentCache(getMemberPsi(), HaxeMemberModel::_getDeclaringClass);
}

///TODO make model reusable and cache result
Expand All @@ -115,12 +115,12 @@ public HaxeModuleModel getDeclaringModule() {
return new HaxeModuleModel(module);
}

private static CachedValueProvider.Result<HaxeClassModel> _getDeclaringClass(PsiMember member) {
private static HaxeClassModel _getDeclaringClass(PsiMember member) {
PsiClass containingClass = member.getContainingClass();
if (containingClass instanceof HaxeClass haxeClass) {
return new CachedValueProvider.Result<>(haxeClass.getModel(), member);
return haxeClass.getModel();
}else {
return new CachedValueProvider.Result<>(null, member);
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public ResultHolder getReturnType(@Nullable HaxeGenericResolver resolver) {
if ((resolver == null || resolver.isEmpty()) // must not use resolver
&& haxeMethod.getReturnType() !=null // must have type tag
&& haxeMethod.getGenericParam() != null) { // must not have generics
return CachedValuesManager.getProjectPsiDependentCache(haxeMethod, HaxeMethodModel::getReturnTypeCacheProvider).getValue();
return CachedValuesManager.getProjectPsiDependentCache(haxeMethod, HaxeMethodModel::getReturnTypeCacheProvider);
}else {
Boolean data = haxeMethod.getUserData(isVoidReturn);
if (data == Boolean.TRUE) {
Expand All @@ -180,9 +180,8 @@ public ResultHolder getReturnType(@Nullable HaxeGenericResolver resolver) {
}
}

private static CachedValueProvider.Result<ResultHolder> getReturnTypeCacheProvider(HaxeMethod haxeMethod) {
ResultHolder type = HaxeTypeResolver.getFieldOrMethodReturnType(haxeMethod, null);
return new CachedValueProvider.Result<>(type, haxeMethod);
private static ResultHolder getReturnTypeCacheProvider(HaxeMethod haxeMethod) {
return HaxeTypeResolver.getFieldOrMethodReturnType(haxeMethod, null);
}

public SpecificFunctionReference getFunctionType(@Nullable HaxeGenericResolver resolver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ protected boolean isReferencingCurrentFile(FullyQualifiedInfo info) {
}

public static HaxeStdTypesFileModel fromFile(@NotNull HaxeFile file) {
return CachedValuesManager.getProjectPsiDependentCache(file, HaxeStdTypesFileModel::cacheValueProvider).getValue();
return CachedValuesManager.getProjectPsiDependentCache(file, HaxeStdTypesFileModel::cacheValueProvider);
}

private static CachedValueProvider.Result<HaxeStdTypesFileModel> cacheValueProvider(@NotNull HaxeFile file) {
return CachedValueProvider.Result.create(new HaxeStdTypesFileModel(file),
ModificationTracker.EVER_CHANGED,
ProjectRootModificationTracker.getInstance(file.getProject()));
private static HaxeStdTypesFileModel cacheValueProvider(@NotNull HaxeFile file) {
return new HaxeStdTypesFileModel(file);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ public HaxeClassReference(String name, @NotNull PsiElement elementContext, boole

private String getClassName(HaxeClassModel clazz) {
if(clazz.getPsi().getParent() != null) {
return CachedValuesManager.getProjectPsiDependentCache(clazz.getPsi(), HaxeClassReference::getNameCached).getValue();
return CachedValuesManager.getProjectPsiDependentCache(clazz.getPsi(), HaxeClassReference::getNameCached);
}else {
return getClassNameInternal(clazz.getPsi().getModel());
}
}

private static CachedValueProvider.Result<String> getNameCached(HaxeClass aClass) {
String name = getClassNameInternal(aClass.getModel());
return CachedValueProvider.Result.create(name, aClass);
private static String getNameCached(HaxeClass aClass) {
return getClassNameInternal(aClass.getModel());
}

private static String getClassNameInternal(HaxeClassModel clazz) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ static ResultHolder handle(final PsiElement element,
final HaxeGenericResolver resolver) {
try {
ProgressIndicatorProvider.checkCanceled();
return _handle(element, context, resolver);

HaxeExpressionEvaluatorCacheService service = element.getProject().getService(HaxeExpressionEvaluatorCacheService.class);
return service.handleWithResultCaching(element, context, resolver);
}
catch (NullPointerException e) {
// Make sure that these get into the log, because the GeneralHighlightingPass swallows them.
Expand All @@ -139,7 +141,7 @@ static ResultHolder handle(final PsiElement element,
}

@NotNull
static private ResultHolder _handle(final PsiElement element,
static ResultHolder _handle(final PsiElement element,
final HaxeExpressionEvaluatorContext context,
HaxeGenericResolver optionalResolver) {
if (element == null) {
Expand Down Expand Up @@ -187,8 +189,7 @@ static private ResultHolder _handle(final PsiElement element,
}

if (element instanceof HaxeIfStatement ifStatement) {
//return handleIfStatement(context, resolver, ifStatement);
return resolveWithCache(ifStatement, resolver, () -> handleIfStatement(context, resolver, ifStatement));
return handleIfStatement(context, resolver, ifStatement);
}
}

Expand Down Expand Up @@ -216,7 +217,7 @@ static private ResultHolder _handle(final PsiElement element,
return handleValueIterator(context, resolver, valueIterator);
}
if (element instanceof HaxeIteratorkey || element instanceof HaxeIteratorValue) {
return resolveWithCache(element, resolver, () -> findIteratorType(element));
return findIteratorType(element);
}


Expand All @@ -229,15 +230,15 @@ static private ResultHolder _handle(final PsiElement element,
if (element instanceof HaxeParameter parameter) {
boolean isUntyped = parameter.getTypeTag() == null && parameter.getVarInit() == null;
if (isUntyped) {
return resolveWithCache(element, resolver, () -> handleParameter(context, resolver, parameter));
return handleParameter(context, resolver, parameter);
}
return handleParameter(context, resolver, parameter);
}

}

if (element instanceof HaxeEnumExtractedValueReference extractedValue) {
return resolveWithCache(extractedValue, resolver, () -> handleEnumExtractedValue(extractedValue, resolver));
return handleEnumExtractedValue(extractedValue, resolver);
}


Expand All @@ -256,11 +257,11 @@ static private ResultHolder _handle(final PsiElement element,
}

if (element instanceof HaxeCallExpression callExpression) {
return resolveWithCache(callExpression, resolver, () -> handleCallExpression(context, resolver, callExpression));
return handleCallExpression(context, resolver, callExpression);
}

if (element instanceof HaxeReferenceExpression referenceExpression) {
return resolveWithCache(referenceExpression, resolver, () -> handleReferenceExpression(context, resolver, referenceExpression));
return handleReferenceExpression(context, resolver, referenceExpression);
}

if (element instanceof HaxeCastExpression castExpression) {
Expand Down Expand Up @@ -335,7 +336,7 @@ static private ResultHolder _handle(final PsiElement element,
}

if (element instanceof HaxeFunctionLiteral function) {
return resolveWithCache(function, resolver, () -> handleFunctionLiteral(context, resolver, function));
return handleFunctionLiteral(context, resolver, function);
}

if (element instanceof HaxeObjectLiteralImpl objectLiteral) {
Expand Down Expand Up @@ -452,33 +453,6 @@ private static ResultHolder handleMacroStatement(HaxeExpressionEvaluatorContext
return HaxeMacroTypeUtil.getExpr(macroStatement).createHolder();
}

private static ResultHolder resolveWithCache(@NotNull PsiElement element, @NotNull HaxeGenericResolver resolver, Supplier<ResultHolder> resolveLogic) {
Map<PsiElement, CacheRecord> map = resultCache.get();
Map<PsiElement, AtomicInteger> hitCounter = resultCacheHits.get();
String resolverAsString = resolver.toCacheString();
if (map.containsKey(element) && map.get(element).resolverAsString().equals(resolverAsString)) {
CacheRecord cacheRecord = map.get(element);
hitCounter.get(element).incrementAndGet();
return cacheRecord.holder();
}else {
ResultHolder result = resolveLogic.get();
if (!result.isUnknown()) {

if (result.getClassType() != null) {
if (!result.isTypeParameter() && result.getClassType().getGenericResolver().isEmpty()) {
hitCounter.put(element, new AtomicInteger(0));
map.put(element, new CacheRecord(result, resolverAsString));
}
}else if (result.isFunctionType() && result.getFunctionType() != null) {
if (!containsUnknowns(result.getFunctionType())) {
hitCounter.put(element, new AtomicInteger(0));
map.put(element, new CacheRecord(result, resolverAsString));
}
}
}
return result;
}
}

private static boolean containsUnknowns(SpecificFunctionReference type) {
if (type.getReturnType().isUnknown()) return true;
Expand Down
Loading

0 comments on commit a8400d2

Please sign in to comment.