Use pre-sized ImmutableLists for yang-data-api 17/78117/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 25 Nov 2018 17:23:50 +0000 (18:23 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 25 Nov 2018 17:26:44 +0000 (18:26 +0100)
ImmutableList.builder() did not pre-size its arrays correctly,
leading to poor performance. That is no longer the case and we can
correctly pre-allocate it -- giving us the ArrayList equivalent in
immutable form, hence skipping copying in
{SchemaPath,YangInstanceIdentifier}.create()

Change-Id: I4a070dca26138f28de69e4a0e1f9b0298ee2312b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java

index 211f8b5..2f94037 100755 (executable)
@@ -10,17 +10,16 @@ package org.opendaylight.controller.cluster.datastore.node.utils.stream;
 
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
 import java.io.DataInput;
 import java.io.IOException;
 import java.io.StringReader;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
-import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -358,12 +357,12 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
 
         final boolean absolute = input.readBoolean();
         final int size = input.readInt();
-        final Collection<QName> qnames = new ArrayList<>(size);
+
+        final Builder<QName> qnames = ImmutableList.builderWithExpectedSize(size);
         for (int i = 0; i < size; ++i) {
             qnames.add(readQName());
         }
-
-        return SchemaPath.create(qnames, absolute);
+        return SchemaPath.create(qnames.build(), absolute);
     }
 
     @Override
@@ -374,13 +373,11 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
 
     private YangInstanceIdentifier readYangInstanceIdentifierInternal() throws IOException {
         int size = input.readInt();
-
-        List<PathArgument> pathArguments = new ArrayList<>(size);
-
+        final Builder<PathArgument> pathArguments = ImmutableList.builderWithExpectedSize(size);
         for (int i = 0; i < size; i++) {
             pathArguments.add(readPathArgument());
         }
-        return YangInstanceIdentifier.create(pathArguments);
+        return YangInstanceIdentifier.create(pathArguments.build());
     }
 
     private Set<String> readObjSet() throws IOException {

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.