Skip to content

Commit 0a5e6cf

Browse files
l46kokcopybara-github
authored andcommitted
Remove cel.bind option from SubexpressionOptimizer
PiperOrigin-RevId: 797496424
1 parent 8fc7387 commit 0a5e6cf

File tree

6 files changed

+38
-301
lines changed

6 files changed

+38
-301
lines changed

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

optimizer/src/test/java/dev/cel/optimizer/optimizers/SubexpressionOptimizerBaselineTest.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public class SubexpressionOptimizerBaselineTest extends BaselineTestCase {
7373
private static final SubexpressionOptimizerOptions OPTIMIZER_COMMON_OPTIONS =
7474
SubexpressionOptimizerOptions.newBuilder()
7575
.populateMacroCalls(true)
76-
.enableCelBlock(true)
7776
.addEliminableFunctions("pure_custom_func")
7877
.build();
7978

@@ -187,28 +186,11 @@ public void subexpression_ast(@TestParameter CseTestOptimizer cseTestOptimizer)
187186
}
188187
}
189188

190-
@Test
191-
public void large_expressions_bind_cascaded() throws Exception {
192-
CelOptimizer celOptimizer =
193-
newCseOptimizer(
194-
CEL,
195-
SubexpressionOptimizerOptions.newBuilder()
196-
.populateMacroCalls(true)
197-
.enableCelBlock(false)
198-
.build());
199-
200-
runLargeTestCases(celOptimizer);
201-
}
202-
203189
@Test
204190
public void large_expressions_block_common_subexpr() throws Exception {
205191
CelOptimizer celOptimizer =
206192
newCseOptimizer(
207-
CEL,
208-
SubexpressionOptimizerOptions.newBuilder()
209-
.populateMacroCalls(true)
210-
.enableCelBlock(true)
211-
.build());
193+
CEL, SubexpressionOptimizerOptions.newBuilder().populateMacroCalls(true).build());
212194

213195
runLargeTestCases(celOptimizer);
214196
}
@@ -220,7 +202,6 @@ public void large_expressions_block_recursion_depth_1() throws Exception {
220202
CEL,
221203
SubexpressionOptimizerOptions.newBuilder()
222204
.populateMacroCalls(true)
223-
.enableCelBlock(true)
224205
.subexpressionMaxRecursionDepth(1)
225206
.build());
226207

@@ -234,7 +215,6 @@ public void large_expressions_block_recursion_depth_2() throws Exception {
234215
CEL,
235216
SubexpressionOptimizerOptions.newBuilder()
236217
.populateMacroCalls(true)
237-
.enableCelBlock(true)
238218
.subexpressionMaxRecursionDepth(2)
239219
.build());
240220

@@ -248,7 +228,6 @@ public void large_expressions_block_recursion_depth_3() throws Exception {
248228
CEL,
249229
SubexpressionOptimizerOptions.newBuilder()
250230
.populateMacroCalls(true)
251-
.enableCelBlock(true)
252231
.subexpressionMaxRecursionDepth(3)
253232
.build());
254233

@@ -312,7 +291,6 @@ private static CelOptimizer newCseOptimizer(Cel cel, SubexpressionOptimizerOptio
312291

313292
@SuppressWarnings("Immutable") // Test only
314293
private enum CseTestOptimizer {
315-
CASCADED_BINDS(OPTIMIZER_COMMON_OPTIONS.toBuilder().enableCelBlock(false).build()),
316294
BLOCK_COMMON_SUBEXPR_ONLY(OPTIMIZER_COMMON_OPTIONS),
317295
BLOCK_RECURSION_DEPTH_1(
318296
OPTIMIZER_COMMON_OPTIONS.toBuilder().subexpressionMaxRecursionDepth(1).build()),

0 commit comments

Comments
 (0)