Skip to content

Commit 836da4f

Browse files
author
James Cor
committed
todos
1 parent feef082 commit 836da4f

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

server/handler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ func (h *Handler) doQuery(
483483
var processedAtLeastOneBatch bool
484484

485485
buf := sql.ByteBufPool.Get().(*sql.ByteBuffer)
486+
buf.Reset()
486487
defer func() {
487-
buf.Reset()
488488
sql.ByteBufPool.Put(buf)
489489
}()
490490

@@ -1196,6 +1196,10 @@ func toSqlHelper(ctx *sql.Context, typ sql.Type, buf *sql.ByteBuffer, val interf
11961196
if buf == nil {
11971197
return typ.SQL(ctx, nil, val)
11981198
}
1199+
// TODO: possible to predict max amount of space needed in backing array.
1200+
// Only number types are written to byte buffer due to strconv.Append...
1201+
// String types already create a new []byte, so it's better to not copy to backing array.
1202+
11991203
ret, err := typ.SQL(ctx, buf.Get(), val)
12001204
buf.Grow(ret.Len()) // TODO: shouldn't we check capacity beforehand?
12011205
return ret, err

sql/byte_buffer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ func NewByteBuffer(initCap int) *ByteBuffer {
4141
// they expect to be protected.
4242
func (b *ByteBuffer) Grow(n int) {
4343
newI := b.i
44-
if b.i+n <= len(b.buf) {
44+
if b.i+n <= cap(b.buf) {
4545
// Increment |b.i| if no alloc
4646
newI += n
47-
}
48-
if b.i+n >= len(b.buf) {
47+
} else if b.i+n >= cap(b.buf) {
4948
// No more space, double.
5049
// An external allocation doubled the cap using the size of
5150
// the override object, which if used could lead to overall
@@ -59,6 +58,7 @@ func (b *ByteBuffer) Grow(n int) {
5958
// here because the runtime only doubles based on slice
6059
// length.
6160
func (b *ByteBuffer) Double() {
61+
// TODO: This wastes memory. The first half of b.buf won't be referenced by anything.
6262
buf := make([]byte, len(b.buf)*2)
6363
copy(buf, b.buf)
6464
b.buf = buf

0 commit comments

Comments
 (0)