[issue26204] compiler: don't emit LOAD_CONST instructions for constant statements?
STINNER Victor
report at bugs.python.org
Tue Jan 26 17:01:33 EST 2016
STINNER Victor added the comment:
Serhiy: "It looks to me that this optimization was added to avoid spending executing time for docstrings."
Hum, let me dig Mercurial history.
----
changeset: 39364:8ef3f8cf90e1
branch: legacy-trunk
user: Neal Norwitz <nnorwitz at gmail.com>
date: Fri Aug 04 05:09:28 2006 +0000
files: Lib/test/test_code.py Misc/NEWS Python/compile.c
description:
Bug #1333982: string/number constants were inappropriately stored
in the byte code and co_consts even if they were not used, ie
immediately popped off the stack.
---
https://bugs.python.org/issue1333982#msg26659:
"For reference, an optimization that got lost: (...) 'a' is the docstring, but the 'b' previously did not show up anywhere in the code object. Now there is the LOAD_CONST/POP_TOP pair."
Ah, it was a regression introduced by the new AST compiler. But the change introduced a new optimization: numbers are now also ignored. In Python 2.4 (before AST), numbers were not ignored:
---
>>> def f():
... "a"
... 1
... "b"
...
>>> import dis; dis.dis(f)
3 0 LOAD_CONST 1 (1)
3 POP_TOP
4 LOAD_CONST 2 (None)
7 RETURN_VALUE
---
If we continue to dig deeper, before AST, I found:
---
changeset: 4991:8276916e1ea8
branch: legacy-trunk
user: Guido van Rossum <guido at python.org>
date: Fri Jan 17 21:04:03 1997 +0000
files: Python/compile.c
description:
Add co_stacksize field to codeobject structure, and stacksize argument
to PyCode_New() argument list. Move MAXBLOCKS constant to conpile.h.
Added accurate calculation of the actual stack size needed by the
generated code.
Also commented out all fprintf statements (except for a new one to
diagnose stack underflow, and one in #ifdef'ed out code), and added
some new TO DO suggestions (now that the stacksize is taken of the TO
DO list).
---
This patch added the following code to com_expr_stmt() in Python/compile.c:
+ /* Forget it if we have just a doc string here */
+ if (NCH(n) == 1 && get_rawdocstring(n) != NULL)
+ return;
I'm unable to find the exact part of the compiler which ignores strings in statement expressions.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26204>
_______________________________________
More information about the Python-bugs-list
mailing list