dweiss commented on code in PR #16355: URL: https://github.com/apache/lucene/pull/16355#discussion_r3527913663
########## lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/LowerCaseBenchmark.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.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; +import org.openjdk.jmh.infra.Blackhole; + +/** + * Benchmark for {@code CharacterUtils.toLowerCase}. Compares the generic Unicode codepoint path + * (codePointAt + toLowerCase + toChars per char) against an optimized ASCII fast path that uses + * arithmetic bit manipulation for ASCII and only falls back to Unicode for non-ASCII characters. + * Token distribution: 90% lowercase ASCII, 8% mixed-case ASCII, 2% non-ASCII (3 to 10 chars). + */ +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Thread) +@Warmup(iterations = 5, time = 1) +@Measurement(iterations = 10, time = 1) +@Fork(3) +public class LowerCaseBenchmark { + + @Param({"1000"}) + int tokenCount; + + private char[][] workBuffers; + + private int[] workLengths; + + private char[][] masterBuffers; + + private int[] masterLengths; + + @Setup(Level.Trial) + public void setup() { + Random rng = new Random(42); + + masterBuffers = new char[tokenCount][]; + masterLengths = new int[tokenCount]; + + for (int t = 0; t < tokenCount; t++) { + int len = 3 + rng.nextInt(8); // 3..10 chars + double roll = rng.nextDouble(); + + char[] token; + if (roll < 0.90) { + // 90%: already lowercase ASCII + token = randomLowercaseAscii(rng, len); + } else if (roll < 0.98) { + // 8%: mixed-case ASCII (first char uppercase, rest lowercase) + token = randomMixedCaseAscii(rng, len); + } else { + // 2%: contains non-ASCII characters + token = randomWithNonAscii(rng, len); Review Comment: Yeah, well. It's optimistic, isn't it. What are the numbers when nearly all strings hit the branch? -- 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]
