InvokerHelper: Refactor: Better StringBuffer pre-allocation

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

Branch: refs/heads/master
Commit: a4e12e189340ebb407a45287f43fa5b5f6e79419
Parents: 2a11f88
Author: Thibault Kruse <thibault.kr...@gmx.de>
Authored: Fri Aug 21 16:06:46 2015 +0200
Committer: paulk <pa...@asert.com.au>
Committed: Thu Jul 28 22:39:51 2016 +1000

----------------------------------------------------------------------
 .../codehaus/groovy/runtime/InvokerHelper.java  | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/a4e12e18/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java 
b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
index 74bfb34..ad4bb26 100644
--- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -62,6 +62,9 @@ public class InvokerHelper {
     protected static final Object[] EMPTY_ARGUMENTS = EMPTY_ARGS;
     protected static final Class[] EMPTY_TYPES = {};
 
+    // heuristic size to pre-alocate stringbuffers for collections of items
+    private static final int ITEM_ALLOCATE_SIZE = 5;
+
     public static final MetaClassRegistry metaRegistry = 
GroovySystem.getMetaClassRegistry();
 
     public static void removeClass(Class clazz) {
@@ -645,7 +648,8 @@ public class InvokerHelper {
         if (map.isEmpty()) {
             return "[:]";
         }
-        StringBuilder buffer = new StringBuilder("[");
+        StringBuilder buffer = new StringBuilder(ITEM_ALLOCATE_SIZE * 
map.size() * 2);
+        buffer.append('[');
         boolean first = true;
         for (Object o : map.entrySet()) {
             if (first) {
@@ -666,7 +670,7 @@ public class InvokerHelper {
                 buffer.append(format(entry.getValue(), verbose, 
sizeLeft(maxSize, buffer)));
             }
         }
-        buffer.append("]");
+        buffer.append(']');
         return buffer.toString();
     }
 
@@ -679,7 +683,8 @@ public class InvokerHelper {
     }
 
     private static String formatList(Collection collection, boolean verbose, 
int maxSize, boolean safe) {
-        StringBuilder buffer = new StringBuilder("[");
+        StringBuilder buffer = new StringBuilder(ITEM_ALLOCATE_SIZE * 
collection.size());
+        buffer.append('[');
         boolean first = true;
         for (Object item : collection) {
             if (first) {
@@ -710,7 +715,7 @@ public class InvokerHelper {
                 buffer.append(str);
             }
         }
-        buffer.append("]");
+        buffer.append(']');
         return buffer.toString();
     }
 
@@ -799,16 +804,15 @@ public class InvokerHelper {
         if (arguments == null) {
             return "null";
         }
-        String sbdry = "[";
-        String ebdry = "]";
-        StringBuilder argBuf = new StringBuilder(sbdry);
+        StringBuilder argBuf = new StringBuilder(arguments.length);
+        argBuf.append('[');
         for (int i = 0; i < arguments.length; i++) {
             if (i > 0) {
                 argBuf.append(", ");
             }
             argBuf.append(format(arguments[i], false));
         }
-        argBuf.append(ebdry);
+        argBuf.append(']');
         return argBuf.toString();
     }
 

Reply via email to