From 221e59a1ffa68caff0ca09e124b20d084f4e8be9 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 28 Apr 2022 10:06:34 +0200 Subject: [PATCH] Reduce volatile variable access 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 (cherry picked from commit 18cb8f2e949db76fb72bfa08921ba6c73a707ec6) --- .../dom/spi/PingPongTransactionChain.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/PingPongTransactionChain.java b/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/PingPongTransactionChain.java index ec2b5a2d5e..09bd03255b 100644 --- a/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/PingPongTransactionChain.java +++ b/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/PingPongTransactionChain.java @@ -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(); -- 2.36.6