cache UserDefinedFunctions

This commit is contained in:
dfsek committed 2022-05-28 17:39:52 -07:00
1 parent a11e02a84a
commit 414c0b8a38
2 files changed
+33 -10

No files matched your search

@@ -13,6 +13,7 @@ import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Objects;
import com.dfsek.terra.api.config.meta.Meta; import com.dfsek.terra.api.config.meta.Meta;
@@ -45,4 +46,17 @@ public class FunctionTemplate implements ObjectTemplate<FunctionTemplate> {
public LinkedHashMap<String, FunctionTemplate> getFunctions() { public LinkedHashMap<String, FunctionTemplate> getFunctions() {
return functions; return functions;
} }
@Override
public boolean equals(Object o) {
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
FunctionTemplate that = (FunctionTemplate) o;
return args.equals(that.args) && function.equals(that.function) && functions.equals(that.functions);
}
@Override
public int hashCode() {
return Objects.hash(args, function, functions);
}
} }
@@ -15,6 +15,8 @@ import com.dfsek.paralithic.functions.dynamic.Context;
import com.dfsek.paralithic.functions.dynamic.DynamicFunction; import com.dfsek.paralithic.functions.dynamic.DynamicFunction;
import com.dfsek.paralithic.node.Statefulness; import com.dfsek.paralithic.node.Statefulness;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.dfsek.terra.addons.noise.config.templates.FunctionTemplate; import com.dfsek.terra.addons.noise.config.templates.FunctionTemplate;
@@ -24,26 +26,33 @@ public class UserDefinedFunction implements DynamicFunction {
private final Expression expression; private final Expression expression;
private final int args; private final int args;
private static final Map<FunctionTemplate, UserDefinedFunction> CACHE = new HashMap<>();
protected UserDefinedFunction(Expression expression, int args) { protected UserDefinedFunction(Expression expression, int args) {
this.expression = expression; this.expression = expression;
this.args = args; this.args = args;
} }
public static UserDefinedFunction newInstance(FunctionTemplate template) throws ParseException { public static UserDefinedFunction newInstance(FunctionTemplate template) throws ParseException {
Parser parser = new Parser(); UserDefinedFunction function = CACHE.get(template);
Scope parent = new Scope(); if(function == null) {
Parser parser = new Parser();
Scope parent = new Scope();
Scope functionScope = new Scope().withParent(parent); Scope functionScope = new Scope().withParent(parent);
template.getArgs().forEach(functionScope::addInvocationVariable); template.getArgs().forEach(functionScope::addInvocationVariable);
for(Entry<String, FunctionTemplate> entry : template.getFunctions().entrySet()) { for(Entry<String, FunctionTemplate> entry : template.getFunctions().entrySet()) {
String id = entry.getKey(); String id = entry.getKey();
FunctionTemplate nest = entry.getValue(); FunctionTemplate nest = entry.getValue();
parser.registerFunction(id, newInstance(nest)); parser.registerFunction(id, newInstance(nest));
}
function = new UserDefinedFunction(parser.parse(template.getFunction(), functionScope), template.getArgs().size());
CACHE.put(template, function);
} }
return function;
return new UserDefinedFunction(parser.parse(template.getFunction(), functionScope), template.getArgs().size());
} }
@Override @Override