Recently I was benchmarking the generated code efficiency for my C compiler antcc, and I noticed something concerning in a simple Lua benchmark:

benchmark    opt       runtime
 lua                    1.048s
 lua         -O1        1.077s
 lua         -O2        1.410s

The "optimized" builds were actually slower. Now, my compiler doesn't implement many optimizations at the moment so I wouldn't expect a huge improvement, but being 1.4x slower should definitely not happen.<sup>[1]</sup> One of the optimizations that antcc does have is inlining, which generally speaking is one of the most impactful compiler optimizations and it usually improves benchmarks all around.

So my first thought was, okay my inlining heuristics and thresholds are very simple and not really tuned, so perhaps it's being too aggressive and blowing up the code size, leading to very bad instruction cache thrashing. If I disabled inlining, the pessimization seemed to go away, which could support that theory. That's not a very satisfactory explanation though, and I would like to fix it, so I looked into it more.