From 7610cb71d2131a31e3b6aee86b19d7f24418e5ff Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 00:44:34 +0000 Subject: [PATCH] Combat 2.1d: Compile math expressions once and evaluate with variables Add Evaluator.compile(String, String...) returning an immutable, thread-safe CompiledExpression. The expression is parsed once with the bundled Javaluator grammar and evaluated from a tree without parsing or allocating per call. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01FaEyTqc8jf3RR3v7Fz7SHm --- .../codex/util/eval/CompiledExpression.java | 91 +++ .../magemonkey/codex/util/eval/Evaluator.java | 21 + .../codex/util/eval/ExpressionCompiler.java | 220 +++++++ .../codex/util/eval/ExpressionNode.java | 123 ++++ .../util/eval/CompiledExpressionTest.java | 607 ++++++++++++++++++ .../codex/util/eval/ExpressionBenchmark.java | 103 +++ .../util/eval/ExpressionBenchmarkTest.java | 18 + 7 files changed, 1183 insertions(+) create mode 100644 codex-api/src/main/java/studio/magemonkey/codex/util/eval/CompiledExpression.java create mode 100644 codex-api/src/main/java/studio/magemonkey/codex/util/eval/ExpressionCompiler.java create mode 100644 codex-api/src/main/java/studio/magemonkey/codex/util/eval/ExpressionNode.java create mode 100644 codex-api/src/test/java/studio/magemonkey/codex/util/eval/CompiledExpressionTest.java create mode 100644 codex-api/src/test/java/studio/magemonkey/codex/util/eval/ExpressionBenchmark.java create mode 100644 codex-api/src/test/java/studio/magemonkey/codex/util/eval/ExpressionBenchmarkTest.java diff --git a/codex-api/src/main/java/studio/magemonkey/codex/util/eval/CompiledExpression.java b/codex-api/src/main/java/studio/magemonkey/codex/util/eval/CompiledExpression.java new file mode 100644 index 00000000..4035580d --- /dev/null +++ b/codex-api/src/main/java/studio/magemonkey/codex/util/eval/CompiledExpression.java @@ -0,0 +1,91 @@ +package studio.magemonkey.codex.util.eval; + +import lombok.Getter; +import lombok.ToString; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * A math expression that was parsed once and can be evaluated many times with different variable values. + *

+ * Get one from {@link Evaluator#compile(String, String...)}: + *

{@code
+ * CompiledExpression ce = Evaluator.compile("max(d / 5, d - 4 * x / max(1, t + 8))", "d", "x", "t");
+ * double v = ce.eval(def, dmg, toughness);
+ * }
+ * The syntax, operators, functions and constants are those of the bundled Javaluator + * {@link studio.magemonkey.codex.util.eval.javaluator.DoubleEvaluator}, and for any expression both accept the + * result is the same double, bit for bit. The differences: + * + * An instance is immutable and may be shared and evaluated from any number of threads at once. + */ +@ToString(onlyExplicitlyIncluded = true) +public final class CompiledExpression { + private static final ExpressionCompiler COMPILER = new ExpressionCompiler(); + + /** The expression as it was given to {@link Evaluator#compile(String, String...)}. */ + @Getter + @ToString.Include + private final String source; + /** The variable names, in the order {@link #eval(double...)} expects their values. Unmodifiable. */ + @Getter + @ToString.Include + private final List variableNames; + private final ExpressionNode root; + + private CompiledExpression(String source, List variableNames, ExpressionNode root) { + this.source = source; + this.variableNames = variableNames; + this.root = root; + } + + /** + * @see Evaluator#compile(String, String...) + */ + static CompiledExpression compile(String expression, String... variables) { + if (expression == null) { + throw new IllegalArgumentException("Cannot compile a null expression"); + } + if (variables == null) { + throw new IllegalArgumentException("Cannot compile '" + expression + "': variable names are null"); + } + List names = new ArrayList<>(variables.length); + Set seen = new HashSet<>(); + for (String name : variables) { + String problem = COMPILER.checkVariableName(name); + if (problem == null && !seen.add(name)) { + problem = "variable name '" + name + "' is given twice"; + } + if (problem != null) { + throw new IllegalArgumentException("Cannot compile '" + expression + "': " + problem); + } + names.add(name); + } + return new CompiledExpression(expression, + Collections.unmodifiableList(names), + COMPILER.compile(expression, names)); + } + + /** + * Evaluates the expression. Does not parse, does not allocate, and does not modify {@code values}. + * + * @param values one value for each variable name, in the order the names were given to {@code compile} + * @return the result. May be NaN or infinite; checking is up to the caller. + * @throws IllegalArgumentException if the number of values differs from the number of variable names + */ + public double eval(double... values) { + if (values.length != variableNames.size()) { + throw new IllegalArgumentException("Expression '" + source + "' takes " + variableNames.size() + + " value(s) " + variableNames + " but got " + values.length); + } + return root.eval(values); + } +} diff --git a/codex-api/src/main/java/studio/magemonkey/codex/util/eval/Evaluator.java b/codex-api/src/main/java/studio/magemonkey/codex/util/eval/Evaluator.java index c10f66a1..0eed7952 100644 --- a/codex-api/src/main/java/studio/magemonkey/codex/util/eval/Evaluator.java +++ b/codex-api/src/main/java/studio/magemonkey/codex/util/eval/Evaluator.java @@ -20,6 +20,27 @@ public static double eval(String exp, int method) { } } + /** + * Parses a math expression once, so it can be evaluated many times with different values for its variables. + * Use this instead of {@link #eval(String, int)} when the same formula runs repeatedly. + *

+ * The syntax is that of the Javaluator evaluator ({@code + - * / ^ %}, unary minus, brackets, {@code min}, + * {@code max}, {@code abs}, {@code sqrt}, the constants {@code pi} and {@code e} and so on). + * + * @param expression the expression, such as {@code max(d / 5, d - 4 * x / max(1, t + 8))} + * @param variables the names of the variables the expression may use. Their values are later given to + * {@link CompiledExpression#eval(double...)} in this order. A name cannot be a function, a + * constant or a number, cannot contain an operator, bracket, comma or whitespace, and cannot + * be repeated. + * @return the compiled expression, which is immutable and thread safe + * @throws IllegalArgumentException with a message naming the expression, if it has a syntax error, mismatched + * brackets, an unknown variable or function, a function with the wrong number + * of arguments, or if a variable name is not usable + */ + public static CompiledExpression compile(String expression, String... variables) { + return CompiledExpression.compile(expression, variables); + } + private static double javaluator(String exp) { try { return jl.evaluate(exp); diff --git a/codex-api/src/main/java/studio/magemonkey/codex/util/eval/ExpressionCompiler.java b/codex-api/src/main/java/studio/magemonkey/codex/util/eval/ExpressionCompiler.java new file mode 100644 index 00000000..4796ad59 --- /dev/null +++ b/codex-api/src/main/java/studio/magemonkey/codex/util/eval/ExpressionCompiler.java @@ -0,0 +1,220 @@ +package studio.magemonkey.codex.util.eval; + +import studio.magemonkey.codex.util.eval.javaluator.*; + +import java.text.NumberFormat; +import java.text.ParsePosition; +import java.util.*; +import java.util.function.DoubleUnaryOperator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Turns an expression into an {@link ExpressionNode} tree using the grammar of {@link DoubleEvaluator}. + *

+ * This is an {@link AbstractEvaluator} whose values are tree nodes instead of numbers, so the tokenizer, the + * operator precedence and associativity, the unary minus handling, the bracket and argument-count checks are all + * the ones {@link DoubleEvaluator} runs. Only the meaning given to each operator and function lives here. + */ +final class ExpressionCompiler extends AbstractEvaluator { + private static final Pattern POSITION = Pattern.compile("At position (\\d+)"); + + private static final ThreadLocal FORMATTER = + ThreadLocal.withInitial(() -> NumberFormat.getNumberInstance(Locale.US)); + + private static final Map UNARY = new IdentityHashMap<>(); + + static { + UNARY.put(DoubleEvaluator.ABS, Math::abs); + UNARY.put(DoubleEvaluator.CEIL, Math::ceil); + UNARY.put(DoubleEvaluator.FLOOR, Math::floor); + UNARY.put(DoubleEvaluator.ROUND, d -> d == Double.NEGATIVE_INFINITY || d == Double.POSITIVE_INFINITY + ? d + : (double) Math.round(d)); + UNARY.put(DoubleEvaluator.SINEH, Math::sinh); + UNARY.put(DoubleEvaluator.COSINEH, Math::cosh); + UNARY.put(DoubleEvaluator.TANGENTH, Math::tanh); + UNARY.put(DoubleEvaluator.SINE, Math::sin); + UNARY.put(DoubleEvaluator.COSINE, Math::cos); + UNARY.put(DoubleEvaluator.TANGENT, Math::tan); + UNARY.put(DoubleEvaluator.ACOSINE, Math::acos); + UNARY.put(DoubleEvaluator.ASINE, Math::asin); + UNARY.put(DoubleEvaluator.ATAN, Math::atan); + UNARY.put(DoubleEvaluator.LN, Math::log); + UNARY.put(DoubleEvaluator.LOG, Math::log10); + UNARY.put(DoubleEvaluator.SQRT, Math::sqrt); + } + + /** The variables of one compilation, as the evaluation context the shared parser asks for values. */ + private static final class Variables implements AbstractVariableSet { + private final Map byName = new HashMap<>(); + + @Override + public ExpressionNode get(String variableName) { + return byName.get(variableName); + } + } + + ExpressionCompiler() { + super(DoubleEvaluator.getDefaultParameters()); + } + + /** + * @param expression the expression to compile + * @param names the variable names, already checked for duplicates + * @return the tree + * @throws IllegalArgumentException if the expression is not valid + */ + ExpressionNode compile(String expression, List names) { + Variables variables = new Variables(); + for (int i = 0; i < names.size(); i++) { + variables.byName.put(names.get(i), new ExpressionNode.Variable(i)); + } + try { + return evaluate(expression, variables); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException(describe(expression, e.getMessage()), e); + } catch (RuntimeException e) { + // The shared parser reports some malformed input (a lone ",", a missing operand) as an NPE or similar. + throw new IllegalArgumentException(describe(expression, "malformed expression"), e); + } + } + + /** + * Checks that a variable name would be read back as a single variable: not blank, no whitespace, no operator or + * bracket in it, not a function, a constant or a number. + * + * @return why the name is not usable, or null if it is + */ + String checkVariableName(String name) { + if (name == null) { + return "variable name is null"; + } + if (name.isEmpty() || name.chars().anyMatch(Character::isWhitespace)) { + return "variable name '" + name + "' is empty or contains whitespace"; + } + if (functions.containsKey(name)) { + return "variable name '" + name + "' is already a function"; + } + if (constants.containsKey(name)) { + return "variable name '" + name + "' is already a constant"; + } + Collection tokens = tokenizer.tokenize(name); + if (tokens.size() != 1 || !tokens.iterator().next().isLiteral()) { + return "variable name '" + name + "' contains an operator, bracket, separator or whitespace"; + } + if (parseNumber(name) != null) { + return "variable name '" + name + "' is a number"; + } + return null; + } + + private static String describe(String expression, String cause) { + String message = cause == null || cause.isEmpty() ? "malformed expression" : cause; + // The shared parser adds a three-line "At position N of line L" excerpt; keep the position, on one line. + int newline = message.indexOf('\n'); + String first = newline < 0 ? message : message.substring(0, newline); + Matcher m = POSITION.matcher(message); + String where = m.find() ? " (at position " + m.group(1) + ")" : ""; + return "Cannot compile '" + expression + "': " + first + where; + } + + private static Double parseNumber(String literal) { + ParsePosition p = new ParsePosition(0); + Number result = FORMATTER.get().parse(literal, p); + if (p.getIndex() == 0 || p.getIndex() != literal.length()) { + return null; + } + return result.doubleValue(); + } + + @Override + protected ExpressionNode toValue(Token literalTok, EvaluationContext context) { + String literal = literalTok.getString(); + // Same parse as DoubleEvaluator.toValue, so a number compiles to the exact double the string evaluator gets. + Double number = parseNumber(literal); + if (number != null) { + return new ExpressionNode.Constant(number); + } + char first = literal.charAt(0); + if ((first >= '0' && first <= '9') || first == '.') { + throw context.getError("invalid number '" + literal + "'", literalTok); + } + throw context.getError("unknown variable or function '" + literal + "'", literalTok); + } + + @Override + protected ExpressionNode evaluate(Constant constant, EvaluationContext context) { + if (DoubleEvaluator.PI == constant) { + return new ExpressionNode.Constant(Math.PI); + } else if (DoubleEvaluator.E == constant) { + return new ExpressionNode.Constant(Math.E); + } + throw new IllegalArgumentException("unsupported constant '" + constant.getName() + "'"); + } + + @Override + protected ExpressionNode evaluate(Operator operator, Iterator operands, EvaluationContext context) { + if (DoubleEvaluator.NEGATE == operator || DoubleEvaluator.NEGATE_HIGH == operator) { + return new ExpressionNode.Unary(d -> -d, operands.next()); + } + ExpressionNode left = operands.next(); + ExpressionNode right = operands.next(); + if (DoubleEvaluator.MINUS == operator) { + return new ExpressionNode.Binary((a, b) -> a - b, left, right); + } else if (DoubleEvaluator.PLUS == operator) { + return new ExpressionNode.Binary((a, b) -> a + b, left, right); + } else if (DoubleEvaluator.MULTIPLY == operator) { + return new ExpressionNode.Binary((a, b) -> a * b, left, right); + } else if (DoubleEvaluator.DIVIDE == operator) { + return new ExpressionNode.Binary((a, b) -> a / b, left, right); + } else if (DoubleEvaluator.EXPONENT == operator) { + return new ExpressionNode.Binary(Math::pow, left, right); + } else if (DoubleEvaluator.MODULO == operator) { + return new ExpressionNode.Binary((a, b) -> a % b, left, right); + } + throw new IllegalArgumentException("unsupported operator '" + operator.getSymbol() + "'"); + } + + @Override + protected void doFunction(Deque values, Token functionTok, int argCount, EvaluationContext context) { + Function f = functionTok.getFunction(); + if (argCount < f.getMinimumArgumentCount() || argCount > f.getMaximumArgumentCount()) { + String expected = f.getMaximumArgumentCount() == Integer.MAX_VALUE + ? "at least " + f.getMinimumArgumentCount() + : f.getMinimumArgumentCount() == f.getMaximumArgumentCount() + ? String.valueOf(f.getMinimumArgumentCount()) + : f.getMinimumArgumentCount() + " to " + f.getMaximumArgumentCount(); + throw context.getError( + "wrong number of arguments for " + f.getName() + ": expected " + expected + " but got " + argCount, + functionTok); + } + super.doFunction(values, functionTok, argCount, context); + } + + @Override + protected ExpressionNode evaluate(Function function, Iterator arguments, EvaluationContext context) { + DoubleUnaryOperator unary = UNARY.get(function); + if (unary != null) { + return new ExpressionNode.Unary(unary, arguments.next()); + } + if (DoubleEvaluator.RANDOM == function) { + return new ExpressionNode.Random(); + } + List list = new ArrayList<>(); + while (arguments.hasNext()) { + list.add(arguments.next()); + } + ExpressionNode[] args = list.toArray(new ExpressionNode[0]); + if (DoubleEvaluator.MIN == function) { + return new ExpressionNode.Fold(Math::min, false, false, args); + } else if (DoubleEvaluator.MAX == function) { + return new ExpressionNode.Fold(Math::max, false, false, args); + } else if (DoubleEvaluator.SUM == function) { + return new ExpressionNode.Fold((a, b) -> a + b, true, false, args); + } else if (DoubleEvaluator.AVERAGE == function) { + return new ExpressionNode.Fold((a, b) -> a + b, true, true, args); + } + throw new IllegalArgumentException("unsupported function '" + function.getName() + "'"); + } +} diff --git a/codex-api/src/main/java/studio/magemonkey/codex/util/eval/ExpressionNode.java b/codex-api/src/main/java/studio/magemonkey/codex/util/eval/ExpressionNode.java new file mode 100644 index 00000000..fbe72bde --- /dev/null +++ b/codex-api/src/main/java/studio/magemonkey/codex/util/eval/ExpressionNode.java @@ -0,0 +1,123 @@ +package studio.magemonkey.codex.util.eval; + +import java.util.function.DoubleBinaryOperator; +import java.util.function.DoubleUnaryOperator; + +/** + * One node of a compiled expression tree. Nodes are immutable and hold no per-call state, so a tree can be + * evaluated from any number of threads at once and evaluating it allocates nothing. + *

+ * Every arithmetic rule here is a copy of the matching branch in + * {@link studio.magemonkey.codex.util.eval.javaluator.DoubleEvaluator}; the differential tests keep the two in step. + */ +abstract class ExpressionNode { + + /** + * @param values the values of the variables, in the order they were given to the compiler. Never modified. + */ + abstract double eval(double[] values); + + static final class Constant extends ExpressionNode { + private final double value; + + Constant(double value) { + this.value = value; + } + + @Override + double eval(double[] values) { + return value; + } + } + + static final class Variable extends ExpressionNode { + private final int index; + + Variable(int index) { + this.index = index; + } + + @Override + double eval(double[] values) { + return values[index]; + } + } + + static final class Unary extends ExpressionNode { + private final DoubleUnaryOperator op; + private final ExpressionNode operand; + + Unary(DoubleUnaryOperator op, ExpressionNode operand) { + this.op = op; + this.operand = operand; + } + + @Override + double eval(double[] values) { + return op.applyAsDouble(operand.eval(values)); + } + } + + static final class Binary extends ExpressionNode { + private final DoubleBinaryOperator op; + private final ExpressionNode left; + private final ExpressionNode right; + + Binary(DoubleBinaryOperator op, ExpressionNode left, ExpressionNode right) { + this.op = op; + this.left = left; + this.right = right; + } + + @Override + double eval(double[] values) { + // Left before right, like the operand order of the string evaluator. + double l = left.eval(values); + double r = right.eval(values); + return op.applyAsDouble(l, r); + } + } + + /** + * A function over any number of arguments, folded from the left. {@code seed} is used for functions that start + * from a fixed value ({@code sum}, {@code avg}) instead of from the first argument. + */ + static final class Fold extends ExpressionNode { + private final DoubleBinaryOperator op; + private final boolean seeded; + private final boolean average; + private final ExpressionNode[] args; + + Fold(DoubleBinaryOperator op, boolean seeded, boolean average, ExpressionNode[] args) { + this.op = op; + this.seeded = seeded; + this.average = average; + this.args = args; + } + + @Override + double eval(double[] values) { + double result; + int from; + if (seeded) { + result = 0.; + from = 0; + } else { + result = args[0].eval(values); + from = 1; + } + for (int i = from; i < args.length; i++) { + result = op.applyAsDouble(result, args[i].eval(values)); + } + return average ? result / args.length : result; + } + } + + /** {@code random()}: a fresh {@link Math#random()} on every call, like the string evaluator. */ + static final class Random extends ExpressionNode { + @Override + double eval(double[] values) { + return Math.random(); + } + } +} diff --git a/codex-api/src/test/java/studio/magemonkey/codex/util/eval/CompiledExpressionTest.java b/codex-api/src/test/java/studio/magemonkey/codex/util/eval/CompiledExpressionTest.java new file mode 100644 index 00000000..9976a556 --- /dev/null +++ b/codex-api/src/test/java/studio/magemonkey/codex/util/eval/CompiledExpressionTest.java @@ -0,0 +1,607 @@ +package studio.magemonkey.codex.util.eval; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; +import studio.magemonkey.codex.util.eval.javaluator.DoubleEvaluator; +import studio.magemonkey.codex.util.eval.javaluator.Function; +import studio.magemonkey.codex.util.eval.javaluator.StaticVariableSet; + +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; + +class CompiledExpressionTest { + private static final String[] NAMES = {"d", "x", "t"}; + + /** Value sets for d, x, t: ordinary, zero, negative, extreme, infinite, negative zero and NaN inputs. */ + private static final double[][] VALUE_SETS = { + {10, 2, 3}, + {0, 0, 0}, + {-5.5, 7.25, -8}, + {100, 4, 0}, + {1e300, 1e-300, 1}, + {Double.MAX_VALUE, Double.MIN_VALUE, -1}, + {Double.POSITIVE_INFINITY, -1, 0.5}, + {-0.0, 0.0, -0.0}, + {0.1, 0.2, 0.3}, + {Double.NaN, 1, 2}, + {37.5, 0.5, 1234.5678}, + {-1, -1, -1}, + }; + + private static final String FORMULA = "max(d / 5, d - 4 * x / max(1, t + 8))"; + + private static final String[] EXPRESSIONS = { + // + - * / and associativity + "d+x+t", "d-x-t", "d-x+t", "d+x-t", "d/x/t", "d/x*t", "d*x/t", "d-x*t", "d+x*t", "d*x-t", "d/x-t", + "5-3-2", "100/10/5", "d-x-t-d-x-t", "d/x/t/d/x", + // brackets + "(d+x)*t", "d*(x+t)*(d-t)", "((((d))))", "d-(x-t)", "d/(x/t)", "(d-x)-t", "((d+x)*(t-d))/(x+t)", + // exponent: javaluator makes ^ left associative, and binds tighter than unary minus + "d^x", "d^x^t", "2^3^2", "2^(-1)", "-d^2", "(-d)^2", "-d^(-x)", "x^0.5", "0^0", "-x^t^d", "d*x^t", "d^x*t", + // modulo + "d%x", "d%x%t", "d*x%t", "-d%x", "d%0", "d+x%t", + // unary minus + "-d", "--d", "-(-d)", "d- -x", "d--x", "d+-x", "2*-d", "-d*-x", "-(d+x)*t", "-0", "0*-d", "-x-t", "-(-(-d))", + // constants + "pi", "e", "pi*d", "e^x", "pi/e", "-pi", "pi-e-d", + // functions + "abs(x)", "abs(-d)", "min(d,x)", "max(d,x)", "max(d,x,t)", "min(d,x,t,1,2,3)", "max(max(d,x),min(x,t))", + "min(d)", "max(d)", "sum(d,x,t)", "avg(d,x,t)", "sum(d)", "avg(d)", "sum(-0)", "min(-0,0)", "max(-0,0)", + "round(d/3)", "round(-d/3)", "round(x*1000/7)", "ceil(x/3)", "floor(-x/3)", "ceil(d)", "floor(d)", + "sqrt(abs(d))", "sin(d)+cos(x)", "tan(t)", "asin(x/d)", "acos(x/d)", "atan(d)", "sinh(x)", "cosh(x)", + "tanh(x)", "ln(abs(d)+1)", "log(abs(d)+1)", "sqrt(d*d+x*x)", "min(max(d,x)-t,abs(min(-d,x*t)))", + "round(0/0)", "round(d/0)", "max(0/0,1)", + // whitespace + " d + x ", "d\t+\tx", "max( d , x )", "( d )", " - d ", "max ( d , x )", + // numbers + "1000", "0.5*d", ".5*d", "5.*d", "007+d", "1234567.891*d", "123456789012345678901234567890*d", + "0.1+0.2", "3.141592653589793238*d", "9007199254740993+d", "0.30000000000000004*d", + "100000000000000000000000000000000000000000000000000*d", "0.000000000000000000000000000001*d", "0", + // the damage formula and spellings of it + FORMULA, "max(d/5,d-4*x/max(1,t+8))", "max(d / 5,\td - 4 * x / max( 1 , t + 8 ))", + "d - 4 * x / max(1, t + 8)", "max(d/5, (d - 4*x) / max(1, t+8))", + }; + + static Stream expressions() { + return Arrays.stream(EXPRESSIONS).map(Arguments::of); + } + + // --------------------------------------------------------------------------------------------------------------- + // Reference: DoubleEvaluator with a StaticVariableSet + // --------------------------------------------------------------------------------------------------------------- + + private static final DoubleEvaluator REFERENCE = new DoubleEvaluator(); + + /** DoubleEvaluator's result, or null if it threw because a function returned NaN. Any other failure propagates. */ + private static Double reference(String expression, String[] names, double[] values) { + StaticVariableSet vars = new StaticVariableSet<>(); + for (int i = 0; i < names.length; i++) { + vars.set(names[i], values[i]); + } + try { + return REFERENCE.evaluate(expression, vars); + } catch (IllegalArgumentException e) { + if (e.getMessage() != null && e.getMessage().startsWith("Invalid argument passed to ")) { + return null; + } + throw e; + } + } + + private static String bits(double v) { + return v + " (0x" + Long.toHexString(Double.doubleToRawLongBits(v)) + ")"; + } + + /** + * Asserts the compiled form equals DoubleEvaluator bit for bit, wherever DoubleEvaluator gives a result. + */ + private static void assertSameAsReference(CompiledExpression ce, String expression, String[] names, double[] values) { + Double expected = reference(expression, names, values); + double actual = ce.eval(values); + String message = "'" + expression + "' with " + Arrays.toString(values); + if (expected == null) { + // DoubleEvaluator threw on a NaN function result. The compiled form carries on (round(NaN) is 0, for + // one), so there is nothing to compare; it only must not throw. + return; + } else { + // doubleToLongBits folds every NaN into one value; the sign of a NaN depends on how it was produced. + assertEquals(Double.doubleToLongBits(expected), Double.doubleToLongBits(actual), + message + ": expected " + bits(expected) + " but got " + bits(actual)); + } + } + + // --------------------------------------------------------------------------------------------------------------- + // Differential tests + // --------------------------------------------------------------------------------------------------------------- + + @Test + void tableHasEnoughExpressions() { + assertTrue(new HashSet<>(Arrays.asList(EXPRESSIONS)).size() >= 40); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("expressions") + void matchesDoubleEvaluatorBitForBit(String expression) { + CompiledExpression ce = Evaluator.compile(expression, NAMES); + for (double[] values : VALUE_SETS) { + assertSameAsReference(ce, expression, NAMES, values); + } + } + + @ParameterizedTest(name = "{0}") + @MethodSource("expressions") + void matchesWithVariablesInAnotherOrder(String expression) { + // Position-to-name resolution has to follow the given list, not alphabetical or first-use order. + String[] reversed = {"t", "x", "d"}; + CompiledExpression ce = Evaluator.compile(expression, reversed); + for (double[] values : VALUE_SETS) { + double[] swapped = {values[2], values[1], values[0]}; // t, x, d + assertSameAsReference(ce, expression, reversed, swapped); + } + } + + @Test + void damageFormulaMatchesHandWrittenJava() { + CompiledExpression ce = Evaluator.compile(FORMULA, "d", "x", "t"); + for (double[] v : VALUE_SETS) { + double d = v[0], x = v[1], t = v[2]; + double expected = Math.max(d / 5, d - 4 * x / Math.max(1, t + 8)); + assertEquals(Double.doubleToLongBits(expected), Double.doubleToLongBits(ce.eval(d, x, t)), + Arrays.toString(v)); + } + } + + @ParameterizedTest + @ValueSource(strings = {"2^-1", "-d^-x", "d^-1"}) + void unaryMinusRightAfterExponentIsRejectedLikeDoubleEvaluatorDoes(String expression) { + // The shared parser pops "^" when it meets a unary minus (lower precedence), leaving it one operand short. + assertThrows(RuntimeException.class, () -> reference(expression, NAMES, VALUE_SETS[0])); + assertMessage(compileFails(expression, NAMES), expression); + } + + @Test + void variablesNotUsedByTheExpressionAreFine() { + CompiledExpression ce = Evaluator.compile("x * 2", "d", "x", "t"); + assertEquals(14, ce.eval(1, 7, 3)); + } + + @Test + void expressionWithoutVariables() { + CompiledExpression ce = Evaluator.compile("2 + 3 * 4"); + assertEquals(14, ce.eval()); + assertTrue(ce.getVariableNames().isEmpty()); + } + + @Test + void longVariableNames() { + CompiledExpression ce = Evaluator.compile("damage_taken - armor2 * Armor", "damage_taken", "armor2", "Armor"); + assertEquals(10 - 3 * 4, ce.eval(10, 3, 4)); + } + + @Test + void variableNamesAreCaseSensitiveLikeFunctions() { + assertEquals(5, Evaluator.compile("D + x", "D", "x").eval(2, 3)); + assertThrows(IllegalArgumentException.class, () -> Evaluator.compile("D + x", "d", "x")); + } + + @Test + void everyFunctionOfDoubleEvaluatorIsSupported() { + // If a function is ever added to DoubleEvaluator, this fails until the compiled form learns it too. + for (Function f : REFERENCE.getFunctions()) { + int args = Math.max(f.getMinimumArgumentCount(), 1); + if (f.getMaximumArgumentCount() == 0) { + args = 0; + } + StringBuilder sb = new StringBuilder(f.getName()).append('('); + for (int i = 0; i < args; i++) { + sb.append(i == 0 ? "" : ",").append(i == 0 ? "d" : "x"); + } + String expression = sb.append(')').toString(); + if (f.getMaximumArgumentCount() == Integer.MAX_VALUE) { + expression = f.getName() + "(d,x,t)"; + } + CompiledExpression ce = Evaluator.compile(expression, NAMES); + if (!f.getName().equals("random")) { + for (double[] values : VALUE_SETS) { + assertSameAsReference(ce, expression, NAMES, values); + } + } + } + } + + @Test + void everyConstantOfDoubleEvaluatorIsSupported() { + REFERENCE.getConstants().forEach(c -> { + CompiledExpression ce = Evaluator.compile(c.getName()); + assertEquals(Double.doubleToLongBits(REFERENCE.evaluate(c.getName())), Double.doubleToLongBits(ce.eval())); + }); + } + + @Test + void everyOperatorOfDoubleEvaluatorIsSupported() { + REFERENCE.getOperators().forEach(op -> { + String expression = op.getOperandCount() == 1 ? op.getSymbol() + "d" : "d " + op.getSymbol() + " x"; + CompiledExpression ce = Evaluator.compile(expression, NAMES); + for (double[] values : VALUE_SETS) { + assertSameAsReference(ce, expression, NAMES, values); + } + }); + } + + @Test + void randomGivesANewNumberEachCall() { + CompiledExpression ce = Evaluator.compile("random()"); + Set seen = new HashSet<>(); + for (int i = 0; i < 100; i++) { + double v = ce.eval(); + assertTrue(v >= 0 && v < 1, "random() out of range: " + v); + seen.add(v); + } + assertTrue(seen.size() > 90, "random() should not repeat: " + seen.size()); + double scaled = Evaluator.compile("random() * 10 + d", "d").eval(100); + assertTrue(scaled >= 100 && scaled < 110); + } + + // --------------------------------------------------------------------------------------------------------------- + // Seeded random expressions + // --------------------------------------------------------------------------------------------------------------- + + private static final String[] UNARY_FUNCTIONS = {"abs", "sqrt", "ceil", "floor", "round", "sin", "cos", "tan", + "atan", "asin", "acos", "sinh", "cosh", "tanh", "ln", "log"}; + private static final String[] VARIADIC = {"min", "max", "sum", "avg"}; + private static final String[] OPERATORS = {"+", "-", "*", "/", "^", "%"}; + + private static String ws(Random r) { + switch (r.nextInt(4)) { + case 0: + return " "; + case 1: + return " "; + case 2: + return "\t"; + default: + return ""; + } + } + + private static String number(Random r) { + switch (r.nextInt(6)) { + case 0: + return String.valueOf(r.nextInt(10)); + case 1: + return String.valueOf(r.nextInt(1000)); + case 2: + return r.nextInt(100) + "." + r.nextInt(1000); + case 3: + return "." + r.nextInt(100); + case 4: + return r.nextInt(100) + "."; + default: + return String.valueOf(r.nextLong() >>> r.nextInt(64)); + } + } + + private static String atom(Random r) { + switch (r.nextInt(7)) { + case 0: + case 1: + return NAMES[r.nextInt(NAMES.length)]; + case 2: + return r.nextBoolean() ? "pi" : "e"; + default: + return number(r); + } + } + + private static String randomExpression(Random r, int depth) { + if (depth <= 0 || r.nextInt(5) == 0) { + return (r.nextInt(6) == 0 ? "-" + ws(r) : "") + atom(r); + } + switch (r.nextInt(7)) { + case 0: + case 1: + case 2: { + String left = randomExpression(r, depth - 1); + String right = randomExpression(r, depth - 1); + return left + ws(r) + OPERATORS[r.nextInt(OPERATORS.length)] + ws(r) + right; + } + case 3: + return "(" + ws(r) + randomExpression(r, depth - 1) + ws(r) + ")"; + case 4: + return "-" + ws(r) + "(" + randomExpression(r, depth - 1) + ")"; + case 5: + return UNARY_FUNCTIONS[r.nextInt(UNARY_FUNCTIONS.length)] + ws(r) + "(" + ws(r) + + randomExpression(r, depth - 1) + ws(r) + ")"; + default: { + int n = 1 + r.nextInt(4); + StringBuilder sb = new StringBuilder(VARIADIC[r.nextInt(VARIADIC.length)]).append('('); + for (int i = 0; i < n; i++) { + sb.append(i == 0 ? "" : "," + ws(r)).append(randomExpression(r, depth - 1)); + } + return sb.append(')').toString(); + } + } + } + + @Test + void seededRandomExpressionsMatchDoubleEvaluator() { + Random r = new Random(0xC0DE_2_1DL); + int compared = 0, nanFunction = 0; + for (int i = 0; i < 4000; i++) { + String expression = randomExpression(r, 1 + r.nextInt(5)); + CompiledExpression ce; + try { + ce = Evaluator.compile(expression, NAMES); + } catch (IllegalArgumentException e) { + // The string evaluator must reject exactly what the compiler rejects. + StaticVariableSet vars = new StaticVariableSet<>(); + for (int k = 0; k < NAMES.length; k++) { + vars.set(NAMES[k], VALUE_SETS[0][k]); + } + // Any exception counts: an expression that is malformed, or whose first NaN function result it rejects. + assertThrows(RuntimeException.class, () -> REFERENCE.evaluate(expression, vars), expression); + continue; + } + for (double[] values : VALUE_SETS) { + if (reference(expression, NAMES, values) == null) { + nanFunction++; + } + assertSameAsReference(ce, expression, NAMES, values); + compared++; + } + } + // Guard against a generator that quietly stops producing valid input. + assertTrue(compared > 40_000, "only " + compared + " comparisons ran"); + assertTrue(nanFunction > 0, "the NaN-function path was never exercised"); + } + + // --------------------------------------------------------------------------------------------------------------- + // Errors + // --------------------------------------------------------------------------------------------------------------- + + private static IllegalArgumentException compileFails(String expression, String... names) { + return assertThrows(IllegalArgumentException.class, () -> Evaluator.compile(expression, names), expression); + } + + private static void assertMessage(IllegalArgumentException e, String expression, String... fragments) { + String message = e.getMessage(); + assertNotNull(message); + assertTrue(message.contains("'" + expression + "'"), "message should name the expression: " + message); + assertFalse(message.contains("\n"), "message should be one line: " + message); + for (String fragment : fragments) { + assertTrue(message.contains(fragment), "message should mention '" + fragment + "': " + message); + } + } + + @ParameterizedTest + @ValueSource(strings = {"1 +", "* 2", "d d", "d x", "1 2", "d + * x", "d +", "()", "", " ", "d,x", "(d,x)", ",", + "max(,)", "max(d,)", "max(,d)", "d ^", "- ", "1..2", "1.2.3", "5d"}) + void syntaxErrorsAreRejected(String expression) { + IllegalArgumentException e = compileFails(expression, "d", "x"); + assertMessage(e, expression); + // And DoubleEvaluator rejects the same input, so nothing valid is turned away. + StaticVariableSet vars = new StaticVariableSet<>(); + vars.set("d", 1.0); + vars.set("x", 2.0); + assertThrows(RuntimeException.class, () -> REFERENCE.evaluate(expression, vars)); + } + + @Test + void syntaxErrorMessageSaysWhatIsWrong() { + assertMessage(compileFails("d + * x", "d", "x"), "d + * x"); + assertMessage(compileFails("1.2.3"), "1.2.3", "invalid number '1.2.3'"); + assertMessage(compileFails("d d", "d"), "d d", "unknown variable or function 'd d'"); + } + + @Test + void unknownVariableIsRejectedAndNamed() { + IllegalArgumentException e = compileFails("d + y", "d"); + assertMessage(e, "d + y", "unknown variable or function 'y'"); + assertTrue(e.getMessage().contains("(at position "), e.getMessage()); + } + + @Test + void unknownFunctionIsRejectedAndNamed() { + assertMessage(compileFails("foo(d) + 1", "d"), "foo(d) + 1", "unknown variable or function 'foo'"); + assertMessage(compileFails("MAX(d, 1)", "d"), "MAX(d, 1)", "'MAX'"); + } + + @Test + void variableUsedWithoutBeingDeclared() { + assertMessage(compileFails("d * x"), "d * x", "'d'"); + } + + @Test + void mismatchedBracketsAreRejected() { + assertMessage(compileFails("(d + 1", "d"), "(d + 1", "mismatched"); + assertMessage(compileFails("d + 1)", "d"), "d + 1)", "mismatched"); + assertMessage(compileFails("max(d, 1", "d"), "max(d, 1", "mismatched"); + assertMessage(compileFails("((d)", "d"), "((d)", "mismatched"); + assertMessage(compileFails(")(", "d"), ")(", "close bracket"); + assertMessage(compileFails("max(d, 1))", "d"), "max(d, 1))", "mismatched"); + } + + @Test + void wrongArgumentCountIsRejected() { + assertMessage(compileFails("max()", "d"), "max()", "max", "at least 1", "got 0"); + assertMessage(compileFails("min()", "d"), "min()", "min", "got 0"); + assertMessage(compileFails("sin(d, 1)", "d"), "sin(d, 1)", "sin", "expected 1", "got 2"); + assertMessage(compileFails("abs()", "d"), "abs()", "abs", "expected 1", "got 0"); + assertMessage(compileFails("random(d)", "d"), "random(d)", "random", "expected 0", "got 1"); + assertMessage(compileFails("sqrt(1, 2, 3)", "d"), "sqrt(1, 2, 3)", "sqrt", "got 3"); + assertMessage(compileFails("1 + max(d, d) + atan(d, d)", "d"), "1 + max(d, d) + atan(d, d)", "atan", "got 2"); + } + + @Test + void variableNamesThatCollideWithFunctionsOrConstantsAreRejected() { + assertMessage(compileFails("1", "max"), "1", "'max'", "function"); + assertMessage(compileFails("1", "d", "abs"), "1", "'abs'", "function"); + assertMessage(compileFails("1", "pi"), "1", "'pi'", "constant"); + assertMessage(compileFails("1", "e"), "1", "'e'", "constant"); + for (Function f : REFERENCE.getFunctions()) { + assertMessage(compileFails("1", f.getName()), "1", "'" + f.getName() + "'"); + } + REFERENCE.getConstants().forEach(c -> assertMessage(compileFails("1", c.getName()), "1", "'" + c.getName() + "'")); + } + + @Test + void unusableVariableNamesAreRejected() { + assertMessage(compileFails("1", ""), "1", "empty"); + assertMessage(compileFails("1", " "), "1", "empty"); + assertMessage(compileFails("1", " d"), "1", "whitespace"); + assertMessage(compileFails("1", "a b"), "1", "'a b'"); + assertMessage(compileFails("1", "a+b"), "1", "'a+b'", "operator"); + assertMessage(compileFails("1", "-"), "1", "operator"); + assertMessage(compileFails("1", "a(b"), "1", "bracket"); + assertMessage(compileFails("1", "a,b"), "1", "'a,b'"); + assertMessage(compileFails("1", "5"), "1", "'5'", "number"); + assertMessage(compileFails("1", "1.5"), "1", "number"); + assertMessage(compileFails("1", (String) null), "1", "null"); + assertMessage(compileFails("1", "d", "x", "d"), "1", "'d'", "twice"); + } + + @Test + void nullInputsAreRejected() { + assertThrows(IllegalArgumentException.class, () -> Evaluator.compile(null, "d")); + assertThrows(IllegalArgumentException.class, () -> Evaluator.compile("1", (String[]) null)); + } + + @Test + void wrongNumberOfValuesIsRejected() { + CompiledExpression ce = Evaluator.compile("d + x", "d", "x"); + IllegalArgumentException few = assertThrows(IllegalArgumentException.class, () -> ce.eval(1)); + assertTrue(few.getMessage().contains("d + x") && few.getMessage().contains("2") && few.getMessage().contains("got 1"), + few.getMessage()); + IllegalArgumentException many = assertThrows(IllegalArgumentException.class, () -> ce.eval(1, 2, 3)); + assertTrue(many.getMessage().contains("got 3"), many.getMessage()); + assertThrows(IllegalArgumentException.class, ce::eval); + assertThrows(IllegalArgumentException.class, () -> Evaluator.compile("1").eval(1)); + } + + // --------------------------------------------------------------------------------------------------------------- + // NaN, infinity, getters, immutability + // --------------------------------------------------------------------------------------------------------------- + + @Test + void nanAndInfinityAreReturnedAsTheyAre() { + assertTrue(Double.isNaN(Evaluator.compile("sqrt(d)", "d").eval(-1))); + assertTrue(Double.isNaN(Evaluator.compile("0 / d", "d").eval(0))); + assertTrue(Double.isNaN(Evaluator.compile("ln(d)", "d").eval(-1))); + assertTrue(Double.isNaN(Evaluator.compile("max(d, 1)", "d").eval(Double.NaN))); + assertEquals(Double.POSITIVE_INFINITY, Evaluator.compile("1 / d", "d").eval(0)); + assertEquals(Double.NEGATIVE_INFINITY, Evaluator.compile("-1 / d", "d").eval(0)); + assertEquals(Double.POSITIVE_INFINITY, Evaluator.compile("d ^ 400", "d").eval(10)); + // DoubleEvaluator refuses the first one, which is why the compiled form is documented to differ here. + assertNull(reference("sqrt(d)", new String[]{"d"}, new double[]{-1})); + } + + @Test + void gettersReturnSourceAndNames() { + CompiledExpression ce = Evaluator.compile(FORMULA, "d", "x", "t"); + assertEquals(FORMULA, ce.getSource()); + assertEquals(Arrays.asList("d", "x", "t"), ce.getVariableNames()); + assertThrows(UnsupportedOperationException.class, () -> ce.getVariableNames().add("y")); + assertTrue(ce.toString().contains(FORMULA), ce.toString()); + } + + @Test + void changingTheNamesArrayAfterCompileChangesNothing() { + String[] names = {"d", "x"}; + CompiledExpression ce = Evaluator.compile("d - x", names); + names[0] = "x"; + names[1] = "d"; + assertEquals(Arrays.asList("d", "x"), ce.getVariableNames()); + assertEquals(5, ce.eval(8, 3)); + } + + @Test + void evalDoesNotModifyTheValues() { + CompiledExpression ce = Evaluator.compile(FORMULA, "d", "x", "t"); + double[] values = {10, 2, 3}; + ce.eval(values); + assertArrayEquals(new double[]{10, 2, 3}, values); + } + + @Test + void evaluatorEvalIsUnchanged() { + assertEquals(14, Evaluator.eval("2 + 3 * 4", 1)); + assertEquals(14, Evaluator.eval("2 + 3 * 4", 0)); + } + + // --------------------------------------------------------------------------------------------------------------- + // Concurrency + // --------------------------------------------------------------------------------------------------------------- + + @Test + void sameInstanceCanBeEvaluatedFromManyThreads() throws Exception { + CompiledExpression ce = Evaluator.compile(FORMULA, "d", "x", "t"); + int threads = 8; + int rounds = 100_000; + ExecutorService pool = Executors.newFixedThreadPool(threads); + CountDownLatch start = new CountDownLatch(1); + AtomicInteger wrong = new AtomicInteger(); + try { + List> futures = new ArrayList<>(); + for (int th = 0; th < threads; th++) { + final int id = th; + futures.add(pool.submit(() -> { + Random r = new Random(id * 7919L); + start.await(); + for (int i = 0; i < rounds; i++) { + double d = r.nextInt(1000) + id, x = r.nextInt(50) - 10 * id, t = r.nextInt(200) - 20; + double expected = Math.max(d / 5, d - 4 * x / Math.max(1, t + 8)); + if (Double.doubleToLongBits(expected) != Double.doubleToLongBits(ce.eval(d, x, t))) { + wrong.incrementAndGet(); + } + } + return null; + })); + } + start.countDown(); + for (Future f : futures) { + f.get(60, TimeUnit.SECONDS); + } + } finally { + pool.shutdownNow(); + } + assertEquals(0, wrong.get(), "evaluations that gave a wrong value under concurrency"); + } + + @Test + void compilingFromManyThreadsAtOnce() throws Exception { + int threads = 8; + ExecutorService pool = Executors.newFixedThreadPool(threads); + CountDownLatch start = new CountDownLatch(1); + try { + List> futures = new ArrayList<>(); + for (int th = 0; th < threads; th++) { + final int id = th; + futures.add(pool.submit(() -> { + start.await(); + double sum = 0; + for (int i = 0; i < 2000; i++) { + sum += Evaluator.compile("d * " + id + " + max(d, " + i + ")", "d").eval(2); + } + return sum; + })); + } + start.countDown(); + for (int th = 0; th < threads; th++) { + double expected = 0; + for (int i = 0; i < 2000; i++) { + expected += 2 * th + Math.max(2, i); + } + assertEquals(expected, futures.get(th).get(60, TimeUnit.SECONDS)); + } + } finally { + pool.shutdownNow(); + } + } +} diff --git a/codex-api/src/test/java/studio/magemonkey/codex/util/eval/ExpressionBenchmark.java b/codex-api/src/test/java/studio/magemonkey/codex/util/eval/ExpressionBenchmark.java new file mode 100644 index 00000000..cdf8e0b2 --- /dev/null +++ b/codex-api/src/test/java/studio/magemonkey/codex/util/eval/ExpressionBenchmark.java @@ -0,0 +1,103 @@ +package studio.magemonkey.codex.util.eval; + +import studio.magemonkey.codex.util.eval.javaluator.DoubleEvaluator; +import studio.magemonkey.codex.util.eval.javaluator.StaticVariableSet; + +import java.util.Arrays; + +/** + * Compares three ways of computing the damage formula: the string evaluator, a {@link CompiledExpression}, and the + * same arithmetic written in Java. Not a JUnit test on purpose: run it with + *

+ * java -cp codex-api/target/classes:codex-api/target/test-classes:<spigot-api and deps> \
+ *      studio.magemonkey.codex.util.eval.ExpressionBenchmark [string|compiled|java] [rounds] [iterations]
+ * 
+ * Run one mode per JVM so the JIT profile of one does not shape another. {@code ExpressionBenchmarkTest} runs a tiny + * pass of it in every build to check the three agree. + */ +public final class ExpressionBenchmark { + static final String FORMULA = "max(d / 5, d - 4 * x / max(1, t + 8))"; + + private static final int INPUTS = 1024; // power of two, so the index wraps with a mask + + private ExpressionBenchmark() { + } + + static double handWritten(double d, double x, double t) { + return Math.max(d / 5, d - 4 * x / Math.max(1, t + 8)); + } + + /** The values every mode evaluates, so their sums can be compared. */ + static double[][] inputs() { + double[][] in = new double[3][INPUTS]; + java.util.Random r = new java.util.Random(42); + for (int i = 0; i < INPUTS; i++) { + in[0][i] = 1 + r.nextInt(2000) / 10.0; // damage + in[1][i] = r.nextInt(200) / 4.0; // x + in[2][i] = r.nextInt(120); // toughness + } + return in; + } + + /** One pass of {@code iterations} evaluations. Returns the sum of results (which also keeps the JIT honest). */ + static double run(String mode, int iterations, double[][] in) { + double sum = 0; + switch (mode) { + case "string": { + DoubleEvaluator evaluator = new DoubleEvaluator(); + StaticVariableSet vars = new StaticVariableSet<>(); + for (int i = 0; i < iterations; i++) { + int k = i & (INPUTS - 1); + vars.set("d", in[0][k]); + vars.set("x", in[1][k]); + vars.set("t", in[2][k]); + sum += evaluator.evaluate(FORMULA, vars); + } + return sum; + } + case "compiled": { + CompiledExpression ce = Evaluator.compile(FORMULA, "d", "x", "t"); + for (int i = 0; i < iterations; i++) { + int k = i & (INPUTS - 1); + sum += ce.eval(in[0][k], in[1][k], in[2][k]); + } + return sum; + } + case "java": { + for (int i = 0; i < iterations; i++) { + int k = i & (INPUTS - 1); + sum += handWritten(in[0][k], in[1][k], in[2][k]); + } + return sum; + } + default: + throw new IllegalArgumentException("mode must be string, compiled or java"); + } + } + + private static double measure(String mode, int rounds, int iterations, double[][] in) { + double[] nanos = new double[rounds]; + double sink = 0; + for (int r = 0; r < rounds; r++) { + long start = System.nanoTime(); + sink += run(mode, iterations, in); + nanos[r] = (System.nanoTime() - start) / (double) iterations; + } + Arrays.sort(nanos); + System.out.printf("%-9s median %9.2f ns/op min %9.2f max %9.2f (%d rounds x %,d ops, checksum %.6f)%n", + mode, nanos[rounds / 2], nanos[0], nanos[rounds - 1], rounds, iterations, sink); + return nanos[rounds / 2]; + } + + public static void main(String[] args) { + String mode = args.length > 0 ? args[0] : "compiled"; + int rounds = args.length > 1 ? Integer.parseInt(args[1]) : 15; + int iterations = args.length > 2 ? Integer.parseInt(args[2]) : "string".equals(mode) ? 200_000 : 20_000_000; + double[][] in = inputs(); + // Warm up: let the JIT compile everything before the rounds that count. + for (int i = 0; i < 5; i++) { + run(mode, iterations / 4, in); + } + measure(mode, rounds, iterations, in); + } +} diff --git a/codex-api/src/test/java/studio/magemonkey/codex/util/eval/ExpressionBenchmarkTest.java b/codex-api/src/test/java/studio/magemonkey/codex/util/eval/ExpressionBenchmarkTest.java new file mode 100644 index 00000000..e3e2aa82 --- /dev/null +++ b/codex-api/src/test/java/studio/magemonkey/codex/util/eval/ExpressionBenchmarkTest.java @@ -0,0 +1,18 @@ +package studio.magemonkey.codex.util.eval; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** Runs a short pass of {@link ExpressionBenchmark}; checks the three ways of computing the formula agree. No timing. */ +class ExpressionBenchmarkTest { + @Test + void allThreeModesComputeTheSameSum() { + double[][] in = ExpressionBenchmark.inputs(); + long string = Double.doubleToLongBits(ExpressionBenchmark.run("string", 3000, in)); + long compiled = Double.doubleToLongBits(ExpressionBenchmark.run("compiled", 3000, in)); + long java = Double.doubleToLongBits(ExpressionBenchmark.run("java", 3000, in)); + assertEquals(java, compiled); + assertEquals(java, string); + } +}