Skip to content

Commit 81491c5

Browse files
author
Harshil Goel
committed
first commit
got performance Fixed a small bug Removed uidlist temp
1 parent d572cae commit 81491c5

File tree

8 files changed

+88
-37
lines changed

8 files changed

+88
-37
lines changed

algo/uidlist.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func IntersectCompressedWith(pack *pb.UidPack, afterUID uint64, v, o *pb.List) {
6060

6161
// Select appropriate function based on heuristics.
6262
ratio := float64(m) / float64(n)
63-
if ratio < 10 {
63+
if ratio <= 10 {
6464
IntersectCompressedWithLinJump(&dec, v.Uids, &dst)
6565
} else {
6666
IntersectCompressedWithBin(&dec, v.Uids, &dst)

algo/uidlist_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ func BenchmarkListIntersectRandom(b *testing.B) {
370370
func BenchmarkListIntersectCompressBin(b *testing.B) {
371371
randomTests := func(sz int, overlap float64) {
372372
rs := []float64{0.01, 0.1, 1, 10, 100}
373+
//rs := []float64{0.002, 0.01, 0.1}
373374
for _, r := range rs {
374375
sz1 := sz
375376
sz2 := int(float64(sz) * r)

codec/codec.go

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func (d *Decoder) UnpackBlock() []uint64 {
173173
if d.blockIdx >= len(d.Pack.Blocks) {
174174
return d.uids
175175
}
176+
176177
block := d.Pack.Blocks[d.blockIdx]
177178

178179
last := block.Base

go.mod

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module github.com/dgraph-io/dgraph
22

33
go 1.19
44

5+
replace github.com/dgraph-io/badger/v4 => ../badger
6+
57
require (
68
contrib.go.opencensus.io/exporter/jaeger v0.1.0
79
contrib.go.opencensus.io/exporter/prometheus v0.1.0
@@ -40,7 +42,7 @@ require (
4042
github.com/mitchellh/panicwrap v1.0.0
4143
github.com/paulmach/go.geojson v0.0.0-20170327170536-40612a87147b
4244
github.com/pkg/errors v0.9.1
43-
github.com/pkg/profile v1.2.1
45+
github.com/pkg/profile v1.7.0
4446
github.com/prometheus/client_golang v1.14.0
4547
github.com/soheilhy/cmux v0.1.4
4648
github.com/spf13/cast v1.3.0
@@ -85,10 +87,12 @@ require (
8587
github.com/eapache/go-resiliency v1.4.0 // indirect
8688
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
8789
github.com/eapache/queue v1.1.0 // indirect
90+
github.com/felixge/fgprof v0.9.3 // indirect
8891
github.com/frankban/quicktest v1.10.2 // indirect
8992
github.com/fsnotify/fsnotify v1.6.0 // indirect
9093
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
9194
github.com/google/flatbuffers v1.12.1 // indirect
95+
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
9296
github.com/hashicorp/errwrap v1.1.0 // indirect
9397
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
9498
github.com/hashicorp/go-multierror v1.1.1 // indirect

go.sum

+8-4
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
145145
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
146146
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
147147
github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
148-
github.com/dgraph-io/badger/v4 v4.2.0 h1:kJrlajbXXL9DFTNuhhu9yCx7JJa4qpYWxtE8BzuWsEs=
149-
github.com/dgraph-io/badger/v4 v4.2.0/go.mod h1:qfCqhPoWDFJRx1gp5QwwyGo8xk1lbHUxvK9nK0OGAak=
150148
github.com/dgraph-io/dgo/v230 v230.0.1 h1:kR7gI7/ZZv0jtG6dnedNgNOCxe1cbSG8ekF+pNfReks=
151149
github.com/dgraph-io/dgo/v230 v230.0.1/go.mod h1:5FerO2h4LPOxR2XTkOAtqUUPaFdQ+5aBOHXPBJ3nT10=
152150
github.com/dgraph-io/gqlgen v0.13.2 h1:TNhndk+eHKj5qE7BenKKSYdSIdOGhLqxR1rCiMso9KM=
@@ -199,6 +197,8 @@ github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHj
199197
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
200198
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
201199
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
200+
github.com/felixge/fgprof v0.9.3 h1:VvyZxILNuCiUCSXtPtYmmtGvb65nqXh2QFWc0Wpf2/g=
201+
github.com/felixge/fgprof v0.9.3/go.mod h1:RdbpDgzqYVh/T9fPELJyV7EYJuHB55UTEULNun8eiPw=
202202
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA=
203203
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
204204
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
@@ -316,6 +316,8 @@ github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hf
316316
github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
317317
github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
318318
github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
319+
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y=
320+
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg=
319321
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
320322
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
321323
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
@@ -394,6 +396,7 @@ github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKe
394396
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
395397
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
396398
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
399+
github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
397400
github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA=
398401
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
399402
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
@@ -557,8 +560,8 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
557560
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
558561
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
559562
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
560-
github.com/pkg/profile v1.2.1 h1:F++O52m40owAmADcojzM+9gyjmMOY/T4oYJkgFDH8RE=
561-
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
563+
github.com/pkg/profile v1.7.0 h1:hnbDkaNWPCLMO9wGLdBFTIZvzDrDfBM2072E1S9gJkA=
564+
github.com/pkg/profile v1.7.0/go.mod h1:8Uer0jas47ZQMJ7VD+OHknK4YDY07LPUC6dEvqDjvNo=
562565
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
563566
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
564567
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -899,6 +902,7 @@ golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7w
899902
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
900903
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
901904
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
905+
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
902906
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
903907
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
904908
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

posting/lists.go

+55-1
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,62 @@ func (lc *LocalCache) getInternal(key []byte, readFromDisk bool) (*List, error)
195195
return lc.SetIfAbsent(skey, pl), nil
196196
}
197197

198-
func (lc *LocalCache) GetSinglePosting(key []byte) (*pb.PostingList, error) {
198+
func (lc *LocalCache) GetBatchSinglePosting(keys [][]byte) ([]*pb.PostingList, error) {
199+
results := make([]*pb.PostingList, len(keys))
200+
remaining_keys := make([][]byte, 0)
201+
lc.RLock()
202+
for i, key := range keys {
203+
pl := &pb.PostingList{}
204+
if delta, ok := lc.deltas[string(key)]; ok && len(delta) > 0 {
205+
err := pl.Unmarshal(delta)
206+
if err != nil {
207+
results[i] = pl
208+
}
209+
} else {
210+
remaining_keys = append(remaining_keys, key)
211+
}
212+
}
213+
lc.RUnlock()
214+
215+
txn := pstore.NewTransactionAt(lc.startTs, false)
216+
items, err := txn.GetBatch(remaining_keys)
217+
idx := 0
218+
219+
for i := 0; i < len(results); i++ {
220+
if results[i] != nil {
221+
continue
222+
}
223+
pl := &pb.PostingList{}
224+
err = items[idx].Value(func(val []byte) error {
225+
if err := pl.Unmarshal(val); err != nil {
226+
return err
227+
}
228+
return nil
229+
})
230+
idx += 1
231+
results[i] = pl
232+
}
199233

234+
for i := 0; i < len(results); i++ {
235+
pl := results[i]
236+
idx := 0
237+
for _, postings := range pl.Postings {
238+
if hasDeleteAll(postings) {
239+
return nil, nil
240+
}
241+
if postings.Op != Del {
242+
pl.Postings[idx] = postings
243+
idx++
244+
}
245+
}
246+
pl.Postings = pl.Postings[:idx]
247+
results[i] = pl
248+
}
249+
250+
return results, err
251+
}
252+
253+
func (lc *LocalCache) GetSinglePosting(key []byte) (*pb.PostingList, error) {
200254
getPostings := func() (*pb.PostingList, error) {
201255
pl := &pb.PostingList{}
202256
lc.RLock()

worker/sort.go

+1-29
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"encoding/hex"
2222
"sort"
2323
"strings"
24-
"time"
2524

2625
"github.com/golang/glog"
2726
"github.com/pkg/errors"
@@ -512,34 +511,7 @@ func processSort(ctx context.Context, ts *pb.SortMessage) (*pb.SortResult, error
512511
cctx, cancel := context.WithCancel(ctx)
513512
defer cancel()
514513

515-
resCh := make(chan *sortresult, 2)
516-
go func() {
517-
select {
518-
case <-time.After(3 * time.Millisecond):
519-
// Wait between ctx chan and time chan.
520-
case <-ctx.Done():
521-
resCh <- &sortresult{err: ctx.Err()}
522-
return
523-
}
524-
r := sortWithoutIndex(cctx, ts)
525-
resCh <- r
526-
}()
527-
528-
go func() {
529-
sr := sortWithIndex(cctx, ts)
530-
resCh <- sr
531-
}()
532-
533-
r := <-resCh
534-
if r.err == nil {
535-
cancel()
536-
// wait for other goroutine to get cancelled
537-
<-resCh
538-
} else {
539-
span.Annotatef(nil, "processSort error: %v", r.err)
540-
r = <-resCh
541-
}
542-
514+
r := sortWithIndex(cctx, ts)
543515
if r.err != nil {
544516
return nil, r.err
545517
}

worker/task.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ func (qs *queryState) handleValuePostings(ctx context.Context, args funcArgs) er
385385
out := &pb.Result{}
386386
outputs[start/width] = out
387387

388+
cache := make([]*pb.PostingList, 0)
388389
for i := start; i < end; i++ {
389390
select {
390391
case <-ctx.Done():
@@ -399,7 +400,21 @@ func (qs *queryState) handleValuePostings(ctx context.Context, args funcArgs) er
399400
fcs := &pb.FacetsList{FacetsList: make([]*pb.Facets, 0)} // TODO Figure out how it is stored
400401

401402
if !getMultiplePosting {
402-
pl, _ := qs.cache.GetSinglePosting(key)
403+
if len(cache) == 0 {
404+
keys := make([][]byte, 10)
405+
keys[0] = key
406+
for j := i + 1; j < i+10 && j < end; j++ {
407+
keys[j-i] = x.DataKey(q.Attr, q.UidList.Uids[j])
408+
}
409+
cache, err = qs.cache.GetBatchSinglePosting(keys)
410+
if err != nil {
411+
return err
412+
}
413+
}
414+
pl := cache[0]
415+
if len(cache) > 1 {
416+
cache = cache[1:]
417+
}
403418
if pl == nil || len(pl.Postings) == 0 {
404419
out.UidMatrix = append(out.UidMatrix, &pb.List{})
405420
out.FacetMatrix = append(out.FacetMatrix, &pb.FacetsList{})

0 commit comments

Comments
 (0)