Skip to content

Commit ba830b2

Browse files
committed
comments
1 parent fcae966 commit ba830b2

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

server/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ func toSqlHelper(ctx *sql.Context, typ sql.Type, buf *sql.ByteBuffer, val interf
936936
return typ.SQL(ctx, nil, val)
937937
}
938938
ret, err := typ.SQL(ctx, buf.Get(), val)
939-
buf.Update(ret.Raw())
939+
buf.Grow(ret.Len())
940940
return ret, err
941941
}
942942

sql/byte_buffer.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,32 @@ func NewByteBuffer(initCap int) *ByteBuffer {
2222
return &ByteBuffer{buf: buf}
2323
}
2424

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

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

49+
// Get returns a zero length slice beginning at a safe
50+
// write position.
4351
func (b *ByteBuffer) Get() []byte {
4452
return b.buf[b.i:b.i]
4553
}

sql/types/sql_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func BenchmarkVarchar10SQL(b *testing.B) {
2828
ctx := sql.NewEmptyContext()
2929
for i := 0; i < b.N; i++ {
3030
res, _ = t.SQL(ctx, buf.Get(), "char")
31-
buf.Update(res.Raw())
31+
buf.Grow(res.Len())
3232
buf.Reset()
3333
}
3434
result_ = res

0 commit comments

Comments
 (0)