You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/02/17 11:00:18 UTC

[groovy] branch GROOVY_3_0_X updated: GROOVY-6360: GString performance is slow with String's method (add special GString DGM method for StringBuilder)

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

paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 03b1431  GROOVY-6360: GString performance is slow with String's method (add special GString DGM method for StringBuilder)
03b1431 is described below

commit 03b1431f73b1a2a2ff0cbe30f62ab9e3b5417f52
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Feb 17 16:43:04 2021 +1000

    GROOVY-6360: GString performance is slow with String's method (add special GString DGM method for StringBuilder)
---
 .../groovy/runtime/StringGroovyMethods.java        | 14 ++++++++
 src/test/groovy/bugs/Groovy6360.groovy             | 40 ++++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
index 10eb344..31788cf 100644
--- a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
@@ -221,6 +221,20 @@ public class StringGroovyMethods extends DefaultGroovyMethodsSupport {
     }
 
     /**
+     * Append the GString to the StringBuilder using a more efficient
+     * approach than Java's default approach for a CharSequence.
+     *
+     * @param sb a StringBuilder
+     * @param gs a GStringImpl
+     * @return the StringBuilder
+     *
+     * @since 3.0.8
+     */
+    public static StringBuilder append(final StringBuilder sb, GStringImpl gs) {
+        return sb.append(gs.toString());
+    }
+
+    /**
      * Convenience method to uncapitalize the first letter of a CharSequence
      * (typically the first letter of a word). Example usage:
      * <pre class="groovyTestCase">
diff --git a/src/test/groovy/bugs/Groovy6360.groovy b/src/test/groovy/bugs/Groovy6360.groovy
new file mode 100644
index 0000000..056bfa4
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy6360.groovy
@@ -0,0 +1,40 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs
+
+import org.junit.Test
+
+final class Groovy6360 {
+    @Test
+    void testStringBuilderAppendOnGString() {
+        Item item = new Item()
+        new StringBuilder().append("item: ${item}")
+        assert item.toStringInvocationCount == 1
+    }
+
+    private static final class Item {
+        private int toStringInvocationCount = 0
+
+        @Override
+        synchronized String toString() {
+            toStringInvocationCount++
+            "item"
+        }
+    }
+}