BUG-650: speedup getSubtransaction() 01/11101/1
authorRobert Varga <rovarga@cisco.com>
Fri, 12 Sep 2014 11:20:37 +0000 (13:20 +0200)
committerRobert Varga <rovarga@cisco.com>
Fri, 12 Sep 2014 12:56:05 +0000 (14:56 +0200)
getSubtransaction() is on fast path and it performs two map lookups:
first with containsKey() and then with get(). We know the map cannot
contain null values, so we can perform only the get() and then make
a precondition check on the returned value being non-null.

Change-Id: I477969c8512e3c22e5b7ae5455e0281613a9c752
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/AbstractDOMForwardedCompositeTransaction.java

index c20c3650d2fc217a0a09109084b260b6d6031275..03f77c3eddfcbd632179351c69ced5b0aec98898 100644 (file)
@@ -8,9 +8,7 @@ package org.opendaylight.controller.md.sal.dom.broker.impl;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
-
 import java.util.Collection;
-
 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -60,8 +58,10 @@ abstract class AbstractDOMForwardedCompositeTransaction<K, T extends DOMStoreTra
      */
     protected final T getSubtransaction(final K key) {
         Preconditions.checkNotNull(key, "key must not be null.");
-        Preconditions.checkArgument(backingTxs.containsKey(key), "No subtransaction associated with %s", key);
-        return backingTxs.get(key);
+
+        final T ret = backingTxs.get(key);
+        Preconditions.checkArgument(ret != null, "No subtransaction associated with %s", key);
+        return ret;
     }
 
     /**
@@ -79,9 +79,8 @@ abstract class AbstractDOMForwardedCompositeTransaction<K, T extends DOMStoreTra
 
     protected void closeSubtransactions() {
         /*
-         *  We share one exception for all failures, which are added
-         *  as supressedExceptions to it.
-         *
+         * We share one exception for all failures, which are added
+         * as supressedExceptions to it.
          */
         IllegalStateException failure = null;
         for (T subtransaction : backingTxs.values()) {
@@ -89,17 +88,17 @@ abstract class AbstractDOMForwardedCompositeTransaction<K, T extends DOMStoreTra
                 subtransaction.close();
             } catch (Exception e) {
                 // If we did not allocated failure we allocate it
-                if(failure == null) {
-                    failure = new IllegalStateException("Uncaught exception occured during closing transaction.", e);
+                if (failure == null) {
+                    failure = new IllegalStateException("Uncaught exception occured during closing transaction", e);
                 } else {
-                    // We update it with addotional exceptions, which occured during error.
+                    // We update it with additional exceptions, which occurred during error.
                     failure.addSuppressed(e);
                 }
             }
         }
         // If we have failure, we throw it at after all attempts to close.
-        if(failure != null) {
+        if (failure != null) {
             throw failure;
         }
     }
-}
\ No newline at end of file
+}