Not sure I follow, isn't Python single threaded by default? Changes to GIL is coming but does it change how the interpreter uses CPU?
Not sure I follow, isn't Python single threaded by default? Changes to GIL is coming but does it change how the interpreter uses CPU?
To back up parent's point, if you compile the code and the resulting assembly is a direct translation, renaming will break the dependency and the CPU will execute the instructions in parallel. Write after read hazard is the applicable section:
This is much more likely a quirk of the interpreter (or possibly a fucked up test). CPU details are like 10000 feet down.
A CPU can do more than one thing at once by computing the next instruction while it's still writing the result of the previous one. However, the CPU can only do that if it's 100% sure that the next instruction does not depend on the previous instruction. This optimization sometimes can't trigger in an interpreter, because of highly mutable variables such as the program counter or the top of the interpreter stack. Fun illustration: https://www.youtube.com/watch?v=cMMAGIefZuM&t=288s
I guess the first thing worth doing when analyzing this would be looking at the differences in the bytecode, then looking at the C code implementing the differing bytecode ops. But there also other factors, like the new adaptive interpreter trying to JIT the code.
Single-line dual assignment:
2 2 LOAD_FAST 0 (a2)
4 LOAD_FAST 1 (c)
6 BINARY_OP 0 (+)
10 LOAD_FAST 1 (c)
12 LOAD_CONST 1 (2)
14 BINARY_OP 0 (+)
18 STORE_FAST 1 (c)
20 STORE_FAST 0 (a2)
vs the two-line version: 2 2 LOAD_FAST 0 (a2)
4 LOAD_FAST 1 (c)
6 BINARY_OP 13 (+=)
10 STORE_FAST 0 (a2)
7 12 LOAD_FAST 1 (c)
14 LOAD_CONST 1 (2)
16 BINARY_OP 13 (+=)
20 STORE_FAST 1 (c)