Skip to content

Commit 05cb0a1

Browse files
l46kokcopybara-github
authored andcommitted
Remove createStruct planinng overhead for type-checked ASTs
PiperOrigin-RevId: 892572698
1 parent cbe0104 commit 05cb0a1

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ private Optional<PlannedInterpretable> maybeInterceptOptionalCalls(
343343

344344
private PlannedInterpretable planCreateStruct(CelExpr celExpr, PlannerContext ctx) {
345345
CelStruct struct = celExpr.struct();
346-
CelType structType = resolveStructType(struct);
346+
CelType structType = resolveStructType(celExpr, ctx);
347347

348348
ImmutableList<Entry> entries = struct.entries();
349349
String[] keys = new String[entries.size()];
@@ -489,7 +489,17 @@ private ResolvedFunction resolveFunction(
489489
return ResolvedFunction.newBuilder().setFunctionName(functionName).setTarget(target).build();
490490
}
491491

492-
private CelType resolveStructType(CelStruct struct) {
492+
private CelType resolveStructType(CelExpr expr, PlannerContext ctx) {
493+
CelType checkedType = ctx.typeMap().get(expr.id());
494+
if (checkedType != null) {
495+
CelKind kind = checkedType.kind();
496+
// Type-checked ASTs do not need a type-provider lookup as long as it's of expected kind.
497+
if (isValidStructKind(kind)) {
498+
return checkedType;
499+
}
500+
}
501+
502+
CelStruct struct = expr.struct();
493503
String messageName = struct.messageName();
494504
for (String typeName : container.resolveCandidateNames(messageName)) {
495505
CelType structType = typeProvider.findType(typeName).orElse(null);
@@ -499,9 +509,7 @@ private CelType resolveStructType(CelStruct struct) {
499509

500510
CelKind kind = structType.kind();
501511

502-
if (!kind.equals(CelKind.STRUCT)
503-
&& !kind.equals(CelKind.TIMESTAMP)
504-
&& !kind.equals(CelKind.DURATION)) {
512+
if (!isValidStructKind(kind)) {
505513
throw new IllegalArgumentException(
506514
String.format(
507515
"Expected struct type for %s, got %s", structType.name(), structType.kind()));
@@ -513,6 +521,12 @@ private CelType resolveStructType(CelStruct struct) {
513521
throw new IllegalArgumentException("Undefined type name: " + messageName);
514522
}
515523

524+
private static boolean isValidStructKind(CelKind kind) {
525+
return kind.equals(CelKind.STRUCT)
526+
|| kind.equals(CelKind.TIMESTAMP)
527+
|| kind.equals(CelKind.DURATION);
528+
}
529+
516530
/** Converts a given expression into a qualified name, if possible. */
517531
private Optional<String> toQualifiedName(CelExpr operand) {
518532
switch (operand.getKind()) {

0 commit comments

Comments
 (0)