VarHandles & LMAX Disruptor

Michael Barker mikeb01 at
Tue Aug 4 21:25:12 UTC 2015

Hi John,

> 5)  return (E) entries[mapSequenceToIndex(sequence, entries.length >> 1)
> & entries.length];
> where the following method may or may not be hand-inlined:
>    static int mapSequenceToIndex(long sequence, int logicalLength) {
>       return sequence & (logicalLength-1) + BUFFER_PAD;
>    }
> Season to taste…  The point is that the JIT sees any random int expression,
> followed by "& entries.length", which is enough to elide the range check.

Thanks, I'll give that approach a try.  While it retains the padding on the
array entries it is lost for entries.length, so while it won't share a
cache line with the array entries it could share it with some other random
unknown thing.  I still have to run some more tests to see what the actual
costs end up being.  I suspect that I'll just suck up the cost of the
bounds checking, given that the Disruptor's focus is predictable latency
the overhead of a few additional instructions and an always correctly
predicted branch is probably better than occasional cache misses.


More information about the valhalla-dev mailing list