Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tree] return blob builders to pool after use #7884

Merged
merged 10 commits into from
May 22, 2024
6 changes: 6 additions & 0 deletions go/libraries/doltcore/migrate/tuples.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ func translateStringField(ctx context.Context, ns tree.NodeStore, value types.St
// note: previously, TEXT fields were serialized as types.String
rd := strings.NewReader(string(value))
bb := ns.BlobBuilder()
defer ns.PutBlobBuilder(bb)

bb.Init(len(value))
_, addr, err := bb.Chunk(ctx, rd)
if err != nil {
Expand Down Expand Up @@ -316,6 +318,8 @@ func translateJSONField(ctx context.Context, ns tree.NodeStore, value types.JSON
buf := bytes.NewBuffer([]byte(s))

bb := ns.BlobBuilder()
defer ns.PutBlobBuilder(bb)

bb.Init(len(s))
_, addr, err := bb.Chunk(ctx, buf)
if err != nil {
Expand Down Expand Up @@ -346,6 +350,8 @@ func translateBlobField(ctx context.Context, ns tree.NodeStore, value types.Blob
}

bb := ns.BlobBuilder()
defer ns.PutBlobBuilder(bb)

bb.Init(int(value.Len()))
_, addr, err := bb.Chunk(ctx, bytes.NewReader(buf))
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions go/store/prolly/tree/blob_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ func (b *BlobBuilder) SetNodeStore(ns NodeStore) {
func (b *BlobBuilder) Reset() {
b.wr = nil
b.topLevel = 0
b.buf = nil
b.vals = nil
b.subtrees = nil
b.lastN = Node{}
b.levelCap = 0
}

// Init calculates tree dimensions for a given blob.
func (b *BlobBuilder) Init(dataSize int) {
b.Reset()

if dataSize == 0 {
return
}
Expand Down
11 changes: 8 additions & 3 deletions go/store/prolly/tree/node_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type NodeStore interface {
Format() *types.NomsBinFormat

BlobBuilder() *BlobBuilder
PutBlobBuilder(*BlobBuilder)
}

type nodeStore struct {
Expand Down Expand Up @@ -176,12 +177,16 @@ func (ns nodeStore) Pool() pool.BuffPool {
// BlobBuilder implements NodeStore.
func (ns nodeStore) BlobBuilder() *BlobBuilder {
bb := ns.bbp.Get().(*BlobBuilder)
if bb.ns == nil {
bb.SetNodeStore(ns)
}
bb.SetNodeStore(ns)
return bb
}

// PutBlobBuilder implements NodeStore.
func (ns nodeStore) PutBlobBuilder(bb *BlobBuilder) {
bb.Reset()
ns.bbp.Put(bb)
}

func (ns nodeStore) Format() *types.NomsBinFormat {
nbf, err := types.GetFormatForVersionString(ns.store.Version())
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go/store/prolly/tree/prolly_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ func serializeGeometry(v interface{}) []byte {

func SerializeBytesToAddr(ctx context.Context, ns NodeStore, r io.Reader, dataSize int) (hash.Hash, error) {
bb := ns.BlobBuilder()
defer ns.PutBlobBuilder(bb)
bb.Init(dataSize)
_, addr, err := bb.Chunk(ctx, r)
if err != nil {
Expand Down
14 changes: 10 additions & 4 deletions go/store/prolly/tree/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ func NewTestNodeStore() NodeStore {
ts := &chunks.TestStorage{}
ns := NewNodeStore(ts.NewViewWithFormat(types.Format_DOLT.VersionString()))
bb := &blobBuilderPool
return nodeStoreValidator{ns: ns, bb: bb}
return nodeStoreValidator{ns: ns, bbp: bb}
}

type nodeStoreValidator struct {
ns NodeStore
bb *sync.Pool
ns NodeStore
bbp *sync.Pool
}

func (v nodeStoreValidator) Read(ctx context.Context, ref hash.Hash) (Node, error) {
Expand Down Expand Up @@ -320,13 +320,19 @@ func (v nodeStoreValidator) Pool() pool.BuffPool {
}

func (v nodeStoreValidator) BlobBuilder() *BlobBuilder {
bb := v.bb.Get().(*BlobBuilder)
bb := v.bbp.Get().(*BlobBuilder)
if bb.ns == nil {
bb.SetNodeStore(v)
}
return bb
}

// PutBlobBuilder implements NodeStore.
func (v nodeStoreValidator) PutBlobBuilder(bb *BlobBuilder) {
bb.Reset()
v.bbp.Put(bb)
}

func (v nodeStoreValidator) Format() *types.NomsBinFormat {
return v.ns.Format()
}
Loading