Reduce volatile variable access 67/101067/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 28 Apr 2022 08:06:34 +0000 (10:06 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 11 May 2022 11:51:54 +0000 (13:51 +0200)
The checkState() and verify() calls are costing us a superfluous
access after we have already accessed the volatile field. Refactor
them to prevent that.

Change-Id: I07c552593a89fb8929985ac637c6fba576b4e659
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 18cb8f2e949db76fb72bfa08921ba6c73a707ec6)
(cherry picked from commit 221e59a1ffa68caff0ca09e124b20d084f4e8be9)

dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/PingPongTransactionChain.java

index ec2b5a2d5eacc3d4b7f70dc93061992217ca16df..09bd03255bedf95999a1b6ae0f8cae0f6506a938 100644 (file)
@@ -8,9 +8,9 @@
 package org.opendaylight.mdsal.dom.spi;
 
 import static com.google.common.base.Preconditions.checkState;
-import static com.google.common.base.Verify.verify;
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.base.VerifyException;
 import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.MoreExecutors;
@@ -263,8 +263,10 @@ public final class PingPongTransactionChain implements DOMTransactionChain {
      * correctness.
      */
     private synchronized void processNextTransaction(final PingPongTransaction tx) {
-        final boolean success = INFLIGHT_UPDATER.compareAndSet(this, tx, null);
-        checkState(success, "Completed transaction %s while %s was submitted", tx, inflightTx);
+        if (!INFLIGHT_UPDATER.compareAndSet(this, tx, null)) {
+            throw new IllegalStateException(String.format("Completed transaction %s while %s was submitted", tx,
+                inflightTx));
+        }
 
         final PingPongTransaction nextTx = READY_UPDATER.getAndSet(this, null);
         if (nextTx == null) {
@@ -295,16 +297,20 @@ public final class PingPongTransactionChain implements DOMTransactionChain {
 
     void readyTransaction(final @NonNull PingPongTransaction tx) {
         // First mark the transaction as not locked.
-        final boolean lockedMatch = LOCKED_UPDATER.compareAndSet(this, tx, null);
-        checkState(lockedMatch, "Attempted to submit transaction %s while we have %s", tx, lockedTx);
+        if (!LOCKED_UPDATER.compareAndSet(this, tx, null)) {
+            throw new IllegalStateException(String.format("Attempted to submit transaction %s while we have %s", tx,
+                lockedTx));
+        }
         LOG.debug("Transaction {} unlocked", tx);
 
         /*
          * The transaction is ready. It will then be picked up by either next allocation,
          * or a background transaction completion callback.
          */
-        final boolean success = READY_UPDATER.compareAndSet(this, null, tx);
-        checkState(success, "Transaction %s collided on ready state", tx, readyTx);
+        if (!READY_UPDATER.compareAndSet(this, null, tx)) {
+            throw new IllegalStateException(String.format("Transaction %s collided on ready state with %s", tx,
+                readyTx));
+        }
         LOG.debug("Transaction {} readied", tx);
 
         /*
@@ -332,8 +338,10 @@ public final class PingPongTransactionChain implements DOMTransactionChain {
     synchronized void cancelTransaction(final PingPongTransaction tx,
             final DOMDataTreeReadWriteTransaction frontendTx) {
         // Attempt to unlock the operation.
-        final boolean lockedMatch = LOCKED_UPDATER.compareAndSet(this, tx, null);
-        verify(lockedMatch, "Cancelling transaction %s collided with locked transaction %s", tx, lockedTx);
+        if (!LOCKED_UPDATER.compareAndSet(this, tx, null)) {
+            throw new VerifyException(String.format("Cancelling transaction %s collided with locked transaction %s", tx,
+                lockedTx));
+        }
 
         // Cancel the backend transaction, so we do not end up leaking it.
         final boolean backendCancelled = tx.getTransaction().cancel();