This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Optimisation of if with constant expression
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.2
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, sdefresne, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2010-01-12 12:22 by sdefresne, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
compile.diff sdefresne, 2010-01-12 12:23 Patch
Messages (4)
msg97631 - (view) Author: Sylvain Defresne (sdefresne) Date: 2010-01-12 12:22
Python compiler detect some constant expression used in if / while statement and evaluate them at compilation. However, it does not perform some simple optimisation (evaluating not unary expression or checking if and or or expression are constant because of the first expression).

The attached patch allow Python to detect some more constant expression, and to optimise test like the following:

   if __debug__ and x:

   if not __debug__:
msg97632 - (view) Author: Sylvain Defresne (sdefresne) Date: 2010-01-12 12:23
Correct version of the patch.
msg169939 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-09-06 20:42
I'm working on a different approach: an AST optimizer. It is already able to replace "if __debug__ and x: ...." with "if x: ..." ("pythonenv" option must be enabled).
msg350915 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-08-31 10:56
This was optimized in 3.7. See issue27169 and issue30501.
Date User Action Args
2022-04-11 14:56:56adminsetgithub: 51931
2019-08-31 10:56:32serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg350915

resolution: out of date
stage: patch review -> resolved
2012-09-06 20:42:43vstinnersetnosy: + vstinner
messages: + msg169939
2010-03-17 17:28:42pitrousetnosy: + rhettinger
2010-01-12 12:45:16pitrousetpriority: normal
stage: patch review
2010-01-12 12:23:03sdefresnesetfiles: + compile.diff

messages: + msg97632
2010-01-12 12:22:28sdefresnesetfiles: - compile.diff
2010-01-12 12:22:02sdefresnecreate