Skip to content

Apache Lucene open-source search software

LUCENE-8219: Do a better job of estimating automaton array sizes up front, to save on reallocation. Committed on behalf of Christian Ziech.

Karl Wright7cadada441d7
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 276909835ab..77508c4b07d 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -105,6 +105,10 @@ New Features
Other
+* LUCENE-8219: Use a realistic estimate of the number of nodes and links in
+ LevensteinAutomaton.java, to save reallocation of arrays.
+ (Christian Ziech)
+
* LUCENE-8214: Improve selection of testPoint for GeoComplexPolygon.
(Ignacio Vera)
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/LevenshteinAutomata.java b/lucene/core/src/java/org/apache/lucene/util/automaton/LevenshteinAutomata.java
index 4a07f4bce3c..bee3c005b65 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/LevenshteinAutomata.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/LevenshteinAutomata.java
@@ -152,9 +152,11 @@ public class LevenshteinAutomata {
final int range = 2*n+1;
ParametricDescription description = descriptions[n];
// the number of states is based on the length of the word and n
- int numStates = description.size();
+ final int numStates = description.size();
+ final int numTransitions = numStates * Math.min(1 + 2 * n, alphabet.length);
+ final int prefixStates = prefix != null ? prefix.codePointCount(0, prefix.length()) : 0;
- Automaton a = new Automaton();
+ final Automaton a = new Automaton(numStates + prefixStates, numTransitions);
int lastState;
if (prefix != null) {
// Insert prefix