neoremind commented on code in PR #16050: URL: https://github.com/apache/lucene/pull/16050#discussion_r3233289411
########## 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 = Review Comment: Wondering if loop unrolling for SIMD can speed up further ([sample](https://github.com/apache/lucene/blob/390146672b5c155fcd2a05faa0884e9bf300edde/lucene/core/src/java25/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.java#L127))? I suspect if we were to profile this, the bottleneck might be serial `values.get(d + i)` gather from packed values, if we could read more compact values with fewer loop iterations, and parallelize the range check with more CPU level pipelines, that would be a win, but need to do performance test to vet. -- 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]
