[issue22358] Unnecessary JUMP_FORWARD(0) (NOP) in if statements without else or elif

Attila Fazekas report at bugs.python.org
Mon Sep 8 01:15:51 CEST 2014


New submission from Attila Fazekas:

The following example function compiles to bytecode which contains,
an unnecessary JUMP_FORWARD 0 instruction:

def func():
    if a: pass

Actual:
dis.dis(func)
  2           0 LOAD_GLOBAL              0 (a)
              3 POP_JUMP_IF_FALSE        9
              6 JUMP_FORWARD             0 (to 9)
        >>    9 LOAD_CONST               0 (None)
             12 RETURN_VALUE  

Expected:
dis.dis(func)
  2           0 LOAD_GLOBAL              0 (a)
              3 POP_JUMP_IF_FALSE        6
        >>    6 LOAD_CONST               0 (None)
              9 RETURN_VALUE

The above JUMP_FORWARD instruction increases the code size and also has a negative performance effect.
I do not see any reason to have the extra NOP in the byte code in this case.

***

The attached patch removes this NOP generation from the code compilation part, so it will take effect by default.

I had a little trouble when the code compiled from ast,
because the If.orelse had a different content. (NULL vs. zero sizes asdl_seq)

* The generated Assembly code updated in dis unit test.
* The compilation test updated to test a real 'if' by using a variable in the condition. (The True and False is not a variable anymore)

----------
components: Interpreter Core
files: python_nop_ifelse.patch
keywords: patch
messages: 226547
nosy: Attila.Fazekas
priority: normal
severity: normal
status: open
title: Unnecessary JUMP_FORWARD(0) (NOP) in if statements without else or elif
type: performance
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5
Added file: http://bugs.python.org/file36569/python_nop_ifelse.patch

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


More information about the Python-bugs-list mailing list