On May 2, 2016 7:55 AM, "Robert van Geel" <robert@bign.nl> wrote:
>
> I'm receiving digests and seem to not have each individual mail, so here's a digested response:
>
> One side of this that's not discussed is the possible optimization. I can imagine if you've got a lot of objectproperty write and -reads this could actually make certain use cases a lot faster such as this one, saving 6 opcodes:
>
> def __init__(self, left, top, width, height, content):
> self.left = left
> self.top = top
> self.width = width
> self.height = height
> self.content = content.upper()
> self.decideColors()
> self.draw()
>
> versus:
>
> def __init__(self, left, top, width, height, content):
> with self:
> .left = left
> .top = top
> .width = width
> .height = height
> .content = content.upper()
> .decideColors()
> .draw()
>
> The suggestion that you could accomplish this with a (peephole) optimizer does not seem quite correct to me:
>
> x = myobject.b()
> ...
> z = myobject.c()
>
> does not necessarily have a consistent pointer to myobject although it would require some acrobatics to change them in a way that can not be seen by an optimizer.
> You can think about exec() or even another thread intervening into a generator function, not something I would do but who knows.
It's actually part of CPython's current semantics that local variables cannot be modified via stack introspection exactly because it allows certain optimizations. You can read them, it might look like you can touch them (you can get access to what's allegedly the real dict of locals), but this is an illusion: modifications to that dict are not reflected back to the running code. See PyFrame_LocalsToFast and vice versa. So teaching the peephole optimizer to do this would be legal afaict.
I'd be surprised and interested to see an example where those 6 opcodes made any non-trivial difference to a real program though -- thanks to the above-mentioned optimizations, accessing a local variable is already one of CPython's fastest operations.
-n