Use ArrayDeque for JapaneseCompletionFilter output buffering - #16416
Use ArrayDeque for JapaneseCompletionFilter output buffering#16416luizmlima wants to merge 1 commit into
Conversation
Additional benchmark resultsI ran an additional JMH benchmark round to better evaluate the impact of replacing The benchmark executes the full Execution time
The I also ran the benchmark with the JMH GC profiler and analyzed Overall, the additional measurements suggest that the main benefit of the change is lower execution time without evidence of a meaningful allocation regression, and better matches the FIFO access pattern. The full experiment report, including methodology, results, discussion, and limitations, is available here: |
Description
JapaneseCompletionFilter.CompletionTokenGeneratorusesoutputsas a FIFO queue.The current implementation uses an
ArrayListwithremove(0), which shifts the remaining elements after each removal. This change replaces it with anArrayDequeand usesaddLast()andremoveFirst().There is no change to the public API or to the generated tokens, their order, offsets, or position increments.
Benchmark
I used JMH to compare the current
ArrayListimplementation with the proposedArrayDequeimplementation.The benchmark runs the full
JapaneseCompletionAnalyzerpipeline. For each input, it creates a token stream and consumes all generated tokens.Both versions were tested with the same benchmark code, JDK, JVM options, hardware, and JMH configuration.
Configuration
-Xms1g -Xmx1g -XX:+AlwaysPreTouchLower values are better.
Environment
Temurin-25.0.3+97.0.11-76070011-genericx86_64Inputs
東京ドラえもんシンシンシンシンシンシンシンシンThe first two are short, more typical inputs. The last two were included to generate larger output batches and make the queue behavior more visible.
Results
東京ドラえもんシンシンシンシンシンシンシンシンNegative values mean that
ArrayDequecompleted the operation in less time.For the shorter inputs, the error ranges overlap, so there was no meaningful performance difference.
For the larger output batches,
ArrayDequeperformed better:シンシンシンシンシンシンシンシンThe largest case went from
8.288 ± 0.138 us/opwithArrayListto7.743 ± 0.079 us/opwithArrayDeque.This matches the way the collection is used.
ArrayList.remove(0)shifts the remaining elements, whileArrayDeque.removeFirst()does not.The change does not improve typical short inputs in a measurable way, but it better matches the FIFO access pattern and avoids the increasing cost of repeated removals from the beginning of an
ArrayList.Validation
The following commands completed successfully:
./gradlew tidy./gradlew -p lucene/analysis/kuromoji test --tests "org.apache.lucene.analysis.ja.TestJapaneseCompletionFilter"./gradlew -p lucene/analysis/kuromoji test --tests "org.apache.lucene.analysis.ja.TestJapaneseCompletionAnalyzer"./gradlew -p lucene/analysis/kuromoji check./gradlew check