You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/07/12 16:46:19 UTC

[groovy] branch GROOVY-9637 created (now 2711f7c)

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

sunlan pushed a change to branch GROOVY-9637
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at 2711f7c  GROOVY-9637: Improve the performance of GString

This branch includes the following new commits:

     new 2711f7c  GROOVY-9637: Improve the performance of GString

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[groovy] 01/01: GROOVY-9637: Improve the performance of GString

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY-9637
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 2711f7c8e8914d297caec19fb37708101420349b
Author: Daniel Sun <su...@apache.org>
AuthorDate: Mon Jul 13 00:45:55 2020 +0800

    GROOVY-9637: Improve the performance of GString
---
 src/main/java/groovy/lang/GString.java | 46 +++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

diff --git a/src/main/java/groovy/lang/GString.java b/src/main/java/groovy/lang/GString.java
index 207a1a6..a9128c1 100644
--- a/src/main/java/groovy/lang/GString.java
+++ b/src/main/java/groovy/lang/GString.java
@@ -27,6 +27,11 @@ import java.io.IOException;
 import java.io.Serializable;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.regex.Pattern;
 
 /**
@@ -48,6 +53,22 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
     public static final String[] EMPTY_STRING_ARRAY = new String[0];
     public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
 
+    private static final Set<Class<?>> IMMUTABLE_TYPE_LIST =
+            new HashSet<>(
+                    Arrays.asList(
+                            String.class, Class.class,
+                            BigDecimal.class, BigInteger.class,
+                            boolean.class, Boolean.class,
+                            char.class, Character.class,
+                            byte.class, Byte.class,
+                            short.class, Short.class,
+                            int.class, Integer.class,
+                            long.class, Long.class,
+                            float.class, Float.class,
+                            double.class, Double.class
+                    )
+            );
+
     /**
      * A GString containing a single empty String and no values.
      */
@@ -68,13 +89,16 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
 
 
     private final Object[] values;
+    private final boolean immutable;
 
     public GString(Object values) {
         this.values = (Object[]) values;
+        this.immutable = isValuesImmutable();
     }
 
     public GString(Object[] values) {
         this.values = values;
+        this.immutable = isValuesImmutable();
     }
 
     // will be static in an instance
@@ -150,8 +174,15 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
         return values[idx];
     }
 
+
+    private String cachedLiteralString;
+
     @Override
     public String toString() {
+        if (null != cachedLiteralString) {
+            return cachedLiteralString;
+        }
+
         Writer buffer = new StringBuilderWriter(calcInitialCapacity());
         try {
             writeTo(buffer);
@@ -159,7 +190,9 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
             throw new StringWriterIOException(e);
         }
 
-        return buffer.toString();
+        String str = buffer.toString();
+
+        return immutable ? (cachedLiteralString = str) : str;
     }
 
     private int calcInitialCapacity() {
@@ -276,4 +309,15 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
     public byte[] getBytes(String charset) throws UnsupportedEncodingException {
         return toString().getBytes(charset);
     }
+
+    private boolean isValuesImmutable() {
+        for (Object value : values) {
+            if (null == value) continue;
+            if (!IMMUTABLE_TYPE_LIST.contains(value.getClass())) {
+                return false;
+            }
+        }
+
+        return true;
+    }
 }