Un-OK. Sometimes when microbenchmarking, we stumble upon a code sequence that hotspot likes. I observed 20% improvement with the below, so switching to that:
public boolean tryAdvance(Consumer<? super E> action) { Objects.requireNonNull(action); final Object[] es = elements; if (fence < 0) { fence = tail; cursor = head; } // late-binding final int i; if ((i = cursor) == fence) return false; E e = nonNullElementAt(es, i); cursor = inc(i, es.length); action.accept(e); return true; } On Thu, Nov 17, 2016 at 7:41 PM, Martin Buchholz <marti...@google.com> wrote: > > > On Thu, Nov 17, 2016 at 12:03 PM, Paul Sandoz <paul.san...@oracle.com> > wrote: > >> >> 843 public boolean tryAdvance(Consumer<? super E> action) { >> 844 if (action == null) >> 845 throw new NullPointerException(); >> 846 int t, i; >> 847 if ((t = fence) < 0) t = getFence(); >> >> Is that for optimisation purposes, since the same check is also performed >> in getFence? If so that seems like overkill >> >> > OK: > > --- src/main/java/util/ArrayDeque.java 18 Nov 2016 03:22:20 -0000 1.114 > +++ src/main/java/util/ArrayDeque.java 18 Nov 2016 03:38:23 -0000 > @@ -866,9 +866,8 @@ > public boolean tryAdvance(Consumer<? super E> action) { > if (action == null) > throw new NullPointerException(); > - int t, i; > - if ((t = fence) < 0) t = getFence(); > - if (t == (i = cursor)) > + final int t, i; > + if ((t = getFence()) == (i = cursor)) > return false; > final Object[] es = elements; > cursor = inc(i, es.length); > > > >> >> 848 if (t == (i = cursor)) >> 849 return false; >> 850 final Object[] es; >> 851 action.accept(nonNullElementAt(es = elements, i)); >> 852 cursor = inc(i, es.length); >> >