Skip to content

Commit 4dd25ce

Browse files
author
James Cor
committed
improved?
1 parent de3bdcf commit 4dd25ce

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

sql/rowexec/join_iters.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ func (i *joinIter) Next(ctx *sql.Context) (sql.Row, error) {
189189
}
190190

191191
if !sql.IsTrue(res) {
192+
// TODO: we are trashing row here, so we can release the memory...right?
193+
i.rowBuffer.Erase(i.rowSize)
192194
continue
193195
}
194196

@@ -212,9 +214,8 @@ func (i *joinIter) buildRow(primary, secondary sql.Row) sql.Row {
212214
}
213215

214216
func (i *joinIter) Close(ctx *sql.Context) (err error) {
215-
//i.rowBuffer.Reset()
216-
//sql.RowBufPool.Put(i.rowBuffer)
217-
i.rowBuffer = nil
217+
i.rowBuffer.Reset()
218+
sql.RowBufPool.Put(i.rowBuffer)
218219

219220
if i.primary != nil {
220221
if err = i.primary.Close(ctx); err != nil {
@@ -417,9 +418,8 @@ func (i *existsIter) buildRow(primary, secondary sql.Row) sql.Row {
417418
}
418419

419420
func (i *existsIter) Close(ctx *sql.Context) (err error) {
420-
i.rowBuffer = nil
421-
//i.rowBuffer.Reset()
422-
//sql.RowBufPool.Put(i.rowBuffer)
421+
i.rowBuffer.Reset()
422+
sql.RowBufPool.Put(i.rowBuffer)
423423

424424
if i.primary != nil {
425425
if err = i.primary.Close(ctx); err != nil {
@@ -589,9 +589,8 @@ func (i *fullJoinIter) buildRow(primary, secondary sql.Row) sql.Row {
589589
}
590590

591591
func (i *fullJoinIter) Close(ctx *sql.Context) (err error) {
592-
i.rowBuffer = nil
593-
//i.rowBuffer.Reset()
594-
//sql.RowBufPool.Put(i.rowBuffer)
592+
i.rowBuffer.Reset()
593+
sql.RowBufPool.Put(i.rowBuffer)
595594

596595
if i.l != nil {
597596
err = i.l.Close(ctx)
@@ -709,9 +708,8 @@ func (i *crossJoinIterator) removeParentRow(r sql.Row) sql.Row {
709708
}
710709

711710
func (i *crossJoinIterator) Close(ctx *sql.Context) (err error) {
712-
i.rowBuffer = nil
713-
//i.rowBuffer.Reset()
714-
//sql.RowBufPool.Put(i.rowBuffer)
711+
i.rowBuffer.Reset()
712+
sql.RowBufPool.Put(i.rowBuffer)
715713

716714
if i.l != nil {
717715
err = i.l.Close(ctx)
@@ -913,9 +911,8 @@ func (i *lateralJoinIterator) Next(ctx *sql.Context) (sql.Row, error) {
913911
}
914912

915913
func (i *lateralJoinIterator) Close(ctx *sql.Context) error {
916-
i.rowBuffer = nil
917-
//i.rowBuffer.Reset()
918-
//sql.RowBufPool.Put(i.rowBuffer)
914+
i.rowBuffer.Reset()
915+
sql.RowBufPool.Put(i.rowBuffer)
919916

920917
var lerr, rerr error
921918
if i.lIter != nil {

sql/rows.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func FormatRow(row Row) string {
8484
return sb.String()
8585
}
8686

87-
const defaultRowBuffCap = 4096
87+
const defaultRowBufCap = 128
8888

8989
type RowBuffer struct {
9090
i int
@@ -93,27 +93,37 @@ type RowBuffer struct {
9393

9494
func NewRowBuffer() *RowBuffer {
9595
return &RowBuffer{
96-
buf: make(Row, defaultRowBuffCap),
96+
buf: make(Row, 0, defaultRowBufCap),
9797
}
9898
}
9999

100100
func (b *RowBuffer) Get(n int) (res Row) {
101101
newI := b.i + n
102-
if newI >= len(b.buf) {
103-
//b.buf = append(b.buf, b.buf) // TODO: not sure if this is correct, but it seems faster
104-
buf := make(Row, len(b.buf)*2)
105-
copy(b.buf, buf)
102+
if newI >= cap(b.buf) {
103+
buf := make(Row, newI*2) // Golang is stupid, don't let it decide the new capacity
104+
copy(buf, b.buf)
106105
b.buf = buf
107106
}
107+
b.buf = b.buf[:newI]
108+
for i := b.i; i < newI; i++ {
109+
b.buf[i] = nil
110+
}
108111
res = b.buf[b.i:newI]
109112
b.i = newI
110113
return
111114
}
112115

113116
func (b *RowBuffer) Reset() {
117+
for i := 0; i < b.i; i++ {
118+
b.buf[i] = nil
119+
}
114120
b.i = 0
115121
}
116122

123+
func (b *RowBuffer) Erase(i int) {
124+
b.i -= i
125+
}
126+
117127
var RowBufPool = sync.Pool{
118128
New: func() any {
119129
return NewRowBuffer()

0 commit comments

Comments
 (0)