From 3d0065135c1495c61451d655c956a11e921dfde2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bl=C3=A4sing?= Date: Thu, 30 Jun 2022 19:18:55 +0200 Subject: [PATCH] Improve performance of JS implementation of AlternativeLocationImpl #getStringLocation reads the original file, but is called from the EDT, so invocations must be kept to a minimum. What is more the method is used by #compareTo and thus is potentially called often. Two optimisations are applied: - the result of #getStringLocation is cached and only calculated once - #compareTo was rewritten to use the relative path and offset directly. Before relative path and the line number was used to create an ordering, as offset and line number are related, switching for ordering shoud have no outside result --- .../editor/navigation/DeclarationFinderImpl.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/navigation/DeclarationFinderImpl.java b/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/navigation/DeclarationFinderImpl.java index 2a1c13278ba0..332e40a61a93 100644 --- a/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/navigation/DeclarationFinderImpl.java +++ b/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/navigation/DeclarationFinderImpl.java @@ -398,6 +398,7 @@ public static class AlternativeLocationImpl implements AlternativeLocation { private final int offset; private final DeclarationLocation location; private final IndexedElement element; + private String stringLocation; public AlternativeLocationImpl(IndexResult iResult) { this.iResult = iResult; @@ -412,7 +413,10 @@ public ElementHandle getElement() { return element; } - private String getStringLocation() { + private String getStringLocation() { + if(stringLocation != null) { + return stringLocation; + } int lineNumber = 0; int count = 0; List asLines; @@ -435,6 +439,7 @@ private String getStringLocation() { if (lineNumber > 0) { result = result + " : " + lineNumber; //NOI18N } + stringLocation = result; return result; } @@ -452,7 +457,13 @@ public DeclarationLocation getLocation() { @Override public int compareTo(AlternativeLocation o) { AlternativeLocationImpl ali = (AlternativeLocationImpl)o; - return getStringLocation().compareTo(ali.getStringLocation()); + String relPath1 = iResult.getRelativePath(); + String relPath2 = ali.iResult.getRelativePath(); + int comparison = relPath1.compareTo(relPath2); + if(comparison != 0) { + return comparison; + } + return offset - ali.offset; } }