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

Opt(bulk): Reuse z.Allocator #7360

Merged
merged 1 commit into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Encoder struct {
BlockSize int
pack *pb.UidPack
uids []uint64
alloc *z.Allocator
Alloc *z.Allocator
buf *bytes.Buffer
}

Expand All @@ -70,7 +70,7 @@ func (e *Encoder) packBlock() {
}

// Allocate blocks manually.
b := e.alloc.AllocateAligned(blockSize)
b := e.Alloc.AllocateAligned(blockSize)
block := (*pb.UidBlock)(unsafe.Pointer(&b[0]))

block.Base = e.uids[0]
Expand Down Expand Up @@ -106,7 +106,7 @@ func (e *Encoder) packBlock() {
}

sz := len(e.buf.Bytes())
block.Deltas = e.alloc.Allocate(sz)
block.Deltas = e.Alloc.Allocate(sz)
x.AssertTrue(sz == copy(block.Deltas, e.buf.Bytes()))
e.pack.Blocks = append(e.pack.Blocks, block)
}
Expand All @@ -117,10 +117,12 @@ var tagEncoder string = "enc"
func (e *Encoder) Add(uid uint64) {
if e.pack == nil {
e.pack = &pb.UidPack{BlockSize: uint32(e.BlockSize)}
e.alloc = z.NewAllocator(1024)
e.alloc.Tag = tagEncoder
e.buf = new(bytes.Buffer)
}
if e.Alloc == nil {
e.Alloc = z.NewAllocator(1024)
e.Alloc.Tag = tagEncoder
}

size := len(e.uids)
if size > 0 && !match32MSB(e.uids[size-1], uid) {
Expand All @@ -138,8 +140,8 @@ func (e *Encoder) Add(uid uint64) {
// Done returns the final output of the encoder. This UidPack MUST BE FREED via a call to FreePack.
func (e *Encoder) Done() *pb.UidPack {
e.packBlock()
if e.pack != nil && e.alloc != nil {
e.pack.AllocRef = e.alloc.Ref
if e.pack != nil && e.Alloc != nil {
e.pack.AllocRef = e.Alloc.Ref
}
return e.pack
}
Expand Down
9 changes: 6 additions & 3 deletions dgraph/cmd/bulk/count_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ func (c *countIndexer) writeIndex(buf *z.Buffer) {
fmt.Printf("Writing count index for %q rev=%v\n", pk.Attr, pk.IsReverse())
}

alloc := z.NewAllocator(8 << 20)
defer alloc.Release()

var pl pb.PostingList
encoder := codec.Encoder{BlockSize: 256}
encoder := codec.Encoder{BlockSize: 256, Alloc: alloc}

outBuf := z.NewBuffer(5 << 20)
defer outBuf.Release()
Expand All @@ -144,13 +147,13 @@ func (c *countIndexer) writeIndex(buf *z.Buffer) {
}

kv := posting.MarshalPostingList(&pl, nil)
codec.FreePack(pl.Pack)
kv.Key = append([]byte{}, lastCe.Key()...)
kv.Version = c.state.writeTs
kv.StreamId = streamId
badger.KVToBuffer(kv, outBuf)

encoder = codec.Encoder{BlockSize: 256}
alloc.Reset()
encoder = codec.Encoder{BlockSize: 256, Alloc: alloc}
pl.Reset()

// Flush out the buffer.
Expand Down
16 changes: 13 additions & 3 deletions dgraph/cmd/bulk/reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,12 @@ func (r *reducer) toList(req *encodeRequest) {
freePostings = append(freePostings, p)
}

alloc := z.NewAllocator(16 << 20)
defer func() {
// We put alloc.Release in defer because we reassign alloc for split posting lists.
alloc.Release()
}()

start, end, num := cbuf.StartOffset(), cbuf.StartOffset(), 0
appendToList := func() {
if num == 0 {
Expand All @@ -595,7 +601,8 @@ func (r *reducer) toList(req *encodeRequest) {
}
}

enc := codec.Encoder{BlockSize: 256}
alloc.Reset()
enc := codec.Encoder{BlockSize: 256, Alloc: alloc}
var lastUid uint64
slice, next := []byte{}, start
for next >= 0 && (next < end || end == -1) {
Expand Down Expand Up @@ -629,7 +636,7 @@ func (r *reducer) toList(req *encodeRequest) {
// the full pb.Posting type is used (which pb.y contains the
// delta packed UID list).
if numUids == 0 {
codec.FreePack(pl.Pack)
// No need to FrePack here because we are reusing alloc.
return
}

Expand Down Expand Up @@ -657,6 +664,9 @@ func (r *reducer) toList(req *encodeRequest) {
kvs, err := l.Rollup(nil)
x.Check(err)

// Assign a new allocator, so we don't reset the one we were using during Rollup.
alloc = z.NewAllocator(16 << 20)

for _, kv := range kvs {
kv.StreamId = r.streamIdFor(pk.Attr)
}
Expand All @@ -666,7 +676,7 @@ func (r *reducer) toList(req *encodeRequest) {
}
} else {
kv := posting.MarshalPostingList(pl, nil)
codec.FreePack(pl.Pack)
// No need to FreePack here, because we are reusing alloc.

kv.Key = y.Copy(currentKey)
kv.Version = writeVersionTs
Expand Down
4 changes: 2 additions & 2 deletions t/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,12 @@ func getPackages() []task {
}

if !isValidPackageForSuite(pkg.ID) {
fmt.Printf("Skipping pacakge %s as its not valid for the selected suite %s \n", pkg.ID, *suite)
fmt.Printf("Skipping package %s as its not valid for the selected suite %s \n", pkg.ID, *suite)
continue
}

if has(skipPkgs, pkg.ID) {
fmt.Printf("Skipping pacakge %s as its available in skip list \n", pkg.ID)
fmt.Printf("Skipping package %s as its available in skip list \n", pkg.ID)
continue
}

Expand Down