Skip to content

Commit

Permalink
Improve performance of JS implementation of AlternativeLocationImpl
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
matthiasblaesing committed Jul 1, 2022
1 parent f6af4cb commit 3d00651
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> asLines;
Expand All @@ -435,6 +439,7 @@ private String getStringLocation() {
if (lineNumber > 0) {
result = result + " : " + lineNumber; //NOI18N
}
stringLocation = result;
return result;
}

Expand All @@ -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;
}

}
Expand Down

0 comments on commit 3d00651

Please sign in to comment.