Skip to content

Commit a76bdab

Browse files
micro optimization
1 parent da11922 commit a76bdab

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

index/scorch/optimize.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,5 +395,7 @@ func (i *IndexSnapshot) unadornedTermFieldReader(
395395
recycle: false,
396396
// signal downstream that this is a special unadorned termFieldReader
397397
unadorned: true,
398+
// unadorned TFRs do not require bytes read tracking
399+
updateBytesRead: false,
398400
}
399401
}

index/scorch/snapshot_index.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ func (is *IndexSnapshot) TermFieldReader(ctx context.Context, term []byte, field
700700
rv.incrementBytesRead(bytesRead - prevBytesReadItr)
701701
}
702702
}
703+
// ONLY update the bytes read value beyond this point for this TFR if scoring is enabled
704+
rv.updateBytesRead = rv.includeFreq || rv.includeNorm || rv.includeTermVectors
703705
atomic.AddUint64(&is.parent.stats.TotTermSearchersStarted, uint64(1))
704706
return rv, nil
705707
}

index/scorch/snapshot_index_tfr.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ type IndexSnapshotTermFieldReader struct {
5151
bytesRead uint64
5252
ctx context.Context
5353
unadorned bool
54+
// flag to indicate whether to increment our bytesRead
55+
// value after creation of the TFR while iterating our postings
56+
// lists
57+
updateBytesRead bool
5458
}
5559

5660
func (i *IndexSnapshotTermFieldReader) incrementBytesRead(val uint64) {
@@ -83,10 +87,15 @@ func (i *IndexSnapshotTermFieldReader) Next(preAlloced *index.TermFieldDoc) (*in
8387
if rv == nil {
8488
rv = &index.TermFieldDoc{}
8589
}
90+
var prevBytesRead uint64
8691
// find the next hit
8792
for i.segmentOffset < len(i.iterators) {
88-
prevBytesRead := i.iterators[i.segmentOffset].BytesRead()
89-
next, err := i.iterators[i.segmentOffset].Next()
93+
// get our current postings iterator
94+
curItr := i.iterators[i.segmentOffset]
95+
if i.updateBytesRead {
96+
prevBytesRead = curItr.BytesRead()
97+
}
98+
next, err := curItr.Next()
9099
if err != nil {
91100
return nil, err
92101
}
@@ -99,13 +108,15 @@ func (i *IndexSnapshotTermFieldReader) Next(preAlloced *index.TermFieldDoc) (*in
99108

100109
i.currID = rv.ID
101110
i.currPosting = next
102-
// postingsIterators is maintain the bytesRead stat in a cumulative fashion.
103-
// this is because there are chances of having a series of loadChunk calls,
104-
// and they have to be added together before sending the bytesRead at this point
105-
// upstream.
106-
bytesRead := i.iterators[i.segmentOffset].BytesRead()
107-
if bytesRead > prevBytesRead {
108-
i.incrementBytesRead(bytesRead - prevBytesRead)
111+
if i.updateBytesRead {
112+
// postingsIterators is maintain the bytesRead stat in a cumulative fashion.
113+
// this is because there are chances of having a series of loadChunk calls,
114+
// and they have to be added together before sending the bytesRead at this point
115+
// upstream.
116+
bytesRead := curItr.BytesRead()
117+
if bytesRead > prevBytesRead {
118+
i.incrementBytesRead(bytesRead - prevBytesRead)
119+
}
109120
}
110121
return rv, nil
111122
}

0 commit comments

Comments
 (0)