mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-04 14:56:28 +00:00
cache UserDefinedFunctions
This commit is contained in:
@@ -13,6 +13,7 @@ import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.dfsek.terra.api.config.meta.Meta;
|
||||
|
||||
@@ -45,4 +46,17 @@ public class FunctionTemplate implements ObjectTemplate<FunctionTemplate> {
|
||||
public LinkedHashMap<String, FunctionTemplate> getFunctions() {
|
||||
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.node.Statefulness;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.dfsek.terra.addons.noise.config.templates.FunctionTemplate;
|
||||
@@ -24,26 +26,33 @@ public class UserDefinedFunction implements DynamicFunction {
|
||||
private final Expression expression;
|
||||
private final int args;
|
||||
|
||||
private static final Map<FunctionTemplate, UserDefinedFunction> CACHE = new HashMap<>();
|
||||
|
||||
protected UserDefinedFunction(Expression expression, int args) {
|
||||
this.expression = expression;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
public static UserDefinedFunction newInstance(FunctionTemplate template) throws ParseException {
|
||||
Parser parser = new Parser();
|
||||
Scope parent = new Scope();
|
||||
|
||||
Scope functionScope = new Scope().withParent(parent);
|
||||
|
||||
template.getArgs().forEach(functionScope::addInvocationVariable);
|
||||
|
||||
for(Entry<String, FunctionTemplate> entry : template.getFunctions().entrySet()) {
|
||||
String id = entry.getKey();
|
||||
FunctionTemplate nest = entry.getValue();
|
||||
parser.registerFunction(id, newInstance(nest));
|
||||
UserDefinedFunction function = CACHE.get(template);
|
||||
if(function == null) {
|
||||
Parser parser = new Parser();
|
||||
Scope parent = new Scope();
|
||||
|
||||
Scope functionScope = new Scope().withParent(parent);
|
||||
|
||||
template.getArgs().forEach(functionScope::addInvocationVariable);
|
||||
|
||||
for(Entry<String, FunctionTemplate> entry : template.getFunctions().entrySet()) {
|
||||
String id = entry.getKey();
|
||||
FunctionTemplate nest = entry.getValue();
|
||||
parser.registerFunction(id, newInstance(nest));
|
||||
}
|
||||
|
||||
function = new UserDefinedFunction(parser.parse(template.getFunction(), functionScope), template.getArgs().size());
|
||||
CACHE.put(template, function);
|
||||
}
|
||||
|
||||
return new UserDefinedFunction(parser.parse(template.getFunction(), functionScope), template.getArgs().size());
|
||||
return function;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user