Used Splitter instead of String.split() - performance improvement 83/7583/3
authorMichal Polkorab <michal.polkorab@pantheon.sk>
Mon, 2 Jun 2014 10:58:50 +0000 (12:58 +0200)
committerMichal Polkorab <michal.polkorab@pantheon.sk>
Mon, 2 Jun 2014 12:09:33 +0000 (14:09 +0200)
- removed ending whitespace characters from .buildBuffer method as those hexStrings should be ended with a hex character

Change-Id: If1e53d7bbcc81293aa7a231b24b1b7a7746cb5c7
Signed-off-by: Michal Polkorab <michal.polkorab@pantheon.sk>
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/action/OF10AbstractIpAddressActionSerializer.java
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/match/AbstractOxmIpv4AddressSerializer.java
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/match/AbstractOxmIpv6AddressSerializer.java
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/ByteBufUtils.java
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/OF10MatchSerializer.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/MultipartReplyMessageFactoryTest.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/OF10FeaturesReplyMessageFactoryTest.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/OF10StatsReplyMessageFactoryTest.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/QueueGetConfigReplyMessageFactoryMultiTest.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/ByteBufUtilsTest.java

index 84db71d26c515351235f67841544d5e1b4ddc74b..c77b4aac5135acda90ac795b749adb27678a68eb 100644 (file)
@@ -13,6 +13,8 @@ import io.netty.buffer.ByteBuf;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IpAddressAction;\r
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;\r
 \r
+import com.google.common.base.Splitter;\r
+\r
 /**\r
  * @author michal.polkorab\r
  *\r
@@ -22,10 +24,10 @@ public abstract class OF10AbstractIpAddressActionSerializer extends AbstractActi
     @Override\r
     public void serialize(Action action, ByteBuf outBuffer) {\r
         super.serialize(action, outBuffer);\r
-        String[] addressGroups = action.getAugmentation(IpAddressAction.class)\r
-                .getIpAddress().getValue().split("\\.");\r
-        for (int i = 0; i < addressGroups.length; i++) {\r
-            outBuffer.writeByte(Integer.parseInt(addressGroups[i]));\r
+        Iterable<String> addressGroups = Splitter.on(".")\r
+                .split(action.getAugmentation(IpAddressAction.class).getIpAddress().getValue());\r
+        for (String group : addressGroups) {\r
+            outBuffer.writeByte(Short.parseShort(group));\r
         }\r
     }\r
 }\r
index 7f07d819bf6c79cde8c730321912ab5856cc2e4b..58b7cd82fa499f464583bb76a483f2f64831c9c8 100644 (file)
@@ -12,6 +12,8 @@ import io.netty.buffer.ByteBuf;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry;\r
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;\r
 \r
+import com.google.common.base.Splitter;\r
+\r
 /**\r
  * Parent for Ipv4 address based match entry serializers\r
  * @author michal.polkorab\r
@@ -26,10 +28,10 @@ public abstract class AbstractOxmIpv4AddressSerializer extends AbstractOxmMatchE
     }\r
 \r
     private static void writeIpv4Address(MatchEntries entry, ByteBuf out) {\r
-        String[] addressGroups = entry.getAugmentation(Ipv4AddressMatchEntry.class)\r
-                .getIpv4Address().getValue().split("\\.");\r
-        for (int i = 0; i < addressGroups.length; i++) {\r
-            out.writeByte(Integer.parseInt(addressGroups[i]));\r
+        Iterable<String> addressGroups = Splitter.on(".")\r
+                .split(entry.getAugmentation(Ipv4AddressMatchEntry.class).getIpv4Address().getValue());\r
+        for (String group : addressGroups) {\r
+            out.writeByte(Short.parseShort(group));\r
         }\r
     }\r
 \r
index d5491c8319b3397e20871deb0cd0c9b69a8aad5f..a0ca6e39f7865d4f8f5fffdc3799fd859bebdc21 100644 (file)
@@ -9,12 +9,17 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.match;
 \r
 import io.netty.buffer.ByteBuf;\r
 \r
+import java.util.ArrayList;\r
 import java.util.Arrays;\r
+import java.util.List;\r
 \r
 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;\r
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;\r
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;\r
 \r
+import com.google.common.base.Splitter;\r
+import com.google.common.collect.Lists;\r
+\r
 /**\r
  * Parent for Ipv6 address based match entry serializers\r
  * @author michal.polkorab\r
@@ -25,49 +30,50 @@ public abstract class AbstractOxmIpv6AddressSerializer extends AbstractOxmMatchE
     public void serialize(MatchEntries entry, ByteBuf outBuffer) {\r
         super.serialize(entry, outBuffer);\r
         String textAddress = entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address().getValue();\r
-        String[] address;\r
+        List<String> address;\r
         if (textAddress.equals("::")) {\r
-            address = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];\r
-            Arrays.fill(address, "0");\r
+            String[] tmp = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];\r
+            Arrays.fill(tmp, "0");\r
+            address = Arrays.asList(tmp);\r
         } else {\r
-            address = parseIpv6Address(textAddress.split(":"));\r
+            address = parseIpv6Address(Lists.newArrayList(Splitter.on(":").split(textAddress)));\r
         }\r
-        for (int i = 0; i < address.length; i++) {\r
-            outBuffer.writeShort(Integer.parseInt(address[i], 16));\r
+        for (String group : address) {\r
+            outBuffer.writeShort(Integer.parseInt(group, 16));\r
         }\r
         writeMask(entry, outBuffer, getValueLength());\r
     }\r
 \r
-    private static String[] parseIpv6Address(String[] addressGroups) {\r
+    private static List<String> parseIpv6Address(ArrayList<String> addressGroups) {\r
         int countEmpty = 0;\r
-        for (int i = 0; i < addressGroups.length; i++) {\r
-            if (addressGroups[i].equals("")){\r
+        for (String group : addressGroups) {\r
+            if (group.equals("")) {\r
                 countEmpty++;\r
             }\r
         }\r
-        String[] ready = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];\r
+        List<String> ready = new ArrayList<>(EncodeConstants.GROUPS_IN_IPV6_ADDRESS);\r
         switch (countEmpty) {\r
         case 0:\r
             ready = addressGroups;\r
             break;\r
         case 1:\r
-            int zerosToBePushed = EncodeConstants.GROUPS_IN_IPV6_ADDRESS - addressGroups.length + 1;\r
-            int index = 0;\r
-            for (int i = 0; i < addressGroups.length; i++) {\r
-                if (addressGroups[i].equals("")) {\r
+            int zerosToBePushed = EncodeConstants.GROUPS_IN_IPV6_ADDRESS - addressGroups.size() + 1;\r
+            for (String group : addressGroups) {\r
+                if (group.equals("")) {\r
                     for (int j = 0; j < zerosToBePushed; j++) {\r
-                        ready[index] = "0";\r
-                        index++;\r
+                        ready.add("0");\r
                     }\r
                 } else {\r
-                    ready[index] = addressGroups[i];\r
-                    index++;\r
+                    ready.add(group);\r
                 }\r
             }\r
             break;\r
         case 2:\r
-            Arrays.fill(ready, "0");\r
-            ready[ready.length - 1] = addressGroups[addressGroups.length - 1];\r
+            String[] tmp = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];\r
+            Arrays.fill(tmp, "0");\r
+            tmp[EncodeConstants.GROUPS_IN_IPV6_ADDRESS - 1] =\r
+                    addressGroups.get(addressGroups.size() - 1);\r
+            ready = Arrays.asList(tmp);\r
             break;\r
         default:\r
             throw new IllegalStateException("Incorrect ipv6 address");\r
index 0e7654d12c47a82986fbcf891ace44e05c8d2540..802c8c0a02bc8d66cc8e6cd5717393dc12abec22 100644 (file)
@@ -21,6 +21,8 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 
 import com.google.common.base.Joiner;
+import com.google.common.base.Splitter;
+import com.google.common.collect.Lists;
 
 /** Class for common operations on ByteBuf
  * @author michal.polkorab
@@ -49,7 +51,7 @@ public abstract class ByteBufUtils {
     public static byte[] hexStringToBytes(String hexSrc) {
         return hexStringToBytes(hexSrc, true);
     }
-    
+
     /**
      * Converts String into byte[]
      * @param hexSrc input String
@@ -61,15 +63,18 @@ public abstract class ByteBufUtils {
         if (!withSpaces) {
             splitPattern = "(?<=\\G.{2})";
         }
-        
-        String[] byteChips = hexSrc.split(splitPattern);
-        byte[] result = new byte[byteChips.length];
-        for (int i = 0; i < byteChips.length; i++) {
-            result[i] = (byte) Short.parseShort(byteChips[i], 16);
+        Iterable<String> tmp = Splitter.onPattern(splitPattern)
+                .omitEmptyStrings().split(hexSrc);
+        ArrayList<String> byteChips = Lists.newArrayList(tmp);
+        byte[] result = new byte[byteChips.size()];
+        int i = 0;
+        for (String chip : byteChips) {
+            result[i] = (byte) Short.parseShort(chip, 16);
+            i++;
         }
         return result;
     }
-    
+
     /**
      * Creates ByteBuf filled with specified data
      * @param hexSrc input String of bytes in hex format
@@ -183,12 +188,13 @@ public abstract class ByteBufUtils {
      * @return byte representation of mac address
      * @see {@link MacAddress}
      */
-    @SuppressWarnings("javadoc")
     public static byte[] macAddressToBytes(String macAddress) {
-        String[] sequences = macAddress.split(":");
+        Iterable<String> addressGroups = Splitter.on(":").split(macAddress);
         byte[] result = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
-        for (int i = 0; i < sequences.length; i++) {
-             result[i] = (byte) Short.parseShort(sequences[i], 16);
+        int i = 0;
+        for (String group : addressGroups) {
+            result[i] = (byte) Short.parseShort(group, 16);
+            i++;
         }
         return result;
     }
@@ -199,7 +205,6 @@ public abstract class ByteBufUtils {
      * @return String representation of mac address
      * @see {@link MacAddress}
      */
-    @SuppressWarnings("javadoc")
     public static String macAddressToString(byte[] address) {
         List<String> groups = new ArrayList<>();
         for(int i=0; i < EncodeConstants.MAC_ADDRESS_LENGTH; i++){
index d934e4c75e129f47b35cfb24d12cfe626cbeefea..e1afce999f7d2c3e19789531d9c437f702a76f62 100644 (file)
@@ -17,6 +17,8 @@ import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
 
+import com.google.common.base.Splitter;
+
 /**
  * Serializes ofp_match (OpenFlow v1.0) structure
  * @author michal.polkorab
@@ -46,13 +48,13 @@ public class OF10MatchSerializer implements OFSerializer<MatchV10> {
         outBuffer.writeByte(match.getNwTos());
         outBuffer.writeByte(match.getNwProto());
         ByteBufUtils.padBuffer(PADDING_IN_MATCH_2, outBuffer);
-        String[] srcGroups = match.getNwSrc().getValue().split("\\.");
-        for (int i = 0; i < srcGroups.length; i++) {
-            outBuffer.writeByte(Integer.parseInt(srcGroups[i]));
+        Iterable<String> srcGroups = Splitter.on(".").split(match.getNwSrc().getValue());
+        for (String group : srcGroups) {
+            outBuffer.writeByte(Short.parseShort(group));
         }
-        String[] dstGroups = match.getNwDst().getValue().split("\\.");
-        for (int i = 0; i < dstGroups.length; i++) {
-            outBuffer.writeByte(Integer.parseInt(dstGroups[i]));
+        Iterable<String> dstGroups = Splitter.on(".").split(match.getNwDst().getValue());
+        for (String group : dstGroups) {
+            outBuffer.writeByte(Short.parseShort(group));
         }
         outBuffer.writeShort(match.getTpSrc());
         outBuffer.writeShort(match.getTpDst());
index cf39f0fd4612b3e195b456c29c565dcfbd8fbdec..bfa70044ca3736ef031b92aad09f7fe9ccfe5567 100644 (file)
@@ -874,7 +874,7 @@ public class MultipartReplyMessageFactoryTest {
                                               "00 01 02 03 "+ //portNo
                                               "00 00 00 00 "+ //padding01
                                               "08 00 27 00 B0 EB " + //mac address
-                                              "00 00 "); //padding02
+                                              "00 00"); //padding02
         //port name
         String portName = "SampleText";
         byte[] portNameBytes = new byte[MAX_PORT_NAME_LEN];
index d9b50ee8432b6ac8083d39e62cfde0f4a9582d7f..25ce7690eeb96b8220789ff9ff9ea06bcede1963 100644 (file)
@@ -94,7 +94,7 @@ public class OF10FeaturesReplyMessageFactoryTest {
                 + "00 10 01 01 05 01 04 02 41 4C 4F 48 41 00 00 00 00 00 00 00 00 00 00 00 00 00 00 15 00 00 01 01 "
                 + "00 00 00 31 00 00 04 42 00 00 03 0C 00 00 08 88 "
                 + "00 10 01 01 05 01 04 02 41 4C 4F 48 41 00 00 00 00 00 00 00 00 00 00 00 00 00 00 15 00 00 01 01 "
-                + "00 00 00 31 00 00 04 42 00 00 03 0C 00 00 08 88 ");
+                + "00 00 00 31 00 00 04 42 00 00 03 0C 00 00 08 88");
         GetFeaturesOutput builtByFactory = BufferHelper.deserialize(featuresFactory, bb);
 
         BufferHelper.checkHeaderV10(builtByFactory);
index bc6b996ed443d0b498c78d3a027ea0896c5d8c2e..267430b711977ffd037215b16b2f95630853c1fd 100644 (file)
@@ -234,7 +234,7 @@ public class OF10StatsReplyMessageFactoryTest {
                 + "FF 02 03 02 03 02 03 02 FF 02 03 02 03 02 03 02 "\r
                 + "FF 02 03 02 03 02 03 02 FF 02 03 02 03 02 03 02 "\r
                 + "FF 02 03 02 03 02 03 02 FF 02 03 02 03 02 03 02 FF 02 03 02 03 02 03 02 "\r
-                + "FF 02 03 02 03 02 03 02 ");\r
+                + "FF 02 03 02 03 02 03 02");\r
 \r
         MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);\r
 \r
@@ -292,7 +292,7 @@ public class OF10StatsReplyMessageFactoryTest {
                 + "00 FF 00 00 00 00 00 10 "\r
                 + "FF 02 03 02 03 02 03 02 "\r
                 + "FF 02 02 02 02 02 02 02 "\r
-                + "FF 02 03 02 03 02 03 02 ");\r
+                + "FF 02 03 02 03 02 03 02");\r
 \r
         MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);\r
 \r
index 048e2371586f2e2bd2ca8324a5e32dd10b91e24a..c87f0143954476827c8cf470f53d26d5510c0aa3 100644 (file)
@@ -77,7 +77,7 @@ public class QueueGetConfigReplyMessageFactoryMultiTest {
                 "00 10 " + // length
                 "00 00 00 00 " + // pad
                 "00 05 " + // rate
-                "00 00 00 00 00 00 " // pad
+                "00 00 00 00 00 00" // pad
         );
 
         GetQueueConfigOutput builtByFactory = BufferHelper.deserialize(
index 61282953b6d568a14dbbee78c028f23e304fb605..990f072f8db9d20cd37cf323aaa58f916caeb36a 100644 (file)
@@ -36,7 +36,7 @@ public class ByteBufUtilsTest {
 
         Assert.assertArrayEquals(expected, data);
     }
-    
+
     /**
      * Test of {@link ByteBufUtils#hexStringToBytes(String, boolean)}
      */
@@ -46,7 +46,7 @@ public class ByteBufUtilsTest {
 
         Assert.assertArrayEquals(expected, data);
     }
-    
+
     /**
      * Test of {@link ByteBufUtils#hexStringToByteBuf(String)}
      */