a,b = 2,3 and [a,b] = [2,3]

Serhiy Storchaka storchaka at gmail.com
Tue Sep 3 04:43:25 EDT 2019


03.09.19 11:02, Chris Angelico пише:
> On Tue, Sep 3, 2019 at 5:53 PM Serhiy Storchaka <storchaka at gmail.com> wrote:
>>
>> 02.09.19 12:24, Chris Angelico пише:
>>> But the curious difference happens in 3.7. I don't know what changed
>>> to cause this, but from there on, the list gets built and then
>>> unpacked.
>>
>> This was a side effect of moving the optimization for `x in [a, b]` from
>> the peepholer to the AST optimizer.
>>
> 
> Ah ha. Thank you.
> 
> Is it worth trying to reinstate that? On the one hand, there's no
> reason to build the list, and this technically is a (performance)
> regression. On the other hand, it's not code people actually write,
> since you can do the same thing with a tuple and it IS optimized.

I tried this.

Adding such optimization on the AST level will add around 30 lines of 
code, but will allow to optimize more complex cases like `[a, b] = [c, 
d] = [1, 2]` or `[a, *b] = [*c, d] = [1, 2]`.

Reintroducing it in the peepholer needs less changes, but will optimize 
only narrow set of special cases.

In any case the benefit looks too small. It may even be that the 
original optimization was not intended, but a side effect of the 
optimization for `x in [a, b]`.




More information about the Python-list mailing list