[issue38381] Possible wordcode optimization for STORE/LOAD pairs

2020-12-21 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38381] Possible wordcode optimization for STORE/LOAD pairs

2019-10-06 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

I thought about this. But STORE_FAST x is followed by LOAD_FAST x just by 
accident. If you change the expression (1+x**2 or f(x**2)) they no longer be 
neighbors. I am not sure this pattern is common enough. There are more common 
pairs.

Also, note that LOAD_FAST belong to the different line of code. It should be 
preserved for debugging purpose. Otherwise you could not set a breakpoint on 
the first line of the loop body.

--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38381] Possible wordcode optimization for STORE/LOAD pairs

2019-10-05 Thread Raymond Hettinger


New submission from Raymond Hettinger :

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_ITER14 (to 20)
  6 STORE_FAST   1 (x)

  3   8 LOAD_FAST1 (x)
 10 LOAD_CONST   1 (2)
 12 BINARY_POWER
 14 YIELD_VALUE
 16 POP_TOP
 18 JUMP_ABSOLUTE4
>>   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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com