Skip to content

Commit 0c5c9b7

Browse files
l46kokcopybara-github
authored andcommitted
Remove unused replaceSubtreeWithNewBindMacro method
PiperOrigin-RevId: 797498049
1 parent 37828da commit 0c5c9b7

17 files changed

Lines changed: 139 additions & 733 deletions

File tree

optimizer/src/main/java/dev/cel/optimizer/AstMutator.java

Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import dev.cel.common.CelAbstractSyntaxTree;
2929
import dev.cel.common.CelMutableAst;
3030
import dev.cel.common.CelMutableSource;
31-
import dev.cel.common.ast.CelConstant;
3231
import dev.cel.common.ast.CelExpr.ExprKind.Kind;
3332
import dev.cel.common.ast.CelExprIdGeneratorFactory;
3433
import dev.cel.common.ast.CelExprIdGeneratorFactory.ExprIdGenerator;
@@ -163,75 +162,6 @@ private CelMutableAst newCallAst(
163162
return CelMutableAst.of(newCallExpr, combinedSource);
164163
}
165164

166-
/**
167-
* Generates a new bind macro using the provided initialization and result expression, then
168-
* replaces the subtree using the new bind expr at the designated expr ID.
169-
*
170-
* <p>The bind call takes the format of: {@code cel.bind(varInit, varName, resultExpr)}
171-
*
172-
* @param ast Original AST to mutate.
173-
* @param varName New variable name for the bind macro call.
174-
* @param varInit Initialization expression to bind to the local variable.
175-
* @param resultExpr Result expression
176-
* @param exprIdToReplace Expression ID of the subtree that is getting replaced.
177-
* @param populateMacroSource If true, populates the cel.bind macro source in the AST.
178-
*/
179-
public CelMutableAst replaceSubtreeWithNewBindMacro(
180-
CelMutableAst ast,
181-
String varName,
182-
CelMutableAst varInit,
183-
CelMutableExpr resultExpr,
184-
long exprIdToReplace,
185-
boolean populateMacroSource) {
186-
// Stabilize incoming varInit AST to avoid collision with the main AST
187-
long maxId = getMaxId(ast);
188-
varInit = stabilizeAst(varInit, maxId);
189-
StableIdGenerator stableIdGenerator = CelExprIdGeneratorFactory.newStableIdGenerator(maxId);
190-
CelMutableExpr newBindMacroExpr =
191-
newBindMacroExpr(
192-
varName, varInit.expr(), CelMutableExpr.newInstance(resultExpr), stableIdGenerator);
193-
CelMutableSource celSource = CelMutableSource.newInstance();
194-
if (populateMacroSource) {
195-
CelMutableExpr newBindMacroSourceExpr =
196-
newBindMacroSourceExpr(newBindMacroExpr, varName, stableIdGenerator);
197-
// In situations where the existing AST already contains a macro call (ex: nested cel.binds),
198-
// its macro source must be normalized to make it consistent with the newly generated bind
199-
// macro.
200-
celSource = combine(ast.source(), varInit.source());
201-
celSource =
202-
normalizeMacroSource(
203-
celSource,
204-
-1, // Do not replace any of the subexpr in the macro map.
205-
newBindMacroSourceExpr,
206-
stableIdGenerator::renumberId);
207-
celSource.addMacroCalls(newBindMacroExpr.id(), newBindMacroSourceExpr);
208-
}
209-
210-
CelMutableAst newBindAst = CelMutableAst.of(newBindMacroExpr, celSource);
211-
212-
return replaceSubtree(ast, newBindAst, exprIdToReplace);
213-
}
214-
215-
/**
216-
* See {@link #replaceSubtreeWithNewBindMacro(CelMutableAst, String, CelMutableAst,
217-
* CelMutableExpr, long, boolean)}.
218-
*/
219-
public CelMutableAst replaceSubtreeWithNewBindMacro(
220-
CelMutableAst ast,
221-
String varName,
222-
CelMutableExpr varInit,
223-
CelMutableExpr resultExpr,
224-
long exprIdToReplace,
225-
boolean populateMacroSource) {
226-
return replaceSubtreeWithNewBindMacro(
227-
ast,
228-
varName,
229-
CelMutableAst.of(varInit, CelMutableSource.newInstance()),
230-
resultExpr,
231-
exprIdToReplace,
232-
populateMacroSource);
233-
}
234-
235165
/** Renumbers all the expr IDs in the given AST in a consecutive manner starting from 1. */
236166
public CelMutableAst renumberIdsConsecutively(CelMutableAst mutableAst) {
237167
StableIdGenerator stableIdGenerator = CelExprIdGeneratorFactory.newStableIdGenerator(0);
@@ -688,46 +618,6 @@ private CelMutableSource mangleIdentsInMacroSource(
688618
return newSource;
689619
}
690620

691-
private CelMutableExpr newBindMacroExpr(
692-
String varName,
693-
CelMutableExpr varInit,
694-
CelMutableExpr resultExpr,
695-
StableIdGenerator stableIdGenerator) {
696-
// Renumber incoming expression IDs in the init and result expression to avoid collision with
697-
// the main AST. Existing IDs are memoized for a macro source sanitization pass at the end
698-
// (e.g: inserting a bind macro to an existing macro expr)
699-
varInit = renumberExprIds(stableIdGenerator::nextExprId, varInit);
700-
resultExpr = renumberExprIds(stableIdGenerator::nextExprId, resultExpr);
701-
702-
long iterRangeId = stableIdGenerator.nextExprId();
703-
long loopConditionId = stableIdGenerator.nextExprId();
704-
long loopStepId = stableIdGenerator.nextExprId();
705-
long comprehensionId = stableIdGenerator.nextExprId();
706-
707-
return CelMutableExpr.ofComprehension(
708-
comprehensionId,
709-
CelMutableComprehension.create(
710-
"#unused",
711-
CelMutableExpr.ofList(iterRangeId, CelMutableList.create()),
712-
varName,
713-
varInit,
714-
CelMutableExpr.ofConstant(loopConditionId, CelConstant.ofValue(false)),
715-
CelMutableExpr.ofIdent(loopStepId, varName),
716-
resultExpr));
717-
}
718-
719-
private CelMutableExpr newBindMacroSourceExpr(
720-
CelMutableExpr bindMacroExpr, String varName, StableIdGenerator stableIdGenerator) {
721-
return CelMutableExpr.ofCall(
722-
0, // Required sentinel value for macro call
723-
CelMutableCall.create(
724-
CelMutableExpr.ofIdent(stableIdGenerator.nextExprId(), "cel"),
725-
"bind",
726-
CelMutableExpr.ofIdent(stableIdGenerator.nextExprId(), varName),
727-
bindMacroExpr.comprehension().accuInit(),
728-
bindMacroExpr.comprehension().result()));
729-
}
730-
731621
private static CelMutableSource combine(
732622
CelMutableSource celSource1, CelMutableSource celSource2) {
733623
return CelMutableSource.newInstance()
@@ -920,8 +810,7 @@ private static void unwrapListArgumentsInMacroCallExpr(
920810
CelMutableCall existingMacroCall = newMacroCallExpr.call();
921811
CelMutableCall newMacroCall =
922812
existingMacroCall.target().isPresent()
923-
? CelMutableCall.create(existingMacroCall.target().get(),
924-
existingMacroCall.function())
813+
? CelMutableCall.create(existingMacroCall.target().get(), existingMacroCall.function())
925814
: CelMutableCall.create(existingMacroCall.function());
926815
newMacroCall.addArgs(
927816
existingMacroCall.args().get(0)); // iter_var is first argument of the call by convention

optimizer/src/main/java/dev/cel/optimizer/optimizers/ConstantFoldingOptimizer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ private static boolean canFoldInOperator(CelNavigableMutableExpr navigableExpr)
202202
CelNavigableMutableExpr parent = identNode.parent().orElse(null);
203203
while (parent != null) {
204204
if (parent.getKind().equals(Kind.COMPREHENSION)) {
205-
if (parent.expr().comprehension().accuVar().equals(identNode.expr().ident().name())) {
205+
String identName = identNode.expr().ident().name();
206+
if (parent.expr().comprehension().accuVar().equals(identName)
207+
|| parent.expr().comprehension().iterVar().equals(identName)) {
206208
// Prevent folding a subexpression if it contains a variable declared by a
207209
// comprehension. The subexpression cannot be compiled without the full context of the
208210
// surrounding comprehension.

optimizer/src/main/java/dev/cel/optimizer/optimizers/SubexpressionOptimizer.java

Lines changed: 7 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@
5757
import java.util.ArrayList;
5858
import java.util.Arrays;
5959
import java.util.Comparator;
60-
import java.util.HashMap;
6160
import java.util.HashSet;
6261
import java.util.List;
63-
import java.util.Optional;
6462
import java.util.Set;
6563

6664
/**
@@ -121,8 +119,7 @@ public static SubexpressionOptimizer newInstance(SubexpressionOptimizerOptions c
121119

122120
@Override
123121
public OptimizationResult optimize(CelAbstractSyntaxTree ast, Cel cel) {
124-
OptimizationResult result =
125-
cseOptions.enableCelBlock() ? optimizeUsingCelBlock(ast, cel) : optimizeUsingCelBind(ast);
122+
OptimizationResult result = optimizeUsingCelBlock(ast, cel);
126123

127124
verifyOptimizedAstCorrectness(result.optimizedAst());
128125

@@ -330,123 +327,8 @@ private static ImmutableList<CelVarDecl> newBlockIndexVariableDeclarations(
330327
return varDeclBuilder.build();
331328
}
332329

333-
private OptimizationResult optimizeUsingCelBind(CelAbstractSyntaxTree ast) {
334-
CelMutableAst astToModify = CelMutableAst.fromCelAst(ast);
335-
if (!cseOptions.populateMacroCalls()) {
336-
astToModify.source().clearMacroCalls();
337-
}
338-
339-
astToModify =
340-
astMutator
341-
.mangleComprehensionIdentifierNames(
342-
astToModify,
343-
MANGLED_COMPREHENSION_ITER_VAR_PREFIX,
344-
MANGLED_COMPREHENSION_ACCU_VAR_PREFIX,
345-
/* incrementSerially= */ true)
346-
.mutableAst();
347-
CelMutableSource sourceToModify = astToModify.source();
348-
349-
int bindIdentifierIndex = 0;
350-
int iterCount;
351-
for (iterCount = 0; iterCount < cseOptions.iterationLimit(); iterCount++) {
352-
CelNavigableMutableAst navAst = CelNavigableMutableAst.fromAst(astToModify);
353-
List<CelMutableExpr> cseCandidates = getCseCandidates(navAst);
354-
if (cseCandidates.isEmpty()) {
355-
break;
356-
}
357-
358-
String bindIdentifier = BIND_IDENTIFIER_PREFIX + bindIdentifierIndex;
359-
bindIdentifierIndex++;
360-
361-
// Replace all CSE candidates with new bind identifier
362-
for (CelMutableExpr cseCandidate : cseCandidates) {
363-
iterCount++;
364-
365-
astToModify =
366-
astMutator.replaceSubtree(
367-
astToModify, CelMutableExpr.ofIdent(bindIdentifier), cseCandidate.id());
368-
}
369-
370-
// Find LCA to insert the new cel.bind macro into.
371-
CelNavigableMutableExpr lca = getLca(navAst, bindIdentifier);
372-
373-
// Insert the new bind call
374-
CelMutableExpr subexpressionToBind = cseCandidates.get(0);
375-
// Re-add the macro source for bind identifiers that might have been lost from previous
376-
// iteration of CSE
377-
astToModify.source().addAllMacroCalls(sourceToModify.getMacroCalls());
378-
astToModify =
379-
astMutator.replaceSubtreeWithNewBindMacro(
380-
astToModify,
381-
bindIdentifier,
382-
subexpressionToBind,
383-
lca.expr(),
384-
lca.id(),
385-
cseOptions.populateMacroCalls());
386-
387-
// Retain the existing macro calls in case if the bind identifiers are replacing a subtree
388-
// that contains a comprehension.
389-
sourceToModify = astToModify.source();
390-
}
391-
392-
if (iterCount >= cseOptions.iterationLimit()) {
393-
throw new IllegalStateException("Max iteration count reached.");
394-
}
395-
396-
if (iterCount == 0) {
397-
// No modification has been made.
398-
return OptimizationResult.create(ast);
399-
}
400-
401-
astToModify = astMutator.renumberIdsConsecutively(astToModify);
402-
403-
return OptimizationResult.create(astToModify.toParsedAst());
404-
}
405-
406-
private static CelNavigableMutableExpr getLca(
407-
CelNavigableMutableAst navAst, String boundIdentifier) {
408-
CelNavigableMutableExpr root = navAst.getRoot();
409-
ImmutableList<CelNavigableMutableExpr> allNodesWithIdentifier =
410-
root.allNodes()
411-
.filter(
412-
node ->
413-
node.getKind().equals(Kind.IDENT)
414-
&& node.expr().ident().name().equals(boundIdentifier))
415-
.collect(toImmutableList());
416-
417-
if (allNodesWithIdentifier.size() < 2) {
418-
throw new IllegalStateException("Expected at least 2 bound identifiers to be present.");
419-
}
420-
421-
CelNavigableMutableExpr lca = root;
422-
long lcaAncestorCount = 0;
423-
HashMap<Long, Long> ancestors = new HashMap<>();
424-
for (CelNavigableMutableExpr navigableExpr : allNodesWithIdentifier) {
425-
Optional<CelNavigableMutableExpr> maybeParent = Optional.of(navigableExpr);
426-
while (maybeParent.isPresent()) {
427-
CelNavigableMutableExpr parent = maybeParent.get();
428-
if (!ancestors.containsKey(parent.id())) {
429-
ancestors.put(parent.id(), 1L);
430-
continue;
431-
}
432-
433-
long ancestorCount = ancestors.get(parent.id());
434-
if (lcaAncestorCount < ancestorCount
435-
|| (lcaAncestorCount == ancestorCount && lca.depth() < parent.depth())) {
436-
lca = parent;
437-
lcaAncestorCount = ancestorCount;
438-
}
439-
440-
ancestors.put(parent.id(), ancestorCount + 1);
441-
maybeParent = parent.parent();
442-
}
443-
}
444-
445-
return lca;
446-
}
447-
448330
private List<CelMutableExpr> getCseCandidates(CelNavigableMutableAst navAst) {
449-
if (cseOptions.enableCelBlock() && cseOptions.subexpressionMaxRecursionDepth() > 0) {
331+
if (cseOptions.subexpressionMaxRecursionDepth() > 0) {
450332
return getCseCandidatesWithRecursionDepth(
451333
navAst, cseOptions.subexpressionMaxRecursionDepth());
452334
} else {
@@ -689,11 +571,9 @@ public abstract static class Builder {
689571
public abstract Builder populateMacroCalls(boolean value);
690572

691573
/**
692-
* Rewrites the optimized AST using cel.@block call instead of cascaded cel.bind macros, aimed
693-
* to produce a more compact AST. {@link CelSource.Extension} field will be populated in the
694-
* AST to inform that special runtime support is required to evaluate the optimized
695-
* expression.
574+
* @deprecated This option is a no-op. cel.@block is always enabled.
696575
*/
576+
@Deprecated
697577
public abstract Builder enableCelBlock(boolean value);
698578

699579
/**
@@ -708,9 +588,8 @@ public abstract static class Builder {
708588
* <p>Note that expressions containing no common subexpressions may become a candidate for
709589
* extraction to satisfy the max depth requirement.
710590
*
711-
* <p>This is a no-op if {@link #enableCelBlock} is set to false, the configured value is less
712-
* than 1, or no subexpression needs to be extracted because the entire expression is already
713-
* under the designated limit.
591+
* <p>This is a no-op if the configured value is less than 1, or no subexpression needs to be
592+
* extracted because the entire expression is already under the designated limit.
714593
*
715594
* <p>Examples:
716595
*
@@ -756,8 +635,8 @@ public Builder addEliminableFunctions(String... functions) {
756635
public static Builder newBuilder() {
757636
return new AutoValue_SubexpressionOptimizer_SubexpressionOptimizerOptions.Builder()
758637
.iterationLimit(500)
638+
.enableCelBlock(true)
759639
.populateMacroCalls(false)
760-
.enableCelBlock(false)
761640
.subexpressionMaxRecursionDepth(0);
762641
}
763642

0 commit comments

Comments
 (0)