You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by ne...@apache.org on 2022/01/13 17:56:07 UTC

[pinot] branch master updated: reduce allocations and speed up StringUtil.sanitizeString (#8013)

This is an automated email from the ASF dual-hosted git repository.

nehapawar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new 61608a4  reduce allocations and speed up StringUtil.sanitizeString (#8013)
61608a4 is described below

commit 61608a40e11bbbeca124a04ada91d06ec62c0230
Author: Richard Startin <ri...@startree.ai>
AuthorDate: Thu Jan 13 17:55:40 2022 +0000

    reduce allocations and speed up StringUtil.sanitizeString (#8013)
---
 .../org/apache/pinot/common/utils/StringUtil.java     | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)

diff --git a/pinot-common/src/main/java/org/apache/pinot/common/utils/StringUtil.java b/pinot-common/src/main/java/org/apache/pinot/common/utils/StringUtil.java
index 82915e3..7a03c96 100644
--- a/pinot-common/src/main/java/org/apache/pinot/common/utils/StringUtil.java
+++ b/pinot-common/src/main/java/org/apache/pinot/common/utils/StringUtil.java
@@ -26,7 +26,8 @@ public class StringUtil {
   private StringUtil() {
   }
 
-  private static final char NULL_CHARACTER = '\0';
+  // prefer string to character because String.indexOf(String) is a fast intrinsic on all JDK versions
+  private static final String NULL_CHARACTER = "\0";
 
   /**
    * Joins the given keys with the separator.
@@ -47,18 +48,10 @@ public class StringUtil {
    * @return Modified value, or value itself if not modified
    */
   public static String sanitizeStringValue(String value, int maxLength) {
-    char[] chars = value.toCharArray();
-    int length = chars.length;
-    int limit = Math.min(length, maxLength);
-    for (int i = 0; i < limit; i++) {
-      if (chars[i] == NULL_CHARACTER) {
-        return new String(chars, 0, i);
-      }
-    }
-    if (limit < length) {
-      return new String(chars, 0, limit);
-    } else {
-      return value;
+    int index = value.indexOf(NULL_CHARACTER);
+    if (index < 0) {
+      return value.length() <= maxLength ? value : value.substring(0, maxLength);
     }
+    return value.substring(0, Math.min(index, maxLength));
   }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org