BUG-1275: do not copy arrays in XmlDocumentUtilsforEachChild() 44/8544/3
authorRobert Varga <rovarga@cisco.com>
Wed, 2 Jul 2014 10:58:22 +0000 (12:58 +0200)
committerRobert Varga <rovarga@cisco.com>
Wed, 2 Jul 2014 16:17:16 +0000 (18:17 +0200)
ImmutableList.Builder() has a nasty habit of performing poorly for
anything that is larger than 2 elements. Let's elide it by following
smarts:

- check if the list of children is empty
- pre-allocate an arraylist, thus copying the array at most once

Change-Id: I08825e15fba92e256bd415c192d3f30d52d6d8eb
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtils.java

index 0c3c5d60d57a5cd17755fb2302cf14cec0e832cf..507cb60c2d99c671779508bf0f3e4c62edca479c 100644 (file)
@@ -343,7 +343,7 @@ public final class BindingGeneratorUtil {
     }
 
     /**
-     * Replaces all the occurances of the <code>removalChar</code> in the
+     * Replaces all the occurrences of the <code>removalChar</code> in the
      * <code>text</code> with empty string and converts following character to
      * upper case.
      *
index a389e679b2cbc3a1df76fc50f2e5152fc634219a..e011d3ec83bb13b85434c3c2261f061490fe7b57 100644 (file)
@@ -17,6 +17,7 @@ import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
 
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -514,17 +515,22 @@ public class XmlDocumentUtils {
     }
 
     private static final <T> List<T> forEachChild(final NodeList nodes, final SchemaContext schemaContext, final Function<ElementWithSchemaContext, Optional<T>> forBody) {
-        ImmutableList.Builder<T> ret = ImmutableList.<T> builder();
-        for (int i = 0; i < nodes.getLength(); i++) {
+        final int l = nodes.getLength();
+        if (l == 0) {
+            return ImmutableList.of();
+        }
+
+        final List<T> list = new ArrayList<>(l);
+        for (int i = 0; i < l; i++) {
             org.w3c.dom.Node child = nodes.item(i);
             if (child instanceof Element) {
                 Optional<T> result = forBody.apply(new ElementWithSchemaContext((Element) child,schemaContext));
                 if (result.isPresent()) {
-                    ret.add(result.get());
+                    list.add(result.get());
                 }
             }
         }
-        return ret.build();
+        return ImmutableList.copyOf(list);
     }
 
     public static final XmlCodecProvider defaultValueCodecProvider() {