[Python-Dev] Possible optimization for LOAD_FAST ?

Benjamin Peterson benjamin at python.org
Tue Dec 28 18:24:36 CET 2010


2010/12/28 Lukas Lueg <lukas.lueg at googlemail.com>:
> Consider the following code:
>
> def foobar(x):
>    for i in range(5):
>        x[i] = i
>
> The bytecode in python 2.7 is the following:
>
>  2           0 SETUP_LOOP              30 (to 33)
>              3 LOAD_GLOBAL              0 (range)
>              6 LOAD_CONST               1 (5)
>              9 CALL_FUNCTION            1
>             12 GET_ITER
>        >>   13 FOR_ITER                16 (to 32)
>             16 STORE_FAST               1 (i)
>
>  3          19 LOAD_FAST                1 (i)
>             22 LOAD_FAST                0 (x)
>             25 LOAD_FAST                1 (i)
>             28 STORE_SUBSCR
>             29 JUMP_ABSOLUTE           13
>        >>   32 POP_BLOCK
>        >>   33 LOAD_CONST               0 (None)
>             36 RETURN_VALUE
>
> Can't we optimize the LOAD_FAST in lines 19 and 25 to a single load
> and put the reference twice on the stack?

Yes. Would it be useful? Unlikely.

-- 
Regards,
Benjamin


More information about the Python-Dev mailing list