In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about what ringbuffer is. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Without saying much, follow the editor to have a look.
What is ringbuffer?
Well, as the name suggests, it is a ring (end-to-end ring) that you can use as a buffer for passing data between different contexts (threads, co-programs).
Basically, ringbuffer has a fixed length, and each location has a sequence number, and it is continuous.
As you keep populating the buffer (and there may be corresponding reads), the sequence number will grow until the ring is bypassed.
Generally speaking, ringbuffer is implemented by arrays, and its performance has been greatly improved because of its continuity in memory.
From the array, it looks like this, it's no longer a ring.
Generally defined data structure
Type RingBuffer struct {buffer [] interface {} read uint64 / / read location write uint64 / / write location size uint64 / / buffer size}
Several major concepts:
When read = write, the buffer is empty
(write + 1)% size = = read, buffer is full
Several major difficulties go back to
Ringbuffer looks like a ring, but it is actually an array that needs to be wound back to the beginning of the array after it is written to the end of the array. However, due to the variable size of the packet, there may be data segmentation at the tail, with half of the packet at the tail and half at the beginning, half at the tail and half at the beginning when the packet is read.
Write too fast, release overwrite
When the write speed is faster than the read speed, the new written data is overwritten with the unread data, so there are two decisions: overwrite the unread data and discard the new written data.
Avoid repeated reads
Because ringbuffer overwrites the written data and does not delete the unread data, it determines whether there is any unread data through the read in the ringbuffer.
Reread / rewrite requirements
Suppose a consumption scenario, consumption is time-consuming, and resources can be released only when the consumer confirms that the object has been consumed. That's when you need to reread / rewrite.
Advantages
We use Ring Buffer as a data structure because it provides us with reliable messaging features.
This reason is enough, but it has some other advantages.
First of all, Ring Buffer is faster than linked lists because it is an array and has an easily predictable access pattern.
CPU cache-friendly (CPU-cache-friendly)-data can be preloaded into the cache at the hardware level, so CPU does not often need to go back to the main memory RAM to find Ring Buffer's next piece of data.
Ring Buffer is an array where you can pre-allocate memory and keep the array elements valid forever. This means that memory garbage collection (GC) does almost nothing in this case. In addition, unlike a linked list, objects are created for each additional piece of data-when the data is deleted from the linked list, the objects are cleaned up.
This is what ringbuffer is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.