groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [2/3] groovy git commit: Refine `GString.plus` for better performance
Date Sun, 31 Dec 2017 16:17:24 GMT
Refine `GString.plus` 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/2b382c9a
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/2b382c9a
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/2b382c9a

Branch: refs/heads/GROOVY_2_4_X
Commit: 2b382c9a857ddfc5b92793cd2ecb7431c296face
Parents: f0eb637
Author: sunlan <sunlan@apache.org>
Authored: Mon Jan 1 00:11:51 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Mon Jan 1 00:11:51 2018 +0800

----------------------------------------------------------------------
 src/main/groovy/lang/GString.java | 45 ++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/2b382c9a/src/main/groovy/lang/GString.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/GString.java b/src/main/groovy/lang/GString.java
index cd88a2b..42a1e43 100644
--- a/src/main/groovy/lang/GString.java
+++ b/src/main/groovy/lang/GString.java
@@ -27,9 +27,6 @@ import java.io.Serializable;
 import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
 import java.util.regex.Pattern;
 
 /**
@@ -94,28 +91,40 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
     }
 
     public GString plus(GString that) {
-        List<String> stringList = new ArrayList<String>(Arrays.asList(getStrings()));
-        List<Object> valueList = new ArrayList<Object>(Arrays.asList(getValues()));
+        Object[] values = getValues();
 
-        List<String> thatStrings = Arrays.asList(that.getStrings());
+        return new GStringImpl(appendValues(values, that.getValues()), appendStrings(getStrings(),
that.getStrings(), values.length));
+    }
+
+    private String[] appendStrings(String[] strings, String[] thatStrings, int valuesLength)
{
+        int stringsLength = strings.length;
+        boolean isStringsLonger = stringsLength > valuesLength;
+        int lastIndexOfStrings = stringsLength - 1;
+        int thatStringsLength = isStringsLonger ? thatStrings.length - 1 : thatStrings.length;
+
+        String[] newStrings = new String[stringsLength + thatStringsLength];
+        System.arraycopy(strings, 0, newStrings, 0, stringsLength);
 
-        int stringListSize = stringList.size();
-        if (stringListSize > valueList.size()) {
-            thatStrings = new ArrayList<String>(thatStrings);
+        if (isStringsLonger) {
             // merge onto end of previous GString to avoid an empty bridging value
-            int lastIndexOfStringList = stringListSize - 1;
-            String s = stringList.get(lastIndexOfStringList);
-            s += thatStrings.remove(0);
-            stringList.set(lastIndexOfStringList, s);
+            System.arraycopy(thatStrings, 1, newStrings, stringsLength, thatStringsLength);
+            newStrings[lastIndexOfStrings] = strings[lastIndexOfStrings] + thatStrings[0];
+        } else {
+            System.arraycopy(thatStrings, 0, newStrings, stringsLength, thatStringsLength);
         }
 
-        stringList.addAll(thatStrings);
-        valueList.addAll(Arrays.asList(that.getValues()));
+        return newStrings;
+    }
+
+    private Object[] appendValues(Object[] values, Object[] thatValues) {
+        int valuesLength = values.length;
+        int thatValuesLength = thatValues.length;
 
-        final String[] newStrings = stringList.toArray(EMPTY_STRING_ARRAY);
-        final Object[] newValues = valueList.toArray();
+        Object[] newValues = new Object[valuesLength + thatValuesLength];
+        System.arraycopy(values, 0, newValues, 0, valuesLength);
+        System.arraycopy(thatValues, 0, newValues, valuesLength, thatValuesLength);
 
-        return new GStringImpl(newValues, newStrings);
+        return newValues;
     }
 
     public GString plus(String that) {


Mime
View raw message