8152951: Avoid calculating the reverse of StringConcatFactory$Recipe elements
authorredestad
Tue, 29 Mar 2016 18:27:33 +0200
changeset 36737 67b2804be5ab
parent 36736 ca66d3e0dfca
child 36738 34a9411515d9
8152951: Avoid calculating the reverse of StringConcatFactory$Recipe elements Reviewed-by: shade, vlivanov
jdk/src/java.base/share/classes/java/lang/invoke/StringConcatFactory.java
--- a/jdk/src/java.base/share/classes/java/lang/invoke/StringConcatFactory.java	Tue Mar 29 18:04:41 2016 +0900
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/StringConcatFactory.java	Tue Mar 29 18:27:33 2016 +0200
@@ -255,7 +255,6 @@
      */
     private static final class Recipe {
         private final List<RecipeElement> elements;
-        private final List<RecipeElement> elementsRev;
 
         public Recipe(String src, Object[] constants) {
             List<RecipeElement> el = new ArrayList<>();
@@ -294,19 +293,13 @@
                 el.add(new RecipeElement(acc.toString()));
             }
 
-            elements = new ArrayList<>(el);
-            Collections.reverse(el);
-            elementsRev = el;
+            elements = el;
         }
 
-        public Collection<RecipeElement> getElements() {
+        public List<RecipeElement> getElements() {
             return elements;
         }
 
-        public Collection<RecipeElement> getElementsReversed() {
-            return elementsRev;
-        }
-
         @Override
         public boolean equals(Object o) {
             if (this == o) return true;
@@ -1310,7 +1303,9 @@
 
             // Compose append calls. This is done in reverse because the application order is
             // reverse as well.
-            for (RecipeElement el : recipe.getElementsReversed()) {
+            List<RecipeElement> elements = recipe.getElements();
+            for (int i = elements.size() - 1; i >= 0; i--) {
+                RecipeElement el = elements.get(i);
                 MethodHandle appender;
                 switch (el.getTag()) {
                     case CONST: {