Issue 10648: Extend peepholer to reverse loads or stores instead of build/unpack (original) (raw)

This modifies the peepholer's BUILD/UNPACK_SEQUENCE for the case when all stores are simple, or all loads are simple

It first scans to see if the pushing is done with simple LOADs. If so, it reverses the loads and removes the build unpack. If not, it scans ahead to see if it is followed by simple STOREs. If so, it reverses the stores and removes the build unpack

Thanks for the patch. I had looked at this long ago when I first added the ROT2 optimization and the ROT3/ROT2 optimization. It wasn't included because it wasn't worth the added complexity in the peepholer logic and because there were concerns about executing internally in a different order than specified by the code.

Since LOAD_NAME and LOAD_GLOBAL are subject to user control, changing their order of evaluation causes a visible change in semantics. For example, the following result is different than before the patch.

class Dict(dict): ... def getitem(self, key): ... print(key) ... return dict.getitem(self, key) ... ns = Dict() exec('c=1; d=2; a,b=c,d', globals(), ns) d c

For the most part, I'm not too excited about the patch because