You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2018/12/11 04:49:49 UTC

curator git commit: Make ZKPaths.makePath() methods to allocate less garbage

Repository: curator
Updated Branches:
  refs/heads/master 2a9f04aa3 -> 123a739f2


Make ZKPaths.makePath() methods to allocate less garbage


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/123a739f
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/123a739f
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/123a739f

Branch: refs/heads/master
Commit: 123a739f258d3d1c35e28454168a209936e51cde
Parents: 2a9f04a
Author: Roman Leventov <le...@gmail.com>
Authored: Mon Dec 10 19:37:52 2018 +0100
Committer: Roman Leventov <le...@gmail.com>
Committed: Mon Dec 10 19:37:52 2018 +0100

----------------------------------------------------------------------
 .../java/org/apache/curator/utils/ZKPaths.java  | 68 ++++++++++++++------
 .../recipes/queue/DistributedDelayQueue.java    |  2 +-
 2 files changed, 51 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/123a739f/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java
----------------------------------------------------------------------
diff --git a/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java b/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java
index 8e94d2b..378e25d 100644
--- a/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java
+++ b/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java
@@ -38,6 +38,8 @@ public class ZKPaths
      */
     public static final String PATH_SEPARATOR = "/";
 
+    private static final char PATH_SEPARATOR_CHAR = '/';
+
     private static final CreateMode NON_CONTAINER_MODE = CreateMode.PERSISTENT;
 
     /**
@@ -120,7 +122,7 @@ public class ZKPaths
     public static String getNodeFromPath(String path)
     {
         PathUtils.validatePath(path);
-        int i = path.lastIndexOf(PATH_SEPARATOR);
+        int i = path.lastIndexOf(PATH_SEPARATOR_CHAR);
         if ( i < 0 )
         {
             return path;
@@ -163,7 +165,7 @@ public class ZKPaths
     public static PathAndNode getPathAndNode(String path)
     {
         PathUtils.validatePath(path);
-        int i = path.lastIndexOf(PATH_SEPARATOR);
+        int i = path.lastIndexOf(PATH_SEPARATOR_CHAR);
         if ( i < 0 )
         {
             return new PathAndNode(path, "");
@@ -192,7 +194,7 @@ public class ZKPaths
         return length > SEQUENTIAL_SUFFIX_DIGITS ? path.substring(length - SEQUENTIAL_SUFFIX_DIGITS) : path;
     }
 
-    private static final Splitter PATH_SPLITTER = Splitter.on(PATH_SEPARATOR).omitEmptyStrings();
+    private static final Splitter PATH_SPLITTER = Splitter.on(PATH_SEPARATOR_CHAR).omitEmptyStrings();
 
     /**
      * Given a full path, return the the individual parts, without slashes.
@@ -271,7 +273,7 @@ public class ZKPaths
         int pos = 1; // skip first slash, root is guaranteed to exist
         do
         {
-            pos = path.indexOf(PATH_SEPARATOR, pos + 1);
+            pos = path.indexOf(PATH_SEPARATOR_CHAR, pos + 1);
 
             if ( pos == -1 )
             {
@@ -388,7 +390,10 @@ public class ZKPaths
      */
     public static String makePath(String parent, String child)
     {
-        StringBuilder path = new StringBuilder();
+        // 2 is the maximum number of additional path separators inserted
+        int maxPathLength = nullableStringLength(parent) + nullableStringLength(child) + 2;
+        // Avoid internal StringBuilder's buffer reallocation by specifying the max path length
+        StringBuilder path = new StringBuilder(maxPathLength);
 
         joinPath(path, parent, child);
 
@@ -405,7 +410,18 @@ public class ZKPaths
      */
     public static String makePath(String parent, String firstChild, String... restChildren)
     {
-        StringBuilder path = new StringBuilder();
+        // 2 is the maximum number of additional path separators inserted
+        int maxPathLength = nullableStringLength(parent) + nullableStringLength(firstChild) + 2;
+        if ( restChildren != null )
+        {
+            for ( String child : restChildren )
+            {
+                // 1 is for possible additional separator
+                maxPathLength += nullableStringLength(child) + 1;
+            }
+        }
+        // Avoid internal StringBuilder's buffer reallocation by specifying the max path length
+        StringBuilder path = new StringBuilder(maxPathLength);
 
         joinPath(path, parent, firstChild);
 
@@ -424,6 +440,11 @@ public class ZKPaths
         }
     }
 
+    private static int nullableStringLength(String s)
+    {
+        return s != null ? s.length() : 0;
+    }
+
     /**
      * Given a parent and a child node, join them in the given {@link StringBuilder path}
      *
@@ -436,13 +457,13 @@ public class ZKPaths
         // Add parent piece, with no trailing slash.
         if ( (parent != null) && (parent.length() > 0) )
         {
-            if ( !parent.startsWith(PATH_SEPARATOR) )
+            if ( parent.charAt(0) != PATH_SEPARATOR_CHAR )
             {
-                path.append(PATH_SEPARATOR);
+                path.append(PATH_SEPARATOR_CHAR);
             }
-            if ( parent.endsWith(PATH_SEPARATOR) )
+            if ( parent.charAt(parent.length() - 1) == PATH_SEPARATOR_CHAR )
             {
-                path.append(parent.substring(0, parent.length() - 1));
+                path.append(parent, 0, parent.length() - 1);
             }
             else
             {
@@ -450,31 +471,42 @@ public class ZKPaths
             }
         }
 
-        if ( (child == null) || (child.length() == 0) || (child.equals(PATH_SEPARATOR)) )
+        if ( (child == null) || (child.length() == 0) ||
+                (child.length() == 1 && child.charAt(0) == PATH_SEPARATOR_CHAR) )
         {
             // Special case, empty parent and child
             if ( path.length() == 0 )
             {
-                path.append(PATH_SEPARATOR);
+                path.append(PATH_SEPARATOR_CHAR);
             }
             return;
         }
 
         // Now add the separator between parent and child.
-        path.append(PATH_SEPARATOR);
+        path.append(PATH_SEPARATOR_CHAR);
 
-        if ( child.startsWith(PATH_SEPARATOR) )
+        int childAppendBeginIndex;
+        if ( child.charAt(0) == PATH_SEPARATOR_CHAR )
+        {
+            childAppendBeginIndex = 1;
+        }
+        else
         {
-            child = child.substring(1);
+            childAppendBeginIndex = 0;
         }
 
-        if ( child.endsWith(PATH_SEPARATOR) )
+        int childAppendEndIndex;
+        if ( child.charAt(child.length() - 1) == PATH_SEPARATOR_CHAR )
+        {
+            childAppendEndIndex = child.length() - 1;
+        }
+        else
         {
-            child = child.substring(0, child.length() - 1);
+            childAppendEndIndex = child.length();
         }
 
         // Finally, add the child.
-        path.append(child);
+        path.append(child, childAppendBeginIndex, childAppendEndIndex);
     }
 
     private ZKPaths()

http://git-wip-us.apache.org/repos/asf/curator/blob/123a739f/curator-recipes/src/main/java/org/apache/curator/framework/recipes/queue/DistributedDelayQueue.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/queue/DistributedDelayQueue.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/queue/DistributedDelayQueue.java
index ff39f42..8f321b3 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/queue/DistributedDelayQueue.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/queue/DistributedDelayQueue.java
@@ -42,7 +42,7 @@ public class DistributedDelayQueue<T> implements Closeable, QueueBase<T>
 {
     private final DistributedQueue<T>      queue;
 
-    private static final String            SEPARATOR = "|";
+    private static final char              SEPARATOR = '|';
 
     DistributedDelayQueue
         (