BUg-2825: optimize prefixForBytes/prefixForByteBuf 44/35944/4
authorRobert Varga <robert.varga@pantheon.sk>
Thu, 25 Feb 2016 20:02:33 +0000 (21:02 +0100)
committerRobert Varga <rovarga@cisco.com>
Wed, 9 Mar 2016 09:38:40 +0000 (10:38 +0100)
Instead of always performing a copy, copy the array only on size
mismatch.

In prefixForByteBuf, allocate a bytearray which has correct size and
read into it -- the rest of it will be zero-initialized, just as it
should be.

Change-Id: I1e71e98e3efba2d731bb8be2c6cfb8062dec00e2
Signed-off-by: Robert Varga <robert.varga@pantheon.sk>
(cherry picked from commit 4d5062600e78c4b8c3c2d0e85d454f72f67b8104)

util/src/main/java/org/opendaylight/protocol/util/Ipv4Util.java
util/src/main/java/org/opendaylight/protocol/util/Ipv6Util.java

index a7552136ae3a4ed38fc1bb9475c10d2eb3d033b3..dfb47a771cfc27e6ed3578c35cc56a5084e5ed5d 100644 (file)
@@ -128,7 +128,14 @@ public final class Ipv4Util {
      */
     public static Ipv4Prefix prefixForBytes(final byte[] bytes, final int length) {
         Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
-        final byte[] tmp = Arrays.copyOfRange(bytes, 0, IP4_LENGTH);
+
+        final byte[] tmp;
+        if (bytes.length != IP4_LENGTH) {
+            tmp = Arrays.copyOfRange(bytes, 0, IP4_LENGTH);
+        } else {
+            tmp = bytes;
+        }
+
         return IetfInetUtil.INSTANCE.ipv4PrefixFor(tmp, length);
     }
 
@@ -136,14 +143,18 @@ public final class Ipv4Util {
      * Creates an Ipv4Prefix object from given ByteBuf. Prefix length is assumed to
      * be in the left most byte of the buffer.
      *
-     * @param bytes IPv4 address
+     * @param buf Buffer containing serialized prefix
      * @return Ipv4Prefix object
      */
-    public static Ipv4Prefix prefixForByteBuf(final ByteBuf bytes) {
-        final int prefixLength = bytes.readByte();
+    public static Ipv4Prefix prefixForByteBuf(final ByteBuf buf) {
+        final int prefixLength = buf.readByte();
         final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
-        Preconditions.checkArgument(size <= bytes.readableBytes(), "Illegal length of IP prefix: %s", bytes.readableBytes());
-        return prefixForBytes(ByteArray.readBytes(bytes, size), prefixLength);
+        final int readable = buf.readableBytes();
+        Preconditions.checkArgument(size <= readable, "Illegal length of IP prefix: %s/%s", size, readable);
+
+        final byte[] bytes = new byte[IP4_LENGTH];
+        buf.readBytes(bytes, 0, size);
+        return IetfInetUtil.INSTANCE.ipv4PrefixFor(bytes, prefixLength);
     }
 
     /**
index 876f30ae55a721937502b60d97aa6827de32e069..ac5085f257919c750c571b94bbf7a0c4888f476f 100644 (file)
@@ -105,7 +105,14 @@ public final class Ipv6Util {
      */
     public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
         Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
-        final byte[] tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH);
+
+        final byte[] tmp;
+        if (bytes.length != IPV6_LENGTH) {
+            tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH);
+        } else {
+            tmp = bytes;
+        }
+
         return IetfInetUtil.INSTANCE.ipv6PrefixFor(tmp, length);
     }
 
@@ -113,16 +120,19 @@ public final class Ipv6Util {
      * Creates an Ipv6Prefix object from given ByteBuf. Prefix length is assumed to
      * be in the left most byte of the buffer.
      *
-     * @param bytes IPv6 address
+     * @param buf IPv6 address
      * @return Ipv6Prefix object
      */
-    public static Ipv6Prefix prefixForByteBuf(final ByteBuf bytes) {
-        final int prefixLength = bytes.readByte();
+    public static Ipv6Prefix prefixForByteBuf(final ByteBuf buf) {
+        final int prefixLength = buf.readByte();
         final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
-        Preconditions.checkArgument(size <= bytes.readableBytes(), "Illegal length of IP prefix: %s", bytes.readableBytes());
-        return prefixForBytes(ByteArray.readBytes(bytes, size), prefixLength);
-    }
+        final int readable = buf.readableBytes();
+        Preconditions.checkArgument(size <= readable, "Illegal length of IP prefix: %s/%s", size, readable);
 
+        final byte[] bytes = new byte[IPV6_LENGTH];
+        buf.readBytes(bytes, 0, size);
+        return IetfInetUtil.INSTANCE.ipv6PrefixFor(bytes, prefixLength);
+    }
 
     /**
      * Creates a list of Ipv6 Prefixes from given byte array.