Visitar URL original
Micro-optimize the LOAD_FAST opcode by sweeneyde · Pull Request #92763 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20220514014847/https://github.com/python/cpython/pull/92763
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

Micro-optimize the LOAD_FAST opcode #92763

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft

Conversation

sweeneyde
Copy link
Member

@sweeneyde sweeneyde commented May 13, 2022

The most common opcode before:

TARGET_LOAD_FAST:
    frame->prev_instr = next_instr++;
    PyObject *value = frame->localsplus[oparg]
    if (value == NULL) { goto unbound_local_error; }
    value->ob_refcnt++;
    *stack_pointer++ = value;
    _Py_CODEUNIT word = *next_instr;
    opcode = word & 255;
    oparg = word >> 8;
    opcode |= cframe.use_tracing;
    goto *opcode_targets[opcode];

The most common opcode after:

TARGET_LOAD_FAST_KNOWN_QUICK:
    next_instr++;
    PyObject *value = frame->localsplus[oparg]
    value->ob_refcnt++;
    *stack_pointer++ = value;
    _Py_CODEUNIT word = *next_instr;
    opcode = word & 255;
    oparg = word >> 8;
    goto *opcode_targets[opcode];

In particular:

  • The write to frame->prev_instr is removed.
  • The NULL-check and branch are removed.
  • The memory read and |= are removed.

None of these were particularly significant in isolation, but together, they accounted for an approximately 1% speedup in pyperformance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants