Skip to content

Commit

Permalink
Avoid redundant reads of head in peakNode
Browse files Browse the repository at this point in the history
Motivation:

There is not need todo redunant reads of head in peakNode as we can just spin on next() until it becomes visible.

Modifications:

Remove redundant reads of head in peakNode. This is based on @nitsanw's patch for akka.
See akka/akka#15596

Result:

Less volatile access.
  • Loading branch information
Norman Maurer committed Aug 21, 2014
1 parent 36dfeb0 commit e23121d
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions common/src/main/java/io/netty/util/internal/MpscLinkedQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,18 @@ final class MpscLinkedQueue<E> extends MpscLinkedQueueTailRef<E> implements Queu
* Returns the node right next to the head, which contains the first element of this queue.
*/
private MpscLinkedQueueNode<E> peekNode() {
for (;;) {
final MpscLinkedQueueNode<E> head = headRef();
final MpscLinkedQueueNode<E> next = head.next();
if (next != null) {
return next;
}
if (head == tailRef()) {
return null;
}

// If we are here, it means:
// * offer() is adding the first element, and
// * it's between replaceTail(newTail) and oldTail.setNext(newTail).
// (i.e. next == oldTail and oldTail.next == null and head == oldTail != newTail)
MpscLinkedQueueNode<E> head = headRef();
MpscLinkedQueueNode<E> next = head.next();
if (next == null && head != tailRef()) {
// if tail != head this is not going to change until consumer makes progress
// we can avoid reading the head and just spin on next until it shows up
//
// See https://github.com/akka/akka/pull/15596
do {
next = head.next();
} while (next == null);
}
return next;
}

@Override
Expand Down

0 comments on commit e23121d

Please sign in to comment.