groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Cache the text of GString for better performance
Date Fri, 05 Jan 2018 12:36:27 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 20fa92c95 -> 628a19aab


Cache the text of GString for better performance


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

Branch: refs/heads/master
Commit: 628a19aabbb222644e18dd54e10f53263930a1a9
Parents: 20fa92c
Author: sunlan <sunlan@apache.org>
Authored: Fri Jan 5 20:36:18 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Fri Jan 5 20:36:18 2018 +0800

----------------------------------------------------------------------
 src/main/groovy/groovy/lang/GString.java | 34 ++++++++++++++++++---------
 1 file changed, 23 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/628a19aa/src/main/groovy/groovy/lang/GString.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/lang/GString.java b/src/main/groovy/groovy/lang/GString.java
index cc13eef..5de910f 100644
--- a/src/main/groovy/groovy/lang/GString.java
+++ b/src/main/groovy/groovy/lang/GString.java
@@ -42,20 +42,26 @@ import java.util.regex.Pattern;
  */
 public abstract class GString extends GroovyObjectSupport implements Comparable, CharSequence,
Writable, Buildable, Serializable {
 
-    static final long serialVersionUID = -2638020355892246323L;
+    private static final long serialVersionUID = -2638020355892246323L;
+    private static final String MKP = "mkp";
+    private static final String YIELD = "yield";
 
     /**
      * A GString containing a single empty String and no values.
      */
     public static final GString EMPTY = new GString(new Object[0]) {
+        private static final long serialVersionUID = -7676746462783374250L;
+
         @Override
         public String[] getStrings() {
-            return new String[]{""};
+            return new String[]{ "" };
         }
     };
+
     public static final String[] EMPTY_STRING_ARRAY = new String[0];
     public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
 
+    private String string;
     private final Object[] values;
 
     public GString(Object values) {
@@ -142,6 +148,10 @@ public abstract class GString extends GroovyObjectSupport implements
Comparable,
 
     @Override
     public String toString() {
+        if (null != string) {
+            return string;
+        }
+
         Writer buffer = new StringBuilderWriter(calcInitialCapacity());
         try {
             writeTo(buffer);
@@ -149,7 +159,8 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
         catch (IOException e) {
             throw new StringWriterIOException(e);
         }
-        return buffer.toString();
+
+        return (string = buffer.toString());
     }
 
     private int calcInitialCapacity() {
@@ -160,7 +171,7 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
             initialCapacity += string.length();
         }
 
-        initialCapacity += values.length * Math.max(Math.ceil(initialCapacity / strings.length),
1);
+        initialCapacity += values.length * Math.max(initialCapacity / strings.length, 1);
 
         return Math.max((int) (initialCapacity  * 1.2), 16);
     }
@@ -176,14 +187,15 @@ public abstract class GString extends GroovyObjectSupport implements
Comparable,
 
                 if (value instanceof Closure) {
                     final Closure c = (Closure) value;
+                    int maximumNumberOfParameters = c.getMaximumNumberOfParameters();
 
-                    if (c.getMaximumNumberOfParameters() == 0) {
+                    if (maximumNumberOfParameters == 0) {
                         InvokerHelper.write(out, c.call());
-                    } else if (c.getMaximumNumberOfParameters() == 1) {
+                    } else if (maximumNumberOfParameters == 1) {
                         c.call(out);
                     } else {
                         throw new GroovyRuntimeException("Trying to evaluate a GString containing
a Closure taking "
-                                + c.getMaximumNumberOfParameters() + " parameters");
+                                + maximumNumberOfParameters + " parameters");
                     }
                 } else {
                     InvokerHelper.write(out, value);
@@ -203,11 +215,11 @@ public abstract class GString extends GroovyObjectSupport implements
Comparable,
         final int numberOfValues = values.length;
 
         for (int i = 0, size = s.length; i < size; i++) {
-            builder.getProperty("mkp");
-            builder.invokeMethod("yield", new Object[]{s[i]});
+            builder.getProperty(MKP);
+            builder.invokeMethod(YIELD, new Object[]{ s[i] });
             if (i < numberOfValues) {
-                builder.getProperty("mkp");
-                builder.invokeMethod("yield", new Object[]{values[i]});
+                builder.getProperty(MKP);
+                builder.invokeMethod(YIELD, new Object[]{ values[i] });
             }
         }
     }


Mime
View raw message