forked from jasder/antlr
v4: Optimize ATNState.onlyHasEpsilonTransitions (5-15% improvement during warmup)
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9495]
This commit is contained in:
parent
8096b3b4c4
commit
583668746f
|
@ -97,7 +97,7 @@ public class ATNState {
|
|||
|
||||
public int ruleIndex; // at runtime, we don't have Rule objects
|
||||
|
||||
public int epsilonOnlyTransitions = -1;
|
||||
public boolean epsilonOnlyTransitions = false;
|
||||
|
||||
/** Track the transitions emanating from this ATN state. */
|
||||
protected final List<Transition> transitions =
|
||||
|
@ -122,7 +122,17 @@ public class ATNState {
|
|||
|
||||
public int getNumberOfTransitions() { return transitions.size(); }
|
||||
|
||||
public void addTransition(Transition e) { transitions.add(e); }
|
||||
public void addTransition(Transition e) {
|
||||
if (transitions.isEmpty()) {
|
||||
epsilonOnlyTransitions = e.isEpsilon();
|
||||
}
|
||||
else if (epsilonOnlyTransitions != e.isEpsilon()) {
|
||||
System.err.format("ATN state %d has both epsilon and non-epsilon transitions.\n", stateNumber);
|
||||
epsilonOnlyTransitions = false;
|
||||
}
|
||||
|
||||
transitions.add(e);
|
||||
}
|
||||
|
||||
public Transition transition(int i) { return transitions.get(i); }
|
||||
|
||||
|
@ -134,16 +144,8 @@ public class ATNState {
|
|||
return serializationTypes.get(this.getClass());
|
||||
}
|
||||
|
||||
//lexer atn sim: getEpTar: 13.2%
|
||||
// ruleCtx.equals 10%
|
||||
// TODO: Sam says this takes a lot of time; optimize
|
||||
public boolean onlyHasEpsilonTransitions() { // 22% time
|
||||
if ( epsilonOnlyTransitions>=0 ) return epsilonOnlyTransitions==1;
|
||||
if ( transitions.size()==0 ) return false;
|
||||
for (Transition t : transitions) {
|
||||
if ( !t.isEpsilon() ) return false;
|
||||
}
|
||||
return true;
|
||||
public final boolean onlyHasEpsilonTransitions() {
|
||||
return epsilonOnlyTransitions;
|
||||
}
|
||||
|
||||
public void setRuleIndex(int ruleIndex) { this.ruleIndex = ruleIndex; }
|
||||
|
|
|
@ -33,7 +33,4 @@ public class DecisionState extends ATNState {
|
|||
public int decision = -1;
|
||||
|
||||
public boolean isGreedy = true;
|
||||
|
||||
@Override
|
||||
public boolean onlyHasEpsilonTransitions() { return true; }
|
||||
}
|
||||
|
|
|
@ -33,6 +33,4 @@ package org.antlr.v4.runtime.atn;
|
|||
* one to the loop back to start of the block and one to exit.
|
||||
*/
|
||||
public class PlusLoopbackState extends DecisionState {
|
||||
@Override
|
||||
public boolean onlyHasEpsilonTransitions() { return true; }
|
||||
}
|
||||
|
|
|
@ -31,6 +31,4 @@ package org.antlr.v4.runtime.atn;
|
|||
|
||||
public class StarLoopEntryState extends DecisionState {
|
||||
public StarLoopbackState loopBackState;
|
||||
@Override
|
||||
public boolean onlyHasEpsilonTransitions() { return true; }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue