mirror of https://github.com/apache/lucene.git
LUCENE-5963: more efficient AnalyzingSuggester.replaceSep
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1626241 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7ec331a6d8
commit
1c2918f66d
|
@ -190,6 +190,9 @@ Optimizations
|
|||
* LUCENE-5959: Don't allocate excess memory when building automaton in
|
||||
finish. (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 and
|
||||
|
|
|
@ -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 transitions
|
||||
Transition 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 over
|
||||
int 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
|
||||
|
|
Loading…
Reference in New Issue