You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "costin (via GitHub)" <gi...@apache.org> on 2026/07/04 09:27:45 UTC
[PR] Add ASCII fast-path to CharacterUtils.toLowerCase [lucene]
costin opened a new pull request, #16355:
URL: https://github.com/apache/lucene/pull/16355
CharacterUtils.toLowerCase() runs the full Unicode codepoint path for every character, even when the token is already all-lowercase ASCII, common case after English tokenization.
The PR introduces a fast path that uses direct arithmetic for ASCII-only tokens instead of the generic Unicode approach.
### Benchmark
AMD EPYC 7R32 (c5a.2xlarge), JDK 25, 1000 tokens (90% lowercase ASCII, 8% mixed-case, 2% non-ASCII). 3 forks, 10 iters × 1s.
| method | throughput (ops/μs) | vs baseline |
|---|---:|---:|
| baseline (current) | 0.056 ± 0.001 | — |
| asciiFastPath | 0.122 ± 0.001 | 2.18x |
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add ASCII fast-path to CharacterUtils.toLowerCase [lucene]
Posted by "dweiss (via GitHub)" <gi...@apache.org>.
dweiss commented on code in PR #16355:
URL: https://github.com/apache/lucene/pull/16355#discussion_r3527941960
##########
lucene/core/src/java/org/apache/lucene/analysis/CharacterUtils.java:
##########
@@ -53,10 +53,19 @@ public static CharacterBuffer newCharacterBuffer(final int bufferSize) {
public static void toLowerCase(final char[] buffer, final int offset, final int limit) {
assert buffer.length >= limit;
assert 0 <= offset && offset <= buffer.length;
- for (int i = offset; i < limit; ) {
- i +=
- Character.toChars(
- Character.toLowerCase(Character.codePointAt(buffer, i, limit)), buffer, i);
+ for (int i = offset; i < limit; i++) {
+ char c = buffer[i];
+ if (c > 127) { // non-ASCII: switch to full Unicode path from here onward
+ for (int j = i; j < limit; ) {
+ j +=
+ Character.toChars(
+ Character.toLowerCase(Character.codePointAt(buffer, j, limit)), buffer, j);
+ }
+ return;
+ }
+ if (c >= 'A' && c <= 'Z') {
+ buffer[i] = (char) (c | 32); // set bit 5 to lowercase ASCII
Review Comment:
Clever. I wonder if `c + ('a' - 'A')` wouldn't be more intuitive though. It's fine with the comment.
##########
lucene/core/src/java/org/apache/lucene/analysis/CharacterUtils.java:
##########
@@ -53,10 +53,19 @@ public static CharacterBuffer newCharacterBuffer(final int bufferSize) {
public static void toLowerCase(final char[] buffer, final int offset, final int limit) {
assert buffer.length >= limit;
assert 0 <= offset && offset <= buffer.length;
- for (int i = offset; i < limit; ) {
- i +=
- Character.toChars(
- Character.toLowerCase(Character.codePointAt(buffer, i, limit)), buffer, i);
+ for (int i = offset; i < limit; i++) {
+ char c = buffer[i];
+ if (c > 127) { // non-ASCII: switch to full Unicode path from here onward
+ for (int j = i; j < limit; ) {
Review Comment:
Can we just continue to reuse i, without declaring another variable?
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add ASCII fast-path to CharacterUtils.toLowerCase [lucene]
Posted by "dweiss (via GitHub)" <gi...@apache.org>.
dweiss commented on PR #16355:
URL: https://github.com/apache/lucene/pull/16355#issuecomment-4894470626
Please, do *not* force push your changes.
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add ASCII fast-path to CharacterUtils.toLowerCase [lucene]
Posted by "dweiss (via GitHub)" <gi...@apache.org>.
dweiss commented on PR #16355:
URL: https://github.com/apache/lucene/pull/16355#issuecomment-4893504420
When making follow-up changes, please don't force-push. It destroys the history of the PR and may even break inline conversations (merge origin/main, resolve any conflicts, commit, push to pr branch normally). Thanks.
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add ASCII fast-path to CharacterUtils.toLowerCase [lucene]
Posted by "dweiss (via GitHub)" <gi...@apache.org>.
dweiss commented on code in PR #16355:
URL: https://github.com/apache/lucene/pull/16355#discussion_r3529324164
##########
lucene/core/src/java/org/apache/lucene/analysis/CharacterUtils.java:
##########
@@ -53,10 +53,19 @@ public static CharacterBuffer newCharacterBuffer(final int bufferSize) {
public static void toLowerCase(final char[] buffer, final int offset, final int limit) {
assert buffer.length >= limit;
assert 0 <= offset && offset <= buffer.length;
- for (int i = offset; i < limit; ) {
- i +=
- Character.toChars(
- Character.toLowerCase(Character.codePointAt(buffer, i, limit)), buffer, i);
+ for (int i = offset; i < limit; i++) {
+ char c = buffer[i];
+ if (c > 127) { // non-ASCII: switch to full Unicode path from here onward
+ for (int j = i; j < limit; ) {
+ j +=
+ Character.toChars(
+ Character.toLowerCase(Character.codePointAt(buffer, j, limit)), buffer, j);
+ }
+ return;
+ }
+ if (c >= 'A' && c <= 'Z') {
+ buffer[i] = (char) (c | 32); // set bit 5 to lowercase ASCII
Review Comment:
Well, it can't overflow - it's inside the if statement so you know exactly the minimum and maximum value it can take. And the compiler would fold the constant 'a' - 'A'. But fine.
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add ASCII fast-path to CharacterUtils.toLowerCase [lucene]
Posted by "costin (via GitHub)" <gi...@apache.org>.
costin commented on PR #16355:
URL: https://github.com/apache/lucene/pull/16355#issuecomment-4896886373
Noted, PEBCAK. Thanks for the fast turnaround!
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add ASCII fast-path to CharacterUtils.toLowerCase [lucene]
Posted by "costin (via GitHub)" <gi...@apache.org>.
costin commented on code in PR #16355:
URL: https://github.com/apache/lucene/pull/16355#discussion_r3529212809
##########
lucene/core/src/java/org/apache/lucene/analysis/CharacterUtils.java:
##########
@@ -53,10 +53,19 @@ public static CharacterBuffer newCharacterBuffer(final int bufferSize) {
public static void toLowerCase(final char[] buffer, final int offset, final int limit) {
assert buffer.length >= limit;
assert 0 <= offset && offset <= buffer.length;
- for (int i = offset; i < limit; ) {
- i +=
- Character.toChars(
- Character.toLowerCase(Character.codePointAt(buffer, i, limit)), buffer, i);
+ for (int i = offset; i < limit; i++) {
+ char c = buffer[i];
+ if (c > 127) { // non-ASCII: switch to full Unicode path from here onward
+ for (int j = i; j < limit; ) {
Review Comment:
Fixed, missed that.
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add ASCII fast-path to CharacterUtils.toLowerCase [lucene]
Posted by "dweiss (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add ASCII fast-path to CharacterUtils.toLowerCase [lucene]
Posted by "costin (via GitHub)" <gi...@apache.org>.
costin commented on code in PR #16355:
URL: https://github.com/apache/lucene/pull/16355#discussion_r3529236368
##########
lucene/core/src/java/org/apache/lucene/analysis/CharacterUtils.java:
##########
@@ -53,10 +53,19 @@ public static CharacterBuffer newCharacterBuffer(final int bufferSize) {
public static void toLowerCase(final char[] buffer, final int offset, final int limit) {
assert buffer.length >= limit;
assert 0 <= offset && offset <= buffer.length;
- for (int i = offset; i < limit; ) {
- i +=
- Character.toChars(
- Character.toLowerCase(Character.codePointAt(buffer, i, limit)), buffer, i);
+ for (int i = offset; i < limit; i++) {
+ char c = buffer[i];
+ if (c > 127) { // non-ASCII: switch to full Unicode path from here onward
+ for (int j = i; j < limit; ) {
+ j +=
+ Character.toChars(
+ Character.toLowerCase(Character.codePointAt(buffer, j, limit)), buffer, j);
+ }
+ return;
+ }
+ if (c >= 'A' && c <= 'Z') {
+ buffer[i] = (char) (c | 32); // set bit 5 to lowercase ASCII
Review Comment:
I had it as an addition initially however ended for the bit operation since I found it easier to reason about being safe (no overflow) regardless at the value of c.
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add ASCII fast-path to CharacterUtils.toLowerCase [lucene]
Posted by "dweiss (via GitHub)" <gi...@apache.org>.
dweiss merged PR #16355:
URL: https://github.com/apache/lucene/pull/16355
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add ASCII fast-path to CharacterUtils.toLowerCase [lucene]
Posted by "dweiss (via GitHub)" <gi...@apache.org>.
dweiss commented on code in PR #16355:
URL: https://github.com/apache/lucene/pull/16355#discussion_r3529355262
##########
lucene/core/src/java/org/apache/lucene/analysis/CharacterUtils.java:
##########
@@ -53,10 +53,19 @@ public static CharacterBuffer newCharacterBuffer(final int bufferSize) {
public static void toLowerCase(final char[] buffer, final int offset, final int limit) {
assert buffer.length >= limit;
assert 0 <= offset && offset <= buffer.length;
- for (int i = offset; i < limit; ) {
- i +=
- Character.toChars(
- Character.toLowerCase(Character.codePointAt(buffer, i, limit)), buffer, i);
+ for (int i = offset; i < limit; i++) {
+ char c = buffer[i];
+ if (c > 127) { // non-ASCII: switch to full Unicode path from here onward
+ for (; i < limit; ) {
Review Comment:
```suggestion
while (i < limit) {
```
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org