Do not instantiate objects for hash values 43/20543/2
authorRobert Varga <rovarga@cisco.com>
Fri, 15 May 2015 17:06:17 +0000 (19:06 +0200)
committerRobert Varga <rovarga@cisco.com>
Fri, 15 May 2015 17:11:53 +0000 (19:11 +0200)
While having a reference saves us from having an additional field, it will
cost us an additional object overhead once it is materialized. Opt for a
primitive type with a volatile guard.

Change-Id: Ib3a71eb9d00555f3929d8bc104a5065d0252561e
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java

index 4488ac32c27d937fc6bc92f19d687ab782289b4e..7a7ce6c28ae91e09ec43c4e34ba2fb10a4407fcb 100644 (file)
@@ -367,11 +367,10 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
     }
 
     private static abstract class AbstractPathArgument implements PathArgument {
-        private static final AtomicReferenceFieldUpdater<AbstractPathArgument, Integer> HASH_UPDATER =
-                AtomicReferenceFieldUpdater.newUpdater(AbstractPathArgument.class, Integer.class, "hash");
         private static final long serialVersionUID = -4546547994250849340L;
         private final QName nodeType;
-        private volatile transient Integer hash = null;
+        private transient int hashValue;
+        private volatile transient boolean hashGuard = false;
 
         protected AbstractPathArgument(final QName nodeType) {
             this.nodeType = Preconditions.checkNotNull(nodeType);
@@ -393,13 +392,12 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
 
         @Override
         public final int hashCode() {
-            Integer ret = hash;
-            if (ret == null) {
-                ret = hashCodeImpl();
-                HASH_UPDATER.lazySet(this, ret);
+            if (!hashGuard) {
+                hashValue = hashCodeImpl();
+                hashGuard = true;
             }
 
-            return ret;
+            return hashValue;
         }
 
         @Override