Skip to content

Commit 1d6a926

Browse files
author
James Cor
committed
larger tuple buffer
1 parent f84ec00 commit 1d6a926

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

go/store/val/tuple.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ var EmptyTuple = Tuple([]byte{0, 0})
6868

6969
func NewTuple(pool pool.BuffPool, values ...[]byte) Tuple {
7070
values = trimNullSuffix(values)
71+
if len(values) > MaxTupleFields {
72+
panic("tuple field maxIdx exceeds maximum")
73+
}
7174

7275
var count int
7376
var pos ByteSize
@@ -78,9 +81,6 @@ func NewTuple(pool pool.BuffPool, values ...[]byte) Tuple {
7881
count++
7982
pos += sizeOf(v)
8083
}
81-
if len(values) > MaxTupleFields {
82-
panic("tuple field maxIdx exceeds maximum")
83-
}
8484
if pos > MaxTupleDataSize {
8585
panic("tuple data size exceeds maximum")
8686
}

go/store/val/tuple_builder.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (om OrdinalMapping) IsIdentityMapping() bool {
6060
return true
6161
}
6262

63-
var defaultTupleLengthTarget int64 = (1 << 11)
63+
var defaultTupleLengthTarget int64 = (1 << 11) // 2048 = 2KB
6464

6565
type TupleBuilder struct {
6666
vs ValueStore
@@ -77,7 +77,7 @@ func NewTupleBuilder(desc *TupleDesc, vs ValueStore) *TupleBuilder {
7777
return &TupleBuilder{
7878
Desc: desc,
7979
fields: make([][]byte, len(desc.Types)),
80-
buf: make([]byte, builderBufferSize),
80+
buf: make([]byte, defaultTupleLengthTarget),
8181
vs: vs,
8282
tupleLengthTarget: defaultTupleLengthTarget,
8383
}
@@ -522,11 +522,12 @@ func (tb *TupleBuilder) putAddr(i int, v hash.Hash) {
522522
}
523523

524524
func (tb *TupleBuilder) ensureCapacity(sz ByteSize) {
525-
need := int(tb.pos+int64(sz)) - len(tb.buf)
526-
if need > 0 {
527-
for i := 0; i < need; i++ {
528-
tb.buf = append(tb.buf, byte(0))
529-
}
525+
need := int(tb.pos+int64(sz)) - cap(tb.buf)
526+
// TODO: This wastes memory when allocating a new backing array.
527+
// The initial part of the new backing array won't be referenced by anything.
528+
// tb.fields will still point to the original backing array.
529+
for i := 0; i < need; i++ {
530+
tb.buf = append(tb.buf, byte(0))
530531
}
531532
}
532533

go/store/val/tuple_builder_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package val
1717
import (
1818
"bytes"
1919
"context"
20+
"fmt"
2021
"math"
2122
"math/rand"
2223
"testing"
@@ -354,3 +355,39 @@ func TestTupleBuilderAdaptiveEncodings(t *testing.T) {
354355
})
355356
}
356357
}
358+
359+
type testBuilder struct {
360+
fields [][]byte
361+
buf []byte
362+
}
363+
364+
func TestTestBuilder(t *testing.T) {
365+
tb := &testBuilder{
366+
fields: make([][]byte, 20),
367+
buf: make([]byte, 10),
368+
}
369+
origbuf := tb.buf
370+
for i := 0; i < len(tb.buf); i++ {
371+
tb.buf[i] = byte(i)
372+
tb.fields[i] = tb.buf[i : i+1]
373+
}
374+
fmt.Printf("Orig Buf: %v\n", origbuf)
375+
fmt.Printf(" Fields: %v\n", tb.fields)
376+
377+
// Allocate new backing array
378+
for i := 10; i < len(tb.fields); i++ {
379+
tb.buf = append(tb.buf, byte(100+i))
380+
tb.fields[i] = tb.buf[i : i+1]
381+
}
382+
newBuf := tb.buf
383+
fmt.Printf("Orig Buf: %v\n", origbuf)
384+
fmt.Printf(" New Buf: %v\n", newBuf)
385+
fmt.Printf(" Fields: %v\n", tb.fields)
386+
387+
// Fields[0] still references the original backing array
388+
origbuf[0] = 99
389+
newBuf[0] = 10
390+
fmt.Printf("Orig Buf: %v\n", origbuf)
391+
fmt.Printf(" New Buf: %v\n", newBuf)
392+
fmt.Printf(" Fields: %v\n", tb.fields)
393+
}

go/store/val/tuple_descriptor.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -585,13 +585,10 @@ func (td *TupleDesc) GetAddr(i int, tup Tuple) (hash.Hash, bool) {
585585
return hash.New(b), true
586586
}
587587

588-
func (td *TupleDesc) ExpectEncoding(i int, encodings ...Encoding) {
589-
for _, enc := range encodings {
590-
if enc == td.Types[i].Enc {
591-
return
592-
}
588+
func (td *TupleDesc) ExpectEncoding(i int, enc Encoding) {
589+
if enc != td.Types[i].Enc {
590+
panic("incorrect value encoding")
593591
}
594-
panic("incorrect value encoding")
595592
}
596593

597594
func (td *TupleDesc) GetCell(i int, tup Tuple) (v Cell, ok bool) {

0 commit comments

Comments
 (0)