Apache Solr open-source search software
LUCENE-5963: more efficient AnalyzingSuggester.replaceSep
Michael McCandless1c2918f66d7b
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 452f86df886..22cfb1c3875 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -190,6 +190,9 @@ Optimizations* LUCENE-5959: Don't allocate excess memory when building automaton infinish. (Markus Heiden via Mike McCandless)+* LUCENE-5963: Reduce memory allocations in+ AnalyzingSuggester. (Markus Heiden via Mike McCandless)+Build* LUCENE-5909: Smoke tester now has better command line parsing anddiff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/Automaton.java b/lucene/core/src/java/org/apache/lucene/util/automaton/Automaton.javaindex bec4fc1921e..3d694e2e242 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@@ -334,6 +334,11 @@ public class Automaton {return nextState/2;}+ /** How many transitions this automaton has. */+ public int getNumTransitions() {+ return nextTransition / 3;+ }+/** How many transitions this state has. */public int getNumTransitions(int state) {int count = states[2*state+1];@@ -676,6 +681,20 @@ public class Automaton {transitions[nextTransition++] = max;}+ /** Add a [virtual] epsilon transition between source and dest.+ * Dest state must already have all transitions added because this+ * method simply copies those same transitions over to source. */+ public void addEpsilon(int source, int dest) {+ for (int upto = 0; upto < nextTransition; upto += 4) {+ if (transitions[upto] == dest) {+ addTransition(source, transitions[upto + 1], transitions[upto + 2], transitions[upto + 3]);+ }+ }+ if (isAccept(dest)) {+ setAccept(source, true);+ }+ }+/** Sorts transitions first then min label ascending, then* max label ascending, then dest ascending */private final Sorter sorter = new InPlaceMergeSorter() {@@ -797,10 +816,11 @@ public class Automaton {public void copy(Automaton other) {int offset = getNumStates();int otherNumStates = other.getNumStates();- for(int s=0;s<otherNumStates;s++) {- int newState = createState();- setAccept(newState, other.isAccept(s));- }++ // Copy all states+ copyStates(other);++ // Copy all transitionsTransition t = new Transition();for(int s=0;s<otherNumStates;s++) {int count = other.initTransition(s, t);@@ -810,5 +830,14 @@ public class Automaton {}}}++ /** Copies over all states from other. */+ public void copyStates(Automaton other) {+ int otherNumStates = other.getNumStates();+ for (int s = 0; s < otherNumStates; s++) {+ int newState = createState();+ setAccept(newState, other.isAccept(s));+ }+ }}}diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.javaindex 98c1113a914..b9f3fd827f0 100644--- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.java+++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.java@@ -299,14 +299,10 @@ public class AnalyzingSuggester extends Lookup {// we were asked to preserve them:private Automaton replaceSep(Automaton a) {- Automaton result = new Automaton();-- // Copy all states overint numStates = a.getNumStates();- for(int s=0;s<numStates;s++) {- result.createState();- result.setAccept(s, a.isAccept(s));- }+ Automaton.Builder result = new Automaton.Builder(numStates, a.getNumTransitions());+ // Copy all states over+ result.copyStates(a);// Go in reverse topo sort so we know we only have to// make one pass:@@ -342,9 +338,7 @@ public class AnalyzingSuggester extends Lookup {}}- result.finishState();-- return result;+ return result.finish();}/** Used by subclass to change the lookup automaton, if