Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary JUMP_FORWARD(0) (NOP) in if statements without else or elif #66554

Closed
AttilaFazekas mannequin opened this issue Sep 7, 2014 · 3 comments
Closed

Unnecessary JUMP_FORWARD(0) (NOP) in if statements without else or elif #66554

AttilaFazekas mannequin opened this issue Sep 7, 2014 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@AttilaFazekas
Copy link
Mannequin

AttilaFazekas mannequin commented Sep 7, 2014

BPO 22358
Nosy @jcea, @serhiy-storchaka
Superseder
  • bpo-11471: If without else generates redundant jump
  • Files
  • python_nop_ifelse.patch: python_nop_ifelse.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2014-09-08.07:28:36.778>
    created_at = <Date 2014-09-07.23:15:51.266>
    labels = ['interpreter-core', 'performance']
    title = 'Unnecessary JUMP_FORWARD(0) (NOP) in if statements without else or elif'
    updated_at = <Date 2014-09-08.07:28:36.776>
    user = 'https://bugs.python.org/AttilaFazekas'

    bugs.python.org fields:

    activity = <Date 2014-09-08.07:28:36.776>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-09-08.07:28:36.778>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2014-09-07.23:15:51.266>
    creator = 'Attila.Fazekas'
    dependencies = []
    files = ['36569']
    hgrepos = []
    issue_num = 22358
    keywords = ['patch']
    message_count = 3.0
    messages = ['226547', '226554', '226560']
    nosy_count = 3.0
    nosy_names = ['jcea', 'serhiy.storchaka', 'Attila.Fazekas']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '11471'
    type = 'performance'
    url = 'https://bugs.python.org/issue22358'
    versions = ['Python 3.5']

    @AttilaFazekas
    Copy link
    Mannequin Author

    AttilaFazekas mannequin commented Sep 7, 2014

    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)

    @AttilaFazekas AttilaFazekas mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Sep 7, 2014
    @jcea
    Copy link
    Member

    jcea commented Sep 8, 2014

    What about the peephole optimizer?.

    @serhiy-storchaka
    Copy link
    Member

    This is a duplicate of bpo-11471.

    Explicit check for NULL is not needed because the asdl_seq_LEN() macro checks its argument for NULL.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants