3 ms·
TL;DR: If you copy paste the same implementation code in different branches, you give the compiler opportunities to generate faster code for each case it wouldn
by voidstarcpp 3y ago
TL;DR: If you copy paste the same implementation code in different branches, you give the compiler opportunities to generate faster code for each case it wouldn't have otherwise generated, without you having to do any manual optimization work.
- spiritplumber 3y agothank you
- resonious 3y agoIf that's the case, then I imagine the inline keyword would have the same effect?
- lionkor 3y agoyeah, languages which can do inking optimizations do this for you, sometimes even without you knowing.
- swatcoder 3y agoThis 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.
- voidstarcpp 3y agoYou 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.
- jrumbut 3y agoThis is really an excellent programming technique article. I think what makes it so great is that your examples hit a sweet spot of simplicity while still being motivating and you show how to get a "good enough" solution very quickly.