0
0
Fork 0

disjunction searcher avoids second ID.Equals() comparison

Optimization for DisjunctionSearcher, where an extra matchingIdxs
helps track the currs that were matching.  This avoids the previous
code's second loop through the currs slice.
This commit is contained in:
Steve Yen 2016-09-18 10:09:40 -07:00
parent 090c08eb46
commit dd7cb14a56
1 changed files with 28 additions and 26 deletions

View File

@ -25,15 +25,16 @@ import (
var DisjunctionMaxClauseCount = 0
type DisjunctionSearcher struct {
indexReader index.IndexReader
searchers OrderedSearcherList
queryNorm float64
currs []*search.DocumentMatch
currentID index.IndexInternalID
scorer *scorers.DisjunctionQueryScorer
min float64
matching []*search.DocumentMatch
initialized bool
indexReader index.IndexReader
searchers OrderedSearcherList
queryNorm float64
currs []*search.DocumentMatch
currentID index.IndexInternalID
scorer *scorers.DisjunctionQueryScorer
min float64
matching []*search.DocumentMatch
matchingIdxs []int
initialized bool
}
func tooManyClauses(count int) bool {
@ -60,12 +61,13 @@ func NewDisjunctionSearcher(indexReader index.IndexReader, qsearchers []search.S
sort.Sort(sort.Reverse(searchers))
// build our searcher
rv := DisjunctionSearcher{
indexReader: indexReader,
searchers: searchers,
currs: make([]*search.DocumentMatch, len(searchers)),
scorer: scorers.NewDisjunctionQueryScorer(explain),
min: min,
matching: make([]*search.DocumentMatch, len(searchers)),
indexReader: indexReader,
searchers: searchers,
currs: make([]*search.DocumentMatch, len(searchers)),
scorer: scorers.NewDisjunctionQueryScorer(explain),
min: min,
matching: make([]*search.DocumentMatch, len(searchers)),
matchingIdxs: make([]int, len(searchers)),
}
rv.computeQueryNorm()
return &rv, nil
@ -140,9 +142,11 @@ func (s *DisjunctionSearcher) Next(ctx *search.SearchContext) (*search.DocumentM
found := false
for !found && s.currentID != nil {
matching := s.matching[:0]
for _, curr := range s.currs {
matchingIdxs := s.matchingIdxs[:0]
for i, curr := range s.currs {
if curr != nil && curr.IndexInternalID.Equals(s.currentID) {
matching = append(matching, curr)
matchingIdxs = append(matchingIdxs, i)
}
}
@ -153,16 +157,14 @@ func (s *DisjunctionSearcher) Next(ctx *search.SearchContext) (*search.DocumentM
}
// invoke next on all the matching searchers
for i, curr := range s.currs {
if curr != nil && curr.IndexInternalID.Equals(s.currentID) {
searcher := s.searchers[i]
if s.currs[i] != rv {
ctx.DocumentMatchPool.Put(s.currs[i])
}
s.currs[i], err = searcher.Next(ctx)
if err != nil {
return nil, err
}
for _, i := range matchingIdxs {
searcher := s.searchers[i]
if s.currs[i] != rv {
ctx.DocumentMatchPool.Put(s.currs[i])
}
s.currs[i], err = searcher.Next(ctx)
if err != nil {
return nil, err
}
}
s.currentID = s.nextSmallestID()