Http://mechanitis.blogspot.com/2011/06/dissecting-disruptor-whats-so-special.html author is Trisha Gee, a female engineer at Lmax.
Recently we open-source Lmax disruptor, which is the key to making our transaction process so fast. Why open source? Because we are aware that there is something wrong with the traditional perception of high-performance programming. We have found a better and faster way to share data between threads. If we don't share it with everyone, it's too selfish. Furthermore, this will make us look cool.
From this website, you can download a technical document explaining what disruptor is and why it is so clever and fast. I got some help in writing from here. In fact, all I really do is add some punctuation and reorganize some sentences that I don't understand. This is too simple.
I found it a little difficult to explain these things at one time, so I prepared a short section to explain them in order to suit my nadd listener.
First-ring buffer. My first impression of disruptor is ring buffer. Later, I gradually realized that the ring Buffer structure is the center of this mode. The key is how the disruptor controls access to it.
What is ring buffer?
As described in the name-it is a circle (Circular, first-and-end), and you can use it as a buffer to transmit data between one thread context and another thread context.
(Well, I used paint. I tried to draw a sketch, hoping that the obsessive-compulsive disorder would not be involved and asked me to draw a perfect circle and straight line ).
Therefore, basically, ring buffer is an array with an serial number pointing to the next available element.
If you keep writing data to the buffer (the data should also be read from it), the sequence number will continue to grow until the entire ring is bypassed.
You can use MoD to find the element pointed to by the current serial number in the array.
Sequence mod array length = array index
Therefore, for the Ring buffer aboveAlgorithmThat is (using the MOD Syntax of Java): 12% 10 = 2. Very simple.
In fact, it is totally unexpected to draw 10 elements in the image. The N-power element of 2 is better, because the computer uses binary thinking.
What about next?
If you find circular buffers from Wikipedia, you will see an important difference between it and our implementation method-there is no pointer to the end. We only have the next available serial number. This is deliberate-the fundamental reason for choosing the ring buffer is to support reliable message communication. We need to store the messages sent by the Service. When the other service sends a Nak (Response rejection signal) saying that they have not received the message, we can resend it to them.
The ring buffer looks ideal. It uses serial numbers to indicate the end of the buffer, and when it receives a Nak signal, it can resend all messages from that point to the current serial number:
The difference between the ring buffer we implement and the traditional queue is that the objects in the buffer will not be destroyed-they will stay there until they are overwritten and written next time. This is because our implementation does not require a tail pointer compared to the version on Wikipedia. In our implementation, determine whether the ring buffer overlaps, it is done outside the data structure (this is part of the behavior of producers and consumers-if you don't have time to wait for me to write a blog to describe it, you can check out the disruptor code by yourself ).
Ring buffer is so good because ...?
We use the ring buffer data structure because it provides us with reliable message transmission features. This is enough, but it has some other advantages.
First, the ring buffer is faster than the linked list because it is an array and has an easy-to-predict access mode. This is very good. It is CPU-friendly (CPU-Cache-friendly)-data can be pre-loaded to the high-speed cache on the hardware layer, therefore, the CPU does not need to often return to the primary Memory RAM to find the next data of the ring buffer.
Second, the ring buffer is an array. You can allocate memory in advance and keep the array elements always valid. This means that garbage collection (GC) in memory is almost useless in this case. In addition, unlike the linked list, an object is created for each added piece of data-when the data is deleted from the linked list, the objects will be cleared.
ArticleMissing parts
I didn't mention how to avoid loop overlap and how to read and write data to the ring buffer. You will also notice that I am comparing it with the data structure like the linked list. I don't think anyone will think that the linked list is a practical solution.
The interesting part is the comparison between disruptor and queue. A queue usually focuses on maintaining the header and tail of a queue and adding and consuming messages. I have not mentioned all these things in the ring buffer section. This is because the ring buffer itself is not responsible for these issues and we have moved these issues outside the data structure.
You can go to this website to read the paper or check outCodeFor more details. You can also go to talk to Mike and Martin last year at qcon San Francisco. Or, wait for five minutes to figure out how to talk about the rest.