1 Star 0 Fork 1

Thoughtworks/go-lock-free-ring-buffer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Lock free ring buffer Go Report Card

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.

API

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).

Method for special circumstance

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.

Performance

  1. Two type of lock-free ring buffer compare with go-channel at different capacities
  2. Two type of lock-free ring buffer compare with go-channel at different producers / consumers ratio
  3. Two type of lock-free ring buffer compare with go-channel at different threads
MIT License Copyright (c) 2021 Lenshood Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Lock-free ring buffer by golang 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/thoughtworks/go-lock-free-ring-buffer.git
git@gitee.com:thoughtworks/go-lock-free-ring-buffer.git
thoughtworks
go-lock-free-ring-buffer
go-lock-free-ring-buffer
master

搜索帮助