This repo is an implementation of lock-free ring buffer built on Golang.
You can find more information about this implementation at my blog post.
Below is the API and how to use it:
type RingBuffer interface {
Offer(interface{}) (success bool)
Poll() (value interface{}, success bool)
SingleProducerOffer(valueSupplier func() (v interface{}, finish bool))
SingleConsumerPoll(valueConsumer func(interface{}))
}
Generally, we can simply call Offer()
and Poll()
to use it like normal queue. Any types are acceptable, but for the memory efficiency concern, pointer is a better choice to handover by the ring buffer, no GC will happen during read/write.
We can create one as follows:
import (
lfring "github.com/LENSHOOD/go-lock-free-ring-buffer"
)
buffer := lfring.New(lfring.Classical, 16)
The first argument of New()
is the type of ring buffer, I currently provide two implementations, they both have same behavior, but benchmark test shows that the "NodeBased" one has better performance.
The second argument is capacity
, which defines how big the ring buffer is (the calculation unit is the number of slots).
There's two other methods you may notice at API:
SingleProducerOffer()
SingleConsumerPoll()
This two methods is to cover two special circumstances as: multi-producer single-consumer, and single-producer multi-consumer.
When the usage scenario is mpsc or spmc, use such method "may" improve the handover performance since in those scenarios we can safely discard some heavy operations.
However, the performance test(in my blog post) shows that SingleConsumerPoll()
do improve the mpsc performance to about 1.4x, but SingleProducerOffer()
become slower at spmc.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型