ring_growing.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package buffer
  14. // defaultRingSize defines the default ring size if not specified
  15. const defaultRingSize = 16
  16. // RingGrowingOptions sets parameters for [RingGrowing] and
  17. // [TypedRingGrowing].
  18. type RingGrowingOptions struct {
  19. // InitialSize is the number of pre-allocated elements in the
  20. // initial underlying storage buffer.
  21. InitialSize int
  22. }
  23. // RingGrowing is a growing ring buffer.
  24. // Not thread safe.
  25. //
  26. // Deprecated: Use TypedRingGrowing[any] instead.
  27. type RingGrowing = TypedRingGrowing[any]
  28. // NewRingGrowing constructs a new RingGrowing instance with provided parameters.
  29. //
  30. // Deprecated: Use NewTypedRingGrowing[any] instead.
  31. func NewRingGrowing(initialSize int) *RingGrowing {
  32. return NewTypedRingGrowing[any](RingGrowingOptions{InitialSize: initialSize})
  33. }
  34. // TypedRingGrowing is a growing ring buffer.
  35. // The zero value has an initial size of 0 and is ready to use.
  36. // Not thread safe.
  37. type TypedRingGrowing[T any] struct {
  38. data []T
  39. n int // Size of Data
  40. beg int // First available element
  41. readable int // Number of data items available
  42. }
  43. // NewTypedRingGrowing constructs a new TypedRingGrowing instance with provided parameters.
  44. func NewTypedRingGrowing[T any](opts RingGrowingOptions) *TypedRingGrowing[T] {
  45. return &TypedRingGrowing[T]{
  46. data: make([]T, opts.InitialSize),
  47. n: opts.InitialSize,
  48. }
  49. }
  50. // ReadOne reads (consumes) first item from the buffer if it is available, otherwise returns false.
  51. func (r *TypedRingGrowing[T]) ReadOne() (data T, ok bool) {
  52. if r.readable == 0 {
  53. return
  54. }
  55. r.readable--
  56. element := r.data[r.beg]
  57. var zero T
  58. r.data[r.beg] = zero // Remove reference to the object to help GC
  59. if r.beg == r.n-1 {
  60. // Was the last element
  61. r.beg = 0
  62. } else {
  63. r.beg++
  64. }
  65. return element, true
  66. }
  67. // WriteOne adds an item to the end of the buffer, growing it if it is full.
  68. func (r *TypedRingGrowing[T]) WriteOne(data T) {
  69. if r.readable == r.n {
  70. // Time to grow
  71. newN := r.n * 2
  72. if newN == 0 {
  73. newN = defaultRingSize
  74. }
  75. newData := make([]T, newN)
  76. to := r.beg + r.readable
  77. if to <= r.n {
  78. copy(newData, r.data[r.beg:to])
  79. } else {
  80. copied := copy(newData, r.data[r.beg:])
  81. copy(newData[copied:], r.data[:(to%r.n)])
  82. }
  83. r.beg = 0
  84. r.data = newData
  85. r.n = newN
  86. }
  87. r.data[(r.readable+r.beg)%r.n] = data
  88. r.readable++
  89. }
  90. // Len returns the number of items in the buffer.
  91. func (r *TypedRingGrowing[T]) Len() int {
  92. return r.readable
  93. }
  94. // Cap returns the capacity of the buffer.
  95. func (r *TypedRingGrowing[T]) Cap() int {
  96. return r.n
  97. }
  98. // RingOptions sets parameters for [Ring].
  99. type RingOptions struct {
  100. // InitialSize is the number of pre-allocated elements in the
  101. // initial underlying storage buffer.
  102. InitialSize int
  103. // NormalSize is the number of elements to allocate for new storage
  104. // buffers once the Ring is consumed and
  105. // can shrink again.
  106. NormalSize int
  107. }
  108. // Ring is a dynamically-sized ring buffer which can grow and shrink as-needed.
  109. // The zero value has an initial size and normal size of 0 and is ready to use.
  110. // Not thread safe.
  111. type Ring[T any] struct {
  112. growing TypedRingGrowing[T]
  113. normalSize int // Limits the size of the buffer that is kept for reuse. Read-only.
  114. }
  115. // NewRing constructs a new Ring instance with provided parameters.
  116. func NewRing[T any](opts RingOptions) *Ring[T] {
  117. return &Ring[T]{
  118. growing: *NewTypedRingGrowing[T](RingGrowingOptions{InitialSize: opts.InitialSize}),
  119. normalSize: opts.NormalSize,
  120. }
  121. }
  122. // ReadOne reads (consumes) first item from the buffer if it is available,
  123. // otherwise returns false. When the buffer has been totally consumed and has
  124. // grown in size beyond its normal size, it shrinks down to its normal size again.
  125. func (r *Ring[T]) ReadOne() (data T, ok bool) {
  126. element, ok := r.growing.ReadOne()
  127. if r.growing.readable == 0 && r.growing.n > r.normalSize {
  128. // The buffer is empty. Reallocate a new buffer so the old one can be
  129. // garbage collected.
  130. r.growing.data = make([]T, r.normalSize)
  131. r.growing.n = r.normalSize
  132. r.growing.beg = 0
  133. }
  134. return element, ok
  135. }
  136. // WriteOne adds an item to the end of the buffer, growing it if it is full.
  137. func (r *Ring[T]) WriteOne(data T) {
  138. r.growing.WriteOne(data)
  139. }
  140. // Len returns the number of items in the buffer.
  141. func (r *Ring[T]) Len() int {
  142. return r.growing.Len()
  143. }
  144. // Cap returns the capacity of the buffer.
  145. func (r *Ring[T]) Cap() int {
  146. return r.growing.Cap()
  147. }