Skip to content

Commit 6e60e6a

Browse files
Fix performance regressions in Strings utility (#91462)
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.
1 parent b46ee9c commit 6e60e6a

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

server/src/main/java/org/elasticsearch/common/Strings.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,13 @@ public static boolean hasText(CharSequence str) {
137137
if (hasLength(str) == false) {
138138
return false;
139139
}
140-
return str.chars().anyMatch(c -> Character.isWhitespace(c) == false);
140+
int strLen = str.length();
141+
for (int i = 0; i < strLen; i++) {
142+
if (Character.isWhitespace(str.charAt(i)) == false) {
143+
return true;
144+
}
145+
}
146+
return false;
141147
}
142148

143149
/**
@@ -151,7 +157,7 @@ public static boolean hasText(CharSequence str) {
151157
* @see #hasText(CharSequence)
152158
*/
153159
public static boolean hasText(String str) {
154-
return hasText((CharSequence) str);
160+
return isNullOrBlank(str) == false;
155161
}
156162

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

288294
public static boolean validFileName(String fileName) {
289-
return fileName.chars().noneMatch(c -> isInvalidFileNameCharacter((char) c));
295+
for (int i = 0; i < fileName.length(); i++) {
296+
if (isInvalidFileNameCharacter(fileName.charAt(i))) {
297+
return false;
298+
}
299+
}
300+
return true;
290301
}
291302

292303
public static boolean validFileNameExcludingAstrix(String fileName) {
293-
return fileName.chars().noneMatch(c -> c != '*' && isInvalidFileNameCharacter((char) c));
304+
for (int i = 0; i < fileName.length(); i++) {
305+
char c = fileName.charAt(i);
306+
if (c != '*' && isInvalidFileNameCharacter(c)) {
307+
return false;
308+
}
309+
}
310+
return true;
294311
}
295312

296313
private static boolean isInvalidFileNameCharacter(char c) {

0 commit comments

Comments
 (0)