[Python-ideas] Allow "assigning" to Ellipse to discard values.

Andrew Barnert abarnert at yahoo.com
Tue Feb 10 22:45:02 CET 2015


On Feb 10, 2015, at 13:33, random832 at fastmail.us wrote:

> On Tue, Feb 10, 2015, at 16:23, Chris Barker wrote:
>> granted, the _ sticks around and keeps the object alive, which may cause
>> issues. But is it often a big deal?
> 
> Is cpython capable of noticing that the variable is never referenced (or
> is only assigned) after this point and dropping it early? Might be a
> good improvement to add if not.

This seems pretty simple in concept, but a lot of code. Currently CPython only runs a keyhole optimizer step; you'd need a whole-function optimizer step that runs after each function definition is complete. But then all it has to do is look for any locals that are never referenced and remove all assignments to them, skipping any functions that use eval of course. (This wouldn't get rid of unnecessary repeated assignments to global, closure, item, or attr, but that wouldn't matter for this use case.)

To play with this and see how much benefit you'd get, someone could write this pretty simply from Python with byteplay (especially since you don't have to write anything fully general and safe for the test) and run some timeits. (Of course to measure how much it slows down the compiler, and how much of your coding time it takes, and how risky the code looks, you'd have to actually do the work, but knowing how much benefit you're talking about may be enough information to guess that the cost isn't worth it.)


More information about the Python-ideas mailing list