Top Banner
LMAX Disruptor 3.0 Advanced Patterns and details (Making the fast, faster) @mikeb2701
71

LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

May 13, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

LMAX Disruptor 3.0 !

Advanced Patterns and details (Making the fast, faster)

!@mikeb2701

Page 2: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

Agenda

• Prove that memory access is key to software performance

Page 3: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

What is LMAX?

Page 4: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

What is LMAX?

• And why do we care about performance?

Page 5: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

Business Logic?

Page 6: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

Producers

file

Page 7: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

What did we want?

• Multicast - Parallel consumers

• Pre-allocation

• Optionally Lock-free

Page 8: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

ConsumersProducers

25

22

23

20

Page 9: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

next() : longget(long) : Tpublish(long) : void

RingBuffer<T>

next() : longpublish(long) : void

Sequencer

WaitStrategy

SingleProducerSequencer

MultiProducerSequencer

get(long) : T

DataProvider<T>

run() : void

BatchEventProcessor<T>

onEvent(T, long, boolean) : void

EventHandler<T>

Page 10: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

class PlayerMove { long id; long direction; long distance;}

Page 11: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

class PlayerMoveFactory implements EventFactory<PlayerMove> {!

public PlayerMove newInstance() { return new PlayerMove(); }}

Page 12: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

class NetworkHandler { RingBuffer<PlayerMove> buffer;! void handle(ByteBuffer packet) {! long next = buffer.next(); try { PlayerMove playerMove = buffer.get(next); playerMove.id = packet.getLong(); playerMove.direction = packet.getLong(); playerMove.distance = packet.getLong(); } finally { buffer.publish(next); } }}

Page 13: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

class PlayerHandler implements EventHandler<PlayerMove> {! public void onEvent(PlayerMove event, long sequence, boolean onBatchEnd) {! Player player = findPlayer(event.id); player.move(event.direction, event.distance); }}

Page 14: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

2 Common Cases

• Immutable Reference Objects

•Serialised Objects

Page 15: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701
Page 16: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

• Temporal Locality

• Spatial Locality

• Striding

Page 17: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

• Temporal Locality

• Spatial Locality

• Striding

Page 18: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

• Temporal Locality

• Spatial Locality

• Striding

Page 19: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

• L1-dcache-loads

• L1-dcache-misses

• LLC-loads

• LLC-misses

• dTLB-loads

• dTLB-misses

Page 20: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

• L1-dcache-loads

• L1-dcache-misses

• LLC-loads

• LLC-misses

• dTLB-loads

• dTLB-misses

Page 21: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

• L1-dcache-loads

• L1-dcache-misses

• LLC-loads

• LLC-misses

• dTLB-loads

• dTLB-misses

Page 22: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

next() : longget(long) : Tpublish(long) : void

RingBuffer<T>

next() : longpublish(long) : void

Sequencer

WaitStrategy

SingleProducerSequencer

MultiProducerSequencer

get(long) : T

DataProvider<T>

run() : void

BatchEventProcessor<T>

onEvent(T, long, boolean) : void

EventHandler<T>

Page 23: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

<Immutability>

Page 24: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public class SimpleEvent { private final long id; private final long v1; private final long v2; private final long v3;! public SimpleEvent(long id, long v1, long v2, long v3) { this.id = id; this.v1 = v1; this.v2 = v2; this.v3 = v3; }}!public class EventHolder { public SimpleEvent event;}

Page 25: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public class SimpleEvent { private final long id; private final long v1; private final long v2; private final long v3;! public SimpleEvent(long id, long v1, long v2, long v3) { this.id = id; this.v1 = v1; this.v2 = v2; this.v3 = v3; }}!public class EventHolder { public SimpleEvent event;}

Page 26: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

EventTranslatorOneArg<EventHolder, SimpleEvent> TRANSLATOR = new EventTranslatorOneArg<>() { public void translateTo(EventHolder holder, long sequence, SimpleEvent event) { holder.event = event; }};

Page 27: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

Object[] EH<T>

EH<T>

EH<T>

T

T

T

T

EH<T>

Page 28: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

Object[] T

T

T

T

Page 29: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public interface EventAccessor<T> { T take(long sequence);}!public class CustomRingBuffer<T> implements DataProvider<EventAccessor<T>>, EventAccessor<T> { private final Sequencer sequencer; private final Object[] buffer;! public CustomRingBuffer(Sequencer sequencer) { this.sequencer = sequencer; buffer = new Object[sequencer.getBufferSize()]; }

Page 30: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public interface EventAccessor<T> { T take(long sequence);}!public class CustomRingBuffer<T> implements DataProvider<EventAccessor<T>>, EventAccessor<T> { private final Sequencer sequencer; private final Object[] buffer;! public CustomRingBuffer(Sequencer sequencer) { this.sequencer = sequencer; buffer = new Object[sequencer.getBufferSize()]; }

Page 31: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public interface EventAccessor<T> { T take(long sequence);}!public class CustomRingBuffer<T> implements DataProvider<EventAccessor<T>>, EventAccessor<T> { private final Sequencer sequencer; private final Object[] buffer;! public CustomRingBuffer(Sequencer sequencer) { this.sequencer = sequencer; buffer = new Object[sequencer.getBufferSize()]; }

Page 32: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public interface EventAccessor<T> { T take(long sequence);}!public class CustomRingBuffer<T> implements DataProvider<EventAccessor<T>>, EventAccessor<T> { private final Sequencer sequencer; private final Object[] buffer;! public CustomRingBuffer(Sequencer sequencer) { this.sequencer = sequencer; buffer = new Object[sequencer.getBufferSize()]; }

Page 33: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(T t) { long next = sequencer.next(); buffer[index(next)] = t; sequencer.publish(next); }! public T take(long sequence) { T t = (T) buffer[index(sequence)]; buffer[index(sequence)] = null; return t; }! public EventAccessor<T> get(long sequence) { return this; }

Page 34: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(T t) { long next = sequencer.next(); buffer[index(next)] = t; sequencer.publish(next); }! public T take(long sequence) { T t = (T) buffer[index(sequence)]; buffer[index(sequence)] = null; return t; }! public EventAccessor<T> get(long sequence) { return this; }

Page 35: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(T t) { long next = sequencer.next(); buffer[index(next)] = t; sequencer.publish(next); }! public T take(long sequence) { T t = (T) buffer[index(sequence)]; buffer[index(sequence)] = null; return t; }! public EventAccessor<T> get(long sequence) { return this; }

Page 36: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(T t) { long next = sequencer.next(); buffer[index(next)] = t; sequencer.publish(next); }! public T take(long sequence) { T t = (T) buffer[index(sequence)]; buffer[index(sequence)] = null; return t; }! public EventAccessor<T> get(long sequence) { return this; }

Page 37: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(T t) { long next = sequencer.next(); buffer[index(next)] = t; sequencer.publish(next); }! public T take(long sequence) { T t = (T) buffer[index(sequence)]; buffer[index(sequence)] = null; return t; }! public EventAccessor<T> get(long sequence) { return this; }

Page 38: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(T t) { long next = sequencer.next(); buffer[index(next)] = t; sequencer.publish(next); }! public T take(long sequence) { T t = (T) buffer[index(sequence)]; buffer[index(sequence)] = null; return t; }! public EventAccessor<T> get(long sequence) { return this; }

Page 39: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(T t) { long next = sequencer.next(); buffer[index(next)] = t; sequencer.publish(next); }! public T take(long sequence) { T t = (T) buffer[index(sequence)]; buffer[index(sequence)] = null; return t; }! public EventAccessor<T> get(long sequence) { return this; }

Page 40: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(T t) { long next = sequencer.next(); buffer[index(next)] = t; sequencer.publish(next); }! public T take(long sequence) { T t = (T) buffer[index(sequence)]; buffer[index(sequence)] = null; return t; }! public EventAccessor<T> get(long sequence) { return this; }

Page 41: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(T t) { long next = sequencer.next(); buffer[index(next)] = t; sequencer.publish(next); }! public T take(long sequence) { T t = (T) buffer[index(sequence)]; buffer[index(sequence)] = null; return t; }! public EventAccessor<T> get(long sequence) { return this; }

Page 42: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public BatchEventProcessor<EventAccessor<T>> createHandler(final EventHandler<T> handler) { BatchEventProcessor<EventAccessor<T>> processor = new BatchEventProcessor<>(this, sequencer.newBarrier(), new EventHandler<EventAccessor<T>>() { public void onEvent(EventAccessor<T> accessor, long sequence, boolean endOfBatch) { handler.onEvent(accessor.take(sequence), sequence, endOfBatch); } });! sequencer.addGatingSequences(processor.getSequence());! return processor;}

Page 43: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public BatchEventProcessor<EventAccessor<T>> createHandler(final EventHandler<T> handler) { BatchEventProcessor<EventAccessor<T>> processor = new BatchEventProcessor<>(this, sequencer.newBarrier(), new EventHandler<EventAccessor<T>>() { public void onEvent(EventAccessor<T> accessor, long sequence, boolean endOfBatch) { handler.onEvent(accessor.take(sequence), sequence, endOfBatch); } });! sequencer.addGatingSequences(processor.getSequence());! return processor;}

Page 44: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public BatchEventProcessor<EventAccessor<T>> createHandler(final EventHandler<T> handler) { BatchEventProcessor<EventAccessor<T>> processor = new BatchEventProcessor<>(this, sequencer.newBarrier(), new EventHandler<EventAccessor<T>>() { public void onEvent(EventAccessor<T> accessor, long sequence, boolean endOfBatch) { handler.onEvent(accessor.take(sequence), sequence, endOfBatch); } });! sequencer.addGatingSequences(processor.getSequence());! return processor;}

Page 45: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public BatchEventProcessor<EventAccessor<T>> createHandler(final EventHandler<T> handler) { BatchEventProcessor<EventAccessor<T>> processor = new BatchEventProcessor<>(this, sequencer.newBarrier(), new EventHandler<EventAccessor<T>>() { public void onEvent(EventAccessor<T> accessor, long sequence, boolean endOfBatch) { handler.onEvent(accessor.take(sequence), sequence, endOfBatch); } });! sequencer.addGatingSequences(processor.getSequence());! return processor;}

Page 46: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public BatchEventProcessor<EventAccessor<T>> createHandler(final EventHandler<T> handler) { BatchEventProcessor<EventAccessor<T>> processor = new BatchEventProcessor<>(this, sequencer.newBarrier(), new EventHandler<EventAccessor<T>>() { public void onEvent(EventAccessor<T> accessor, long sequence, boolean endOfBatch) { handler.onEvent(accessor.take(sequence), sequence, endOfBatch); } });! sequencer.addGatingSequences(processor.getSequence());! return processor;}

Page 47: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

GC Pause Histogram - Nehalem Server

Page 48: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

Naive: 11.55s Custom: 6.80s

Page 49: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

Simple Custom

L1-dcache-loads 13,382,248,612 8,007,239,157

L1-dcache-misses 442,342,087 (3.31%)

533,198,971 (6.66%)

LLC-loads 83,664,103 271,897,732

LLC-misses 47,647,402!(56.95%)

10,928,138!(4.02%)

dTLB-loads 13,432,146,520 8,002,371,051

dTLB-misses 2,328,319 (0.02%)

1,671,855 (0.02%)

CPU Cache Stats - Nehalem Server

Page 50: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

<Serialised>

Page 51: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

Object[]BB

BB

BB

BB

M

M

M

M

Page 52: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

BB

MBB

BB

Thread Local

Page 53: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public class OffHeapRingBufferimplements DataProvider<ByteBuffer> { private final Sequencer sequencer; private final int entrySize; private final ByteBuffer buffer; ThreadLocal<ByteBuffer> perThreadBuffer = new ThreadLocal<ByteBuffer>() { protected ByteBuffer initialValue() { return buffer.duplicate(); } };! public OffHeapRingBuffer(Sequencer sequencer, int entrySize) { this.sequencer = sequencer; this.entrySize = entrySize; buffer = ByteBuffer.allocateDirect( sequencer.getBufferSize() * entrySize); }

Page 54: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public class OffHeapRingBufferimplements DataProvider<ByteBuffer> { private final Sequencer sequencer; private final int entrySize; private final ByteBuffer buffer; ThreadLocal<ByteBuffer> perThreadBuffer = new ThreadLocal<ByteBuffer>() { protected ByteBuffer initialValue() { return buffer.duplicate(); } };! public OffHeapRingBuffer(Sequencer sequencer, int entrySize) { this.sequencer = sequencer; this.entrySize = entrySize; buffer = ByteBuffer.allocateDirect( sequencer.getBufferSize() * entrySize); }

Page 55: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public class OffHeapRingBufferimplements DataProvider<ByteBuffer> { private final Sequencer sequencer; private final int entrySize; private final ByteBuffer buffer; ThreadLocal<ByteBuffer> perThreadBuffer = new ThreadLocal<ByteBuffer>() { protected ByteBuffer initialValue() { return buffer.duplicate(); } };! public OffHeapRingBuffer(Sequencer sequencer, int entrySize) { this.sequencer = sequencer; this.entrySize = entrySize; buffer = ByteBuffer.allocateDirect( sequencer.getBufferSize() * entrySize); }

Page 56: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public class OffHeapRingBufferimplements DataProvider<ByteBuffer> { private final Sequencer sequencer; private final int entrySize; private final ByteBuffer buffer; ThreadLocal<ByteBuffer> perThreadBuffer = new ThreadLocal<ByteBuffer>() { protected ByteBuffer initialValue() { return buffer.duplicate(); } };! public OffHeapRingBuffer(Sequencer sequencer, int entrySize) { this.sequencer = sequencer; this.entrySize = entrySize; buffer = ByteBuffer.allocateDirect( sequencer.getBufferSize() * entrySize); }

Page 57: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public class OffHeapRingBufferimplements DataProvider<ByteBuffer> { private final Sequencer sequencer; private final int entrySize; private final ByteBuffer buffer; ThreadLocal<ByteBuffer> perThreadBuffer = new ThreadLocal<ByteBuffer>() { protected ByteBuffer initialValue() { return buffer.duplicate(); } };! public OffHeapRingBuffer(Sequencer sequencer, int entrySize) { this.sequencer = sequencer; this.entrySize = entrySize; buffer = ByteBuffer.allocateDirect( sequencer.getBufferSize() * entrySize); }

Page 58: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public ByteBuffer get(long sequence) { int index = index(sequence); int position = index * entrySize; int limit = position + entrySize; ByteBuffer byteBuffer = perThreadBuffer.get(); byteBuffer.position(position).limit(limit); return byteBuffer; }

Page 59: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public ByteBuffer get(long sequence) { int index = index(sequence); int position = index * entrySize; int limit = position + entrySize; ByteBuffer byteBuffer = perThreadBuffer.get(); byteBuffer.position(position).limit(limit); return byteBuffer; }

Page 60: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public ByteBuffer get(long sequence) { int index = index(sequence); int position = index * entrySize; int limit = position + entrySize; ByteBuffer byteBuffer = perThreadBuffer.get(); byteBuffer.position(position).limit(limit); return byteBuffer; }

Page 61: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public ByteBuffer get(long sequence) { int index = index(sequence); int position = index * entrySize; int limit = position + entrySize; ByteBuffer byteBuffer = perThreadBuffer.get(); byteBuffer.position(position).limit(limit); return byteBuffer; }

Page 62: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public ByteBuffer get(long sequence) { int index = index(sequence); int position = index * entrySize; int limit = position + entrySize; ByteBuffer byteBuffer = perThreadBuffer.get(); byteBuffer.position(position).limit(limit); return byteBuffer; }

Page 63: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(byte[] data) { long next = sequencer.next(); try { get(next).put(data); } finally { sequencer.publish(next); } }

Page 64: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(byte[] data) { long next = sequencer.next(); try { get(next).put(data); } finally { sequencer.publish(next); } }

Page 65: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(byte[] data) { long next = sequencer.next(); try { get(next).put(data); } finally { sequencer.publish(next); } }

Page 66: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public void put(byte[] data) { long next = sequencer.next(); try { get(next).put(data); } finally { sequencer.publish(next); } }

Page 67: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

public class BufferEventHandler implements EventHandler<ByteBuffer> {! public void onEvent(ByteBuffer buffer, long sequence, boolean endOfBatch) { // Do stuff... }}

Page 68: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

0

0.9

1.8

2.7

3.6

Runs

0 1 2 3 4 5 6

Direct Buffer Single Buffer

Throughput (MOps/sec) - Nehalem Server

Page 69: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

Direct Buffer Single Buffer

L1-dcache-loads 52,194,082,109 35,236,312,790

L1-dcache-misses 1,251,459,098 (2.40%)

599,414,551 (1.70%)

LLC-loads 520,972,754 23,697,987

LLC-misses 479,054,619 (91.95%)

17,083,178 (72.09%)

dTLB-loads 52,206,185,418 35,230,048,700

dTLB-misses 137,088,875 (0.26%)

922,520 (0.00%)

CPU Cache Stats - Nehalem Server

Page 70: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

In Conclusion…

Page 71: LMAX Disruptor 3 - slides.yowconference.com · LMAX Disruptor 3.0!! Advanced Patterns and details! (Making the fast, faster)! @mikeb2701

<Q&A>• http://lmax-exchange.github.io/disruptor/

• We’re Hiring: [email protected]