Hacker News new | ask | show | jobs
by resonious 980 days ago
If that's the case, then I imagine the inline keyword would have the same effect?
3 comments

You need to use a compiler specific "always inline" directive if you want macro-like functionality of actually inlining code.

On its own, the C++ "inline" keyword does not cause inlining to happen, although compilers may treat it like a hint depending on optimization level. GCC does not inline an "inline" function on O0.

"Inline" in the C++ standard means "multiple definitions permitted" so the same entity can exist in multiple translation units without upsetting the linker. This is why C++17 added "inline" variables, which can be initialized in a header that's included in multiple places, even though the inlining concept has no applicability to a variable. The keyword was adopted for this purpose because of the primary association with affecting linkage behavior.

This isn’t an optimization you should consider without insight into your actual bottlenecks, but compilers can be even more aggressive with code that’s strictly local to one translation unit (i.e. inside an anonymous namespace in a cpp file) than they typically would be when seeing an inline hint elsewhere.

It’s not quite the same.

Plus, another benefit of duplication is that you can more freely hand-tune your implementation once you’ve decided its private. Memory alignment, pointer aliasing hints, clever loop structures, SSE stuff, etc can all be used more freely when you know nothing else needs to use this version.

The article is a good teaser around how unintuitive optimization can be, but it only scratches the surface.

yeah, languages which can do inking optimizations do this for you, sometimes even without you knowing.