Title: Optimisation of if with constant expression
Type: performance Stage: patch review
Components: Interpreter Core Versions: Python 3.2
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: haypo, rhettinger, sdefresne
Priority: normal Keywords: patch

Created on 2010-01-12 12:22 by sdefresne, last changed 2012-09-06 20:42 by haypo.

File name Uploaded Description Edit
compile.diff sdefresne, 2010-01-12 12:23 Patch
Messages (3)
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 (haypo) * (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).
Date User Action Args
2012-09-06 20:42:43hayposetnosy: + haypo
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