Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
max-hoffman committed Dec 20, 2024
1 parent fcae966 commit ba830b2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ func toSqlHelper(ctx *sql.Context, typ sql.Type, buf *sql.ByteBuffer, val interf
return typ.SQL(ctx, nil, val)
}
ret, err := typ.SQL(ctx, buf.Get(), val)
buf.Update(ret.Raw())
buf.Grow(ret.Len())
return ret, err
}

Expand Down
14 changes: 11 additions & 3 deletions sql/byte_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,32 @@ func NewByteBuffer(initCap int) *ByteBuffer {
return &ByteBuffer{buf: buf}
}

func (b *ByteBuffer) Update(buf []byte) {
if b.i+len(buf) > len(b.buf) {
// Grow records the latest used byte position. Callers
// are responsible for accurately reporting which bytes
// they expect to be protected.
func (b *ByteBuffer) Grow(n int) {
if b.i+n > len(b.buf) {
// Runtime alloc'd into a separate backing array, but it chooses
// the doubling cap using the non-optimal |cap(b.buf)-b.i|*2.
// We do not need to increment |b.i| b/c the latest value is in
// the other array.
b.Double()
} else {
b.i += len(buf)
b.i += n
}
}

// Double expands the backing array by 2x. We do this
// here because the runtime only doubles based on slice
// length.
func (b *ByteBuffer) Double() {
buf := make([]byte, len(b.buf)*2)
copy(buf, b.buf)
b.buf = buf
}

// Get returns a zero length slice beginning at a safe
// write position.
func (b *ByteBuffer) Get() []byte {
return b.buf[b.i:b.i]
}
Expand Down
2 changes: 1 addition & 1 deletion sql/types/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func BenchmarkVarchar10SQL(b *testing.B) {
ctx := sql.NewEmptyContext()
for i := 0; i < b.N; i++ {
res, _ = t.SQL(ctx, buf.Get(), "char")
buf.Update(res.Raw())
buf.Grow(res.Len())
buf.Reset()
}
result_ = res
Expand Down

0 comments on commit ba830b2

Please sign in to comment.