Too much cpu usage during processSort

I do some profile on dgraph. Basically it is getting the timeline of the social user. I found it use over 900% CPU time in my 16core machine. I have do some profile using pprof, and the problem is in the processSort function(worker/sort.go), sortWithIndex and sortWithoutIndex are doing together(of course the result will not return in 3ms). Could somebody explain why dgraph do in this way?

go func() {
	select {
	case <-time.After(3 * time.Millisecond):
		// Wait between ctx chan and time chan.
	case <-ctx.Done():
		resCh <- &sortresult{err: ctx.Err()}
		return
	}
	r := sortWithoutIndex(cctx, ts)
	resCh <- r
}()


go func() {
	sr := sortWithIndex(cctx, ts)
	resCh <- sr
}()

An old optimization. We were seeing that sometimes it was faster to sort without index, than to sort with index. So, if sort with index didn’t finish in 3 ms, then sort without index would start.

1 Like