Bläddra i källkod

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 år sedan
förälder
incheckning
6e60e6ac9e
1 ändrade filer med 21 tillägg och 4 borttagningar
  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) {