Skip to content

Commit c9c88e1

Browse files
committed
Add buffer.Ring(Growing)Options to initialize ring buffers
1 parent 0315323 commit c9c88e1

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

buffer/ring_growing.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ limitations under the License.
1616

1717
package buffer
1818

19+
// RingGrowingOptions sets parameters for [RingGrowing] and
20+
// [TypedRingGrowing].
21+
type RingGrowingOptions struct {
22+
// InitialSize is the number of pre-allocated elements in the
23+
// initial underlying storage buffer.
24+
InitialSize int
25+
}
26+
1927
// RingGrowing is a growing ring buffer.
2028
// Not thread safe.
2129
//
@@ -26,7 +34,7 @@ type RingGrowing = TypedRingGrowing[any]
2634
//
2735
// Deprecated: Use NewTypedRingGrowing[any] instead.
2836
func NewRingGrowing(initialSize int) *RingGrowing {
29-
return NewTypedRingGrowing[any](initialSize)
37+
return NewTypedRingGrowing[any](RingGrowingOptions{InitialSize: initialSize})
3038
}
3139

3240
// TypedRingGrowing is a growing ring buffer.
@@ -39,10 +47,10 @@ type TypedRingGrowing[T any] struct {
3947
}
4048

4149
// NewTypedRingGrowing constructs a new TypedRingGrowing instance with provided parameters.
42-
func NewTypedRingGrowing[T any](initialSize int) *TypedRingGrowing[T] {
50+
func NewTypedRingGrowing[T any](opts RingGrowingOptions) *TypedRingGrowing[T] {
4351
return &TypedRingGrowing[T]{
44-
data: make([]T, initialSize),
45-
n: initialSize,
52+
data: make([]T, opts.InitialSize),
53+
n: opts.InitialSize,
4654
}
4755
}
4856

@@ -95,6 +103,16 @@ func (r *TypedRingGrowing[T]) Cap() int {
95103
return r.n
96104
}
97105

106+
// RingGrowingOptions sets parameters for [Ring].
107+
type RingOptions struct {
108+
// InitialSize is the number of pre-allocated elements in the
109+
// initial underlying storage buffer.
110+
InitialSize int
111+
// NormalSize is the number of elements to allocate for new storage
112+
// buffers once the Ring is consumed.
113+
NormalSize int
114+
}
115+
98116
// Ring is a dynamically-sized ring buffer which can grow and shrink as-needed.
99117
// Not thread safe.
100118
type Ring[T any] struct {
@@ -103,10 +121,10 @@ type Ring[T any] struct {
103121
}
104122

105123
// NewRing constructs a new Ring instance with provided parameters.
106-
func NewRing[T any](initialSize int) *Ring[T] {
124+
func NewRing[T any](opts RingOptions) *Ring[T] {
107125
return &Ring[T]{
108-
growing: *NewTypedRingGrowing[T](initialSize),
109-
normalSize: initialSize,
126+
growing: *NewTypedRingGrowing[T](RingGrowingOptions{InitialSize: opts.InitialSize}),
127+
normalSize: opts.NormalSize,
110128
}
111129
}
112130

buffer/ring_growing_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
func TestGrowthGrowing(t *testing.T) {
2424
t.Parallel()
2525
initialSize := 1
26-
g := NewTypedRingGrowing[int](initialSize)
26+
g := NewTypedRingGrowing[int](RingGrowingOptions{InitialSize: initialSize})
2727

2828
if expected, actual := 0, g.Len(); expected != actual {
2929
t.Fatalf("expected Len to be %d, got %d", expected, actual)
@@ -72,7 +72,8 @@ func TestGrowthGrowing(t *testing.T) {
7272
func TestGrowth(t *testing.T) {
7373
t.Parallel()
7474
initialSize := 1
75-
g := NewRing[int](initialSize)
75+
normalSize := 2
76+
g := NewRing[int](RingOptions{InitialSize: initialSize, NormalSize: normalSize})
7677

7778
if expected, actual := 0, g.Len(); expected != actual {
7879
t.Fatalf("expected Len to be %d, got %d", expected, actual)
@@ -113,14 +114,14 @@ func TestGrowth(t *testing.T) {
113114
if expected, actual := 0, g.Len(); expected != actual {
114115
t.Fatalf("expected Len to be %d, got %d", expected, actual)
115116
}
116-
if expected, actual := initialSize, g.Cap(); expected != actual {
117+
if expected, actual := normalSize, g.Cap(); expected != actual {
117118
t.Fatalf("expected Cap to be %d, got %d", expected, actual)
118119
}
119120
}
120121

121122
func TestEmpty(t *testing.T) {
122123
t.Parallel()
123-
g := NewTypedRingGrowing[struct{}](1)
124+
g := NewTypedRingGrowing[struct{}](RingGrowingOptions{InitialSize: 1})
124125
_, ok := g.ReadOne()
125126
if ok != false {
126127
t.Fatal("expected false")

0 commit comments

Comments
 (0)