From 4c45a4bc661f029f2af1f7f6e87a700be0080aa6 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 6 Jun 2013 15:39:22 -0500 Subject: [PATCH] Reduce GC requirements by creating fewer objects during closure --- .../runtime/atn/ArrayPredictionContext.java | 21 ------------------- .../org/antlr/v4/runtime/atn/LL1Analyzer.java | 6 +++--- .../v4/runtime/atn/LexerATNSimulator.java | 18 ++++------------ .../v4/runtime/atn/ParserATNSimulator.java | 8 +++---- .../v4/runtime/atn/PredictionContext.java | 7 +------ .../atn/SingletonPredictionContext.java | 18 ---------------- 6 files changed, 12 insertions(+), 66 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ArrayPredictionContext.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ArrayPredictionContext.java index 60dd4303d..5540622fe 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ArrayPredictionContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ArrayPredictionContext.java @@ -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 iterator() { - return new Iterator() { - 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 && diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java index e175a7618..1e6a858b8 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java @@ -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) { diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java index 2abec2682..b71837bff 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java @@ -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); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java index 987efd831..7409bfd8e 100755 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java @@ -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 diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/PredictionContext.java b/runtime/Java/src/org/antlr/v4/runtime/atn/PredictionContext.java index c93088b5f..e6d89b0f6 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/PredictionContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/PredictionContext.java @@ -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, - Comparable // to sort node lists by id +public abstract class PredictionContext implements Comparable // 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 iterator(); - public abstract int size(); public abstract PredictionContext getParent(int index); diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/SingletonPredictionContext.java b/runtime/Java/src/org/antlr/v4/runtime/atn/SingletonPredictionContext.java index 3cf538b84..9f7682ba0 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/SingletonPredictionContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/SingletonPredictionContext.java @@ -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 iterator() { - final SingletonPredictionContext self = this; - return new Iterator() { - 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;