diff --git a/common/addons/config-noise-function/src/main/java/com/dfsek/terra/addons/noise/config/templates/FunctionTemplate.java b/common/addons/config-noise-function/src/main/java/com/dfsek/terra/addons/noise/config/templates/FunctionTemplate.java index f126a9324..a7a18efdd 100644 --- a/common/addons/config-noise-function/src/main/java/com/dfsek/terra/addons/noise/config/templates/FunctionTemplate.java +++ b/common/addons/config-noise-function/src/main/java/com/dfsek/terra/addons/noise/config/templates/FunctionTemplate.java @@ -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 { public LinkedHashMap 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); + } } diff --git a/common/addons/config-noise-function/src/main/java/com/dfsek/terra/addons/noise/paralithic/defined/UserDefinedFunction.java b/common/addons/config-noise-function/src/main/java/com/dfsek/terra/addons/noise/paralithic/defined/UserDefinedFunction.java index a07fcb5e6..fe72ed070 100644 --- a/common/addons/config-noise-function/src/main/java/com/dfsek/terra/addons/noise/paralithic/defined/UserDefinedFunction.java +++ b/common/addons/config-noise-function/src/main/java/com/dfsek/terra/addons/noise/paralithic/defined/UserDefinedFunction.java @@ -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 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 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 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