项目作者: iamjinlei

项目描述 :
Ringbuffer with concurrent insertion and Iteration support
高级语言: Go
项目地址: git://github.com/iamjinlei/goringbuffer.git
创建时间: 2019-02-14T05:25:13Z
项目社区:https://github.com/iamjinlei/goringbuffer

开源协议:GNU General Public License v3.0

下载


Build Status

Ring Buffer

A simple thread safe ring buffer implementation that supports add and iterate operation. The position in ring buffer does not necessarily indicate data freshness at the tiem when data are added concurrently, but eventually would. An example of such scenario:

Assume we have following ring buffer

  1. +----+----+----+----+----+----
  2. | p0 | p1 | p2 | p3 | p4 |
  3. +----+----+----+----+----+----

And the code is

  1. r := goringbuffer.New(10)
  2. go r.Add(100) // to p0 with old value 10
  3. go r.Add(200) // to p1 with old value 20
  4. sum := []int{0}
  5. go r.Do(func(e interface{}){
  6. sum[0] += e.(int)
  7. })

Even though the above code may settle 100 into p0 and 200 into p1 eventually, sum[0] may be 30 (10 + 20), 300 (100 + 200), 210 (10 + 200) or 120 (100 + 20).

Perf

Intel Core i5 2.3GHz

size = 16

  1. BenchmarkAdd-8 50000000 36.1 ns/op
  2. BenchmarkDo-8 20000000 104 ns/op

size = 64

  1. BenchmarkAdd-8 30000000 36.7 ns/op
  2. BenchmarkDo-8 5000000 322 ns/op