classification
Title: Optimisation of if with constant expression
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.2
process
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 2019-08-31 10:56 by serhiy.storchaka. This issue is now closed.

Files
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:
       pass

   if not __debug__:
       pass
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).
https://bitbucket.org/haypo/astoptimizer/
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.
History
Date User Action Args
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