Inlining - the ultimate optimisation Written by me, proof-read by an LLM. Details at end. Sixteen days in, and I’ve been dancing around what many consider the fundamental compiler optimisation: inlining. Not because it’s complicated - quite the opposite! - but because inlining is less interesting for what it does (copy-paste code), and more interesting for what it enables. Initially inlining was all about avoiding the expense of the call itself, but nowadays inlining enables many other optimisations to shine. We’ve already encountered inlining (though I tried to limit it until now): On day 8 to get the size of a vector, we called its .size() method. I completely glossed over the fact that while size() is a method on std::vector, we don’t see a call in the assembly code, just the subtraction and shift. So, how does inlining enable other optimisations? Using ARMv7, let’s convert a string to uppercase. We might have a utility change_case function that either turns a single character from upper to lower, or lower to upper, so, we’ll use it in our code: The compiler decides to inline change_case into make_upper, and then seeing that upper is always true, it can simplify the whole code to: .LBB0_1: ldrb r2, [r0] ; read next `c`; c = *string; sub r3, r2, #97 ; tmp = c - 'a' uxtb r3, r3 ; tmp = tmp & 0xff cmp r3, #26 ; check tmp against 26 sublo r2, r2, #32 ; if lower than 26 then c = c - 32 ; c = ((c - 'a') & 0xff) < 26 ? c - 32 : c; strb r2, [r0], #1 ; store 'c' back; *string++ = c subs r1, r1, #1 ; reduce counter bne .LBB0_1 ; loop if not zero There’s no trace left of the !upper case and the compiler, having inlined the code, has a fresh copy of the code to then further modify to take advantage of things it knows are true. It does a neat trick of avoiding a branch to check whether the character is uppercase: If (c - 'a') & 0xff is less than 26, it must be a lowercase character. It then conditionally subtracts 32, which has the effect of making a into A. Inlining gives th...
First seen: 2026-01-13 20:06
Last seen: 2026-01-13 22:07