The catastrophic cost of a junior's 'clever' variable swap
Description
A multi-panel meme using the 'Breaking Bad' format with characters Jesse Pinkman and Walter White. The top panel sets up a conversation: Jesse says, 'YO MISTER WHITE! someone's at the door,' and Walter, with a cool demeanor, replies, 'who is it.' The bottom panel delivers the punchline. It first shows a snippet of C-like code commented '// Swapping without using a third variable', which then implements a variable swap using three arithmetic operations (a=a+b; b=a-b; a=a-b;). Below the code, an enraged Walter White is shown yelling an extremely long, dense, and highly technical paragraph. The rant explains in excruciating detail why this 'clever' code is disastrous for performance on modern CPUs, mentioning concepts like integer overflow, LLVM compiler pipelines, speculative execution, out-of-order execution, L1 cache prefetch logic, instruction-level parallelism, and cache locality. The humor comes from the extreme and deeply knowledgeable overreaction to a common 'code golf' trick, perfectly capturing the frustration of a senior/principal engineer explaining to a junior why their attempt to be clever has backfired and ignored decades of compiler and hardware optimization
Comments
30Comment deleted
This code is the interview question equivalent of a honeypot. You think you're showing how smart you are, but you're actually just revealing you've never had to debug a performance-critical path in production
Teach a junior the +/ - swap and he’ll DOS the cache; hand him std::swap and the TLB lives to see another sprint
The real crime here isn't cooking meth, it's cooking up 'clever' swap algorithms that make the CPU pipeline wish it could enter witness protection. Walter's transformation from high school chemistry teacher to compiler optimization evangelist is the character arc we didn't know we needed
When your 'clever' integer swap trick from a 1980s C programming book becomes a masterclass in how to single-handedly defeat three decades of compiler optimization research. Modern CPUs have spent billions of transistors building speculative execution pipelines, out-of-order execution units, and sophisticated branch predictors - only to have it all brought to its knees by someone who thought saving one temporary variable was worth destroying instruction-level parallelism. The compiler team spent years implementing SSA form and register allocation algorithms, but sure, your arithmetic swap that creates serial dependencies and prevents micro-op fusion is definitely the optimization we needed. Dennis Ritchie is indeed rolling in his grave, but not for the reasons you think - he's trying to escape the code review
Use a temp - otherwise your 'optimization' converts a wide OoO core into a nostalgic in-order 6502, and LLVM still has to clean up the mess
The only thing your no‑temp a+b swap saves is a stack slot the optimizer will resurrect as a phi node - while you pay with pipeline bubbles, a confused prefetcher, and a perf flamegraph that just says "why"
Ah, a=b; b=a; - the swap that doesn't, but nukes your µop cache and sends Dennis Ritchie tumbling through the page table
Such code in production usually earns a good whipping at the stables Comment deleted
a ^= b; b ^= a; a ^= b; Comment deleted
You done did it now Comment deleted
Did it wrong second time Comment deleted
No Comment deleted
a -> A xor B B xor A xor B is A A xor B xor A is B Comment deleted
Good Comment deleted
No mistake Comment deleted
who the FUCK needs to swap variables Comment deleted
idk happens sometimes Comment deleted
literally never happened Comment deleted
we do a little sorting Comment deleted
i wanna Comment deleted
👌 Comment deleted
Pfff hold my bear : [a, b] =[b, a] Comment deleted
hold my array allocation Comment deleted
And my axe! Comment deleted
Tuple assignment (a, b) = (b, a) Comment deleted
That's some high level stuff Comment deleted
c = a ^ b a = c ^ a b = c ^ b Comment deleted
But what's the point if you're using the third variable? Comment deleted
iirc local variables will be reduced to registers Comment deleted
> manually swapping the registers Comment deleted