mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #6353 from petermattis/pmattis/grow-inflights-buffer
raft: grow the inflights buffer instead of preallocating
This commit is contained in:
commit
b24527f2f0
@ -189,8 +189,7 @@ type inflights struct {
|
|||||||
|
|
||||||
func newInflights(size int) *inflights {
|
func newInflights(size int) *inflights {
|
||||||
return &inflights{
|
return &inflights{
|
||||||
size: size,
|
size: size,
|
||||||
buffer: make([]uint64, size),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,10 +202,28 @@ func (in *inflights) add(inflight uint64) {
|
|||||||
if next >= in.size {
|
if next >= in.size {
|
||||||
next -= in.size
|
next -= in.size
|
||||||
}
|
}
|
||||||
|
if next >= len(in.buffer) {
|
||||||
|
in.growBuf()
|
||||||
|
}
|
||||||
in.buffer[next] = inflight
|
in.buffer[next] = inflight
|
||||||
in.count++
|
in.count++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// grow the inflight buffer by doubling up to inflights.size. We grow on demand
|
||||||
|
// instead of preallocating to inflights.size to handle systems which have
|
||||||
|
// thousands of Raft groups per process.
|
||||||
|
func (in *inflights) growBuf() {
|
||||||
|
newSize := len(in.buffer) * 2
|
||||||
|
if newSize == 0 {
|
||||||
|
newSize = 1
|
||||||
|
} else if newSize > in.size {
|
||||||
|
newSize = in.size
|
||||||
|
}
|
||||||
|
newBuffer := make([]uint64, newSize)
|
||||||
|
copy(newBuffer, in.buffer)
|
||||||
|
in.buffer = newBuffer
|
||||||
|
}
|
||||||
|
|
||||||
// freeTo frees the inflights smaller or equal to the given `to` flight.
|
// freeTo frees the inflights smaller or equal to the given `to` flight.
|
||||||
func (in *inflights) freeTo(to uint64) {
|
func (in *inflights) freeTo(to uint64) {
|
||||||
if in.count == 0 || to < in.buffer[in.start] {
|
if in.count == 0 || to < in.buffer[in.start] {
|
||||||
@ -228,6 +245,11 @@ func (in *inflights) freeTo(to uint64) {
|
|||||||
// free i inflights and set new start index
|
// free i inflights and set new start index
|
||||||
in.count -= i
|
in.count -= i
|
||||||
in.start = idx
|
in.start = idx
|
||||||
|
if in.count == 0 {
|
||||||
|
// inflights is empty, reset the start index so that we don't grow the
|
||||||
|
// buffer unnecessarily.
|
||||||
|
in.start = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (in *inflights) freeFirstOne() { in.freeTo(in.buffer[in.start]) }
|
func (in *inflights) freeFirstOne() { in.freeTo(in.buffer[in.start]) }
|
||||||
|
@ -155,10 +155,10 @@ func TestInflightFreeTo(t *testing.T) {
|
|||||||
in.freeTo(14)
|
in.freeTo(14)
|
||||||
|
|
||||||
wantIn4 := &inflights{
|
wantIn4 := &inflights{
|
||||||
start: 5,
|
start: 0,
|
||||||
count: 0,
|
count: 0,
|
||||||
size: 10,
|
size: 10,
|
||||||
// ↓
|
// ↓
|
||||||
buffer: []uint64{10, 11, 12, 13, 14, 5, 6, 7, 8, 9},
|
buffer: []uint64{10, 11, 12, 13, 14, 5, 6, 7, 8, 9},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user