From 3e34fa8ac92bb0d4e0261758d06c01324140689e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=B8=87=E7=A8=8B?= <1498425439@qq.com> Date: Sun, 21 Jan 2024 16:18:47 +0800 Subject: [PATCH] perf(ReadAction): use nonBlocking and runReadActionInSmartMode --- .../github/linwancen/plugin/show/LineEnd.java | 7 ++- .../linwancen/plugin/show/LineEndCopy.java | 44 ++++++++++--------- .../plugin/show/cache/LineEndCacheUtils.java | 28 ++++++------ .../plugin/show/cache/TreeCacheUtils.java | 22 ++++++---- .../plugin/show/ext/conf/ConfCache.java | 41 +++++++++-------- 5 files changed, 78 insertions(+), 64 deletions(-) diff --git a/src/main/java/io/github/linwancen/plugin/show/LineEnd.java b/src/main/java/io/github/linwancen/plugin/show/LineEnd.java index 776dcd8..374906c 100644 --- a/src/main/java/io/github/linwancen/plugin/show/LineEnd.java +++ b/src/main/java/io/github/linwancen/plugin/show/LineEnd.java @@ -87,11 +87,16 @@ public class LineEnd extends EditorLinePainter { if (doc == null) { return null; } + return lineExtText(info, info.appSettings.lineEndPrefix + doc); + } + + @NotNull + public static LineExtensionInfo lineExtText(@NotNull LineInfo info, String text) { @NotNull TextAttributes textAttr = info.file.getFileType().equals(JsonFileType.INSTANCE) || info.file.getFileType().equals(Json5FileType.INSTANCE) ? info.appSettings.lineEndJsonTextAttr : info.appSettings.lineEndTextAttr; - return new LineExtensionInfo(info.appSettings.lineEndPrefix + doc, textAttr); + return new LineExtensionInfo(text, textAttr); } public static void textWithDoc(@NotNull FileInfo fileInfo, int startLine, int endLine, diff --git a/src/main/java/io/github/linwancen/plugin/show/LineEndCopy.java b/src/main/java/io/github/linwancen/plugin/show/LineEndCopy.java index 6ff649a..75ca54c 100644 --- a/src/main/java/io/github/linwancen/plugin/show/LineEndCopy.java +++ b/src/main/java/io/github/linwancen/plugin/show/LineEndCopy.java @@ -9,6 +9,7 @@ import com.intellij.openapi.ide.CopyPasteManager; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.Task; import com.intellij.openapi.project.DumbAwareAction; +import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import io.github.linwancen.plugin.show.bean.FileInfo; import io.github.linwancen.plugin.show.settings.ShowBundle; @@ -53,27 +54,28 @@ public class LineEndCopy extends DumbAwareAction { @Override public void run(@NotNull ProgressIndicator indicator) { indicator.setIndeterminate(false); - ApplicationManager.getApplication().runReadAction(() -> { - int startLine = 0; - int endLine = info.document.getLineCount() - 1; - // if select - @Nullable Editor editor = event.getData(CommonDataKeys.EDITOR); - if (editor != null) { - @NotNull Caret primaryCaret = editor.getCaretModel().getPrimaryCaret(); - int start = primaryCaret.getSelectionStart(); - int end = primaryCaret.getSelectionEnd(); - try { - startLine = info.document.getLineNumber(start); - endLine = info.document.getLineNumber(end); - } catch (Exception e) { - return; - } - } - LineEnd.textWithDoc(info, startLine, endLine, indicator, s -> { - @NotNull StringSelection content = new StringSelection(s); - CopyPasteManager.getInstance().setContents(content); - }); - }); + DumbService.getInstance(project).runReadActionInSmartMode(() -> + ApplicationManager.getApplication().runReadAction(() -> { + int startLine = 0; + int endLine = info.document.getLineCount() - 1; + // if select + @Nullable Editor editor = event.getData(CommonDataKeys.EDITOR); + if (editor != null) { + @NotNull Caret primaryCaret = editor.getCaretModel().getPrimaryCaret(); + int start = primaryCaret.getSelectionStart(); + int end = primaryCaret.getSelectionEnd(); + try { + startLine = info.document.getLineNumber(start); + endLine = info.document.getLineNumber(end); + } catch (Exception e) { + return; + } + } + LineEnd.textWithDoc(info, startLine, endLine, indicator, s -> { + @NotNull StringSelection content = new StringSelection(s); + CopyPasteManager.getInstance().setContents(content); + }); + })); } }.queue(); } diff --git a/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCacheUtils.java b/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCacheUtils.java index 5c2ab4e..621945f 100644 --- a/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCacheUtils.java +++ b/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCacheUtils.java @@ -1,8 +1,8 @@ package io.github.linwancen.plugin.show.cache; -import com.intellij.openapi.application.Application; -import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.editor.LineExtensionInfo; +import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; @@ -36,7 +36,7 @@ public class LineEndCacheUtils { @NotNull LineInfo oldInfo = lineCache.info; lineCache.info = info; lineCache.show = true; - checkScheduleAndInit(); + checkScheduleAndInit(info.project); @Nullable List list = lineCache.map.get(info.text); // load from other line if (list == null && info.lineCount != oldInfo.lineCount) { @@ -65,8 +65,11 @@ public class LineEndCacheUtils { private static volatile boolean isRun = false; - private static void checkScheduleAndInit() { + private static void checkScheduleAndInit(Project project) { if (!isRun) { + if (DumbService.isDumb(project)) { + return; + } synchronized (LineEndCacheUtils.class) { if (!isRun) { isRun = true; @@ -89,22 +92,15 @@ public class LineEndCacheUtils { cache.remove(project); return; } - if (DumbService.isDumb(project)) { - return; - } fileMap.forEach((file, lineMap) -> lineMap.forEach((lineNumber, lineCache) -> { @NotNull LineInfo info = lineCache.info; @Nullable List list = lineCache.map.get(info.text); if (!(lineCache.needUpdate() || list == null)) { return; } - Application application = ApplicationManager.getApplication(); - if (application == null) { - return; - } - application.runReadAction(() -> { + ReadAction.nonBlocking(() -> { try { - if (project.isDisposed()) { + if (project.isDisposed() || DumbService.isDumb(project)) { return; } @Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info); @@ -123,16 +119,18 @@ public class LineEndCacheUtils { if (list != null) { list.add(lineExt); } else { - ArrayList lineExtList = new ArrayList<>(); + ArrayList lineExtList = new ArrayList<>(1); lineExtList.add(lineExt); lineCache.map.put(info.text, lineExtList); } } lineCache.updated(); + } catch (ProcessCanceledException ignore) { + // ignore } catch (Exception e) { LOG.info("LineEndCacheUtils lineMap.forEach catch Throwable but log to record.", e); } - }); + }).inSmartMode(project).executeSynchronously(); })); } catch (Exception e) { LOG.info("LineEndCacheUtils cache.forEach catch Throwable but log to record.", e); diff --git a/src/main/java/io/github/linwancen/plugin/show/cache/TreeCacheUtils.java b/src/main/java/io/github/linwancen/plugin/show/cache/TreeCacheUtils.java index b3d9028..17303f8 100644 --- a/src/main/java/io/github/linwancen/plugin/show/cache/TreeCacheUtils.java +++ b/src/main/java/io/github/linwancen/plugin/show/cache/TreeCacheUtils.java @@ -1,6 +1,7 @@ package io.github.linwancen.plugin.show.cache; import com.intellij.ide.projectView.ProjectViewNode; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; @@ -27,7 +28,7 @@ public class TreeCacheUtils { .computeIfAbsent(project, a -> new ConcurrentHashMap<>()) .computeIfAbsent(node, a -> new TreeCache()); treeCache.needUpdate = true; - checkScheduleAndInit(); + checkScheduleAndInit(project); return treeCache.doc; } catch (ProcessCanceledException e) { return null; @@ -39,8 +40,11 @@ public class TreeCacheUtils { private static volatile boolean isRun = false; - private static void checkScheduleAndInit() { + private static void checkScheduleAndInit(Project project) { if (!isRun) { + if (DumbService.isDumb(project)) { + return; + } synchronized (TreeCacheUtils.class) { if (!isRun) { isRun = true; @@ -63,19 +67,21 @@ public class TreeCacheUtils { cache.remove(project); return; } - if (DumbService.isDumb(project)) { - return; - } nodeCache.forEach((node, treeCache) -> { if (treeCache.needUpdate) { - treeCache.needUpdate = false; - DumbService.getInstance(project).runReadActionInSmartMode(() -> { + ReadAction.nonBlocking(() -> { try { + if (project.isDisposed() || DumbService.isDumb(project)) { + return; + } treeCache.doc = Tree.treeDoc(node, project); + treeCache.needUpdate = false; + } catch (ProcessCanceledException ignore) { + // ignore } catch (Exception e) { LOG.info("TreeCacheUtils nodeCache.forEach catch Throwable but log to record.", e); } - }); + }).inSmartMode(project).executeSynchronously(); } }); } catch (Exception e) { diff --git a/src/main/java/io/github/linwancen/plugin/show/ext/conf/ConfCache.java b/src/main/java/io/github/linwancen/plugin/show/ext/conf/ConfCache.java index 9ed49c0..b4b4881 100644 --- a/src/main/java/io/github/linwancen/plugin/show/ext/conf/ConfCache.java +++ b/src/main/java/io/github/linwancen/plugin/show/ext/conf/ConfCache.java @@ -4,6 +4,7 @@ import com.intellij.ide.projectView.ProjectView; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.Task; +import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.search.FilenameIndex; @@ -127,25 +128,27 @@ public class ConfCache { @Override public void run(@NotNull ProgressIndicator indicator) { indicator.setIndeterminate(false); - ApplicationManager.getApplication().runReadAction(() -> { - @NotNull Collection files = FilenameIndex.getAllFilesByExt(project, TsvLoader.EXT); - @NotNull StringBuilder sb = new StringBuilder(); - double i = 0; - for (@NotNull VirtualFile file : files) { - indicator.setText(file.getName()); - load(file); - i++; - indicator.setFraction(i / files.size()); - sb.append(file.getName()).append("\n"); - } - if (files.isEmpty()) { - return; - } - if (!project.isDisposed()) { - ProjectView.getInstance(project).refresh(); - } - LOG.info("Ext doc conf load all complete {} files\n{}", files.size(), sb); - }); + DumbService.getInstance(project).runReadActionInSmartMode(() -> + ApplicationManager.getApplication().runReadAction(() -> { + @NotNull Collection files = FilenameIndex.getAllFilesByExt(project, + TsvLoader.EXT); + @NotNull StringBuilder sb = new StringBuilder(); + double i = 0; + for (@NotNull VirtualFile file : files) { + indicator.setText(file.getName()); + load(file); + i++; + indicator.setFraction(i / files.size()); + sb.append(file.getName()).append("\n"); + } + if (files.isEmpty()) { + return; + } + if (!project.isDisposed()) { + ProjectView.getInstance(project).refresh(); + } + LOG.info("Ext doc conf load all complete {} files\n{}", files.size(), sb); + })); } }.queue(); }