Skip to content

Commit 705546a

Browse files
Static optimizer for constant folding (#804)
* Optimizer API with Constant Folding implementatiton * Better logical folds and additional tests * Add a configurable limit to constant folding
1 parent 509c1d6 commit 705546a

9 files changed

Lines changed: 1575 additions & 8 deletions

File tree

cel/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ go_library(
1010
"cel.go",
1111
"decls.go",
1212
"env.go",
13+
"folding.go",
1314
"io.go",
1415
"library.go",
1516
"macro.go",
17+
"optimizer.go",
1618
"options.go",
1719
"program.go",
1820
"validator.go",
@@ -56,6 +58,7 @@ go_test(
5658
"cel_test.go",
5759
"decls_test.go",
5860
"env_test.go",
61+
"folding_test.go",
5962
"io_test.go",
6063
"validator_test.go",
6164
],

cel/env.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type Ast struct {
4343
}
4444

4545
// Expr returns the proto serializable instance of the parsed/checked expression.
46+
//
47+
// Deprecated: prefer cel.AstToCheckedExpr() or cel.AstToParsedExpr() and call GetExpr()
48+
// the result instead.
4649
func (ast *Ast) Expr() *exprpb.Expr {
4750
if ast == nil {
4851
return nil
@@ -221,6 +224,11 @@ func (e *Env) Check(ast *Ast) (*Ast, *Issues) {
221224
source: ast.Source(),
222225
impl: checked}
223226

227+
// Avoid creating a validator config if it's not needed.
228+
if len(e.validators) == 0 {
229+
return ast, nil
230+
}
231+
224232
// Generate a validator configuration from the set of configured validators.
225233
vConfig := newValidatorConfig()
226234
for _, v := range e.validators {

0 commit comments

Comments
 (0)