Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-128706: Evaluate constant comparisons in fold_compare #128705

Closed
wants to merge 9 commits into from
Closed
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
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ configuration mechanisms).

Other language changes
======================
* Constant comparsion expressions are now folded and evaluated before runtime.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a terminology that would be like "at compilation time" instead of "before runtime"? (using "compilation" time may cause users to think that it's only when .pyc files are created but this can be skipped with -B I think?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how we are actually ordering the changes. Since this is a fairly niche optimization, I would put it at the end of the list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, new changes are added at the end of a section.

For example, expressions like: ``"str" in ("str",)`` or ``1 == 1.0 == True``
are now pre-evaluated.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
are now pre-evaluated.
are now pre-evaluated during compilation.

(Contributed by Yan Yanchii in :gh:`128706`.)

* The :func:`map` built-in now has an optional keyword-only *strict* flag
like :func:`zip` to check that all the iterables are of equal length.
Expand Down
65 changes: 58 additions & 7 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3180,7 +3180,8 @@ def create_unaryop(operand):
self.assert_ast(result_code, non_optimized_target, optimized_target)

def test_folding_not(self):
code = "not (1 %s (1,))"
# use list as left-hand side to avoid folding constant expression to True/False
code = "not ([] %s (1,))"
operators = {
"in": ast.In(),
"is": ast.Is(),
Expand All @@ -3192,7 +3193,7 @@ def test_folding_not(self):

def create_notop(operand):
return ast.UnaryOp(op=ast.Not(), operand=ast.Compare(
left=ast.Constant(value=1),
left=ast.List(),
ops=[operators[operand]],
comparators=[ast.Tuple(elts=[ast.Constant(value=1)])]
))
Expand All @@ -3201,7 +3202,7 @@ def create_notop(operand):
result_code = code % op
non_optimized_target = self.wrap_expr(create_notop(op))
optimized_target = self.wrap_expr(
ast.Compare(left=ast.Constant(1), ops=[opt_operators[op]], comparators=[ast.Constant(value=(1,))])
ast.Compare(left=ast.List(), ops=[opt_operators[op]], comparators=[ast.Constant(value=(1,))])
)

with self.subTest(
Expand Down Expand Up @@ -3239,8 +3240,58 @@ def test_folding_tuple(self):

self.assert_ast(code, non_optimized_target, optimized_target)

def test_folding_comparator(self):
code = "1 %s %s1%s"
def test_folding_compare(self):
true = self.wrap_expr(ast.Constant(value=True))
false = self.wrap_expr(ast.Constant(value=False))

folded_cases = (
("3 > 2 > 1", (ast.Constant(3), [ast.Gt(), ast.Gt()], [ast.Constant(value=2), ast.Constant(value=1)]), true),
("3 > 4 > 1", (ast.Constant(3), [ast.Gt(), ast.Gt()], [ast.Constant(value=4), ast.Constant(value=1)]), false),
("3 >= 3 >= 1", (ast.Constant(3), [ast.GtE(), ast.GtE()], [ast.Constant(value=3), ast.Constant(value=1)]), true),
("3 >= 4 >= 1", (ast.Constant(3), [ast.GtE(), ast.GtE()], [ast.Constant(value=4), ast.Constant(value=1)]), false),
("1 < 2 < 3", (ast.Constant(1), [ast.Lt(), ast.Lt()], [ast.Constant(value=2), ast.Constant(value=3)]), true),
("1 < 0 < 3", (ast.Constant(1), [ast.Lt(), ast.Lt()], [ast.Constant(value=0), ast.Constant(value=3)]), false),
("1 <= 2 <= 3", (ast.Constant(1), [ast.LtE(), ast.LtE()], [ast.Constant(value=2), ast.Constant(value=3)]), true),
("1 <= 0 <= 3", (ast.Constant(1), [ast.LtE(), ast.LtE()], [ast.Constant(value=0), ast.Constant(value=3)]), false),
("1 == 1.0 == True", (ast.Constant(1), [ast.Eq(), ast.Eq()], [ast.Constant(value=1.0), ast.Constant(value=True)]), true),
("1 == 2 == True", (ast.Constant(1), [ast.Eq(), ast.Eq()], [ast.Constant(value=2), ast.Constant(value=True)]), false),
("1 != 2 != 3", (ast.Constant(1), [ast.NotEq(), ast.NotEq()], [ast.Constant(value=2), ast.Constant(value=3)]), true),
("1 != 1 != 3", (ast.Constant(1), [ast.NotEq(), ast.NotEq()], [ast.Constant(value=1), ast.Constant(value=3)]), false),
("1 in [1, 2]", (ast.Constant(1), [ast.In()], [ast.List(elts=[ast.Constant(1), ast.Constant(2)])]), true),
("1 in [2, 2]", (ast.Constant(1), [ast.In()], [ast.List(elts=[ast.Constant(2), ast.Constant(2)])]), false),
("1 not in [1, 2]", (ast.Constant(1), [ast.NotIn()], [ast.List(elts=[ast.Constant(1), ast.Constant(2)])]), false),
("1 not in [2, 2]", (ast.Constant(1), [ast.NotIn()], [ast.List(elts=[ast.Constant(2), ast.Constant(2)])]), true),
)

for code, original, folded in folded_cases:
left, ops, comparators = original
unfolded = self.wrap_expr(ast.Compare(left=left, ops=ops, comparators=comparators))
self.assert_ast(code=code, non_optimized_target=unfolded, optimized_target=folded)

# these should stay as they were
unfolded_cases = (
("3 > 2 > []", ast.Compare(left=ast.Constant(3), ops=[ast.Gt(), ast.Gt()], comparators=[ast.Constant(2), ast.List()])),
("1 > [] > 0", ast.Compare(left=ast.Constant(1), ops=[ast.Gt(), ast.Gt()], comparators=[ast.List(), ast.Constant(0)])),
("1 >= [] >= 0", ast.Compare(left=ast.Constant(1), ops=[ast.GtE(), ast.GtE()], comparators=[ast.List(), ast.Constant(0)])),
("1 < [] < 0", ast.Compare(left=ast.Constant(1), ops=[ast.Lt(), ast.Lt()], comparators=[ast.List(), ast.Constant(0)])),
("1 <= [] <= 0", ast.Compare(left=ast.Constant(1), ops=[ast.LtE(), ast.LtE()], comparators=[ast.List(), ast.Constant(0)])),
("1 == [] == 0", ast.Compare(left=ast.Constant(1), ops=[ast.Eq(), ast.Eq()], comparators=[ast.List(), ast.Constant(0)])),
("1 != [] != 0", ast.Compare(left=ast.Constant(1), ops=[ast.NotEq(), ast.NotEq()], comparators=[ast.List(), ast.Constant(0)])),
("1 is 1", ast.Compare(left=ast.Constant(1), ops=[ast.Is()], comparators=[ast.Constant(1)])),
("1 is not 1", ast.Compare(left=ast.Constant(1), ops=[ast.IsNot()], comparators=[ast.Constant(1)])),
# invalid also should stay as they were
("1 in 1", ast.Compare(left=ast.Constant(1), ops=[ast.In()], comparators=[ast.Constant(1)])),
("1 not in 1", ast.Compare(left=ast.Constant(1), ops=[ast.NotIn()], comparators=[ast.Constant(1)])),
)

for code, expected in unfolded_cases:
self.assertTrue(ast.compare(ast.parse(code), self.wrap_expr(expected)))

def test_folding_comparator_list_set_subst(self):
"""Test substitution of list/set with tuple/frozenset in expressions like "1 in [1]" or "1 in {1}" """

# use list as left-hand side to avoid folding constant comparison expression to True/False
code = "[] %s %s1%s"
operators = [("in", ast.In()), ("not in", ast.NotIn())]
braces = [
("[", "]", ast.List, (1,)),
Expand All @@ -3249,11 +3300,11 @@ def test_folding_comparator(self):
for left, right, non_optimized_comparator, optimized_comparator in braces:
for op, node in operators:
non_optimized_target = self.wrap_expr(ast.Compare(
left=ast.Constant(1), ops=[node],
left=ast.List(), ops=[node],
comparators=[non_optimized_comparator(elts=[ast.Constant(1)])]
))
optimized_target = self.wrap_expr(ast.Compare(
left=ast.Constant(1), ops=[node],
left=ast.List(), ops=[node],
comparators=[ast.Constant(value=optimized_comparator)]
))
self.assert_ast(code % (op, left, right), non_optimized_target, optimized_target)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add constant folding for constant comparisons.
62 changes: 62 additions & 0 deletions Python/ast_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,68 @@ fold_compare(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
return 0;
}
}

static const int richcompare_table[] = {
[Eq] = Py_EQ,
[NotEq] = Py_NE,
[Gt] = Py_GT,
[Lt] = Py_LT,
[GtE] = Py_GE,
[LtE] = Py_LE,
};

if (node->v.Compare.left->kind == Constant_kind) {
PyObject *lhs = node->v.Compare.left->v.Constant.value;
for (Py_ssize_t i = 0; i < asdl_seq_LEN(args); i++) {
expr_ty curr_expr = (expr_ty)asdl_seq_GET(args, i);
if (curr_expr->kind != Constant_kind) {
/* try to fold only if every comparator is constant */
picnixz marked this conversation as resolved.
Show resolved Hide resolved
return 1;
}
int op = asdl_seq_GET(ops, i);
if (op == Is || op == IsNot) {
/* Do not fold "is" and "is not" expressions since this breaks
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Do not fold "is" and "is not" expressions since this breaks
/* Do not fold "is" and "is not" expressions since it would break

expected syntax warnings. For example:
>>> 1 is 1
<python-input-0>:1: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
*/
return 1;
}
PyObject *rhs = curr_expr->v.Constant.value;
int res;
switch (op) {
case Eq:
case NotEq:
case Gt:
case Lt:
case GtE:
case LtE: {
res = PyObject_RichCompareBool(lhs, rhs, richcompare_table[op]);
break;
}
case In:
case NotIn: {
res = PySequence_Contains(rhs, lhs);
if (op == NotIn && res >= 0) {
res = !res;
}
break;
}
default:
Py_UNREACHABLE();
}
if (res == 0) {
/* shortcut, whole expression is False */
return make_const(node, Py_False, arena);
}
else if (res < 0) {
return make_const(node, NULL, arena);
}
picnixz marked this conversation as resolved.
Show resolved Hide resolved
lhs = rhs;
}
/* whole expression is True */
return make_const(node, Py_True, arena);
}
return 1;
}

Expand Down
Loading