Skip to content

Commit a284eef

Browse files
authored
moved lookup of generic keyword parameters to declaration site of a constructor, such that it can be kept static and reused between field accesses (#2417)
* added generic keyword parameter cache as an experiment * clear kwparam cache when reloading
1 parent 3cffb74 commit a284eef

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/org/rascalmpl/interpreter/env/ModuleEnvironment.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ public class ModuleEnvironment extends Environment {
7878
private boolean bootstrap;
7979
private String deprecated;
8080
protected Map<String, AbstractFunction> resourceImporters;
81+
protected Map<Type, Set<GenericKeywordParameters>> cachedGeneralKeywordParameters;
8182

8283
protected static final TypeFactory TF = TypeFactory.getInstance();
8384

84-
public final static String SHELL_MODULE = "$shell$";
85+
public final static String SHELL_MODULE = "$";
8586

8687
public ModuleEnvironment(String name, GlobalEnvironment heap) {
8788
super(ValueFactoryFactory.getValueFactory().sourceLocation(URIUtil.assumeCorrect("main", name, "")), name);
@@ -95,6 +96,7 @@ public ModuleEnvironment(String name, GlobalEnvironment heap) {
9596
this.syntaxDefined = false;
9697
this.bootstrap = false;
9798
this.resourceImporters = new HashMap<String, AbstractFunction>();
99+
this.cachedGeneralKeywordParameters = null;
98100
}
99101

100102
/**
@@ -113,25 +115,27 @@ protected ModuleEnvironment(ModuleEnvironment env) {
113115
this.syntaxDefined = env.syntaxDefined;
114116
this.bootstrap = env.bootstrap;
115117
this.resourceImporters = env.resourceImporters;
118+
this.cachedGeneralKeywordParameters = null;
116119
this.deprecated = env.deprecated;
117120
}
118121

119122
@Override
120123
public void reset() {
121124
super.reset();
122-
this.importedModules = new HashSet<String>();
123-
this.concreteSyntaxTypes = new HashMap<String, NonTerminalType>();
125+
this.importedModules = new HashSet<>();
126+
this.concreteSyntaxTypes = new HashMap<>();
124127
this.typeStore = new TypeStore();
125128
this.productions = new HashSet<IValue>();
126129
this.initialized = false;
127130
this.syntaxDefined = false;
128131
this.bootstrap = false;
129132
this.extended = new HashSet<String>();
130133
this.deprecated = null;
134+
this.generalKeywordParameters = new HashMap<>();
135+
this.cachedGeneralKeywordParameters = null;
131136
}
132137

133138
public void extend(ModuleEnvironment other) {
134-
// super.extend(other);
135139
extendNameFlags(other);
136140

137141
// First extend the imports before functions and variables
@@ -386,10 +390,12 @@ public void unImport(String moduleName) {
386390
typeStore.unimportStores(new TypeStore[] { old.getStore() });
387391
}
388392
}
393+
cachedGeneralKeywordParameters = null;
389394
}
390395

391396
public void unExtend(String moduleName) {
392397
extended.remove(moduleName);
398+
cachedGeneralKeywordParameters = null;
393399
}
394400

395401
@Override
@@ -710,6 +716,16 @@ public List<KeywordFormal> getFormals() {
710716

711717
@Override
712718
public Set<GenericKeywordParameters> lookupGenericKeywordParameters(Type adt) {
719+
if (cachedGeneralKeywordParameters != null) {
720+
var result = cachedGeneralKeywordParameters.get(adt);
721+
if (result != null) {
722+
return result;
723+
}
724+
}
725+
else {
726+
cachedGeneralKeywordParameters = io.usethesource.capsule.Map.Transient.of();
727+
}
728+
713729
Set<GenericKeywordParameters> result = new HashSet<>();
714730
List<KeywordFormal> list = generalKeywordParameters.get(adt);
715731
if (list != null) {
@@ -725,6 +741,9 @@ public Set<GenericKeywordParameters> lookupGenericKeywordParameters(Type adt) {
725741
}
726742
}
727743

744+
// save the result for next time
745+
cachedGeneralKeywordParameters.put(adt, result);
746+
728747
return result;
729748
}
730749

0 commit comments

Comments
 (0)