sgup432 commented on code in PR #16050: URL: https://github.com/apache/lucene/pull/16050#discussion_r3237284237
########## 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); + maskBits &= maskBits - 1; + } + } + } + + // Scalar tail for remaining docs + for (int d = loopBound; d < toDoc; d++) { + long v = values.get(d); + if (v >= minValue && v <= maxValue) { + bitSet.set(d - offset); + } + } Review Comment: Hmm good idea. I think it won't help much in this vectorized approach where we are processing at most 7 docs for the tail? But for scalar approach or default, where we are processing upto 4096 docs in a block, so we can probably use `FixedBitSet.set(int startIndex, int endIndex)` for a batch of matching docs. Doing something like below might work? Let me what you think. ``` int runStart = -1; for (int d = fromDoc; d < toDoc; d++) { long v = values.get(d); boolean matches = v >= minValue && v <= maxValue; if (matches) { if (runStart == -1) runStart = d; // a new run } else { if (runStart != -1) { bitSet.set(runStart - offset, d - offset); // set in a batch runStart = -1; } } } // for remaining matching docs if (runStart != -1) { bitSet.set(runStart - offset, toDoc - offset); } ``` -- 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]
