Optimize list manipulation 42/7142/3
authorRobert Varga <robert.varga@pantheon.sk>
Sat, 17 May 2014 20:29:15 +0000 (22:29 +0200)
committerMichal Rehak <mirehak@cisco.com>
Mon, 19 May 2014 10:36:52 +0000 (12:36 +0200)
This patch optimizes the various allocations of ArrayList where we know
the target size.
- by versionOrder the size equals to the highest version value + 1

Change-Id: I2a54a4cacc21fe8d382080954de5fd8d2c16f472
Signed-off-by: Robert Varga <robert.varga@pantheon.sk>
Signed-off-by: Michal Rehak <mirehak@cisco.com>
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/MDController.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/MessageFactory.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/lldp/LLDPSpeaker.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtil.java

index 0dc084e258fd047c3cb4c9b29a8bb249ef6e773c..20dff550083cdf445e2f43d284975999c1a49d88 100644 (file)
@@ -251,8 +251,8 @@ public class MDController implements IMDController, AutoCloseable {
         
         switchConnectionHandler.setErrorHandler(errorHandler);
         switchConnectionHandler.init();
-        
-        List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
+
+        List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
         for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
             switchConnectionPrv.setSwitchConnectionHandler(switchConnectionHandler);
             ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
@@ -282,7 +282,7 @@ public class MDController implements IMDController, AutoCloseable {
      */
     public void stop() {
         LOG.debug("stopping");
-        List<ListenableFuture<Boolean>> stopChain = new ArrayList<>();
+        List<ListenableFuture<Boolean>> stopChain = new ArrayList<>(switchConnectionProviders.size());
         try {
             for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
                 ListenableFuture<Boolean> shutdown =  switchConnectionPrv.shutdown();
index c876917f28c8e9e97ed97bdb1a175f84bc1f9484..febf50e48f8994f5ba4e65f1b2fc0f64c251695f 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.openflowplugin.openflow.md.core;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;
@@ -55,11 +56,14 @@ public abstract class MessageFactory {
     public static HelloInput createHelloInput(short helloVersion, long helloXid, List<Short> versionOrder) {
         HelloInputBuilder helloInputbuilder = prepareHelloInputBuilder(helloVersion, helloXid);
         if (versionOrder != null) {
-            List<Elements> elementList = new ArrayList<>();
             
             ElementsBuilder elementsBuilder = new ElementsBuilder();
             elementsBuilder.setType(HelloElementType.VERSIONBITMAP);
-            List<Boolean> booleanList = new ArrayList<>();
+            int resultVersionListSize = 0;
+            if (!versionOrder.isEmpty()) {
+                resultVersionListSize = versionOrder.get(0) + 1;
+            }
+            List<Boolean> booleanList = new ArrayList<>(resultVersionListSize);
             
             int versionOrderIndex = versionOrder.size() - 1;
             
@@ -74,7 +78,8 @@ public abstract class MessageFactory {
             }
             
             elementsBuilder.setVersionBitmap(booleanList);
-            elementList.add(elementsBuilder.build());
+
+            List<Elements> elementList = Collections.singletonList(elementsBuilder.build());
             helloInputbuilder.setElements(elementList);
         }
         return helloInputbuilder.build();
index a2686f81e80082e90d3d3a7a7052cee09a604e88..b5abbb859e1a7349157e6adde5b7b49f2363aa8f 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.lldp;
 
-import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Timer;
@@ -129,8 +129,7 @@ public class LLDPSpeaker {
                 .setValue(customValue);
 
         // Create LLDP Custom Option list
-        List<LLDPTLV> customList = new ArrayList<LLDPTLV>();
-        customList.add(customTlv);
+        List<LLDPTLV> customList = Collections.singletonList(customTlv);
 
         // Create discovery pkt
         LLDP discoveryPkt = new LLDP();
index 54d95fce20234b63abf415da115d0dd6f9a4fcfd..774d9d0dc22b1e8f01d0faceaa9505f33f257811 100644 (file)
@@ -128,7 +128,7 @@ public abstract class ByteUtil {
      * @see {@link MacAddress}
      */
     public static String macAddressToString(byte[] address) {
-        List<String> groups = new ArrayList<>();
+        List<String> groups = new ArrayList<>(MAC_ADDRESS_LENGTH);
         for(int i=0; i < MAC_ADDRESS_LENGTH; i++){
             groups.add(String.format("%02X", address[i]));
         }