-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
Comparison operators could benefit from constant folding #128706
Comments
Hello! Unfortunately, I do think that we can't do this at the moment. ======================================================================
FAIL: test_comparison_is_literal (test.test_grammar.GrammarTests.test_comparison_is_literal)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/runner/work/cpython/cpython/Lib/test/test_grammar.py", line 1435, in test_comparison_is_literal
check('None is 1', '"is" with \'int\' literal')
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/runner/work/cpython/cpython/Lib/test/test_grammar.py", line 1425, in check
self.check_syntax_warning(test, msg)
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
File "/Users/runner/work/cpython/cpython/Lib/test/support/warnings_helper.py", line 23, in check_syntax_warning
testcase.assertEqual(len(warns), 1, warns)
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 0 != 1 : [] This happens because at the codegen stage Also please see the #126835. Later, we can add folding for constant comparisons (where's both operands are constants) in the CFG. |
Yes, I expected and saw some tests fail, but skipped fixing them as I did not know if this will be rejected or not.
Interesting. So ast optimizer will be removed and all folding will happen in CFG? I did not know that. |
It was already proposed several times (see for example #70909, there were more). The reason why it was not implemented in first place is that the optimizer only performs the most useful practical optimizations. So I suggest to close this issue as all previous attempts. |
I've read the tests in the PR and all the expressions that are used for testing are actually expressions I don't think someone is ever going to use in production. Constant folding for arithmetic operations is relevant as it improves readability most of the time, but constant folding on comparisons (which only return True or False in the end) are not needed in production IMO. |
I would expect folding of every constant expression in a programming language. But this is my personal opinion. Additionally, if constant propagation is somehow implemented in future, wouldn't constant folding be useful in this scenario as well? |
I agree that this feature is not quite useful. Real code does not usually have expressions like But, I am not strongly -1, more like -0. In any case, thanks a lot for your PR! |
I agree that there is no real benefit of this. Count me as -1. Suggesting to close as wont-implement. |
Feature or enhancement
Proposal:
Constant expresisons like:
are folded in AST optimizer, but constant comparisons are not. For instance:
etc...
Proposing to add constant comparison folding. Linking draft PR. No tests yet in case proposal is rejected. cc @Eclips4 as codeowner and AST specialist.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
fold_compare
#128705The text was updated successfully, but these errors were encountered: