New submission from STINNER Victor:
The bytecode compilers ignores ast.Str and ast.Num nodes:
----------------------------
>>> def func():
... 123
... "test"
...
>>> import dis; dis.dis(func)
3 0 LOAD_CONST 0 (None)
3 RETURN_VALUE
----------------------------
But other ast nodes which are constant are not ignored:
----------------------------
>>> def func2():
... b'bytes'
... (1, 2)
...
>>> import dis; dis.dis(func2)
2 0 LOAD_CONST 1 (b'bytes')
3 POP_TOP
3 4 LOAD_CONST 4 ((1, 2))
7 POP_TOP
8 LOAD_CONST 0 (None)
11 RETURN_VALUE
----------------------------
I don't understand the point of loading a constant and then unload it (POP_TOP).
Attached patch changes the compiler to not emit LOAD_CONST+POP_TOP anymore.
My patch only affects constants. Example with the patch:
----------------------------
>>> def f():
... x
...
>>> import dis
>>> dis.dis(f)
2 0 LOAD_GLOBAL 0 (x)
3 POP_TOP
4 LOAD_CONST 0 (None)
7 RETURN_VALUE
----------------------------
The compiler still emits "LOAD_GLOBAL x" for the instruction "x".
Ignoring the Python instruction "x" would change the Python semantics, because
the function would not raise a NameError anymore if x is not defined.
Note: I noticed this inefficient bytecode while working on the issue #26146
(add ast.Constant).
----------
components: Interpreter Core
files: compiler.patch
keywords: patch
messages: 258939
nosy: haypo, yselivanov
priority: normal
severity: normal
status: open
title: compiler: don't emit LOAD_CONST instructions for constant expressions?
versions: Python 3.6
Added file: http://bugs.python.org/file41716/compiler.patch
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue26204>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com