You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2019/10/10 18:13:54 UTC

[GitHub] [lucene-solr] danmuzi commented on a change in pull request #939: LUCENE-9003: Compute numDocs() lazily.

danmuzi commented on a change in pull request #939: LUCENE-9003: Compute numDocs() lazily.
URL: https://github.com/apache/lucene-solr/pull/939#discussion_r333663514
 
 

 ##########
 File path: lucene/core/src/java/org/apache/lucene/index/BaseCompositeReader.java
 ##########
 @@ -102,6 +101,23 @@ public final Fields getTermVectors(int docID) throws IOException {
   @Override
   public final int numDocs() {
     // Don't call ensureOpen() here (it could affect performance)
+    // We want to compute numDocs() lazily so that creating a wrapper that hides
+    // some documents isn't slow at wrapping time, but on the first time that
+    // numDocs() is called. This can help as there are lots of use-cases of a
+    // reader that don't involve calling numDocs().
+    // However it's not crucial to make sure that we don't call numDocs() more
+    // than once on the sub readers, since they likely cache numDocs() anyway,
+    // hence the opaque read.
+    // http://gee.cs.oswego.edu/dl/html/j9mm.html#opaquesec.
+    int numDocs = this.numDocs.getOpaque();
+    if (numDocs == -1) {
+      numDocs = 0;
+      for (IndexReader r : subReaders) {
+        numDocs += r.numDocs();
+      }
+      assert numDocs >= 0;
+      this.numDocs.set(numDocs);
 
 Review comment:
   Isn't
   `this.numDocs.setOpaque(numDocs)`
   right?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

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