Author rhettinger
Recipients rhettinger
Date 2019-10-05.22:23:55
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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:

            PyObject *value = GETLOCAL(oparg);
            if (value == NULL) {
                format_exc_check_arg(tstate, PyExc_UnboundLocalError,
                                     PyTuple_GetItem(co->co_varnames, oparg));
                goto error;
            SETLOCAL(oparg, value);

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
Date User Action Args
2019-10-05 22:23:55rhettingersetrecipients: + rhettinger
2019-10-05 22:23:55rhettingersetmessageid: <>
2019-10-05 22:23:55rhettingerlinkissue38381 messages
2019-10-05 22:23:55rhettingercreate