gf2121 commented on code in PR #14935:
URL: https://github.com/apache/lucene/pull/14935#discussion_r2201126320


##########
lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/BitsetToArrayBenchmark.java:
##########
@@ -0,0 +1,379 @@
+/*
+ * 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.benchmark.jmh;
+
+import java.util.SplittableRandom;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.IntStream;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Warmup;
+
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.MICROSECONDS)
+@State(Scope.Benchmark)
+@Warmup(iterations = 3, time = 1)
+@Measurement(iterations = 5, time = 1)
+@Fork(
+    value = 1,
+    jvmArgsAppend = {"-Xmx1g", "-Xms1g", "-XX:+AlwaysPreTouch"})
+public class BitsetToArrayBenchmark {
+
+  private final SplittableRandom R = new SplittableRandom(4314123142L);
+
+  @Param({"5", "10", "20", "30", "40", "50", "60"})
+  int bitCount;
+
+  private final int[] scratch = new int[64];
+  private long word;
+  private int[] resultArray;
+  private int base;
+  private int offset;
+
+  @Setup(Level.Trial)
+  public void setup() {
+    base = R.nextInt(1000);
+    resultArray = new int[bitCount + Long.SIZE];
+  }
+
+  @Setup(Level.Invocation)
+  public void setupInvocation() {
+    word = 0L;
+    while (Long.bitCount(word) < bitCount) {
+      word |= 1L << R.nextInt(64);
+    }
+    offset = R.nextInt(64);
+  }
+
+  @Benchmark
+  public int whileLoop() {
+    return _whileLoop(word, resultArray, offset, base);
+  }
+
+  @Benchmark
+  public int forLoop() {
+    return _forLoop(word, resultArray, offset, base);
+  }
+
+  @Benchmark
+  public int forLoopManualUnrolling() {
+    return _forLoopManualUnrolling(word, resultArray, offset, base);
+  }
+
+  @Benchmark
+  public int dense() {
+    return _dense(word, resultArray, offset, base);
+  }
+
+  @Benchmark
+  public int denseBranchLess() {
+    return _denseBranchLess(word, resultArray, offset, base);
+  }
+
+  @Benchmark
+  public int denseBranchLessUnrolling() {
+    return _denseBranchLessUnrolling(word, resultArray, offset, base);
+  }
+
+  @Benchmark
+  public int denseBranchLessVectorized() {
+    return _denseBranchLessVectorized(word, resultArray, offset, base, 
scratch);
+  }
+
+  @Benchmark
+  public int denseInvert() {
+    return _denseInvert(word, resultArray, offset, base);
+  }
+
+  @Benchmark
+  public int hybrid() {
+    return _hybrid(word, resultArray, offset, base, scratch);
+  }
+
+  private static int _whileLoop(long word, int[] resultArray, int offset, int 
base) {
+    while (word != 0) {
+      int bit = Long.numberOfTrailingZeros(word);
+      resultArray[offset++] = base + bit;
+      word ^= 1L << bit;
+    }
+    return offset;
+  }
+
+  private static int _forLoop(long word, int[] resultArray, int offset, int 
base) {
+    int to = offset + Long.bitCount(word);
+    for (int i = offset; i < to; i++) {
+      int bit = Long.numberOfTrailingZeros(word);
+      resultArray[i] = base + bit;
+      word ^= 1L << bit;
+    }
+    return to;
+  }
+
+  private static int _forLoopManualUnrolling(long word, int[] resultArray, int 
offset, int base) {
+    int to = offset + Long.bitCount(word);
+    int i = offset;
+
+    for (; i < to - 3; i += 4) {
+      int ntz = Long.numberOfTrailingZeros(word);
+      resultArray[i] = base + ntz;
+      word ^= 1L << ntz;
+      ntz = Long.numberOfTrailingZeros(word);
+      resultArray[i] = base + ntz;
+      word ^= 1L << ntz;
+      ntz = Long.numberOfTrailingZeros(word);
+      resultArray[i] = base + ntz;
+      word ^= 1L << ntz;
+      ntz = Long.numberOfTrailingZeros(word);
+      resultArray[i] = base + ntz;
+      word ^= 1L << ntz;
+    }
+
+    for (; i < to; i++) {
+      int ntz = Long.numberOfTrailingZeros(word);
+      resultArray[i] = base + ntz;
+      word ^= 1L << ntz;
+    }
+
+    return to;
+  }
+
+  private static int _dense(long word, int[] resultArray, int offset, int 
base) {
+    for (int i = 0; i < Long.SIZE; i++) {
+      if ((word & (1L << i)) != 0) {
+        resultArray[offset++] = base + i;
+      }
+    }
+    return offset;
+  }
+
+  private static int _denseBranchLess(long word, int[] resultArray, int 
offset, int base) {
+    int lWord = (int) word;
+    int hWord = (int) (word >>> 32);
+    for (int i = 0; i < Integer.SIZE; i++) {
+      resultArray[offset] = base + i;
+      offset += lWord & 1;
+      lWord >>>= 1;
+    }
+    for (int i = Integer.SIZE; i < Long.SIZE; i++) {
+      resultArray[offset] = base + i;
+      offset += hWord & 1;
+      hWord >>>= 1;
+    }
+    return offset;

Review Comment:
   I find that it can be complied to `cmovne` when bitCount=20/30/40/50, but 
can not when bitCount=5/10/60.



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