Skip to content

Commit

Permalink
[tree] return blob builders to pool after use (#7884)
Browse files Browse the repository at this point in the history
* [tree] return blob builders to pool after use

* [ga-format-pr] Run go/utils/repofmt/format_repo.sh and go/Godeps/update.sh

* reset builders

* better builder reset

* still trying to repro dangling ref

* still trying to repro

* think this fixes dropped ref

* remove duplicate reset

---------

Co-authored-by: max-hoffman <max-hoffman@users.noreply.github.com>
  • Loading branch information
max-hoffman and max-hoffman authored May 22, 2024
1 parent e958f36 commit b091820
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
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}
}