PY-82783 Reset type-cache after low memory notification

(cherry picked from commit 2c50752bdc25b75f756a1a8166e72e8e3abd2710)

IJ-MR-169930

GitOrigin-RevId: d9bda4e6dbeb6efdd82b3ec83c542857695fb86f
This commit is contained in:
Andrey Vokin
2025-07-21 16:07:47 +02:00
committed by intellij-monorepo-bot
parent d623c92075
commit 1f55555bbb

View File

@@ -1,7 +1,10 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.jetbrains.python.psi.types;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.LowMemoryWatcher;
import com.intellij.openapi.util.SimpleModificationTracker;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
@@ -18,8 +21,10 @@ import java.util.concurrent.ConcurrentMap;
*
* @author Ilya.Kazakevich
*/
final class TypeEvalContextCacheImpl implements TypeEvalContextCache {
final class TypeEvalContextCacheImpl implements TypeEvalContextCache, Disposable {
private final @NotNull CachedValue<ConcurrentMap<TypeEvalConstraints, TypeEvalContext>> myCachedMapStorage;
private final LowMemoryWatcher myLowMemoryWatcher;
private final SimpleModificationTracker myLowMemoryModificationTracker = new SimpleModificationTracker();
TypeEvalContextCacheImpl(@NotNull Project project) {
myCachedMapStorage = CachedValuesManager.getManager(project).createCachedValue(new CachedValueProvider<>() {
@@ -28,9 +33,14 @@ final class TypeEvalContextCacheImpl implements TypeEvalContextCache {
// This method is called if cache is empty. Create new map for it.
// Concurrent map allows several threads to call get and put, so it is thread safe but not atomic
final ConcurrentMap<TypeEvalConstraints, TypeEvalContext> map = ContainerUtil.createConcurrentSoftValueMap();
return new CachedValueProvider.Result<>(map, PsiModificationTracker.MODIFICATION_COUNT);
return new CachedValueProvider.Result<>(map, PsiModificationTracker.MODIFICATION_COUNT, myLowMemoryModificationTracker);
}
});
myLowMemoryWatcher = LowMemoryWatcher.register(() -> {
myLowMemoryModificationTracker.incModificationCount();
myCachedMapStorage.getValue();
});
}
@Override
@@ -47,4 +57,9 @@ final class TypeEvalContextCacheImpl implements TypeEvalContextCache {
map.putIfAbsent(key, standard);// ConcurrentMap guarantees happens-before so from this moment get() should work in other threads
return oldValue == null ? standard : oldValue;
}
@Override
public void dispose() {
myLowMemoryWatcher.stop();
}
}