You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2026/10/14 17:19:13 UTC

(lucene) branch main updated: Add a JMH benchmark for byte vector kernels after all variants are compiled (#16831)

This is an automated email from the ASF dual-hosted git repository.

rmuir pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 63df54ad867 Add a JMH benchmark for byte vector kernels after all variants are compiled (#16831)
63df54ad867 is described below

commit 63df54ad867237b4f098e3a230a0707162778d7e
Author: Lantao Jin <lt...@amazon.com>
AuthorDate: Thu Oct 15 01:19:06 2026 +0800

    Add a JMH benchmark for byte vector kernels after all variants are compiled (#16831)
    
    JMH forks a new JVM for every benchmark, so VectorUtilBenchmark measures
    each byte kernel in a JVM where no other variant has run, and it did not
    show #16807. VectorUtilMixingBenchmark first runs every signed and
    unsigned dot product and square distance long enough for C2 to compile
    it, then measures each kernel on its own (pollute=all), and compares that
    with a fresh JVM (pollute=none). A combined method calls each kernel
    through its own DONT_INLINE helper, so that each kernel has its own
    compilation as behind the vector scorers, and the result does not depend
    on how many Vector API kernels C2 can inline into one benchmark method.
---
 .../benchmark/jmh/VectorUtilMixingBenchmark.java   | 154 +++++++++++++++++++++
 1 file changed, 154 insertions(+)

diff --git a/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/VectorUtilMixingBenchmark.java b/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/VectorUtilMixingBenchmark.java
new file mode 100644
index 00000000000..ae34b20af2e
--- /dev/null
+++ b/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/VectorUtilMixingBenchmark.java
@@ -0,0 +1,154 @@
+/*
+ * 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.Random;
+import java.util.concurrent.TimeUnit;
+import org.apache.lucene.util.VectorUtil;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.CompilerControl;
+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;
+
+/**
+ * Byte vector kernels measured in a JVM where the other variants have already been compiled. Each
+ * kernel is called from its own compiled method, as the vector scorers call them, so the result
+ * does not depend on how many kernels C2 can inline into one benchmark method.
+ */
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.MICROSECONDS)
+@State(Scope.Benchmark)
+@Warmup(iterations = 4, time = 1)
+@Measurement(iterations = 5, time = 1)
+@Fork(
+    value = 3,
+    jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"},
+    jvmArgsAppend = {"-Xmx2g", "-Xms2g", "-XX:+AlwaysPreTouch"})
+public class VectorUtilMixingBenchmark {
+
+  @Param({"1024"})
+  int size;
+
+  /** "none": measure each kernel in a fresh JVM; "all": compile every variant first. */
+  @Param({"none", "all"})
+  String pollute;
+
+  byte[] bytesA;
+  byte[] bytesB;
+  long sink;
+
+  @Setup(Level.Trial)
+  public void setup() {
+    Random random = new Random(42);
+    bytesA = new byte[size];
+    bytesB = new byte[size];
+    random.nextBytes(bytesA);
+    random.nextBytes(bytesB);
+    if (pollute.equals("all")) {
+      // run each variant long enough for C2 to compile it, one after the other; a few calls
+      // only reach the interpreter and do not reproduce problems that need compiled code
+      for (int round = 0; round < 2; round++) {
+        sink += warmDotProduct(100_000);
+        sink += warmUint8DotProduct(100_000);
+        sink += warmSquareDistance(100_000);
+        sink += warmUint8SquareDistance(100_000);
+      }
+    }
+  }
+
+  private long warmDotProduct(int n) {
+    long sum = 0;
+    for (int i = 0; i < n; i++) sum += VectorUtil.dotProduct(bytesA, bytesB);
+    return sum;
+  }
+
+  private long warmUint8DotProduct(int n) {
+    long sum = 0;
+    for (int i = 0; i < n; i++) sum += VectorUtil.uint8DotProduct(bytesA, bytesB);
+    return sum;
+  }
+
+  private long warmSquareDistance(int n) {
+    long sum = 0;
+    for (int i = 0; i < n; i++) sum += VectorUtil.squareDistance(bytesA, bytesB);
+    return sum;
+  }
+
+  private long warmUint8SquareDistance(int n) {
+    long sum = 0;
+    for (int i = 0; i < n; i++) sum += VectorUtil.uint8SquareDistance(bytesA, bytesB);
+    return sum;
+  }
+
+  @Benchmark
+  public int dotProduct() {
+    return VectorUtil.dotProduct(bytesA, bytesB);
+  }
+
+  @Benchmark
+  public int uint8DotProduct() {
+    return VectorUtil.uint8DotProduct(bytesA, bytesB);
+  }
+
+  @Benchmark
+  public int squareDistance() {
+    return VectorUtil.squareDistance(bytesA, bytesB);
+  }
+
+  @Benchmark
+  public int uint8SquareDistance() {
+    return VectorUtil.uint8SquareDistance(bytesA, bytesB);
+  }
+
+  /** All four kernels per call, each from its own compiled method. */
+  @Benchmark
+  public int allKernels() {
+    return callDotProduct()
+        + callUint8DotProduct()
+        + callSquareDistance()
+        + callUint8SquareDistance();
+  }
+
+  @CompilerControl(CompilerControl.Mode.DONT_INLINE)
+  private int callDotProduct() {
+    return VectorUtil.dotProduct(bytesA, bytesB);
+  }
+
+  @CompilerControl(CompilerControl.Mode.DONT_INLINE)
+  private int callUint8DotProduct() {
+    return VectorUtil.uint8DotProduct(bytesA, bytesB);
+  }
+
+  @CompilerControl(CompilerControl.Mode.DONT_INLINE)
+  private int callSquareDistance() {
+    return VectorUtil.squareDistance(bytesA, bytesB);
+  }
+
+  @CompilerControl(CompilerControl.Mode.DONT_INLINE)
+  private int callUint8SquareDistance() {
+    return VectorUtil.uint8SquareDistance(bytesA, bytesB);
+  }
+}