Skip to content

Commit

Permalink
Fix performance regressions in Strings utility (#91462)
Browse files Browse the repository at this point in the history
Some of the changes in
#90672 introduced
considerable performance hits for many shards benchmarks because the
changed methods are used in hot loops here and there.
This reverts some of the changes to the previous code and delegates to
the fast `isNullOrBlank` to fix `hasText` performance for strings.
  • Loading branch information
original-brownbear authored Nov 9, 2022
1 parent b46ee9c commit 6e60e6a
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions server/src/main/java/org/elasticsearch/common/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ public static boolean hasText(CharSequence str) {
if (hasLength(str) == false) {
return false;
}
return str.chars().anyMatch(c -> Character.isWhitespace(c) == false);
int strLen = str.length();
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(str.charAt(i)) == false) {
return true;
}
}
return false;
}

/**
Expand All @@ -151,7 +157,7 @@ public static boolean hasText(CharSequence str) {
* @see #hasText(CharSequence)
*/
public static boolean hasText(String str) {
return hasText((CharSequence) str);
return isNullOrBlank(str) == false;
}

/**
Expand Down Expand Up @@ -286,11 +292,22 @@ private static String changeFirstCharacterCase(String str, boolean capitalize) {
.collect(Collectors.joining(",", "[", "]"));

public static boolean validFileName(String fileName) {
return fileName.chars().noneMatch(c -> isInvalidFileNameCharacter((char) c));
for (int i = 0; i < fileName.length(); i++) {
if (isInvalidFileNameCharacter(fileName.charAt(i))) {
return false;
}
}
return true;
}

public static boolean validFileNameExcludingAstrix(String fileName) {
return fileName.chars().noneMatch(c -> c != '*' && isInvalidFileNameCharacter((char) c));
for (int i = 0; i < fileName.length(); i++) {
char c = fileName.charAt(i);
if (c != '*' && isInvalidFileNameCharacter(c)) {
return false;
}
}
return true;
}

private static boolean isInvalidFileNameCharacter(char c) {
Expand Down

0 comments on commit 6e60e6a

Please sign in to comment.