-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement and use ringbuffer in framer
- Loading branch information
Showing
5 changed files
with
146 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package ringbuffer | ||
|
||
type RingBuffer[T any] struct { | ||
ring []T | ||
headPos, tailPos int | ||
full bool | ||
} | ||
|
||
func (r *RingBuffer[T]) Init(size int) { | ||
r.ring = make([]T, size) | ||
} | ||
|
||
func (r *RingBuffer[T]) Len() int { | ||
if r.full { | ||
return len(r.ring) | ||
} | ||
if r.tailPos >= r.headPos { | ||
return r.tailPos - r.headPos | ||
} | ||
return r.tailPos - r.headPos + len(r.ring) | ||
} | ||
|
||
func (r *RingBuffer[T]) Empty() bool { | ||
return !r.full && r.headPos == r.tailPos | ||
} | ||
|
||
func (r *RingBuffer[T]) PushBack(t T) { | ||
if r.full || len(r.ring) == 0 { | ||
r.grow() | ||
} | ||
r.ring[r.tailPos] = t | ||
r.tailPos++ | ||
if r.tailPos == len(r.ring) { | ||
r.tailPos = 0 | ||
} | ||
if r.tailPos == r.headPos { | ||
r.full = true | ||
} | ||
} | ||
|
||
func (r *RingBuffer[T]) PopFront() T { | ||
if r.Empty() { | ||
panic("github.com/quic-go/quic-go/internal/utils/ringbuffer: pop from an empty queue") | ||
} | ||
r.full = false | ||
t := r.ring[r.headPos] | ||
r.ring[r.headPos] = *new(T) | ||
r.headPos++ | ||
if r.headPos == len(r.ring) { | ||
r.headPos = 0 | ||
} | ||
return t | ||
} | ||
|
||
// Grow the maximum size of the queue. | ||
// This method assume the queue is full. | ||
func (r *RingBuffer[T]) grow() { | ||
oldRing := r.ring | ||
newSize := len(oldRing) * 2 | ||
if newSize == 0 { | ||
newSize = 1 | ||
} | ||
r.ring = make([]T, newSize) | ||
headLen := copy(r.ring, oldRing[r.headPos:]) | ||
copy(r.ring[headLen:], oldRing[:r.headPos]) | ||
r.headPos, r.tailPos, r.full = 0, len(oldRing), false | ||
} | ||
|
||
func (r *RingBuffer[T]) Clear() { | ||
var zeroValue T | ||
for i := range r.ring { | ||
r.ring[i] = zeroValue | ||
} | ||
r.headPos, r.tailPos, r.full = 0, 0, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ringbuffer | ||
|
||
import "testing" | ||
|
||
func BenchmarkRingBuffer(b *testing.B) { | ||
r := RingBuffer[int]{} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
r.PushBack(i) | ||
r.PopFront() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ringbuffer | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestTestdata(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "ringbuffer suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ringbuffer | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("RingBuffer", func() { | ||
It("push and pop", func() { | ||
r := RingBuffer[int]{} | ||
Expect(len(r.ring)).To(Equal(0)) | ||
Expect(func() { r.PopFront() }).To(Panic()) | ||
r.PushBack(1) | ||
r.PushBack(2) | ||
r.PushBack(3) | ||
Expect(r.PopFront()).To(Equal(1)) | ||
Expect(r.PopFront()).To(Equal(2)) | ||
r.PushBack(4) | ||
r.PushBack(5) | ||
Expect(r.Len()).To(Equal(3)) | ||
r.PushBack(6) | ||
Expect(r.Len()).To(Equal(4)) | ||
Expect(r.PopFront()).To(Equal(3)) | ||
Expect(r.PopFront()).To(Equal(4)) | ||
Expect(r.PopFront()).To(Equal(5)) | ||
Expect(r.PopFront()).To(Equal(6)) | ||
}) | ||
It("clear", func() { | ||
r := RingBuffer[int]{} | ||
r.Init(2) | ||
r.PushBack(1) | ||
r.PushBack(2) | ||
Expect(r.full).To(BeTrue()) | ||
r.Clear() | ||
Expect(r.full).To(BeFalse()) | ||
Expect(r.Len()).To(Equal(0)) | ||
}) | ||
}) |