Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions 27 optimizer/src/main/java/dev/cel/optimizer/AstMutator.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,23 @@ private static MangledComprehensionName getMangledComprehensionName(
}

private static int countComprehensionNestingLevel(CelNavigableMutableExpr comprehensionExpr) {
int nestedLevel = 0;
Optional<CelNavigableMutableExpr> maybeParent = comprehensionExpr.parent();
while (maybeParent.isPresent()) {
if (maybeParent.get().getKind().equals(Kind.COMPREHENSION)) {
nestedLevel++;
}

maybeParent = maybeParent.get().parent();
}
return nestedLevel;
return comprehensionExpr
.descendants()
.filter(node -> node.getKind().equals(Kind.COMPREHENSION))
.mapToInt(
node -> {
int nestedLevel = 1;
CelNavigableMutableExpr maybeParent = node.parent().orElse(null);
while (maybeParent != null && maybeParent.id() != comprehensionExpr.id()) {
if (maybeParent.getKind().equals(Kind.COMPREHENSION)) {
nestedLevel++;
}
maybeParent = maybeParent.parent().orElse(null);
}
return nestedLevel;
})
.max()
.orElse(0);
}

/**
Expand Down
40 changes: 20 additions & 20 deletions 40 optimizer/src/test/java/dev/cel/optimizer/AstMutatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,8 @@ public void mangleComprehensionVariable_adjacentMacros_sameIterVarTypes() throws

assertThat(CEL_UNPARSER.unparse(mangledAst))
.isEqualTo(
"[1, 2, 3].map(@it:0:0, [1, 2, 3].map(@it:1:0, @it:1:0 + 1)) == "
+ "[1, 2, 3].map(@it:0:0, [1, 2, 3].map(@it:1:0, @it:1:0 + 1))");
"[1, 2, 3].map(@it:1:0, [1, 2, 3].map(@it:0:0, @it:0:0 + 1)) == "
+ "[1, 2, 3].map(@it:1:0, [1, 2, 3].map(@it:0:0, @it:0:0 + 1))");
assertThat(CEL.createProgram(CEL.check(mangledAst).getAst()).eval()).isEqualTo(true);
assertConsistentMacroCalls(ast);
}
Expand All @@ -831,9 +831,9 @@ public void mangleComprehensionVariable_withTwoIterVars_adjacentMacros_sameIterV

assertThat(CEL_UNPARSER.unparse(mangledAst))
.isEqualTo(
"[1, 2].transformMap(@it:0:0, @it2:0:0, [1, 2].transformMap(@it:1:0, @it2:1:0,"
+ " @it:1:0)) == [1, 2].transformMap(@it:0:0, @it2:0:0, [1,"
+ " 2].transformMap(@it:1:0, @it2:1:0, @it:1:0))");
"[1, 2].transformMap(@it:1:0, @it2:1:0, [1, 2].transformMap(@it:0:0, @it2:0:0,"
+ " @it:0:0)) == [1, 2].transformMap(@it:1:0, @it2:1:0, [1,"
+ " 2].transformMap(@it:0:0, @it2:0:0, @it:0:0))");
assertThat(CEL.createProgram(CEL.check(mangledAst).getAst()).eval()).isEqualTo(true);
assertConsistentMacroCalls(ast);
}
Expand All @@ -854,8 +854,8 @@ public void mangleComprehensionVariable_adjacentMacros_differentIterVarTypes() t

assertThat(CEL_UNPARSER.unparse(mangledAst))
.isEqualTo(
"[1, 2, 3].map(@it:0:0, [1, 2, 3].map(@it:1:0, @it:1:0)) == "
+ "dyn([1u, 2u, 3u].map(@it:0:1, [1u, 2u, 3u].map(@it:1:1, @it:1:1)))");
"[1, 2, 3].map(@it:1:0, [1, 2, 3].map(@it:0:0, @it:0:0)) == "
+ "dyn([1u, 2u, 3u].map(@it:1:1, [1u, 2u, 3u].map(@it:0:1, @it:0:1)))");
assertThat(CEL.createProgram(CEL.check(mangledAst).getAst()).eval()).isEqualTo(true);
assertConsistentMacroCalls(ast);
}
Expand Down Expand Up @@ -892,7 +892,7 @@ public void mangleComprehensionVariable_nestedMacroWithShadowedVariables() throw
assertThat(mangledAst.getExpr().toString())
.isEqualTo(
"COMPREHENSION [27] {\n"
+ " iter_var: @it:0:0\n"
+ " iter_var: @it:1:0\n"
+ " iter_range: {\n"
+ " LIST [1] {\n"
+ " elements: {\n"
Expand All @@ -902,7 +902,7 @@ public void mangleComprehensionVariable_nestedMacroWithShadowedVariables() throw
+ " }\n"
+ " }\n"
+ " }\n"
+ " accu_var: @ac:0:0\n"
+ " accu_var: @ac:1:0\n"
+ " accu_init: {\n"
+ " CONSTANT [20] { value: false }\n"
+ " }\n"
Expand All @@ -914,7 +914,7 @@ public void mangleComprehensionVariable_nestedMacroWithShadowedVariables() throw
+ " function: !_\n"
+ " args: {\n"
+ " IDENT [21] {\n"
+ " name: @ac:0:0\n"
+ " name: @ac:1:0\n"
+ " }\n"
+ " }\n"
+ " }\n"
Expand All @@ -926,20 +926,20 @@ public void mangleComprehensionVariable_nestedMacroWithShadowedVariables() throw
+ " function: _||_\n"
+ " args: {\n"
+ " IDENT [24] {\n"
+ " name: @ac:0:0\n"
+ " name: @ac:1:0\n"
+ " }\n"
+ " COMPREHENSION [19] {\n"
+ " iter_var: @it:1:0\n"
+ " iter_var: @it:0:0\n"
+ " iter_range: {\n"
+ " LIST [5] {\n"
+ " elements: {\n"
+ " IDENT [6] {\n"
+ " name: @it:0:0\n"
+ " name: @it:1:0\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " accu_var: @ac:1:0\n"
+ " accu_var: @ac:0:0\n"
+ " accu_init: {\n"
+ " CONSTANT [12] { value: false }\n"
+ " }\n"
Expand All @@ -951,7 +951,7 @@ public void mangleComprehensionVariable_nestedMacroWithShadowedVariables() throw
+ " function: !_\n"
+ " args: {\n"
+ " IDENT [13] {\n"
+ " name: @ac:1:0\n"
+ " name: @ac:0:0\n"
+ " }\n"
+ " }\n"
+ " }\n"
Expand All @@ -963,13 +963,13 @@ public void mangleComprehensionVariable_nestedMacroWithShadowedVariables() throw
+ " function: _||_\n"
+ " args: {\n"
+ " IDENT [16] {\n"
+ " name: @ac:1:0\n"
+ " name: @ac:0:0\n"
+ " }\n"
+ " CALL [10] {\n"
+ " function: _==_\n"
+ " args: {\n"
+ " IDENT [9] {\n"
+ " name: @it:1:0\n"
+ " name: @it:0:0\n"
+ " }\n"
+ " CONSTANT [11] { value: 1 }\n"
+ " }\n"
Expand All @@ -979,7 +979,7 @@ public void mangleComprehensionVariable_nestedMacroWithShadowedVariables() throw
+ " }\n"
+ " result: {\n"
+ " IDENT [18] {\n"
+ " name: @ac:1:0\n"
+ " name: @ac:0:0\n"
+ " }\n"
+ " }\n"
+ " }\n"
Expand All @@ -988,12 +988,12 @@ public void mangleComprehensionVariable_nestedMacroWithShadowedVariables() throw
+ " }\n"
+ " result: {\n"
+ " IDENT [26] {\n"
+ " name: @ac:0:0\n"
+ " name: @ac:1:0\n"
+ " }\n"
+ " }\n"
+ "}");
assertThat(CEL_UNPARSER.unparse(mangledAst))
.isEqualTo("[x].exists(@it:0:0, [@it:0:0].exists(@it:1:0, @it:1:0 == 1))");
.isEqualTo("[x].exists(@it:1:0, [@it:1:0].exists(@it:0:0, @it:0:0 == 1))");
assertThat(CEL.createProgram(CEL.check(mangledAst).getAst()).eval(ImmutableMap.of("x", 1)))
.isEqualTo(true);
assertConsistentMacroCalls(ast);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ private enum CseTestCase {
+ "[1].exists(k, z, k > 1 && z > 0) && [2].exists(l, w, l > 1 && w > 0)"),
NESTED_MACROS("[1,2,3].map(i, [1, 2, 3].map(i, i + 1)) == [[2, 3, 4], [2, 3, 4], [2, 3, 4]]"),
NESTED_MACROS_2("[1, 2].map(y, [1, 2, 3].filter(x, x == y)) == [[1], [2]]"),
NESTED_MACROS_3("[1,2,3].map(i, i * 2) + [1,2,3].map(i, i * 2).map(i, i * 2)"),
NESTED_MACROS_COMP_V2_1(
"[1,2,3].transformList(i, v, [1, 2, 3].transformList(i, v, i + v + 1)) == "
+ "[[2, 4, 6], [2, 4, 6], [2, 4, 6]]"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,21 @@ Result: true
[BLOCK_RECURSION_DEPTH_8]: true
[BLOCK_RECURSION_DEPTH_9]: true

Test case: NESTED_MACROS_3
Source: [1,2,3].map(i, i * 2) + [1,2,3].map(i, i * 2).map(i, i * 2)
=====>
Result: [2, 4, 6, 4, 8, 12]
[BLOCK_COMMON_SUBEXPR_ONLY]: [2, 4, 6, 4, 8, 12]
[BLOCK_RECURSION_DEPTH_1]: [2, 4, 6, 4, 8, 12]
[BLOCK_RECURSION_DEPTH_2]: [2, 4, 6, 4, 8, 12]
[BLOCK_RECURSION_DEPTH_3]: [2, 4, 6, 4, 8, 12]
[BLOCK_RECURSION_DEPTH_4]: [2, 4, 6, 4, 8, 12]
[BLOCK_RECURSION_DEPTH_5]: [2, 4, 6, 4, 8, 12]
[BLOCK_RECURSION_DEPTH_6]: [2, 4, 6, 4, 8, 12]
[BLOCK_RECURSION_DEPTH_7]: [2, 4, 6, 4, 8, 12]
[BLOCK_RECURSION_DEPTH_8]: [2, 4, 6, 4, 8, 12]
[BLOCK_RECURSION_DEPTH_9]: [2, 4, 6, 4, 8, 12]

Test case: NESTED_MACROS_COMP_V2_1
Source: [1,2,3].transformList(i, v, [1, 2, 3].transformList(i, v, i + v + 1)) == [[2, 4, 6], [2, 4, 6], [2, 4, 6]]
=====>
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.