Fewer and finer locks on ATNSimulator.sharedContextCache

This commit is contained in:
Sam Harwell 2013-04-12 13:19:04 -05:00
parent 05f667d2e5
commit 8b3062175d
3 changed files with 14 additions and 20 deletions

View File

@ -104,11 +104,13 @@ public abstract class ATNSimulator {
public PredictionContext getCachedContext(PredictionContext context) {
if ( sharedContextCache==null ) return context;
IdentityHashMap<PredictionContext, PredictionContext> visited =
new IdentityHashMap<PredictionContext, PredictionContext>();
return PredictionContext.getCachedContext(context,
sharedContextCache,
visited);
synchronized (sharedContextCache) {
IdentityHashMap<PredictionContext, PredictionContext> visited =
new IdentityHashMap<PredictionContext, PredictionContext>();
return PredictionContext.getCachedContext(context,
sharedContextCache,
visited);
}
}
public static ATN deserialize(@NotNull char[] data) {

View File

@ -1562,9 +1562,7 @@ public class ParserATNSimulator extends ATNSimulator {
if ( existing!=null ) return existing;
D.stateNumber = dfa.states.size();
synchronized (sharedContextCache) {
D.configs.optimizeConfigs(this);
}
D.configs.optimizeConfigs(this);
D.configs.setReadonly(true);
dfa.states.put(D, D);
if ( debug ) System.out.println("adding new DFA state: "+D);

View File

@ -562,12 +562,10 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
return existing;
}
synchronized (contextCache) {
existing = contextCache.get(context);
if (existing != null) {
visited.put(context, existing);
return existing;
}
existing = contextCache.get(context);
if (existing != null) {
visited.put(context, existing);
return existing;
}
boolean changed = false;
@ -589,9 +587,7 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
}
if (!changed) {
synchronized (contextCache) {
contextCache.add(context);
}
contextCache.add(context);
visited.put(context, context);
return context;
}
@ -608,9 +604,7 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
updated = new ArrayPredictionContext(parents, arrayPredictionContext.returnStates);
}
synchronized (contextCache) {
contextCache.add(updated);
}
contextCache.add(updated);
visited.put(updated, updated);
visited.put(context, updated);