Reduce GC requirements by creating fewer objects during closure

This commit is contained in:
Sam Harwell 2013-06-06 15:39:22 -05:00
parent eeda06b698
commit 4c45a4bc66
6 changed files with 12 additions and 66 deletions

View File

@ -31,7 +31,6 @@
package org.antlr.v4.runtime.atn;
import java.util.Arrays;
import java.util.Iterator;
public class ArrayPredictionContext extends PredictionContext {
/** Parent can be null only if full ctx mode and we make an array
@ -58,26 +57,6 @@ public class ArrayPredictionContext extends PredictionContext {
this.returnStates = returnStates;
}
@Override
public Iterator<SingletonPredictionContext> iterator() {
return new Iterator<SingletonPredictionContext>() {
int i = 0;
@Override
public boolean hasNext() { return i < parents.length; }
@Override
public SingletonPredictionContext next() {
SingletonPredictionContext ctx =
SingletonPredictionContext.create(parents[i], returnStates[i]);
i++;
return ctx;
}
@Override
public void remove() { throw new UnsupportedOperationException(); }
};
}
@Override
public boolean isEmpty() {
return size()==1 &&

View File

@ -160,14 +160,14 @@ public class LL1Analyzer {
if ( ctx != PredictionContext.EMPTY ) {
// run thru all possible stack tops in ctx
for (SingletonPredictionContext p : ctx) {
ATNState returnState = atn.states.get(p.returnState);
for (int i = 0; i < ctx.size(); i++) {
ATNState returnState = atn.states.get(ctx.getReturnState(i));
// System.out.println("popping back to "+retState);
boolean removed = calledRuleStack.get(returnState.ruleIndex);
try {
calledRuleStack.clear(returnState.ruleIndex);
_LOOK(returnState, p.parent, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
_LOOK(returnState, ctx.getParent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
}
finally {
if (removed) {

View File

@ -433,20 +433,10 @@ public class LexerATNSimulator extends ATNSimulator {
}
if ( config.context!=null && !config.context.isEmpty() ) {
for (SingletonPredictionContext ctx : config.context) {
if ( !ctx.isEmpty() ) {
PredictionContext newContext = ctx.parent; // "pop" return state
if ( ctx.returnState==PredictionContext.EMPTY_RETURN_STATE ) {
// we have no context info. Don't pursue but
// record a config that indicates how we hit end
LexerATNConfig c = new LexerATNConfig(config, config.state, ctx);
if ( debug ) System.out.println("FALLING off token "+
recog.getRuleNames()[config.state.ruleIndex]+
" record "+c);
configs.add(c);
continue;
}
ATNState returnState = atn.states.get(ctx.returnState);
for (int i = 0; i < config.context.size(); i++) {
if (config.context.getReturnState(i) != PredictionContext.EMPTY_RETURN_STATE) {
PredictionContext newContext = config.context.getParent(i); // "pop" return state
ATNState returnState = atn.states.get(config.context.getReturnState(i));
LexerATNConfig c = new LexerATNConfig(returnState, config.alt, newContext);
currentAltReachedAcceptState = closure(input, c, configs, currentAltReachedAcceptState, speculative);
}

View File

@ -1069,8 +1069,8 @@ public class ParserATNSimulator extends ATNSimulator {
// We hit rule end. If we have context info, use it
// run thru all possible stack tops in ctx
if ( !config.context.isEmpty() ) {
for (SingletonPredictionContext ctx : config.context) {
if ( ctx.returnState==PredictionContext.EMPTY_RETURN_STATE ) {
for (int i = 0; i < config.context.size(); i++) {
if ( config.context.getReturnState(i)==PredictionContext.EMPTY_RETURN_STATE ) {
if (fullCtx) {
configs.add(new ATNConfig(config, config.state, PredictionContext.EMPTY), mergeCache);
continue;
@ -1084,8 +1084,8 @@ public class ParserATNSimulator extends ATNSimulator {
}
continue;
}
ATNState returnState = atn.states.get(ctx.returnState);
PredictionContext newContext = ctx.parent; // "pop" return state
ATNState returnState = atn.states.get(config.context.getReturnState(i));
PredictionContext newContext = config.context.getParent(i); // "pop" return state
ATNConfig c = new ATNConfig(returnState, config.alt, newContext,
config.semanticContext);
// While we have context to pop back from, we may have

View File

@ -42,12 +42,10 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public abstract class PredictionContext implements Iterable<SingletonPredictionContext>,
Comparable<PredictionContext> // to sort node lists by id
public abstract class PredictionContext implements Comparable<PredictionContext> // to sort node lists by id
{
/**
* Represents {@code $} in local context prediction, which means wildcard.
@ -117,9 +115,6 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
return SingletonPredictionContext.create(parent, transition.followState.stateNumber);
}
@Override
public abstract Iterator<SingletonPredictionContext> iterator();
public abstract int size();
public abstract PredictionContext getParent(int index);

View File

@ -30,8 +30,6 @@
package org.antlr.v4.runtime.atn;
import java.util.Iterator;
public class SingletonPredictionContext extends PredictionContext {
public final PredictionContext parent;
public final int returnState;
@ -51,22 +49,6 @@ public class SingletonPredictionContext extends PredictionContext {
return new SingletonPredictionContext(parent, returnState);
}
@Override
public Iterator<SingletonPredictionContext> iterator() {
final SingletonPredictionContext self = this;
return new Iterator<SingletonPredictionContext>() {
int i = 0;
@Override
public boolean hasNext() { return i==0; }
@Override
public SingletonPredictionContext next() { i++; return self; }
@Override
public void remove() { throw new UnsupportedOperationException(); }
};
}
@Override
public int size() {
return 1;