Disruptor之RingBuffer

   (1) What is RingBuffer?

     RingBuffer, as its name implies, is a memory ring. Every read and write operation recycles this memory ring, so as to avoid frequent allocation and reclamation of memory and reduce GC pressure. At the same time, Ring Buffer can be implemented as a lock-free queue, which greatly improves the system as a whole. performance.

                                                

    RingBuffer does not have a tail pointer, it only maintains a sequence number pointing to the next available location. The difference between RingBuffer and the commonly used queue is that the data in the buffer is not deleted, that is to say, the data is always stored in the buffer until new data overwrites them.

Why is it so good?

    The reason ringbuffer uses this data structure is because it has good performance in reliable message delivery.

    First, because it's an array, it's faster than a linked list and has an easily predictable access pattern. ( The memory addresses of elements within the array are stored contiguously ). This is CPU cache friendly - that is, at the hardware level, the elements in the array are preloaded, so in the ringbuffer the CPU doesn't need to go to main memory every now and then to load the next element in the array. (Note: because as long as one element is loaded into the cache line, several other adjacent elements will also be loaded into the same cache line)

    Second, you can pre-allocate memory for the array so that the array object persists (unless the program terminates). This means that you don't need to spend a lot of time on garbage collection. Also, unlike a linked list, a node object needs to be created for each object added to it—correspondingly, when a node is deleted, a corresponding memory cleanup operation needs to be performed.

(2) How to read from Ringbuffer

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325392168&siteId=291194637