[Patches] [ python-Patches-1144842 ] Replace store/load pair with a single new opcode

SourceForge.net noreply at sourceforge.net
Mon Feb 21 17:12:51 CET 2005


Patches item #1144842, was opened at 2005-02-20 10:41
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1144842&group_id=5470

Category: Parser/Compiler
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Nobody/Anonymous (nobody)
Summary: Replace store/load pair with a single new opcode

Initial Comment:
The folds the two steps into a new opcode.  In the case
of store_name/load_name, it saves one three byte
instruction, a trip around the eval-loop, two stack
mutations, a incref/decref pair, a dictionary lookup,
and an error check (for the lookup).  While it acts
like a dup followed by a store, it is implemented more
simply as a store that doesn’t pop the stack.  The
transformation is broadly applicable and occurs
thousands of times in the standard library and test suite.

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2005-02-21 11:12

Message:
Logged In: YES 
user_id=80475

Thanks for looking at the patch.

Added patch to Doc/lib/libdis.tex.

Added a comment and assertion documenting how the pattern
recognizer stays within the string boundaries (it uses
RETURN_VALUE as a guard).

The transformations are invariant with respect to the
preconditions and postconditions (in terms of stack effect
and dictionary state).  They change the how without changing
the what.   In this case, saving the unnecessary dictionary
lookup is the point of the transformation.  The code
generator is free to produce STORE x LOAD x or the
equivalent DUP STORE x.  This is the lead example in Skip's
paper on the subject: 
http://www.foretec.com/python/workshops/1998-11/proceedings/papers/montanaro/montanaro.html

The pattern is generated by code in the form:
  x = f()
  y = x + 1

The postcondition states of x and y remain unchanged by the
transformation.  The other peepholer transforms work the
same way (i.e. the condition jump to conditional jump
simplification results in fewer comparisons because the
results of identical comparisons are expected to be the same).

There is a limit to this.  For instance, x * 2 cannot be
replaced with x + x because it results in a different method
being called.  The multiply call is guaranteed.  This
contrasts with the incidental getitem call in x=f(); y=x+1
where the compiler guarantees the call to f() and the final
states of x and y.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2005-02-20 18:24

Message:
Logged In: YES 
user_id=21627

Please add a patch to Doc/lib/libdis.tex. Also, it would be
good if a comment explained why these (all of the peephole
optimizations) can never read over the end of the string.

It appears that this patch introduces a change in semantics
for STORE_NAME if f_locals is not a dictionary - with the
patch, the dictionary-like object will see one less call to
GetItem.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1144842&group_id=5470


More information about the Patches mailing list