Skip to content

Commit

Permalink
Replace multiplication with shift
Browse files Browse the repository at this point in the history
Since the multiplier is a power-of-two, we should avoid relying on the
compiler noticing that and perform the optimization directly. Of course,
the JIT does realize this in C2, but ideally we don't write code making
those assumptions as easy to regress.
  • Loading branch information
ben-manes committed Aug 5, 2019
1 parent 2ce49a4 commit c06c5cc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public void expire(ThreadState threadState) {
timerWheel.schedule(timer);
}

@Benchmark
public long getExpirationDelay() {
return timerWheel.getExpirationDelay();
}

static final class Timer extends Node<Integer, Integer> {
Node<Integer, Integer> prev;
Node<Integer, Integer> next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ void unlink(Node<K, V> node) {
}

/** Returns the duration until the next bucket expires, or {@link Long.MAX_VALUE} if none. */
@SuppressWarnings("IntLongMath")
public long getExpirationDelay() {
for (int i = 0; i < SHIFT.length; i++) {
Node<K, V>[] timerWheel = wheel[i];
Expand All @@ -247,7 +248,8 @@ public long getExpirationDelay() {
Node<K, V> sentinel = timerWheel[(j & mask)];
Node<K, V> next = sentinel.getNextInVariableOrder();
if (sentinel != next) {
long delay = ((j - start) * SPANS[i]) - (nanos & spanMask);
long buckets = (j - start);
long delay = (buckets << SHIFT[i]) - (nanos & spanMask);
return (delay > 0) ? delay : SPANS[i];
}
}
Expand Down

0 comments on commit c06c5cc

Please sign in to comment.