-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-128706: Evaluate constant comparisons in fold_compare
#128705
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
Changes from 5 commits
de026a9
22bcb10
713fde5
285f266
c3abf4a
5cb7cd8
65093fb
891bff5
4f5f1b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add constant folding for constant comparisons | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -639,6 +639,90 @@ 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) { | ||||||||||
|
|
||||||||||
picnixz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| PyObject *lhs = node->v.Compare.left->v.Constant.value; | ||||||||||
|
|
||||||||||
| for (int i=0; i < asdl_seq_LEN(args); i++) { | ||||||||||
picnixz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
|
||||||||||
| 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
|
||||||||||
| goto exit; | ||||||||||
picnixz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| int op = asdl_seq_GET(ops, i); | ||||||||||
|
|
||||||||||
| if (op == Is || op == IsNot) { | ||||||||||
| /* | ||||||||||
| Do not fold expression for now if "is"/"is not" is present. | ||||||||||
| It breaks expected syntax warnings. For example: | ||||||||||
| >>> 1 is 1 | ||||||||||
| <python-input-0>:1: SyntaxWarning: "is" with 'int' literal. Did you mean "=="? | ||||||||||
| */ | ||||||||||
picnixz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| goto exit; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| PyObject *rhs = curr_expr->v.Constant.value; | ||||||||||
|
|
||||||||||
| switch (op) { | ||||||||||
| case Eq: case NotEq: | ||||||||||
| case Gt: case Lt: | ||||||||||
| case GtE: case LtE: | ||||||||||
| { | ||||||||||
|
||||||||||
| case GtE: case LtE: | |
| { | |
| case GtE: | |
| case LtE: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And have each case on each line even though they can be paired.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By convention res < 0 indicates an error so this comment is not really needed IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had it for myself while developing. I can remove
Uh oh!
There was an error while loading. Please reload this page.