Skip to content

Commit ecaef2e

Browse files
committed
Add cache to importedModulesResolved
1 parent ebf322d commit ecaef2e

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public class ModuleEnvironment extends Environment {
9090
private Map<String, AbstractFunction> resourceImporters;
9191
private Map<Type, Set<GenericKeywordParameters>> cachedGeneralKeywordParameters;
9292
private Map<String, List<AbstractFunction>> cachedPublicFunctions;
93+
private List<ModuleEnvironment> cachedImportedModulesResolved;
9394

9495
private static final TypeFactory TF = TypeFactory.getInstance();
9596

@@ -109,6 +110,7 @@ public ModuleEnvironment(String name, GlobalEnvironment heap) {
109110
this.resourceImporters = new HashMap<String, AbstractFunction>();
110111
this.cachedGeneralKeywordParameters = null;
111112
this.cachedPublicFunctions = null;
113+
this.cachedImportedModulesResolved = null;
112114
}
113115

114116
@Override
@@ -127,12 +129,14 @@ public void reset() {
127129
this.generalKeywordParameters = new HashMap<>();
128130
this.cachedGeneralKeywordParameters = null;
129131
this.cachedPublicFunctions = null;
132+
this.cachedImportedModulesResolved = null;
130133
}
131134

132135
public void clearLookupCaches() {
133136
importedModules.replaceAll((k, v) -> Optional.empty());
134137
cachedGeneralKeywordParameters = null;
135138
cachedPublicFunctions = null;
139+
cachedImportedModulesResolved = null;
136140
}
137141

138142
/**
@@ -382,12 +386,14 @@ public void addImport(String name, ModuleEnvironment env) {
382386
typeStore.importStore(env.typeStore);
383387
this.cachedGeneralKeywordParameters = null;
384388
this.cachedPublicFunctions = null;
389+
this.cachedImportedModulesResolved = null;
385390
}
386391

387392
void removeModule(String name) {
388393
importedModules.computeIfPresent(name, (k, v) -> Optional.empty());
389394
this.cachedGeneralKeywordParameters = null;
390395
this.cachedPublicFunctions = null;
396+
this.cachedImportedModulesResolved = null;
391397
}
392398

393399
public void addExtend(String name) {
@@ -397,6 +403,7 @@ public void addExtend(String name) {
397403
extended.add(name);
398404
this.cachedGeneralKeywordParameters = null;
399405
this.cachedPublicFunctions = null;
406+
this.cachedImportedModulesResolved = null;
400407
}
401408

402409
public List<AbstractFunction> getTests() {
@@ -453,6 +460,7 @@ public void unImport(String moduleName) {
453460
}
454461
cachedGeneralKeywordParameters = null;
455462
cachedPublicFunctions = null;
463+
cachedImportedModulesResolved = null;
456464
}
457465

458466
public void unExtend(String moduleName) {
@@ -885,6 +893,17 @@ public ModuleEnvironment getImport(String moduleName) {
885893
}
886894

887895
private Iterable<ModuleEnvironment> importedModulesResolved =
896+
() -> getImportedModulesResolved().iterator();
897+
898+
private List<ModuleEnvironment> getImportedModulesResolved() {
899+
if (cachedImportedModulesResolved == null) {
900+
cachedImportedModulesResolved = new ArrayList<>();
901+
importedModulesResolver.forEach(cachedImportedModulesResolved::add);
902+
}
903+
return cachedImportedModulesResolved;
904+
}
905+
906+
private Iterable<ModuleEnvironment> importedModulesResolver =
888907
() -> new Iterator<ModuleEnvironment>() {
889908
Iterator<Entry<String, Optional<ModuleEnvironment>>> iterator = importedModules.entrySet().iterator();
890909
@Override

0 commit comments

Comments
 (0)