Skip to content

Commit 486a380

Browse files
l46kokcopybara-github
authored andcommitted
Allow specifying a set of optimizers to run to the policy compiler
PiperOrigin-RevId: 885225062
1 parent 4f6a571 commit 486a380

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

policy/src/main/java/dev/cel/policy/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ java_library(
138138
],
139139
deps = [
140140
":compiler",
141+
"//optimizer:ast_optimizer",
141142
"@maven//:com_google_errorprone_error_prone_annotations",
142143
],
143144
)
@@ -214,6 +215,7 @@ java_library(
214215
"//common/types",
215216
"//common/types:type_providers",
216217
"//optimizer",
218+
"//optimizer:ast_optimizer",
217219
"//optimizer:optimization_exception",
218220
"//optimizer:optimizer_builder",
219221
"//optimizer/optimizers:common_subexpression_elimination",

policy/src/main/java/dev/cel/policy/CelPolicyCompilerBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import com.google.errorprone.annotations.CanIgnoreReturnValue;
1818
import com.google.errorprone.annotations.CheckReturnValue;
19+
import dev.cel.optimizer.CelAstOptimizer;
20+
import java.util.List;
1921

2022
/** Interface for building an instance of {@link CelPolicyCompiler} */
2123
public interface CelPolicyCompilerBuilder {
@@ -38,6 +40,10 @@ public interface CelPolicyCompilerBuilder {
3840
@CanIgnoreReturnValue
3941
CelPolicyCompilerBuilder setAstDepthLimit(int iterationLimit);
4042

43+
/** Configures the policy compiler to run the provided optimizers on compiled policies. */
44+
@CanIgnoreReturnValue
45+
CelPolicyCompilerBuilder setOptimizers(List<CelAstOptimizer> optimizers);
46+
4147
@CheckReturnValue
4248
CelPolicyCompiler build();
4349
}

policy/src/main/java/dev/cel/policy/CelPolicyCompilerImpl.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import dev.cel.common.formats.ValueString;
3434
import dev.cel.common.types.CelType;
3535
import dev.cel.common.types.SimpleType;
36+
import dev.cel.optimizer.CelAstOptimizer;
3637
import dev.cel.optimizer.CelOptimizationException;
3738
import dev.cel.optimizer.CelOptimizer;
3839
import 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

Comments
 (0)