Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 197 additions & 0 deletions core/src/jmh/java/org/projectnessie/cel/EvaluatorBaselineBench.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* Copyright (C) 2021 The Authors of CEL-Java
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.projectnessie.cel;

import static org.projectnessie.cel.Env.newEnv;
import static org.projectnessie.cel.EnvOption.declarations;
import static org.projectnessie.cel.EnvOption.types;
import static org.projectnessie.cel.EvalOption.OptOptimize;
import static org.projectnessie.cel.ProgramOption.evalOptions;
import static org.projectnessie.cel.interpreter.Activation.newActivation;

import dev.cel.expr.conformance.proto3.TestAllTypes;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.projectnessie.cel.Env.AstIssuesTuple;
import org.projectnessie.cel.checker.Decls;
import org.projectnessie.cel.interpreter.Activation;

/** Attributes evaluator cost at the raw, public-program, and native-result boundaries. */
@Warmup(iterations = 3, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Fork(1)
@Threads(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class EvaluatorBaselineBench {

@State(Scope.Benchmark)
public static class EvaluationState {
@Param({
"identityInt",
"addInt",
"chainInt",
"chainDouble",
"chainString",
"shortCircuit",
"mapSelection",
"protoSelection"
})
public String expression;

Program program;
Prog internalProgram;
Map<String, Object> variables;
Activation activation;
Class<?> nativeResultType;
Supplier<Object> nativeJava;

@Setup
public void init() {
Env env;
String source;
switch (expression) {
case "identityInt":
env = newEnv(declarations(Decls.newVar("x", Decls.Int)));
source = "x";
variables = Map.of("x", 41L);
nativeResultType = Long.class;
nativeJava = () -> variables.get("x");
break;
case "addInt":
env = newEnv(declarations(Decls.newVar("x", Decls.Int)));
source = "x + 1";
variables = Map.of("x", 41L);
nativeResultType = Long.class;
nativeJava = () -> (long) variables.get("x") + 1L;
break;
case "chainInt":
env = newEnv(declarations(Decls.newVar("x", Decls.Int)));
source = "((x + 1) * 3 - 2) / 2";
variables = Map.of("x", 41L);
nativeResultType = Long.class;
nativeJava = () -> (((long) variables.get("x") + 1L) * 3L - 2L) / 2L;
break;
case "chainDouble":
env = newEnv(declarations(Decls.newVar("x", Decls.Double)));
source = "((x + 1.25) * 3.0 - 2.0) / 2.0";
variables = Map.of("x", 41.5d);
nativeResultType = Double.class;
nativeJava = () -> (((double) variables.get("x") + 1.25d) * 3.0d - 2.0d) / 2.0d;
break;
case "chainString":
env =
newEnv(
declarations(
Decls.newVar("prefix", Decls.String),
Decls.newVar("x", Decls.String),
Decls.newVar("suffix", Decls.String)));
source = "prefix + x + suffix";
variables = Map.of("prefix", "pre-", "x", "value", "suffix", "-post");
nativeResultType = String.class;
nativeJava =
() -> (String) variables.get("prefix") + variables.get("x") + variables.get("suffix");
break;
case "shortCircuit":
env =
newEnv(
declarations(
Decls.newVar("enabled", Decls.Bool), Decls.newVar("expensive", Decls.Bool)));
source = "enabled && expensive";
variables = Map.of("enabled", false, "expensive", true);
nativeResultType = Boolean.class;
nativeJava =
() -> (boolean) variables.get("enabled") && (boolean) variables.get("expensive");
break;
case "mapSelection":
env =
newEnv(
declarations(
Decls.newVar("attrs", Decls.newMapType(Decls.String, Decls.Int)),
Decls.newVar("key", Decls.String),
Decls.newVar("target", Decls.Int)));
source = "attrs[key] == target";
variables = Map.of("attrs", Map.of("answer", 42L), "key", "answer", "target", 42L);
nativeResultType = Boolean.class;
nativeJava =
() ->
((Map<?, ?>) variables.get("attrs"))
.get(variables.get("key"))
.equals(variables.get("target"));
break;
case "protoSelection":
env =
newEnv(
types(TestAllTypes.getDefaultInstance()),
declarations(
Decls.newVar(
"msg", Decls.newObjectType("cel.expr.conformance.proto3.TestAllTypes")),
Decls.newVar("target", Decls.Int)));
source = "msg.single_int64 == target";
TestAllTypes message = TestAllTypes.newBuilder().setSingleInt64(42L).build();
variables = Map.of("msg", message, "target", 42L);
nativeResultType = Boolean.class;
nativeJava = () -> message.getSingleInt64() == (long) variables.get("target");
break;
default:
throw new IllegalArgumentException(
"Unknown evaluator benchmark expression: " + expression);
}

AstIssuesTuple ast = env.compile(source);
if (ast.hasIssues()) {
throw ast.getIssues().err();
}
program = env.program(ast.getAst(), evalOptions(OptOptimize));
internalProgram = (Prog) program;
activation = newActivation(variables);
}
}

@Benchmark
public Object rawEvaluator(EvaluationState state) {
return state.internalProgram.interpretable.eval(state.activation);
}

@Benchmark
public Program.EvalResult programEval(EvaluationState state) {
return state.program.eval(state.variables);
}

@Benchmark
public Object programEvalNative(EvaluationState state) {
return state.internalProgram.e.adapter.valueToNative(
state.program.eval(state.variables).getVal(), state.nativeResultType);
}

@Benchmark
public Object nativeJava(EvaluationState state) {
return state.nativeJava.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
import org.projectnessie.cel.common.types.ref.TypeRegistry;
import org.projectnessie.cel.common.types.ref.Val;
import org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute;
import org.projectnessie.cel.interpreter.Interpretable.EvalAttr;
import org.projectnessie.cel.interpreter.Interpretable.EvalTestOnly;

@Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 200, timeUnit = TimeUnit.MILLISECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@
import org.projectnessie.cel.common.types.traits.Trait;
import org.projectnessie.cel.extension.MathLib;
import org.projectnessie.cel.extension.StringsLib;
import org.projectnessie.cel.interpreter.Interpretable.EvalBinary;
import org.projectnessie.cel.interpreter.Interpretable.EvalConst;
import org.projectnessie.cel.interpreter.Interpretable.EvalQuaternary;
import org.projectnessie.cel.interpreter.Interpretable.EvalQuinary;
import org.projectnessie.cel.interpreter.Interpretable.EvalReceiverVarArgs;
import org.projectnessie.cel.interpreter.Interpretable.EvalTernary;
import org.projectnessie.cel.interpreter.Interpretable.EvalUnary;
import org.projectnessie.cel.interpreter.Interpretable.EvalVarArgs;
import org.projectnessie.cel.interpreter.Interpretable.EvalZeroArity;
import org.projectnessie.cel.interpreter.functions.BinaryOp;
import org.projectnessie.cel.interpreter.functions.FunctionOp;
import org.projectnessie.cel.interpreter.functions.QuaternaryOp;
Expand Down
24 changes: 6 additions & 18 deletions core/src/main/java/org/projectnessie/cel/common/types/DoubleT.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.projectnessie.cel.common.types.Err.newTypeConversionError;
import static org.projectnessie.cel.common.types.Err.noSuchOverload;
import static org.projectnessie.cel.common.types.Err.rangeError;
import static org.projectnessie.cel.common.types.IntT.IntZero;
import static org.projectnessie.cel.common.types.IntT.intOf;
import static org.projectnessie.cel.common.types.IntT.intOfCompare;
import static org.projectnessie.cel.common.types.StringT.stringOf;
Expand Down Expand Up @@ -193,18 +192,11 @@ public Val divide(Val other) {
public Val compare(Val other) {
switch (other.type().typeEnum()) {
case Uint:
return intOfCompare(NumericComparison.compareDoubleUint(d, other.intValue()));
case Int:
return intOfCompare(NumericComparison.compareDoubleInt(d, other.intValue()));
case Double:
Val converted = other.convertToType(type());
if (converted.type().typeEnum() == TypeEnum.Err) {
return converted;
}
double od = ((DoubleT) converted).d;
if (d == od) {
// work around for special case of -0.0d == 0.0d (IEEE 754)
return IntZero;
}
return intOfCompare(Double.compare(d, od));
return intOfCompare(NumericComparison.compareDouble(d, other.doubleValue()));
default:
return noSuchOverload(this, "compare", other);
}
Expand All @@ -215,15 +207,11 @@ public Val compare(Val other) {
public Val equal(Val other) {
switch (other.type().typeEnum()) {
case Uint:
return boolOf(NumericComparison.equalDoubleUint(d, other.intValue()));
case Int:
return boolOf(NumericComparison.equalDoubleInt(d, other.intValue()));
case Double:
Val converted = other.convertToType(type());
if (converted.type().typeEnum() == TypeEnum.Err) {
return converted;
}
double o = ((DoubleT) converted).d;
// TODO: Handle NaNs properly.
return boolOf(d == o);
return boolOf(NumericComparison.equalDouble(d, other.doubleValue()));
case Null:
case Bool:
case Bytes:
Expand Down
63 changes: 13 additions & 50 deletions core/src/main/java/org/projectnessie/cel/common/types/IntT.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.projectnessie.cel.common.types;

import static org.projectnessie.cel.common.types.BoolT.False;
import static org.projectnessie.cel.common.types.DoubleT.DoubleType;
import static org.projectnessie.cel.common.types.DoubleT.doubleOf;
import static org.projectnessie.cel.common.types.Err.divideByZero;
import static org.projectnessie.cel.common.types.Err.errIntOverflow;
Expand Down Expand Up @@ -202,60 +201,24 @@ public Val convertToType(Type typeValue) {
/** Compare implements traits.Comparer.Compare. */
@Override
public Val compare(Val other) {
switch (other.type().typeEnum()) {
case Double:
DoubleT cmp = (DoubleT) convertToType(DoubleType);
return cmp.compare(other);
case Uint:
if (i < 0L) {
// this int is negative, so it MUST be smaller than any uint
return IntNegOne;
}
if (other.intValue() < 0L) {
// the OTHER uint is > Integer.MAX_VALUE, so THIS int MUST be smaller
return IntNegOne;
}
return intOfCompare(Long.compareUnsigned(i, other.intValue()));
case Int:
Val converted = other.convertToType(type());
if (converted.type().typeEnum() == TypeEnum.Err) {
return converted;
}
return intOfCompare(Long.compare(i, converted.intValue()));
default:
return noSuchOverload(this, "compare", other);
}
return switch (other.type().typeEnum()) {
case Double -> intOfCompare(NumericComparison.compareIntDouble(i, other.doubleValue()));
case Uint -> intOfCompare(NumericComparison.compareIntUint(i, other.intValue()));
case Int -> intOfCompare(NumericComparison.compareInt(i, other.intValue()));
default -> noSuchOverload(this, "compare", other);
};
}

/** Equal implements ref.Val.Equal. */
@Override
public Val equal(Val other) {
switch (other.type().typeEnum()) {
case Double:
DoubleT cmp = (DoubleT) convertToType(DoubleType);
return cmp.equal(other);
case Uint:
if (other.intValue() < 0L) {
return False;
}
case Int:
Val converted = other.convertToType(type());
if (converted.type().typeEnum() == TypeEnum.Err) {
return converted;
}
return boolOf(i == converted.intValue());
case Null:
case Bool:
case Bytes:
case List:
case Map:
case Object:
case String:
case Type:
return False;
default:
return noSuchOverload(this, "equal", other);
}
return switch (other.type().typeEnum()) {
case Double -> boolOf(NumericComparison.equalIntDouble(i, other.doubleValue()));
case Uint -> boolOf(NumericComparison.equalIntUint(i, other.intValue()));
case Int -> boolOf(NumericComparison.equalInt(i, other.intValue()));
case Null, Bool, Bytes, List, Map, Object, String, Type -> False;
default -> noSuchOverload(this, "equal", other);
};
}

/** Divide implements traits.Divider.Divide. */
Expand Down
Loading
Loading