From db792717a6d83b0354dfbf9aa5a7ad0e9331db5b Mon Sep 17 00:00:00 2001 From: Steve Yen Date: Sat, 17 Mar 2018 10:59:57 -0700 Subject: [PATCH] scorch zap postingsIter reuses nextLocs/nextSegmentLocs The previous code would inefficiently throw away the nextLocs and would also throw away the []segment.Location slice if there were no locations, such as if it was a 1-hit postings list. This change tries to reuse the nextLocs/nextSegmentLocs for all cases. --- index/scorch/segment/zap/posting.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/index/scorch/segment/zap/posting.go b/index/scorch/segment/zap/posting.go index 02e28657..3fecaa23 100644 --- a/index/scorch/segment/zap/posting.go +++ b/index/scorch/segment/zap/posting.go @@ -154,6 +154,9 @@ func (p *PostingsList) iterator(rv *PostingsIterator) *PostingsIterator { freqChunkOffsets := rv.freqChunkOffsets[:0] locChunkOffsets := rv.locChunkOffsets[:0] + nextLocs := rv.nextLocs[:0] + nextSegmentLocs := rv.nextSegmentLocs[:0] + buf := rv.buf *rv = PostingsIterator{} // clear the struct @@ -167,6 +170,9 @@ func (p *PostingsList) iterator(rv *PostingsIterator) *PostingsIterator { rv.freqChunkOffsets = freqChunkOffsets rv.locChunkOffsets = locChunkOffsets + rv.nextLocs = nextLocs + rv.nextSegmentLocs = nextSegmentLocs + rv.buf = buf } rv.postings = p @@ -314,8 +320,9 @@ type PostingsIterator struct { locChunkOffsets []uint64 locChunkStart uint64 - next Posting // reused across Next() calls - nextLocs []Location // reused across Next() calls + next Posting // reused across Next() calls + nextLocs []Location // reused across Next() calls + nextSegmentLocs []segment.Location // reused across Next() calls docNum1Hit uint64 normBits1Hit uint64 @@ -469,8 +476,7 @@ func (i *PostingsIterator) Next() (segment.Posting, error) { return nil, err } - reuseLocs := i.next.locs // hold for reuse before struct clearing - i.next = Posting{} // clear the struct + i.next = Posting{} // clear the struct rv := &i.next rv.docNum = docNum @@ -491,11 +497,10 @@ func (i *PostingsIterator) Next() (segment.Posting, error) { } else { i.nextLocs = make([]Location, rv.freq) } - if cap(reuseLocs) >= int(rv.freq) { - rv.locs = reuseLocs[0:rv.freq] - } else { - rv.locs = make([]segment.Location, rv.freq) + if cap(i.nextSegmentLocs) < int(rv.freq) { + i.nextSegmentLocs = make([]segment.Location, rv.freq) } + rv.locs = i.nextSegmentLocs[0:rv.freq] for j := 0; j < int(rv.freq); j++ { err := i.readLocation(&i.nextLocs[j]) if err != nil {