There are literally thousands of compiler engineers who pore over the assembly a compiler generates, and then finds ways to make it better. I get paid to figure out where the compiler can do better, and often a step in that is to hand-code my own replacement. I then teach the compiler to do that.<p>But even beyond that, the compiler can't make certain assumptions that an assembly writer can. Such as whether a callee-saved register really does need to be saved in some particular routine. Or even pushing an extra parameter in unusual cases.<p>So it is entirely possible to beat the compiler, it's doable under certain circumstances, even today.<p>But you also have the danger of your loving hand-crafted assembly beating the compiler today. But next year the compiler is even smarter, the hardware may have changed in subtle ways, and the compiler will know and improve the code it generates.<p>Your hand-written code won't change unless you revisit it.
> Such as whether a callee-saved register really does need to be saved in some particular routine.<p>Ironically this is your preconceived abstraction of how a compiler has to operate. An ideal compiler could allocate registers differently for each called function: F1()->F2()->F3(), F1 uses r0-5, F2 uses r6-10, F3 uses r11-15, no register saving required in the whole chain. There's no need for a fixed ABI. Such a compiler would look very different from today's ones.
Yeah, that's what MSVC and GCC did on x86, called "custom calling conventions" on MSVC and the regparm attribute on GCC.<p>All of these were dropped on x64, on x64 (and ARM) you get standard calling conventions for just about everything with proper unwind tables for functions.<p>It doesn't really "cheat" on the registers unless it inlines a function entirely. LLVM has support for custom calling conventions and pragmas to specify them, this is used by GHC on Haskell and other things, but it's practically unheard of in "normal" C/C++ code.
[deleted]
What do you think the most impactful savings from hand-written assembly are over optimized Rust today?
Now I'm wondering if Claude can beat the compiler if I ever need to optimize something a bit more.
> In modern times, everyone knows that writing assembly is a fool's errand<p>ffmpeg is like 10% assembly. I think something similar is true of all video encoders. OpenSSL and libsodium write some of their core math routines in assembly (e.g,. NTT).<p>So maybe this myth should die?
Previous discussion from 2024 (linked in the "Post-publication notes" section):<p><a href="https://news.ycombinator.com/item?id=40948353">https://news.ycombinator.com/item?id=40948353</a>
Compilers are pretty good. Really good, even. But languages, even C, are abstractions, which necessarily constrain the level below. This threaded-code jump thing is just not possible to express in C. Even the best abstraction can often be beaten by something the abstraction can't express. Self-modifying code is one example. So is this threaded interpreter with its non-structured control flow.<p>But it takes longer. It's more difficult. That's why the abstraction exists and is still very useful despite its limitations.