瀏覽代碼

Fix performance regressions in Strings utility (#91462)

Some of the changes in
https://github.com/elastic/elasticsearch/pull/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.
Armin Braun 3 年之前
父節點
當前提交
6e60e6ac9e
共有 1 個文件被更改,包括 21 次插入4 次删除
  1. 21 4
      server/src/main/java/org/elasticsearch/common/Strings.java

+ 21 - 4
server/src/main/java/org/elasticsearch/common/Strings.java

@@ -137,7 +137,13 @@ public class Strings {
         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;
     }
 
     /**
@@ -151,7 +157,7 @@ public class Strings {
      * @see #hasText(CharSequence)
      */
     public static boolean hasText(String str) {
-        return hasText((CharSequence) str);
+        return isNullOrBlank(str) == false;
     }
 
     /**
@@ -286,11 +292,22 @@ public class Strings {
         .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) {