5757import java .util .ArrayList ;
5858import java .util .Arrays ;
5959import java .util .Comparator ;
60- import java .util .HashMap ;
6160import java .util .HashSet ;
6261import java .util .List ;
63- import java .util .Optional ;
6462import 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