mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Strong references removed from `PythonPathCache` since cache should not prevent anything from GC
This commit is contained in:
@@ -19,6 +19,7 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.vfs.*;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.QualifiedName;
|
||||
import com.intellij.reference.SoftReference;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -26,13 +27,14 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public abstract class PythonPathCache {
|
||||
private final Map<QualifiedName, List<PsiElement>> myCache = ContainerUtil.newConcurrentMap();
|
||||
private final Map<VirtualFile, List<QualifiedName>> myQNameCache = ContainerUtil.newConcurrentMap();
|
||||
private final Map<QualifiedName, List<SoftReference<PsiElement>>> myCache = ContainerUtil.newConcurrentMap();
|
||||
private final Map<String, List<QualifiedName>> myQNameCache = ContainerUtil.newConcurrentMap();
|
||||
|
||||
public void clearCache() {
|
||||
myCache.clear();
|
||||
@@ -41,10 +43,11 @@ public abstract class PythonPathCache {
|
||||
|
||||
@Nullable
|
||||
public List<PsiElement> get(QualifiedName qualifiedName) {
|
||||
final List<PsiElement> result = myCache.get(qualifiedName);
|
||||
if (result == null) {
|
||||
final List<SoftReference<PsiElement>> references = myCache.get(qualifiedName);
|
||||
if (references == null) {
|
||||
return null;
|
||||
}
|
||||
final List<PsiElement> result = references.stream().map(r -> r.get()).filter(p -> p != null).collect(Collectors.toList());
|
||||
final boolean staleElementRemoved = result.removeIf(e -> !e.isValid());
|
||||
if (staleElementRemoved) {
|
||||
Logger.getInstance(PythonPathCache.class).warn("Removing invalid element from cache");
|
||||
@@ -53,15 +56,21 @@ public abstract class PythonPathCache {
|
||||
}
|
||||
|
||||
public void put(QualifiedName qualifiedName, List<PsiElement> results) {
|
||||
myCache.put(qualifiedName, new ArrayList<>(results));
|
||||
if (results != null) {
|
||||
myCache.put(qualifiedName, new ArrayList<>(results.stream().map(e -> new SoftReference<>(e)).collect(Collectors.toList())));
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public List<QualifiedName> getNames(VirtualFile vFile) {
|
||||
return myQNameCache.get(vFile);
|
||||
if (vFile == null) {
|
||||
return null;
|
||||
}
|
||||
return myQNameCache.get(vFile.getUrl());
|
||||
}
|
||||
|
||||
public void putNames(VirtualFile vFile, List<QualifiedName> qNames) {
|
||||
myQNameCache.put(vFile, new ArrayList<>(qNames));
|
||||
myQNameCache.put(vFile.getUrl(), new ArrayList<>(qNames));
|
||||
}
|
||||
|
||||
protected class MyVirtualFileAdapter extends VirtualFileAdapter {
|
||||
|
||||
Reference in New Issue
Block a user