Skip to content

Commit de84f36

Browse files
committed
Cache scripts to improve performance
Required for CyclopsMC/IntegratedDynamics#1439
1 parent a033199 commit de84f36

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/main/java/org/cyclops/integratedscripting/core/network/ScriptingNetwork.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.collect.Maps;
44
import com.google.common.collect.Sets;
55
import net.minecraft.network.chat.Component;
6+
import org.apache.commons.lang3.tuple.Pair;
67
import org.apache.commons.lang3.tuple.Triple;
78
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
89
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
@@ -26,6 +27,7 @@
2627
public class ScriptingNetwork implements IScriptingNetwork {
2728

2829
private final Set<Integer> disks = Sets.newHashSet();
30+
private final Map<Pair<Integer, Path>, IScript> scriptCache = Maps.newHashMap();
2931
private final Map<Triple<Integer, Path, String>, ScriptVariable> variableCache = Maps.newHashMap();
3032

3133
@Override
@@ -46,12 +48,21 @@ public Set<Integer> getDisks() {
4648
@Nullable
4749
@Override
4850
public IScript getScript(int disk, Path path) throws EvaluationException {
49-
ILanguageHandler languageHandler = LanguageHandlers.REGISTRY.getProvider(path);
50-
if (languageHandler == null) {
51-
throw new EvaluationException(Component.translatable("script.integratedscripting.error.unsupported_language", path.toString()));
51+
Pair<Integer, Path> cacheKey = Pair.of(disk, path);
52+
IScript script = scriptCache.get(cacheKey);
53+
if (script == null) {
54+
ILanguageHandler languageHandler = LanguageHandlers.REGISTRY.getProvider(path);
55+
if (languageHandler == null) {
56+
throw new EvaluationException(Component.translatable("script.integratedscripting.error.unsupported_language", path.toString()));
57+
}
58+
IScriptFactory scriptFactory = languageHandler.getScriptFactory();
59+
script = scriptFactory.getScript(disk, path);
60+
if (script != null) {
61+
script.addInvalidationListener(() -> scriptCache.remove(cacheKey));
62+
scriptCache.put(cacheKey, script);
63+
}
5264
}
53-
IScriptFactory scriptFactory = languageHandler.getScriptFactory();
54-
return scriptFactory.getScript(disk, path);
65+
return script;
5566
}
5667

5768
@Override

0 commit comments

Comments
 (0)