Skip to content
DevMeme
6044 of 7590
Performance Post #6617 · source on Telegram

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

30
Anonymous ★ Top Pick 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
  1. Anonymous ★ Top Pick

    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

  2. Anonymous

    Teach a junior the +/ - swap and he’ll DOS the cache; hand him std::swap and the TLB lives to see another sprint

  3. Anonymous

    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

  4. Anonymous

    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

  5. Anonymous

    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

  6. Anonymous

    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"

  7. Anonymous

    Ah, a=b; b=a; - the swap that doesn't, but nukes your µop cache and sends Dennis Ritchie tumbling through the page table

  8. @kitbot256 1y

    Such code in production usually earns a good whipping at the stables

  9. @GLXBX 1y

    a ^= b; b ^= a; a ^= b;

    1. @saniel42 1y

      You done did it now

  10. @GLXBX 1y

    Did it wrong second time

  11. @GLXBX 1y

    No

  12. @GLXBX 1y

    a -> A xor B B xor A xor B is A A xor B xor A is B

  13. @GLXBX 1y

    Good

  14. @GLXBX 1y

    No mistake

  15. @Algoinde 1y

    who the FUCK needs to swap variables

    1. @RiedleroD 1y

      idk happens sometimes

      1. @Algoinde 1y

        literally never happened

        1. @azizhakberdiev 1y

          we do a little sorting

    2. @Sp1cyP3pp3r 1y

      i wanna

  16. @kitbot256 1y

    👌

  17. D Y 1y

    Pfff hold my bear : [a, b] =[b, a]

    1. @Algoinde 1y

      hold my array allocation

      1. D Y 1y

        And my axe!

  18. @blade_prime 1y

    Tuple assignment (a, b) = (b, a)

    1. losiwo 1y

      That's some high level stuff

  19. @Mikle_Bond 1y

    c = a ^ b a = c ^ a b = c ^ b

    1. @Artkash 1y

      But what's the point if you're using the third variable?

  20. @azizhakberdiev 1y

    iirc local variables will be reduced to registers

  21. @blade_prime 1y

    > manually swapping the registers

Use J and K for navigation