Skip to content

Apache Lucene open-source search software

LUCENE-7406: Automaton and PrefixQuery tweaks (fewer object (re)allocations).

Christine Poerschke0583e5aa4719
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index e83e6cba370..c5db1438d8b 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -159,6 +159,9 @@ Optimizations
* LUCENE-7396, LUCENE-7399: Faster flush of points.
(Adrien Grand, Mike McCandless)
+* LUCENE-7406: Automaton and PrefixQuery tweaks (fewer object (re)allocations).
+ (Christine Poerschke)
+
Other
* LUCENE-4787: Fixed some highlighting javadocs. (Michael Dodsworth via Adrien
diff --git a/lucene/core/src/java/org/apache/lucene/search/PrefixQuery.java b/lucene/core/src/java/org/apache/lucene/search/PrefixQuery.java
index f4b5e5026cf..84ae8deae6f 100644
--- a/lucene/core/src/java/org/apache/lucene/search/PrefixQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/PrefixQuery.java
@@ -41,7 +41,8 @@ public class PrefixQuery extends AutomatonQuery {
/** Build an automaton accepting all terms with the specified prefix. */
public static Automaton toAutomaton(BytesRef prefix) {
- Automaton automaton = new Automaton();
+ final int numStatesAndTransitions = prefix.length+1;
+ final Automaton automaton = new Automaton(numStatesAndTransitions, numStatesAndTransitions);
int lastState = automaton.createState();
for(int i=0;i<prefix.length;i++) {
int state = automaton.createState();
@@ -66,7 +67,7 @@ public class PrefixQuery extends AutomatonQuery {
StringBuilder buffer = new StringBuilder();
if (!getField().equals(field)) {
buffer.append(getField());
- buffer.append(":");
+ buffer.append(':');
}
buffer.append(term.text());
buffer.append('*');
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/Automaton.java b/lucene/core/src/java/org/apache/lucene/util/automaton/Automaton.java
index 42f28edacb0..0dd449c9961 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/Automaton.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/Automaton.java
@@ -357,13 +357,13 @@ public class Automaton implements Accountable {
}
private void growStates() {
- if (nextState+2 >= states.length) {
+ if (nextState+2 > states.length) {
states = ArrayUtil.grow(states, nextState+2);
}
}
private void growTransitions() {
- if (nextTransition+3 >= transitions.length) {
+ if (nextTransition+3 > transitions.length) {
transitions = ArrayUtil.grow(transitions, nextTransition+3);
}
}