I am struggling to understand how much 'optimizing' is the Python compiler. I did a few tests, for example consider this really simple script:
def to_optimize():
for i in range(10000):
a = 1
b = 2
c = a + b
print(c)
When I import this function from another script and disassemble it I get:
>>> dis.dis(optimization.to_optimize)
2 0 LOAD_GLOBAL 0 (range)
2 LOAD_CONST 1 (10000)
4 CALL_FUNCTION 1
6 GET_ITER
>> 8 FOR_ITER 28 (to 38)
10 STORE_FAST 0 (i)
3 12 LOAD_CONST 2 (1)
14 STORE_FAST 1 (a)
4 16 LOAD_CONST 3 (2)
18 STORE_FAST 2 (b)
5 20 LOAD_FAST 1 (a)
22 LOAD_FAST 2 (b)
24 BINARY_ADD
26 STORE_FAST 3 (c)
6 28 LOAD_GLOBAL 1 (print)
30 LOAD_FAST 3 (c)
32 CALL_FUNCTION 1
34 POP_TOP
36 JUMP_ABSOLUTE 8
>> 38 LOAD_CONST 0 (None)
40 RETURN_VALUE
As you can see almost no optimization at all is applied. I think a piece of code like this would be very easy to optimize more. I know that Python compiles 'in-time', so I do not expect the .pyc files created during a normal execution to be really optimized, but even when I pre-compile the script with python3 -m compileall the result is the same, and no additional optimization flag exists.
Is this really the maximum optimization achievable for a Python script? Is there any language limitation?