Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ private static CelMutableExpr newOptionalNoneExpr() {
}

@Override
// Internal optimization steps preserve the initial mutable AST instance if no mutations occur.
// Using == avoids deep .equals() comparison.
@SuppressWarnings("ReferenceEquality")
public OptimizationResult optimize(CelAbstractSyntaxTree ast, Cel cel)
throws CelOptimizationException {
CelBuilder builder = cel.toCelBuilder();
Expand All @@ -134,12 +137,17 @@ public OptimizationResult optimize(CelAbstractSyntaxTree ast, Cel cel)
// Override the environment's expected type to generally allow all subtrees to be folded.
Cel optimizerEnv = builder.setResultType(SimpleType.DYN).build();

CelMutableAst mutableAst = CelMutableAst.fromCelAst(ast);
ImmutableMap<String, CelType> identTypes = precomputeIdentTypes(mutableAst);
CelMutableAst initialMutableAst = CelMutableAst.fromCelAst(ast);
ImmutableMap<String, CelType> identTypes = precomputeIdentTypes(initialMutableAst);

mutableAst = foldConstants(optimizerEnv, valueProvider, identTypes, mutableAst);
CelMutableAst mutableAst =
foldConstants(optimizerEnv, valueProvider, identTypes, initialMutableAst);
mutableAst = pruneOptionalElements(mutableAst);

if (mutableAst == initialMutableAst) {
return OptimizationResult.create(ast);
}

return OptimizationResult.create(astMutator.renumberIdsConsecutively(mutableAst).toParsedAst());
}

Expand Down Expand Up @@ -735,10 +743,21 @@ private CelMutableAst pruneOptionalListElements(CelMutableAst mutableAst, CelMut
updatedIndicesBuilder.add(newOptIndex);
}

// An optional list is modified if:
// 1. An optional.none() was dropped - it this case, the updatedElements.size() decreases.
// 2. An optional.of(literal) was unwrapped into a regular element - in this case,
// updatedIndices.size() decreases.
// If both counts are unchanged, neither case occurred, and we can return the original AST.
ImmutableList<CelMutableExpr> updatedElements = updatedElemBuilder.build();
ImmutableList<Integer> updatedIndices = updatedIndicesBuilder.build();
if (updatedElements.size() == list.elements().size()
&& updatedIndices.size() == list.optionalIndices().size()) {
return mutableAst;
}

return astMutator.replaceSubtree(
mutableAst,
CelMutableExpr.ofList(
CelMutableList.create(updatedElemBuilder.build(), updatedIndicesBuilder.build())),
CelMutableExpr.ofList(CelMutableList.create(updatedElements, updatedIndices)),
expr.id());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ public static InliningOptimizer newInstance(
}

@Override
// Internal optimization steps preserve the initial mutable AST instance if no mutations occur.
// Using == avoids deep .equals() comparison.
@SuppressWarnings("ReferenceEquality")
public OptimizationResult optimize(CelAbstractSyntaxTree ast, Cel cel) {
CelMutableAst mutableAst = CelMutableAst.fromCelAst(ast);
CelMutableAst initialMutableAst = CelMutableAst.fromCelAst(ast);
CelMutableAst mutableAst = initialMutableAst;
for (InlineVariable inlineVariable : inlineVariables) {
mutableAst =
astMutator.mutateUntilFixedPoint(
Expand All @@ -125,6 +129,10 @@ public OptimizationResult optimize(CelAbstractSyntaxTree ast, Cel cel) {
});
}

if (mutableAst == initialMutableAst) {
return OptimizationResult.create(ast);
}

return OptimizationResult.create(astMutator.renumberIdsConsecutively(mutableAst).toParsedAst());
}

Expand Down
Loading