[New-bugs-announce] [issue24014] Second pass of PyCode_Optimize
Joe Jevnik
report at bugs.python.org
Mon Apr 20 11:09:20 CEST 2015
New submission from Joe Jevnik:
There are a lot of optimizations that are being missed by only running a single pass of PyCode_Optimize. I originally started by trying to optimize for De Morgan's Laws in if tests; however, I realized that the issue actually went away if you run the optimizer twice. I imagine that this would provide a lot of other performance increases because some of the patterns don't show up until the optimizer has already run once. For example:
>>> def f(a, b):
... if not a and not b:
... return True
... return False
...
On default, this generates:
>>> dis(f)
2 0 LOAD_FAST 0 (a)
3 UNARY_NOT
4 POP_JUMP_IF_FALSE 18
7 LOAD_FAST 1 (b)
10 UNARY_NOT
11 POP_JUMP_IF_FALSE 18
3 14 LOAD_CONST 1 (True)
17 RETURN_VALUE
4 >> 18 LOAD_CONST 2 (False)
21 RETURN_VALUE
If we run the optimizer again, we can collapse some of the UNARY_NOT -> POP_JUMP_IF_FALSE back into POP_JUMP_IF_TRUE, like this:
>>> dis(f)
2 0 LOAD_FAST 0 (a)
3 POP_JUMP_IF_TRUE 16
6 LOAD_FAST 1 (b)
9 POP_JUMP_IF_TRUE 16
3 12 LOAD_CONST 1 (True)
15 RETURN_VALUE
4 >> 16 LOAD_CONST 2 (False)
19 RETURN_VALUE
Also, Python/importlib.h blew up in the diff; should this be included in the patch?
----------
components: Interpreter Core
files: secondpass.patch
keywords: patch
messages: 241626
nosy: llllllllll
priority: normal
severity: normal
status: open
title: Second pass of PyCode_Optimize
type: performance
versions: Python 3.5
Added file: http://bugs.python.org/file39144/secondpass.patch
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24014>
_______________________________________
More information about the New-bugs-announce
mailing list