[New-bugs-announce] [issue38381] Possible wordcode optimization for STORE/LOAD pairs

Raymond Hettinger report at bugs.python.org
Sat Oct 5 18:23:55 EDT 2019


New submission from Raymond Hettinger <raymond.hettinger at gmail.com>:

In the show code below, the STORE_FAST x is FOLLOWED by LOAD_FAST x.   This is a common word code pairing.  Perhaps a new combined opcode would help:

        case TARGET(LOAD_AND_STORE_FAST): {
            PyObject *value = GETLOCAL(oparg);
            if (value == NULL) {
                format_exc_check_arg(tstate, PyExc_UnboundLocalError,
                                     UNBOUNDLOCAL_ERROR_MSG,
                                     PyTuple_GetItem(co->co_varnames, oparg));
                goto error;
            }
            Py_INCREF(value);
            SETLOCAL(oparg, value);
            FAST_DISPATCH();
        }

The combined opcode saves one one trip around the eval-loop and it saves
unnecessary stack manipulations (a PUSH() immediately followed by a POP()).

The code generation would likely need to be a compiler or AST step because it crosses basic block boundaries.  Care would need to be taken to not adversely affect tracing the code in a debugger.

Note in the following code, the "x" is never used after the STORE/LOAD pair.  In theory, the two opcodes could be dropped entirely; however, would affect a call to "locals()".

-------- Code disassembly ------

>>> def f(s):
	for x in g:
		yield x**2

		
>>> dis(f)
  2           0 LOAD_GLOBAL              0 (g)
              2 GET_ITER
        >>    4 FOR_ITER                14 (to 20)
              6 STORE_FAST               1 (x)

  3           8 LOAD_FAST                1 (x)
             10 LOAD_CONST               1 (2)
             12 BINARY_POWER
             14 YIELD_VALUE
             16 POP_TOP
             18 JUMP_ABSOLUTE            4
        >>   20 LOAD_CONST               0 (None)
             22 RETURN_VALUE

----------
components: Interpreter Core
messages: 354024
nosy: rhettinger
priority: normal
severity: normal
status: open
title: Possible wordcode optimization for STORE/LOAD pairs
type: performance
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38381>
_______________________________________


More information about the New-bugs-announce mailing list