dsmiley commented on a change in pull request #1343: LUCENE-8103: Use two-phase 
iteration in Query- and DoubleValuesSource
URL: https://github.com/apache/lucene-solr/pull/1343#discussion_r392222264
 
 

 ##########
 File path: 
lucene/core/src/java/org/apache/lucene/search/DoubleValuesSource.java
 ##########
 @@ -604,18 +604,26 @@ public DoubleValues getValues(LeafReaderContext ctx, 
DoubleValues scores) throws
       Scorer scorer = weight.scorer(ctx);
       if (scorer == null)
         return DoubleValues.EMPTY;
-      DocIdSetIterator it = scorer.iterator();
+
       return new DoubleValues() {
+        private final TwoPhaseIterator tpi = scorer.twoPhaseIterator();
+        private final DocIdSetIterator disi = (tpi == null) ? 
scorer.iterator() : tpi.approximation();
+
+        private int scorerDoc = -1;
+        private boolean thisDocMatches = false;
+
         @Override
         public double doubleValue() throws IOException {
-          return scorer.score();
+          return thisDocMatches ? scorer.score() : Double.NaN;
         }
 
         @Override
         public boolean advanceExact(int doc) throws IOException {
-          if (it.docID() > doc)
-            return false;
-          return it.docID() == doc || it.advance(doc) == doc;
+          if (scorerDoc < doc) {
+            scorerDoc = disi.advance(doc);
+            thisDocMatches = tpi==null || tpi.matches();
 
 Review comment:
   If scorerDoc is > "this" doc (doc param), then we should not even compute 
tpi.matches(); it's wasted potentially expensive computation.  Maybe if 
thisDocMatches is a Object Boolean, we can differentiate an unkinown state.
   
   Perhaps
   ```
   if (scorerDoc < doc) {
     scorerDoc = disi.advance(doc);
     thisDocMatches = null;
   }
   if (scorerDoc == doc) {
    if (thisDocMatches == null) {
     thisDocMatches = tpi == null || tpi.matches();
    }
    return thisDocMatches;
   } else {
     return thisDocMatches = false;
   }
   ```
   
   And I don't think we even need scorerDoc to be a field since it's 
disi.doc();  Local-variable is nice.
   Hmm; if I'm not mistaken, the very first check (if scorerDoc < doc) can be 
replaced with an assertion since otherwise the caller is violating the API 
contract.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to