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

Comparison operators could benefit from constant folding #128706

Closed
WolframAlph opened this issue Jan 10, 2025 · 7 comments
Closed

Comparison operators could benefit from constant folding #128706

WolframAlph opened this issue Jan 10, 2025 · 7 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement

Comments

@WolframAlph
Copy link
Contributor

WolframAlph commented Jan 10, 2025

Feature or enhancement

Proposal:

Constant expresisons like:

1 + 1

are folded in AST optimizer, but constant comparisons are not. For instance:

1 in (1, 2)
3 > 2 > 1

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

@Eclips4
Copy link
Member

Eclips4 commented Jan 10, 2025

Hello!
Firstly, thanks for your efforts, Yan!

Unfortunately, I do think that we can't do this at the moment.
You can see in your PR that CI is failing (there are some failing tests, but let's concentrate on test_grammar):

======================================================================
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 None is 1 is already folded into a LOAD_CONST False, so Python/codegen.c::check_is_arg isn't working as intended and can't raise a warning about the usage of is operator with constants.

Also please see the #126835. Later, we can add folding for constant comparisons (where's both operands are constants) in the CFG.

@WolframAlph
Copy link
Contributor Author

Yes, I expected and saw some tests fail, but skipped fixing them as I did not know if this will be rejected or not.

Also please see the #126835.

Interesting. So ast optimizer will be removed and all folding will happen in CFG? I did not know that.
I will leave it up to you then whether to close this one or keep for later and link this issue.
Additionally, would you accept some help with #126835 ?

@picnixz picnixz added performance Performance or resource usage interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jan 10, 2025
@serhiy-storchaka
Copy link
Member

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. 24*60*60, 2**32-1, 1/3, b'A'[0] -- are pretty common use cases, and there is a benefit of calculating them at the compile time. The benefit/cost ration for constant folding of comparison operations is not so great.

So I suggest to close this issue as all previous attempts.

@picnixz
Copy link
Member

picnixz commented Jan 10, 2025

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.

@WolframAlph
Copy link
Contributor Author

WolframAlph commented Jan 10, 2025

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?

@sobolevn
Copy link
Member

I agree that this feature is not quite useful. Real code does not usually have expressions like 1 > 2 or 1 in (1, 2). Linters should catch cases like this, because they are most likely just typos.

But, I am not strongly -1, more like -0.

In any case, thanks a lot for your PR!

@erlend-aasland
Copy link
Contributor

I agree that there is no real benefit of this. Count me as -1. Suggesting to close as wont-implement.

@serhiy-storchaka serhiy-storchaka closed this as not planned Won't fix, can't repro, duplicate, stale Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

6 participants