@wolf480pl @LukeAlmighty @SuperDicq So by using a temp variable, a and b will be used where needed, executed in parallel by the CPU, as the swap is basically optimized away by the LLVM? 🤔
@coded_artist @LukeAlmighty @SuperDicq
If you use arithmetic tricks to swap two values, anything that uses any of the two values in the future depends on those swap instructions, which in turn depend on both the instructions that calculated a, and the instructions that calculated b.
That may prevent CPU from doing things in parallel, or reordering instructions, which again, makes your code slow.
7/
@coded_artist @LukeAlmighty @SuperDicq
Branch misprediction is when it guesses incorrectly, and it costs a lot of time.
If a value used inside an if condition is known some time before the execution reaches the if, that might make branch prediction easier for a sufficiently smart CPU.
But when you obscure which value is which by using arithmetic to swap two variables, the CPU might not be able to see through that, resulting in frequent mispredictions, and making your code slow.
5/
@coded_artist @LukeAlmighty @SuperDicq
The SSA form is a type of internal representation of a program inside a compiler, in which instructions do not have a destination operand. Instead, every instruction creates a new variable with its result, and other instructions can use that as their source operands, but they cannot modify that variable
This means there are lots of temporary variables, so LLVM must be very good at optimizing them out.
Which means adding a temp var in your code is cheap
2/
I draw, code, and make memes sometimes.