0
0
Fork 0

optimize Advance() with pre-allocated in-out param

This perf-related change helps the code and API reach more similarity
with the Next() methods, which now take a pre-allocate param.
This commit is contained in:
Steve Yen 2016-07-29 14:14:58 -07:00
parent 3c82086805
commit 4822cff63a
20 changed files with 54 additions and 50 deletions

View File

@ -117,7 +117,7 @@ type TermFieldReader interface {
// Advance resets the enumeration at specified document or its immediate
// follower.
Advance(ID string) (*TermFieldDoc, error)
Advance(ID string, preAlloced *TermFieldDoc) (*TermFieldDoc, error)
// Count returns the number of documents contains the term in this field.
Count() uint64

View File

@ -94,7 +94,7 @@ func (r *UpsideDownCouchTermFieldReader) Next(preAlloced *index.TermFieldDoc) (*
return nil, nil
}
func (r *UpsideDownCouchTermFieldReader) Advance(docID string) (*index.TermFieldDoc, error) {
func (r *UpsideDownCouchTermFieldReader) Advance(docID string, preAlloced *index.TermFieldDoc) (*index.TermFieldDoc, error) {
if r.iterator != nil {
tfr := NewTermFrequencyRow(r.term, r.field, []byte(docID), 0, 0)
r.iterator.Seek(tfr.Key())
@ -104,14 +104,18 @@ func (r *UpsideDownCouchTermFieldReader) Advance(docID string) (*index.TermField
if err != nil {
return nil, err
}
rv := index.TermFieldDoc{
ID: string(tfr.doc),
Freq: tfr.freq,
Norm: float64(tfr.norm),
Vectors: r.indexReader.index.termFieldVectorsFromTermVectors(tfr.vectors),
rv := preAlloced
if rv == nil {
rv = &index.TermFieldDoc{}
}
rv.ID = string(tfr.doc)
rv.Freq = tfr.freq
rv.Norm = float64(tfr.norm)
if tfr.vectors != nil {
rv.Vectors = r.indexReader.index.termFieldVectorsFromTermVectors(tfr.vectors)
}
r.iterator.Next()
return &rv, nil
return rv, nil
}
}
return nil, nil

View File

@ -145,7 +145,7 @@ func TestIndexReader(t *testing.T) {
t.Errorf("Error accessing term field reader: %v", err)
}
match, err = reader.Advance("2")
match, err = reader.Advance("2", nil)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
@ -155,7 +155,7 @@ func TestIndexReader(t *testing.T) {
if match.ID != "2" {
t.Errorf("Expected ID '2', got '%s'", match.ID)
}
match, err = reader.Advance("3")
match, err = reader.Advance("3", nil)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
@ -183,7 +183,7 @@ func TestIndexReader(t *testing.T) {
if match != nil {
t.Errorf("expected nil, got %v", match)
}
match, err = reader.Advance("anywhere")
match, err = reader.Advance("anywhere", nil)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

View File

@ -27,7 +27,7 @@ func (ss *stubSearcher) Next(preAllocated *search.DocumentMatch) (*search.Docume
return nil, nil
}
func (ss *stubSearcher) Advance(ID string) (*search.DocumentMatch, error) {
func (ss *stubSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
for ss.index < len(ss.matches) && ss.matches[ss.index].ID < ID {
ss.index++

View File

@ -98,7 +98,7 @@ func (c DocumentMatchCollection) Less(i, j int) bool { return c[i].Score > c[j].
type Searcher interface {
Next(preAllocated *DocumentMatch) (*DocumentMatch, error)
Advance(ID string) (*DocumentMatch, error)
Advance(ID string, preAllocated *DocumentMatch) (*DocumentMatch, error)
Close() error
Weight() float64
SetQueryNorm(float64)

View File

@ -163,7 +163,7 @@ func (s *BooleanSearcher) Next(preAllocated *search.DocumentMatch) (*search.Docu
for s.currentID != "" {
if s.currMustNot != nil && s.currMustNot.ID < s.currentID {
// advance must not searcher to our candidate entry
s.currMustNot, err = s.mustNotSearcher.Advance(s.currentID)
s.currMustNot, err = s.mustNotSearcher.Advance(s.currentID, nil)
if err != nil {
return nil, err
}
@ -186,7 +186,7 @@ func (s *BooleanSearcher) Next(preAllocated *search.DocumentMatch) (*search.Docu
if s.currShould != nil && s.currShould.ID < s.currentID {
// advance should searcher to our candidate entry
s.currShould, err = s.shouldSearcher.Advance(s.currentID)
s.currShould, err = s.shouldSearcher.Advance(s.currentID, nil)
if err != nil {
return nil, err
}
@ -255,7 +255,7 @@ func (s *BooleanSearcher) Next(preAllocated *search.DocumentMatch) (*search.Docu
return rv, nil
}
func (s *BooleanSearcher) Advance(ID string) (*search.DocumentMatch, error) {
func (s *BooleanSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
if !s.initialized {
err := s.initSearchers()
@ -266,19 +266,19 @@ func (s *BooleanSearcher) Advance(ID string) (*search.DocumentMatch, error) {
var err error
if s.mustSearcher != nil {
s.currMust, err = s.mustSearcher.Advance(ID)
s.currMust, err = s.mustSearcher.Advance(ID, nil)
if err != nil {
return nil, err
}
}
if s.shouldSearcher != nil {
s.currShould, err = s.shouldSearcher.Advance(ID)
s.currShould, err = s.shouldSearcher.Advance(ID, nil)
if err != nil {
return nil, err
}
}
if s.mustNotSearcher != nil {
s.currMustNot, err = s.mustNotSearcher.Advance(ID)
s.currMustNot, err = s.mustNotSearcher.Advance(ID, nil)
if err != nil {
return nil, err
}
@ -292,7 +292,7 @@ func (s *BooleanSearcher) Advance(ID string) (*search.DocumentMatch, error) {
s.currentID = ""
}
return s.Next(nil)
return s.Next(preAllocated)
}
func (s *BooleanSearcher) Count() uint64 {

View File

@ -117,7 +117,7 @@ OUTER:
continue OUTER
}
// this reader doesn't have the currentID, try to advance
s.currs[i], err = termSearcher.Advance(s.currentID)
s.currs[i], err = termSearcher.Advance(s.currentID, nil)
if err != nil {
return nil, err
}
@ -155,7 +155,7 @@ OUTER:
return rv, nil
}
func (s *ConjunctionSearcher) Advance(ID string) (*search.DocumentMatch, error) {
func (s *ConjunctionSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
if !s.initialized {
err := s.initSearchers()
if err != nil {
@ -164,13 +164,13 @@ func (s *ConjunctionSearcher) Advance(ID string) (*search.DocumentMatch, error)
}
var err error
for i, searcher := range s.searchers {
s.currs[i], err = searcher.Advance(ID)
s.currs[i], err = searcher.Advance(ID, nil)
if err != nil {
return nil, err
}
}
s.currentID = ID
return s.Next(nil)
return s.Next(preAllocated)
}
func (s *ConjunctionSearcher) Count() uint64 {

View File

@ -164,7 +164,7 @@ func (s *DisjunctionSearcher) Next(preAllocated *search.DocumentMatch) (*search.
return rv, nil
}
func (s *DisjunctionSearcher) Advance(ID string) (*search.DocumentMatch, error) {
func (s *DisjunctionSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
if !s.initialized {
err := s.initSearchers()
if err != nil {
@ -174,7 +174,7 @@ func (s *DisjunctionSearcher) Advance(ID string) (*search.DocumentMatch, error)
// get all searchers pointing at their first match
var err error
for i, termSearcher := range s.searchers {
s.currs[i], err = termSearcher.Advance(ID)
s.currs[i], err = termSearcher.Advance(ID, nil)
if err != nil {
return nil, err
}
@ -182,7 +182,7 @@ func (s *DisjunctionSearcher) Advance(ID string) (*search.DocumentMatch, error)
s.currentID = s.nextSmallestID()
return s.Next(nil)
return s.Next(preAllocated)
}
func (s *DisjunctionSearcher) Count() uint64 {

View File

@ -158,7 +158,7 @@ func TestDisjunctionAdvance(t *testing.T) {
t.Fatal(err)
}
match, err := martyOrDustinSearcher.Advance("3")
match, err := martyOrDustinSearcher.Advance("3", nil)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

View File

@ -88,9 +88,9 @@ func (s *DocIDSearcher) Next(preAllocated *search.DocumentMatch) (*search.Docume
}
func (s *DocIDSearcher) Advance(ID string) (*search.DocumentMatch, error) {
func (s *DocIDSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
s.current = sort.SearchStrings(s.ids, ID)
return s.Next(nil)
return s.Next(preAllocated)
}
func (s *DocIDSearcher) Close() error {

View File

@ -91,7 +91,7 @@ func testDocIDSearcher(t *testing.T, indexed, searched, wanted []string) {
}
before := id[:1]
for _, target := range []string{before, id} {
m, err := searcher.Advance(target)
m, err := searcher.Advance(target, nil)
if err != nil {
t.Fatal(err)
}
@ -102,7 +102,7 @@ func testDocIDSearcher(t *testing.T, indexed, searched, wanted []string) {
}
// Seek after the end of the sequence
after := "zzz"
m, err = searcher.Advance(after)
m, err = searcher.Advance(after, nil)
if err != nil {
t.Fatal(err)
}

View File

@ -112,8 +112,8 @@ func (s *FuzzySearcher) Next(preAllocated *search.DocumentMatch) (*search.Docume
}
func (s *FuzzySearcher) Advance(ID string) (*search.DocumentMatch, error) {
return s.searcher.Advance(ID)
func (s *FuzzySearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
return s.searcher.Advance(ID, preAllocated)
}
func (s *FuzzySearcher) Close() error {

View File

@ -63,7 +63,7 @@ func (s *MatchAllSearcher) Next(preAllocated *search.DocumentMatch) (*search.Doc
}
func (s *MatchAllSearcher) Advance(ID string) (*search.DocumentMatch, error) {
func (s *MatchAllSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
id, err := s.reader.Advance(ID)
if err != nil {
return nil, err

View File

@ -40,7 +40,7 @@ func (s *MatchNoneSearcher) Next(preAllocated *search.DocumentMatch) (*search.Do
return nil, nil
}
func (s *MatchNoneSearcher) Advance(ID string) (*search.DocumentMatch, error) {
func (s *MatchNoneSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
return nil, nil
}

View File

@ -100,8 +100,8 @@ func (s *NumericRangeSearcher) Next(preAllocated *search.DocumentMatch) (*search
return s.searcher.Next(preAllocated)
}
func (s *NumericRangeSearcher) Advance(ID string) (*search.DocumentMatch, error) {
return s.searcher.Advance(ID)
func (s *NumericRangeSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
return s.searcher.Advance(ID, preAllocated)
}
func (s *NumericRangeSearcher) Close() error {

View File

@ -160,7 +160,7 @@ func (s *PhraseSearcher) Next(preAllocated *search.DocumentMatch) (*search.Docum
return nil, nil
}
func (s *PhraseSearcher) Advance(ID string) (*search.DocumentMatch, error) {
func (s *PhraseSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
if !s.initialized {
err := s.initSearchers()
if err != nil {
@ -168,11 +168,11 @@ func (s *PhraseSearcher) Advance(ID string) (*search.DocumentMatch, error) {
}
}
var err error
s.currMust, err = s.mustSearcher.Advance(ID)
s.currMust, err = s.mustSearcher.Advance(ID, nil)
if err != nil {
return nil, err
}
return s.Next(nil)
return s.Next(preAllocated)
}
func (s *PhraseSearcher) Count() uint64 {

View File

@ -111,8 +111,8 @@ func (s *RegexpSearcher) Next(preAllocated *search.DocumentMatch) (*search.Docum
}
func (s *RegexpSearcher) Advance(ID string) (*search.DocumentMatch, error) {
return s.searcher.Advance(ID)
func (s *RegexpSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
return s.searcher.Advance(ID, preAllocated)
}
func (s *RegexpSearcher) Close() error {

View File

@ -70,8 +70,8 @@ func (s *TermSearcher) Next(preAllocated *search.DocumentMatch) (*search.Documen
}
func (s *TermSearcher) Advance(ID string) (*search.DocumentMatch, error) {
termMatch, err := s.reader.Advance(ID)
func (s *TermSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
termMatch, err := s.reader.Advance(ID, s.tfd.Reset())
if err != nil {
return nil, err
}
@ -81,7 +81,7 @@ func (s *TermSearcher) Advance(ID string) (*search.DocumentMatch, error) {
}
// score match
docMatch := s.scorer.Score(termMatch, nil)
docMatch := s.scorer.Score(termMatch, preAllocated)
// return doc match
return docMatch, nil

View File

@ -75,8 +75,8 @@ func (s *TermPrefixSearcher) Next(preAllocated *search.DocumentMatch) (*search.D
}
func (s *TermPrefixSearcher) Advance(ID string) (*search.DocumentMatch, error) {
return s.searcher.Advance(ID)
func (s *TermPrefixSearcher) Advance(ID string, preAllocated *search.DocumentMatch) (*search.DocumentMatch, error) {
return s.searcher.Advance(ID, preAllocated)
}
func (s *TermPrefixSearcher) Close() error {

View File

@ -170,7 +170,7 @@ func TestTermSearcher(t *testing.T) {
if docMatch.ID != "a" {
t.Errorf("expected result ID to be 'a', got '%s", docMatch.ID)
}
docMatch, err = searcher.Advance("c")
docMatch, err = searcher.Advance("c", nil)
if err != nil {
t.Errorf("expected result, got %v", err)
}
@ -179,7 +179,7 @@ func TestTermSearcher(t *testing.T) {
}
// try advancing past end
docMatch, err = searcher.Advance("z")
docMatch, err = searcher.Advance("z", nil)
if err != nil {
t.Fatal(err)
}