perf(cache): line end doc clear later when switch tab, runReadAction only need, idea version doc

This commit is contained in:
林万程
2024-01-10 18:33:40 +08:00
parent c4f98fae71
commit 48934406bd
6 changed files with 59 additions and 51 deletions

View File

@@ -82,6 +82,7 @@ tasks.withType(Javadoc) {
patchPluginXml {
// The performance of 2019.3 has been greatly improved.
// change plugins without restarting the IDE in 2020.1.
// 2020.2 JCEF, 2022.3 JDK17, 2023.3 AI
sinceBuild = '201.1'
untilBuild = ''
changeNotes = """

View File

@@ -18,8 +18,6 @@ import java.util.Map;
*/
public class CacheUpdateEditorListener implements FileEditorManagerListener {
private static final Logger LOG = LoggerFactory.getLogger(CacheUpdateEditorListener.class);
@Override
public void selectionChanged(@NotNull FileEditorManagerEvent event) {
@NotNull Project project = event.getManager().getProject();
@@ -52,8 +50,10 @@ public class CacheUpdateEditorListener implements FileEditorManagerListener {
LineEndCacheUtils.cache.remove(project);
return;
}
Map<VirtualFile, Map<Integer, LineEndCache>> fileMap = LineEndCacheUtils.cache.get(project);
fileMap.remove(file);
@Nullable Map<VirtualFile, Map<Integer, LineEndCache>> fileMap = LineEndCacheUtils.cache.get(project);
if (fileMap != null) {
fileMap.remove(file);
}
});
}
}

View File

@@ -5,23 +5,18 @@ import io.github.linwancen.plugin.show.bean.LineInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class LineEndCache {
@NotNull public String code;
@NotNull public List<LineExtensionInfo> lineExtList;
public boolean selectChanged = false;
@NotNull public volatile String code;
@NotNull public final List<LineExtensionInfo> lineExtList = new ArrayList<>(1);
public volatile boolean selectChanged = false;
/** null if updated */
@Nullable public volatile LineInfo info;
public LineEndCache(@NotNull String code, @NotNull List<LineExtensionInfo> right, @NotNull LineInfo info) {
public LineEndCache(@NotNull String code, @NotNull LineInfo info) {
this.code = code;
this.lineExtList = right;
this.info = info;
}
public void updated() {
selectChanged = false;
info = null;
}
}

View File

@@ -29,15 +29,17 @@ public class LineEndCacheUtils {
@NotNull LineEndCache lineCache = cache
.computeIfAbsent(info.project, a -> new ConcurrentHashMap<>())
.computeIfAbsent(info.file, a -> new ConcurrentHashMap<>())
.computeIfAbsent(info.lineNumber, a -> new LineEndCache(info.text, new ArrayList<>(1), info));
if (lineCache.selectChanged || !info.text.equals(lineCache.code)) {
lineCache.lineExtList.clear();
.computeIfAbsent(info.lineNumber, a -> new LineEndCache(info.text, info));
if (lineCache.selectChanged) {
lineCache.info = info;
} else if (!info.text.equals(lineCache.code)) {
lineCache.info = info;
lineCache.lineExtList.clear();
}
checkScheduleAndInit();
return lineCache.lineExtList;
} catch (Throwable e) {
LOG.info("LineEndCache catch Throwable but log to record.", e);
LOG.info("LineEndCacheUtils catch Throwable but log to record.", e);
return null;
}
}
@@ -51,9 +53,9 @@ public class LineEndCacheUtils {
isRun = true;
AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> {
try {
ApplicationManager.getApplication().runReadAction(LineEndCacheUtils::cacheUpdate);
cacheUpdate();
} catch (Exception e) {
LOG.info("LineEndCache checkScheduleAndInit catch Throwable but log to record.", e);
LOG.info("LineEndCacheUtils checkScheduleAndInit catch Throwable but log to record.", e);
}
}, 0L, 1L, TimeUnit.SECONDS);
}
@@ -71,26 +73,34 @@ public class LineEndCacheUtils {
if (DumbService.isDumb(project)) {
return;
}
fileMap.forEach((file, lineMap) ->
lineMap.forEach((lineNumber, lineEndCache) -> {
try {
@Nullable LineInfo info = lineEndCache.info;
if (info == null) {
return;
}
@Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info);
lineEndCache.updated();
lineEndCache.code = info.text;
if (lineExt == null) {
return;
}
lineEndCache.lineExtList.add(lineExt);
} catch (Exception e) {
LOG.info("LineEndCache lineMap.forEach catch Throwable but log to record.", e);
fileMap.forEach((file, lineMap) -> lineMap.forEach((lineNumber, lineEndCache) -> {
if (lineEndCache.info == null) {
return;
}
ApplicationManager.getApplication().runReadAction(() -> {
try {
@Nullable LineInfo info = lineEndCache.info;
if (info == null) {
return;
}
}));
lineEndCache.info = null;
if (lineEndCache.selectChanged) {
lineEndCache.selectChanged = false;
lineEndCache.lineExtList.clear();
}
@Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info);
if (lineExt != null) {
lineEndCache.lineExtList.add(lineExt);
}
// change after ext is updated
lineEndCache.code = info.text;
} catch (Exception e) {
LOG.info("LineEndCacheUtils lineMap.forEach catch Throwable but log to record.", e);
}
});
}));
} catch (Exception e) {
LOG.info("LineEndCache cache.forEach catch Throwable but log to record.", e);
LOG.info("LineEndCacheUtils cache.forEach catch Throwable but log to record.", e);
}
});
}

View File

@@ -4,6 +4,6 @@ import org.jetbrains.annotations.Nullable;
public class TreeCache {
@Nullable
public String doc;
public volatile String doc;
public volatile boolean needUpdate = true;
}

View File

@@ -1,10 +1,8 @@
package io.github.linwancen.plugin.show.cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.intellij.ide.projectView.ProjectViewNode;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.util.concurrency.AppExecutorUtil;
@@ -32,8 +30,10 @@ public class TreeCacheUtils {
treeCache.needUpdate = true;
checkScheduleAndInit();
return treeCache.doc;
} catch (ProcessCanceledException e) {
return null;
} catch (Throwable e) {
LOG.info("LineEndCache catch Throwable but log to record.", e);
LOG.info("TreeCacheUtils catch Throwable but log to record.", e);
return null;
}
}
@@ -47,7 +47,7 @@ public class TreeCacheUtils {
isRun = true;
AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> {
try {
ApplicationManager.getApplication().runReadAction(TreeCacheUtils::cacheUpdate);
cacheUpdate();
} catch (Exception e) {
LOG.info("TreeCacheUtils checkScheduleAndInit catch Throwable but log to record.", e);
}
@@ -68,13 +68,15 @@ public class TreeCacheUtils {
return;
}
nodeCache.forEach((node, treeCache) -> {
try {
if (treeCache.needUpdate) {
treeCache.needUpdate = false;
treeCache.doc = Tree.treeDoc(node, project);
}
} catch (Exception e) {
LOG.info("TreeCacheUtils nodeCache.forEach catch Throwable but log to record.", e);
if (treeCache.needUpdate) {
treeCache.needUpdate = false;
ApplicationManager.getApplication().runReadAction(() -> {
try {
treeCache.doc = Tree.treeDoc(node, project);
} catch (Exception e) {
LOG.info("TreeCacheUtils nodeCache.forEach catch Throwable but log to record.", e);
}
});
}
});
} catch (Exception e) {