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

Optimize LOAD_NAME bytecode #69743

Closed
vstinner opened this issue Nov 5, 2015 · 7 comments
Closed

Optimize LOAD_NAME bytecode #69743

vstinner opened this issue Nov 5, 2015 · 7 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@vstinner
Copy link
Member

vstinner commented Nov 5, 2015

BPO 25557
Nosy @rhettinger, @vstinner, @benjaminp, @serhiy-storchaka
Files
  • pydict_loadname.patch
  • pydict_loadname-2.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 2015-11-20.08:28:57.355>
    created_at = <Date 2015-11-05.10:11:19.444>
    labels = ['interpreter-core', 'performance']
    title = 'Optimize LOAD_NAME bytecode'
    updated_at = <Date 2015-11-20.08:58:24.586>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2015-11-20.08:58:24.586>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-11-20.08:28:57.355>
    closer = 'vstinner'
    components = ['Interpreter Core']
    creation = <Date 2015-11-05.10:11:19.444>
    creator = 'vstinner'
    dependencies = []
    files = ['40950', '40954']
    hgrepos = []
    issue_num = 25557
    keywords = ['patch']
    message_count = 7.0
    messages = ['254097', '254101', '254102', '254112', '254956', '254957', '254959']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'vstinner', 'benjamin.peterson', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue25557'
    versions = ['Python 3.6']

    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 5, 2015

    The LOAD_GLOBAL bytecode has a fast-path when globals and builtins have exactly the type 'dict'. It calls the _PyDict_LoadGlobal() function.

    I propose to implement a similar optimization for LOAD_NAME, see attached patch.

    The patch also fixes LOAD_GLOBAL and LOAD_NAME bytecodes when locals, globals or builtins are not exactly the type 'dict'. It clears the KeyError before trying the next PyObject_GetItem().

    The patch changes also _PyDict_LoadGlobal() to call PyObject_Hash() if the hash was not computed yet. It might make it a little bit faster.

    @vstinner vstinner added interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Nov 5, 2015
    @serhiy-storchaka
    Copy link
    Member

    When LOAD_NAME is generated? Is it worth to optimize this case? Aren't LOAD_FAST and LOAD_GLOBAL used in performance critical code?

    It looks to me that there is a bug in fast path of _PyDict_LoadGlobal. If the first lookup fails, it can raise an exception. We have to add

    if (PyErr_Occurred())
        return NULL;
    

    before the second lookup.

    Just for reference, the fast path for LOAD_GLOBAL was added in 8f8fe990e82c.

    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 5, 2015

    When LOAD_NAME is generated? Is it worth to optimize this case? Aren't LOAD_FAST and LOAD_GLOBAL used in performance critical code?

    I guess that it's only used to execute the body of modules.

    Is it worth to optimize this case?

    Hum, I don't know :-)

    It looks to me that there is a bug in fast path of _PyDict_LoadGlobal. If the first lookup fails, it can raise an exception.

    Sorry, where exactly? Can you maybe put a comment on the review? I see many checks to handle errors.

    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 5, 2015

    Rebased patch (ceval.c was modified by the changeset of c1414f80ebc9, issue bpo-25556).

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Nov 20, 2015

    New changeset c35d65c9ded3 by Victor Stinner in branch 'default':
    Issue bpo-25557: Refactor _PyDict_LoadGlobal()
    https://hg.python.org/cpython/rev/c35d65c9ded3

    @vstinner
    Copy link
    Member Author

    Since LOAD_NAME is rare, I don't think that it's worth to optimize it.

    I pushed a partial version of pydict_loadname-2.patch to add comments to the code.

    @serhiy-storchaka
    Copy link
    Member

    Thanks Victor.

    @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