BUG-7464: Cleanup CNode instantiation 74/49874/6
authorRobert Varga <rovarga@cisco.com>
Fri, 30 Dec 2016 13:36:36 +0000 (14:36 +0100)
committerRobert Varga <rovarga@cisco.com>
Mon, 9 Jan 2017 14:17:12 +0000 (15:17 +0100)
Reorder arguments being passed in so we can use variadic
arguments for nodes. This allows us to make the code more
concise by leaving array allocation up to the compiler.

For root nodes reuse an empty array instance, hosted in
CNode instead of allocating a new one for each new TrieMap.

Change-Id: Ic0574917d5d38c62b94594d1bce27a731c90838c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/CNode.java
third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/TrieMap.java

index 581594346e0728aa21f1a4c313cc5afad63ad580..489a6ef109826ccc0ea3e4f11abae12d8cab1a9b 100644 (file)
 package org.opendaylight.yangtools.triemap;
 
 final class CNode<K, V> extends CNodeBase<K, V> {
+    private static final BasicNode[] EMPTY_ARRAY = new BasicNode[0];
 
     final int bitmap;
     final BasicNode[] array;
     final Gen gen;
 
-    CNode(final int bitmap, final BasicNode[] array, final Gen gen) {
+    CNode(final Gen gen) {
+        this(gen, 0, EMPTY_ARRAY);
+    }
+
+    private CNode(final Gen gen, final int bitmap, final BasicNode... array) {
         this.bitmap = bitmap;
         this.array = array;
         this.gen = gen;
@@ -76,7 +81,7 @@ final class CNode<K, V> extends CNodeBase<K, V> {
         BasicNode[] narr = new BasicNode[len];
         System.arraycopy(array, 0, narr, 0, len);
         narr[pos] = nn;
-        return new CNode<>(bitmap, narr, gen);
+        return new CNode<>(gen, bitmap, narr);
     }
 
     CNode<K, V> removedAt(final int pos, final int flag, final Gen gen) {
@@ -85,7 +90,7 @@ final class CNode<K, V> extends CNodeBase<K, V> {
         BasicNode[] narr = new BasicNode[len - 1];
         System.arraycopy(arr, 0, narr, 0, pos);
         System.arraycopy(arr, pos + 1, narr, pos, len - pos - 1);
-        return new CNode<>(bitmap ^ flag, narr, gen);
+        return new CNode<>(gen, bitmap ^ flag, narr);
     }
 
     CNode<K, V> insertedAt(final int pos, final int flag, final BasicNode nn, final Gen gen) {
@@ -95,7 +100,7 @@ final class CNode<K, V> extends CNodeBase<K, V> {
         System.arraycopy(array, 0, narr, 0, pos);
         narr [pos] = nn;
         System.arraycopy(array, pos, narr, pos + 1, len - pos);
-        return new CNode<>(bmp | flag, narr, gen);
+        return new CNode<>(gen, bmp | flag, narr);
     }
 
     /**
@@ -117,7 +122,7 @@ final class CNode<K, V> extends CNodeBase<K, V> {
             }
             i += 1;
         }
-        return new CNode<>(bitmap, narr, ngen);
+        return new CNode<>(ngen, bitmap, narr);
     }
 
     private BasicNode resurrect(final INode<K, V> inode, final Object inodemain) {
@@ -166,7 +171,7 @@ final class CNode<K, V> extends CNodeBase<K, V> {
             i += 1;
         }
 
-        return new CNode<K, V> (bmp, tmparray, gen).toContracted (lev);
+        return new CNode<K, V>(gen, bmp, tmparray).toContracted(lev);
     }
 
     @Override
@@ -217,12 +222,12 @@ final class CNode<K, V> extends CNodeBase<K, V> {
 
             if (xidx == yidx) {
                 INode<K, V> subinode = new INode<>(gen, dual(x, xhc, y, yhc, lev + 5, gen));
-                return new CNode<>(bmp, new BasicNode[] { subinode }, gen);
+                return new CNode<>(gen, bmp, subinode);
             } else {
                 if (xidx < yidx) {
-                    return new CNode<>(bmp, new BasicNode[] { x, y }, gen);
+                    return new CNode<>(gen, bmp, x, y);
                 } else {
-                    return new CNode<>(bmp, new BasicNode[] { y, x }, gen);
+                    return new CNode<>(gen, bmp, y, x);
                 }
             }
         } else {
index 881090e7b3b40c93e334e79c27648c088e823e08..311f76349105481626fdbe92c8acd906276301f3 100644 (file)
@@ -155,11 +155,9 @@ public class TrieMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
     // } while (obj != TrieMapSerializationEnd);
     // }
 
-
     private static <K,V> INode<K,V> newRootNode() {
         final Gen gen = new Gen();
-        final CNode<K, V> cn = new CNode<>(0, new BasicNode[] {}, gen);
-        return new INode<>(gen, cn);
+        return new INode<>(gen, new CNode<>(gen));
     }
 
     final boolean CAS_ROOT (final Object ov, final Object nv) {