[issue6250] Python compiles dead code

James Abbatiello report at bugs.python.org
Mon Jun 15 21:08:28 CEST 2009


James Abbatiello <abbeyj at gmail.com> added the comment:

I should add that the patch doesn't only address dead user-code.  It
also eliminates code that the compiler generates itself but that would
be unreachable at runtime.  Consider the following function:

def foo(x):
  if x:
    raise Something
  else:
    raise SomethingElse


Without the patch this would compile to:
  2           0 LOAD_FAST                0 (x)
              3 POP_JUMP_IF_FALSE       15

  3           6 LOAD_GLOBAL              0 (Something)
              9 RAISE_VARARGS            1
             12 JUMP_FORWARD             6 (to 21)

  5     >>   15 LOAD_GLOBAL              1 (SomethingElse)
             18 RAISE_VARARGS            1
        >>   21 LOAD_CONST               0 (None)
             24 RETURN_VALUE


With the patch this slims down to:

  2           0 LOAD_FAST                0 (x)
              3 POP_JUMP_IF_FALSE       12

  3           6 LOAD_GLOBAL              0 (Something)
              9 RAISE_VARARGS            1

  5     >>   12 LOAD_GLOBAL              1 (SomethingElse)
             15 RAISE_VARARGS            1


So there are benefits even for normal code.

Also the END_FINALLY handling would be really hard to do in a pure
bytecode walker since context from the AST is needed (am I in a
try-finally or try-except or with statement?)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6250>
_______________________________________


More information about the Python-bugs-list mailing list