From 7804678bea7ea05ba76cf9d32e511687c6f6c01e Mon Sep 17 00:00:00 2001 From: zhouhui Date: Thu, 17 Oct 2024 17:49:38 +0800 Subject: [PATCH] Use Arrays.mismatch in FSTCompiler#add. --- .../org/apache/lucene/util/fst/FSTCompiler.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/FSTCompiler.java b/lucene/core/src/java/org/apache/lucene/util/fst/FSTCompiler.java index d51c256d48e..f56b1be09cd 100644 --- a/lucene/core/src/java/org/apache/lucene/util/fst/FSTCompiler.java +++ b/lucene/core/src/java/org/apache/lucene/util/fst/FSTCompiler.java @@ -30,6 +30,7 @@ import static org.apache.lucene.util.fst.FST.getNumPresenceBytes; import java.io.IOException; +import java.util.Arrays; import java.util.Objects; import org.apache.lucene.store.ByteArrayDataOutput; import org.apache.lucene.store.DataOutput; @@ -869,14 +870,14 @@ public void add(IntsRef input, T output) throws IOException { } // compare shared prefix length - int pos1 = 0; - int pos2 = input.offset; - final int pos1Stop = Math.min(lastInput.length(), input.length); - while (pos1 < pos1Stop && lastInput.intAt(pos1) == input.ints[pos2]) { - pos1++; - pos2++; + int pos = 0; + if (lastInput.length() > 0) { + int mismatch = + Arrays.mismatch( + lastInput.ints(), 0, lastInput.length(), input.ints, input.offset, input.length); + pos += mismatch == -1 ? lastInput.length() : mismatch; } - final int prefixLenPlus1 = pos1 + 1; + final int prefixLenPlus1 = pos + 1; if (frontier.length < input.length + 1) { final UnCompiledNode[] next = ArrayUtil.grow(frontier, input.length + 1);