Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/sql: use ring.Buffer in StmtBuf #41379

Merged
merged 2 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions pkg/sql/conn_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/ring"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/errors"
"github.com/lib/pq/oid"
Expand Down Expand Up @@ -94,7 +95,7 @@ type StmtBuf struct {
cond *sync.Cond

// data contains the elements of the buffer.
data []Command
data ring.Buffer // []Command

// startPos indicates the index of the first command currently in data
// relative to the start of the connection.
Expand Down Expand Up @@ -398,7 +399,7 @@ func (buf *StmtBuf) Push(ctx context.Context, cmd Command) error {
if buf.mu.closed {
return errors.AssertionFailedf("buffer is closed")
}
buf.mu.data = append(buf.mu.data, cmd)
buf.mu.data.AddLast(cmd)
buf.mu.lastPos++

buf.mu.cond.Signal()
Expand Down Expand Up @@ -426,10 +427,11 @@ func (buf *StmtBuf) CurCmd() (Command, CmdPos, error) {
if err != nil {
return nil, 0, err
}
if cmdIdx < len(buf.mu.data) {
return buf.mu.data[cmdIdx], curPos, nil
len := buf.mu.data.Len()
if cmdIdx < len {
return buf.mu.data.Get(cmdIdx).(Command), curPos, nil
}
if cmdIdx != len(buf.mu.data) {
if cmdIdx != len {
return nil, 0, errors.AssertionFailedf(
"can only wait for next command; corrupt cursor: %d", errors.Safe(curPos))
}
Expand Down Expand Up @@ -473,8 +475,7 @@ func (buf *StmtBuf) ltrim(ctx context.Context, pos CmdPos) {
if buf.mu.startPos == pos {
break
}
buf.mu.data[0] = nil
buf.mu.data = buf.mu.data[1:]
buf.mu.data.RemoveFirst()
buf.mu.startPos++
}
}
Expand Down Expand Up @@ -509,7 +510,7 @@ func (buf *StmtBuf) seekToNextBatch() error {
buf.mu.Unlock()
return err
}
if cmdIdx == len(buf.mu.data) {
if cmdIdx == buf.mu.data.Len() {
buf.mu.Unlock()
return errors.AssertionFailedf("invalid seek start point")
}
Expand All @@ -529,7 +530,7 @@ func (buf *StmtBuf) seekToNextBatch() error {
return err
}

if _, ok := buf.mu.data[cmdIdx].(Sync); ok {
if _, ok := buf.mu.data.Get(cmdIdx).(Sync); ok {
foundSync = true
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/conn_io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestStmtBufLtrim(t *testing.T) {
buf.AdvanceOne()
trimPos := CmdPos(2)
buf.ltrim(ctx, trimPos)
if l := len(buf.mu.data); l != 3 {
if l := buf.mu.data.Len(); l != 3 {
t.Fatalf("expected 3 left, got: %d", l)
}
if s := buf.mu.startPos; s != 2 {
Expand Down
147 changes: 76 additions & 71 deletions pkg/util/ring/ring_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

package ring

const bufferInitialSize = 8

// Buffer is a deque maintained over a ring buffer. Note: it is backed by
// a slice (unlike container/ring one that is backed by a linked list).
// Buffer is a deque maintained over a ring buffer.
//
// Note: it is backed by a slice (unlike container/ring which is backed by a
// linked list).
type Buffer struct {
buffer []interface{}
head int // the index of the front of the deque.
tail int // the index of the first position right after the end of the deque.
head int // the index of the front of the buffer
tail int // the index of the first position after the end of the buffer

// indicates whether the deque is empty, necessary to distinguish
// between an empty deque and a deque that uses all of its capacity.
// Indicates whether the buffer is empty. Necessary to distinguish
// between an empty buffer and a buffer that uses all of its capacity.
nonEmpty bool
}

// Len returns the number of elements in the deque.
func (r Buffer) Len() int {
// Len returns the number of elements in the Buffer.
func (r *Buffer) Len() int {
if !r.nonEmpty {
return 0
}
Expand All @@ -38,83 +38,78 @@ func (r Buffer) Len() int {
}
}

// AddFirst add element to the front of the deque
// and doubles it's underlying slice if necessary.
func (r *Buffer) AddFirst(element interface{}) {
if cap(r.buffer) == 0 {
r.buffer = make([]interface{}, bufferInitialSize)
r.buffer[0] = element
r.tail = 1
} else {
if r.Len() == cap(r.buffer) {
newBuffer := make([]interface{}, 2*cap(r.buffer))
if r.head < r.tail {
copy(newBuffer[:r.Len()], r.buffer[r.head:r.tail])
} else {
copy(newBuffer[:cap(r.buffer)-r.head], r.buffer[r.head:])
copy(newBuffer[cap(r.buffer)-r.head:r.Len()], r.buffer[:r.tail])
}
r.head = 0
r.tail = cap(r.buffer)
r.buffer = newBuffer
}
r.head = (cap(r.buffer) + r.head - 1) % cap(r.buffer)
r.buffer[r.head] = element
}
r.nonEmpty = true
// Cap returns the capacity of the Buffer.
func (r *Buffer) Cap() int {
return cap(r.buffer)
}

// AddLast adds element to the end of the deque
// and doubles it's underlying slice if necessary.
func (r *Buffer) AddLast(element interface{}) {
if cap(r.buffer) == 0 {
r.buffer = make([]interface{}, bufferInitialSize)
r.buffer[0] = element
r.tail = 1
} else {
if r.Len() == cap(r.buffer) {
newBuffer := make([]interface{}, 2*cap(r.buffer))
if r.head < r.tail {
copy(newBuffer[:r.Len()], r.buffer[r.head:r.tail])
} else {
copy(newBuffer[:cap(r.buffer)-r.head], r.buffer[r.head:])
copy(newBuffer[cap(r.buffer)-r.head:r.Len()], r.buffer[:r.tail])
}
r.head = 0
r.tail = cap(r.buffer)
r.buffer = newBuffer
}
r.buffer[r.tail] = element
r.tail = (r.tail + 1) % cap(r.buffer)
}
r.nonEmpty = true
}

// Get returns an element at position pos in the deque (zero-based).
func (r Buffer) Get(pos int) interface{} {
// Get returns an element at position pos in the Buffer (zero-based).
func (r *Buffer) Get(pos int) interface{} {
if !r.nonEmpty || pos < 0 || pos >= r.Len() {
panic("unexpected behavior: index out of bounds")
panic("index out of bounds")
}
return r.buffer[(pos+r.head)%cap(r.buffer)]
}

// GetFirst returns an element at the front of the deque.
func (r Buffer) GetFirst() interface{} {
// GetFirst returns an element at the front of the Buffer.
func (r *Buffer) GetFirst() interface{} {
if !r.nonEmpty {
panic("unexpected behavior: getting first from empty deque")
panic("getting first from empty ring buffer")
}
return r.buffer[r.head]
}

// GetLast returns an element at the front of the deque.
func (r Buffer) GetLast() interface{} {
// GetLast returns an element at the front of the Buffer.
func (r *Buffer) GetLast() interface{} {
if !r.nonEmpty {
panic("unexpected behavior: getting last from empty deque")
panic("getting last from empty ring buffer")
}
return r.buffer[(cap(r.buffer)+r.tail-1)%cap(r.buffer)]
}

// RemoveFirst removes a single element from the front of the deque.
func (r *Buffer) grow(n int) {
newBuffer := make([]interface{}, n)
if r.head < r.tail {
copy(newBuffer[:r.Len()], r.buffer[r.head:r.tail])
} else {
copy(newBuffer[:cap(r.buffer)-r.head], r.buffer[r.head:])
copy(newBuffer[cap(r.buffer)-r.head:r.Len()], r.buffer[:r.tail])
}
r.head = 0
r.tail = cap(r.buffer)
r.buffer = newBuffer
}

func (r *Buffer) maybeGrow() {
if r.Len() != cap(r.buffer) {
return
}
n := 2 * cap(r.buffer)
if n == 0 {
n = 1
}
r.grow(n)
}

// AddFirst add element to the front of the Buffer and doubles it's underlying
// slice if necessary.
func (r *Buffer) AddFirst(element interface{}) {
r.maybeGrow()
r.head = (cap(r.buffer) + r.head - 1) % cap(r.buffer)
r.buffer[r.head] = element
r.nonEmpty = true
}

// AddLast adds element to the end of the Buffer and doubles it's underlying
// slice if necessary.
func (r *Buffer) AddLast(element interface{}) {
r.maybeGrow()
r.buffer[r.tail] = element
r.tail = (r.tail + 1) % cap(r.buffer)
r.nonEmpty = true
}

// RemoveFirst removes a single element from the front of the Buffer.
func (r *Buffer) RemoveFirst() {
if r.Len() == 0 {
panic("removing first from empty ring buffer")
Expand All @@ -126,7 +121,7 @@ func (r *Buffer) RemoveFirst() {
}
}

// RemoveLast removes a single element from the end of the deque.
// RemoveLast removes a single element from the end of the Buffer.
func (r *Buffer) RemoveLast() {
if r.Len() == 0 {
panic("removing last from empty ring buffer")
Expand All @@ -139,6 +134,16 @@ func (r *Buffer) RemoveLast() {
}
}

// Reserve reserves the provided number of elemnets in the Buffer. It is an
// error to reserve a size less than the Buffer's current length.
func (r *Buffer) Reserve(n int) {
if n < r.Len() {
panic("reserving fewer elements than current length")
} else if n > cap(r.buffer) {
r.grow(n)
}
}

// Reset makes Buffer treat its underlying memory as if it were empty. This
// allows for reusing the same memory again without explicitly removing old
// elements.
Expand Down
Loading