From d02b823ba37c9123b0a227b18dee65275f060e1e Mon Sep 17 00:00:00 2001 From: Travis Date: Wed, 8 Mar 2017 16:36:11 -0600 Subject: [PATCH] Move the top-n `trim` performance enhancement over to the v0.2 branch. --- executor.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/executor.go b/executor.go index 75c55a74d..6dc6d1dec 100644 --- a/executor.go +++ b/executor.go @@ -182,7 +182,16 @@ func (e *Executor) executeTopN(ctx context.Context, db string, c *pql.TopN, slic other.BitmapIDs = Pairs(pairs).Keys() sort.Sort(uint64Slice(other.BitmapIDs)) - return e.executeTopNSlices(ctx, db, &other, slices, opt) + trimmedList, err := e.executeTopNSlices(ctx, db, &other, slices, opt) + if err != nil { + return nil, err + } + + if c.N != 0 && int(c.N) < len(trimmedList) { + trimmedList = trimmedList[0:c.N] + } + return trimmedList, nil + } func (e *Executor) executeTopNSlices(ctx context.Context, db string, c *pql.TopN, slices []uint64, opt *ExecOptions) ([]Pair, error) { @@ -206,11 +215,6 @@ func (e *Executor) executeTopNSlices(ctx context.Context, db string, c *pql.TopN // Sort final merged results. sort.Sort(Pairs(results)) - // Only keep the top n after sorting. - if c.N > 0 && len(results) > c.N { - results = results[0:c.N] - } - return results, nil }