3333import dev .cel .common .formats .ValueString ;
3434import dev .cel .common .types .CelType ;
3535import dev .cel .common .types .SimpleType ;
36+ import dev .cel .optimizer .CelAstOptimizer ;
3637import dev .cel .optimizer .CelOptimizationException ;
3738import dev .cel .optimizer .CelOptimizer ;
3839import dev .cel .optimizer .CelOptimizerFactory ;
@@ -63,6 +64,7 @@ final class CelPolicyCompilerImpl implements CelPolicyCompiler {
6364 private final Cel cel ;
6465 private final String variablesPrefix ;
6566 private final int iterationLimit ;
67+ private final ImmutableList <CelAstOptimizer > optimizers ;
6668 private final Optional <CelAstValidator > astDepthValidator ;
6769
6870 @ Override
@@ -140,19 +142,7 @@ public CelAbstractSyntaxTree compose(CelPolicy policy, CelCompiledRule compiledR
140142 }
141143
142144 CelOptimizer astOptimizer =
143- CelOptimizerFactory .standardCelOptimizerBuilder (cel )
144- .addAstOptimizers (
145- ConstantFoldingOptimizer .getInstance (),
146- SubexpressionOptimizer .newInstance (
147- SubexpressionOptimizerOptions .newBuilder ()
148- // "record" is used for recording subexpression results via
149- // BlueprintLateFunctionBinding. Safely eliminable, since repeated
150- // invocation does not change the intermediate results.
151- .addEliminableFunctions ("record" )
152- .populateMacroCalls (true )
153- .enableCelBlock (true )
154- .build ()))
155- .build ();
145+ CelOptimizerFactory .standardCelOptimizerBuilder (cel ).addAstOptimizers (optimizers ).build ();
156146 try {
157147 // Optimize the composed graph using const fold and CSE
158148 ast = astOptimizer .optimize (ast );
@@ -339,6 +329,7 @@ static final class Builder implements CelPolicyCompilerBuilder {
339329 private final Cel cel ;
340330 private String variablesPrefix ;
341331 private int iterationLimit ;
332+ private ImmutableList <CelAstOptimizer > optimizers ;
342333 private Optional <CelAstValidator > astDepthLimitValidator ;
343334
344335 private Builder (Cel cel ) {
@@ -362,7 +353,7 @@ public Builder setIterationLimit(int iterationLimit) {
362353
363354 @ Override
364355 @ CanIgnoreReturnValue
365- public CelPolicyCompilerBuilder setAstDepthLimit (int astDepthLimit ) {
356+ public Builder setAstDepthLimit (int astDepthLimit ) {
366357 if (astDepthLimit < 0 ) {
367358 astDepthLimitValidator = Optional .empty ();
368359 } else {
@@ -371,27 +362,40 @@ public CelPolicyCompilerBuilder setAstDepthLimit(int astDepthLimit) {
371362 return this ;
372363 }
373364
365+ @ Override
366+ public Builder setOptimizers (List <CelAstOptimizer > optimizers ) {
367+ this .optimizers = ImmutableList .copyOf (optimizers );
368+ return this ;
369+ }
370+
374371 @ Override
375372 public CelPolicyCompiler build () {
376373 return new CelPolicyCompilerImpl (
377- cel , this .variablesPrefix , this .iterationLimit , astDepthLimitValidator );
374+ cel , this .variablesPrefix , this .iterationLimit , this . optimizers , astDepthLimitValidator );
378375 }
379376 }
380377
381378 static Builder newBuilder (Cel cel ) {
382379 return new Builder (cel )
383380 .setVariablesPrefix (DEFAULT_VARIABLE_PREFIX )
384- .setIterationLimit (DEFAULT_ITERATION_LIMIT );
381+ .setIterationLimit (DEFAULT_ITERATION_LIMIT )
382+ .setOptimizers (
383+ ImmutableList .of (
384+ ConstantFoldingOptimizer .getInstance (),
385+ SubexpressionOptimizer .newInstance (
386+ SubexpressionOptimizerOptions .newBuilder ().populateMacroCalls (true ).build ())));
385387 }
386388
387389 private CelPolicyCompilerImpl (
388390 Cel cel ,
389391 String variablesPrefix ,
390392 int iterationLimit ,
393+ ImmutableList <CelAstOptimizer > optimizers ,
391394 Optional <CelAstValidator > astDepthValidator ) {
392395 this .cel = checkNotNull (cel );
393396 this .variablesPrefix = checkNotNull (variablesPrefix );
394397 this .iterationLimit = iterationLimit ;
398+ this .optimizers = optimizers ;
395399 this .astDepthValidator = astDepthValidator ;
396400 }
397401}
0 commit comments