epotyom commented on code in PR #13625:
URL: https://github.com/apache/lucene/pull/13625#discussion_r1700906253


##########
lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java:
##########
@@ -337,34 +337,23 @@ private int firstDoc(int i4096, int i4096upper) {
 
   @Override
   public int nextSetBit(int i) {
-    assert i < length;
-    final int i4096 = i >>> 12;
-    final long index = indices[i4096];
-    final long[] bitArray = this.bits[i4096];
-    int i64 = i >>> 6;
-    int o = Long.bitCount(index & ((1L << i64) - 1));
-    if ((index & (1L << i64)) != 0) {
-      // There is at least one bit that is set in the current long, check if
-      // one of them is after i
-      final long bits = bitArray[o] >>> i; // shifts are mod 64
-      if (bits != 0) {
-        return i + Long.numberOfTrailingZeros(bits);
-      }
-      o += 1;
-    }
-    final long indexBits = index >>> i64 >>> 1;
-    if (indexBits == 0) {
-      // no more bits are set in the current block of 4096 bits, go to the 
next one
-      return firstDoc(i4096 + 1, indices.length);
-    }
-    // there are still set bits
-    i64 += 1 + Long.numberOfTrailingZeros(indexBits);
-    final long bits = bitArray[o];
-    return (i64 << 6) | Long.numberOfTrailingZeros(bits);
+    // Override with a version that skips the bound check on the result since 
we know it will not
+    // go OOB:
+    return nextSetBitInRange(i, length);
   }
 
   @Override
   public int nextSetBit(int start, int upperBound) {
+    int res = nextSetBitInRange(start, upperBound);
+    return res < upperBound ? res : DocIdSetIterator.NO_MORE_DOCS;
+  }
+
+  /**
+   * Returns the next set bit in the specified range, but treats `upperBound` 
as a best-effort hint
+   * rather than a hard requirement. Note that this may return a result that 
is >= upperBound in
+   * some cases, so callers must add their own check if `upperBound` is a hard 
requirement.
+   */
+  private int nextSetBitInRange(int start, int upperBound) {
     assert start < length;
     assert upperBound > start;

Review Comment:
   I know it's preexisting, but maybe we can extend the assert to make 
potential debugging easier?
   ```suggestion
       assert upperBound > start && upperBound <= length : "upperBound=" + 
upperBound + ", start=" + start + ", length=" + length;
   ```



-- 
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.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to