sgup432 commented on code in PR #16050:
URL: https://github.com/apache/lucene/pull/16050#discussion_r3237207455


##########
lucene/core/src/java25/org/apache/lucene/internal/vectorization/PanamaDocValuesRangeSupport.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.internal.vectorization;
+
+import jdk.incubator.vector.LongVector;
+import jdk.incubator.vector.VectorMask;
+import jdk.incubator.vector.VectorOperators;
+import jdk.incubator.vector.VectorSpecies;
+import org.apache.lucene.util.FixedBitSet;
+import org.apache.lucene.util.LongValues;
+
+/** Panama Vector API implementation of {@link DocValuesRangeSupport}. */
+final class PanamaDocValuesRangeSupport implements DocValuesRangeSupport {
+
+  static final PanamaDocValuesRangeSupport INSTANCE = new 
PanamaDocValuesRangeSupport();
+
+  private static final VectorSpecies<Long> LONG_SPECIES = 
LongVector.SPECIES_PREFERRED;
+
+  private PanamaDocValuesRangeSupport() {}
+
+  @Override
+  public void rangeIntoBitSet(
+      LongValues values,
+      int fromDoc,
+      int toDoc,
+      long minValue,
+      long maxValue,
+      FixedBitSet bitSet,
+      int offset) {
+    final int vectorLen = LONG_SPECIES.length();
+
+    // Only use SIMD if vector length >= 4 (AVX-256 or better).
+    // On 128-bit SIMD (2 longs), the scratch buffer overhead outweighs the 
benefit.
+    if (vectorLen < 4) {
+      // Scalar fallback: tight loop that JIT can auto-vectorize
+      for (int d = fromDoc; d < toDoc; d++) {
+        long v = values.get(d);
+        if (v >= minValue && v <= maxValue) {
+          bitSet.set(d - offset);
+        }
+      }
+      return;
+    }
+
+    // Stack-allocated scratch buffer — vectorLen is at most 8 for AVX-512 (64 
bytes).
+    final long[] scratch = new long[vectorLen];
+    final int loopBound = fromDoc + LONG_SPECIES.loopBound(toDoc - fromDoc);
+
+    // SIMD loop: load vectorLen values into scratch, compare all at once
+    for (int d = fromDoc; d < loopBound; d += vectorLen) {
+      for (int i = 0; i < vectorLen; i++) {
+        scratch[i] = values.get(d + i);
+      }
+      LongVector v = LongVector.fromArray(LONG_SPECIES, scratch, 0);
+      VectorMask<Long> inRange =
+          v.compare(VectorOperators.GE, 
minValue).and(v.compare(VectorOperators.LE, maxValue));
+      long maskBits = inRange.toLong();
+      if (maskBits != 0) {
+        int base = d - offset;
+        while (maskBits != 0) {
+          int bit = Long.numberOfTrailingZeros(maskBits);
+          bitSet.set(base + bit);

Review Comment:
   Yeah! Seems like @benwtrent also pointed this out indirectly below.
   The current approach is indeed in-efficient and the while loop can instead 
be replaced by a single OR.
   Thanks for pointing out.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to