Split uint factory methods 10/82910/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 3 Jul 2019 12:49:45 +0000 (14:49 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 3 Jul 2019 12:49:45 +0000 (14:49 +0200)
This splits up internal methods lookup methods, so that they are
more easily inlined. Also mark cache access as potential VarHandle
users.

Change-Id: I1892290d347fba9e662394fda0ca5ce4394514d3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint16.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint32.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint64.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint8.java

index 458543d2017fa419e66ecce374f175921c0fc1e8..bbfbbdcab88151a24fd17c11d36dbdfa74c547f6 100644 (file)
@@ -95,16 +95,20 @@ public class Uint16 extends Number implements CanonicalValue<Uint16> {
 
     private static Uint16 instanceFor(final short value) {
         final int slot = Short.toUnsignedInt(value);
-        if (slot >= CACHE.length) {
-            for (Uint16 c : COMMON) {
-                if (c.value == value) {
-                    return c;
-                }
-            }
+        return slot < CACHE.length ? fromCache(slot, value) : fromCommon(value);
+    }
 
-            return LRU.getUnchecked(value);
+    private static Uint16 fromCommon(final short value) {
+        for (Uint16 c : COMMON) {
+            if (c.value == value) {
+                return c;
+            }
         }
+        return LRU.getUnchecked(value);
+    }
 
+    private static Uint16 fromCache(final int slot, final short value) {
+        // FIXME: 4.0.0: use VarHandles here
         Uint16 ret = CACHE[slot];
         if (ret == null) {
             synchronized (CACHE) {
@@ -115,7 +119,6 @@ public class Uint16 extends Number implements CanonicalValue<Uint16> {
                 }
             }
         }
-
         return ret;
     }
 
index 4c5561932a6cfe379e53e4d81840efc2c92ba9f8..0818206172c6cdac457d19a2883d7666bae3eda5 100644 (file)
@@ -97,17 +97,20 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
 
     private static Uint32 instanceFor(final int value) {
         final long longSlot = Integer.toUnsignedLong(value);
-        if (longSlot >= CACHE.length) {
-            for (Uint32 c : COMMON) {
-                if (c.value == value) {
-                    return c;
-                }
-            }
+        return longSlot < CACHE.length ? fromCache((int)longSlot, value) : fromCommon(value);
+    }
 
-            return LRU.getUnchecked(value);
+    private static Uint32 fromCommon(final int value) {
+        for (Uint32 c : COMMON) {
+            if (c.value == value) {
+                return c;
+            }
         }
+        return LRU.getUnchecked(value);
+    }
 
-        final int slot = (int)longSlot;
+    private static Uint32 fromCache(final int slot, final int value) {
+        // FIXME: 4.0.0: use VarHandles here
         Uint32 ret = CACHE[slot];
         if (ret == null) {
             synchronized (CACHE) {
@@ -118,7 +121,6 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
                 }
             }
         }
-
         return ret;
     }
 
index 4c4f6c778a303c97feca98543d625f4f081ed20b..d4aabc0525fe73d4c261e23ffd3235c273e9f26f 100644 (file)
@@ -99,16 +99,20 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
 
     private static Uint64 instanceFor(final long value) {
         final int slot = (int)value;
-        if (slot < 0 || slot >= CACHE.length) {
-            for (Uint64 c : COMMON) {
-                if (c.value == value) {
-                    return c;
-                }
-            }
+        return slot >= 0 && slot < CACHE.length ? fromCache(slot, value) : fromCommon(value);
+    }
 
-            return LRU.getUnchecked(value);
+    private static Uint64 fromCommon(final long value) {
+        for (Uint64 c : COMMON) {
+            if (c.value == value) {
+                return c;
+            }
         }
+        return LRU.getUnchecked(value);
+    }
 
+    private static Uint64 fromCache(final int slot, final long value) {
+        // FIXME: 4.0.0: use VarHandles here
         Uint64 ret = CACHE[slot];
         if (ret == null) {
             synchronized (CACHE) {
@@ -119,7 +123,6 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
                 }
             }
         }
-
         return ret;
     }
 
index 6b2f706b4bf2730ca0c469d83f61ffefa29bd4bf..b5adf503657c010273f60ea9c97770ae11973019 100644 (file)
@@ -60,6 +60,7 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
     private static Uint8 instanceFor(final byte value) {
         final int slot = Byte.toUnsignedInt(value);
 
+        // FIXME: 4.0.0: use VarHandles here
         Uint8 ret = CACHE[slot];
         if (ret == null) {
             synchronized (CACHE) {