Skip to content

Commit

Permalink
refact: avoid calling String.length() repeatedly inside loops
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jan 15, 2025
1 parent 6c51528 commit 8221cad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public static String getFilterFromDocument(IDocument document, int cursorOffset,
int prefixToCursorLength = cursorOffset - completionInsertionOffset;
String prefixToCursor = document.get(completionInsertionOffset, prefixToCursorLength);
int i;
for (i = 0; i < prefixToCursor.length(); i++) {
for (i = 0; i < prefixToCursorLength; i++) {
if (!isSubstringFoundOrderedInString(
prefixToCursor.substring(prefixToCursorLength - i - 1, prefixToCursorLength),
completionItemFilter)) {
break;
}
}
return prefixToCursor.substring(prefixToCursor.length() - i);
return prefixToCursor.substring(prefixToCursorLength - i);
}

/**
Expand Down Expand Up @@ -105,11 +105,13 @@ public static int getCategoryOfFilterMatch(String documentFilter, String complet
if (subIndex == -1) {
return isSubstringFoundOrderedInString(documentFilter, completionFilter) ? 4 : 5;
}
final int documentFilterLength = documentFilter.length();
final int completionFilterLength = completionFilter.length();
while (subIndex != -1) {
if (subIndex > 0 && Character.isLetterOrDigit(completionFilter.charAt(subIndex - 1))) {
topCategory = Math.min(topCategory, 3);
} else if (subIndex + documentFilter.length() < completionFilter.length() - 1
&& Character.isLetterOrDigit(completionFilter.charAt(subIndex + documentFilter.length() + 1))) {
} else if (subIndex + documentFilterLength < completionFilterLength - 1
&& Character.isLetterOrDigit(completionFilter.charAt(subIndex + documentFilterLength + 1))) {
topCategory = Math.min(topCategory, 2);
} else {
topCategory = 1;
Expand Down Expand Up @@ -158,12 +160,14 @@ private static int getScoreOfFilterMatchHelper(int prefixLength, String document
if (i == -1) {
return -1;
}
if (documentFilter.length() == 1) {

final int documentFilterLength = documentFilter.length();
if (documentFilterLength == 1) {
return i + prefixLength;
}

int matchLength = lengthOfPrefixMatch(documentFilter, completionFilter.substring(i));
if (matchLength == documentFilter.length()) {
if (matchLength == documentFilterLength) {
return i + prefixLength;
}
int bestScore = i + getScoreOfFilterMatchHelper(prefixLength + i + matchLength,
Expand All @@ -173,7 +177,7 @@ private static int getScoreOfFilterMatchHelper(int prefixLength, String document
i = completionFilter.indexOf(searchChar, i + 1);
while (i != -1) {
matchLength = lengthOfPrefixMatch(documentFilter, completionFilter.substring(i));
if (matchLength == documentFilter.length()) {
if (matchLength == documentFilterLength) {
return i + prefixLength;
}
int score = i + getScoreOfFilterMatchHelper(prefixLength + i + matchLength,
Expand All @@ -190,7 +194,8 @@ private static int getScoreOfFilterMatchHelper(int prefixLength, String document

private static int lengthOfPrefixMatch(String first, String second) {
int i;
for (i = 0; i < Math.min(first.length(), second.length()); i++) {
final var maxLength = Math.min(first.length(), second.length());
for (i = 0; i < maxLength; i++) {
if (first.charAt(i) != second.charAt(i))
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,13 @@ public int getPrefixCompletionStart(IDocument document, int completionOffset) {
LanguageServerPlugin.logError(e);
}
}
String insertText = getInsertText();
final String insertText = getInsertText();
final int insertTextLength = insertText.length();
try {
String subDoc = document.get(
Math.max(0, completionOffset - insertText.length()),
Math.min(insertText.length(), completionOffset));
for (int i = 0; i < insertText.length() && i < completionOffset; i++) {
Math.max(0, completionOffset - insertTextLength),
Math.min(insertTextLength, completionOffset));
for (int i = 0; i < insertTextLength && i < completionOffset; i++) {
String tentativeCommonString = subDoc.substring(i);
if (insertText.startsWith(tentativeCommonString)) {
return completionOffset - tentativeCommonString.length();
Expand Down Expand Up @@ -487,7 +488,8 @@ protected void apply(IDocument document, char trigger, int stateMask, int offset
// try to reuse existing characters after completion location
int shift = offset - bestOffset;
int commonSize = 0;
while (commonSize < insertText.length() - shift
final int insertTextLength = insertText.length();
while (commonSize < insertTextLength - shift
&& document.getLength() > offset + commonSize
&& document.getChar(bestOffset + shift + commonSize) == insertText.charAt(commonSize + shift)) {
commonSize++;
Expand Down

0 comments on commit 8221cad

Please sign in to comment.