rajat315315 commented on code in PR #16361:
URL: https://github.com/apache/lucene/pull/16361#discussion_r3609686961


##########
lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/CompiledAutomatonBenchmark.java:
##########
@@ -0,0 +1,64 @@
+package org.apache.lucene.benchmark.jmh;
+
+import java.io.IOException;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.BytesRefBuilder;
+import org.apache.lucene.util.automaton.Automaton;
+import org.apache.lucene.util.automaton.CompiledAutomaton;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Warmup;
+
+@Fork(1)
+@Warmup(iterations = 3, time = 2)
+@Measurement(iterations = 5, time = 2)
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@State(Scope.Benchmark)
+public class CompiledAutomatonBenchmark {
+
+  private CompiledAutomaton compiled;
+  private BytesRef[] inputs;
+  private BytesRefBuilder output;
+  private int index;
+
+  @Setup
+  public void setup() throws IOException {
+    Automaton.Builder builder = new Automaton.Builder();
+    int state0 = builder.createState();
+    int destState = builder.createState();
+    builder.setAccept(destState, true);
+    // Add 120 transitions to state 0 with sorted labels
+    for (int i = 0; i < 120; i++) {
+      builder.addTransition(state0, destState, i * 2, i * 2);
+    }

Review Comment:
   I have updated the benchmark to ensure that every iteration explicitly and 
directly measures `addTail`():
   
   Strictly Single-Byte Transitions:
   
   The parameterized transition sizes are set to 5 and 60 (instead of 5 and 
120). Since the transitions are constructed on even labels `(i * 2)`, a maximum 
of 60 transitions ensures that the highest transition label is 118 (which is 
strictly below 128 and encoded as a single-byte UTF-8 character). This keeps 
all transitions single-byte at state 0.
   Guaranteed Mismatches to Invoke `addTail()`:
   
   The query input labels are generated as random odd values 
(`rand.nextInt(numTransitions)` * 2 + 1).
   Because all transition labels on state 0 are even integers, the inputs will 
always miss on state 0.
   A mismatch forces `CompiledAutomaton.floor` to backtrack and call 
`addTail()` on every single benchmark query execution.



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