5 ms·
IMO the reason the compiler doesn't add special cases for the simplest version is that it doesn't know which of its _many_ special cases to use. If you actually
by drmikeando 3y ago
IMO the reason the compiler doesn't add special cases for the simplest version is that it doesn't know which of its _many_ special cases to use. If you actually use the unoptimised version of the code like
void withSwitch(vector<int>& Values, bool v) {
if (v) {
multiply1(Values, 2.0);
} else {
multiply1(Values, 3.0);
}
}
Then it actually inlines the code and optimises each one correctly, as it has context about which special cases are available. (Doesn't even need the `inline` keyword for this at `-O2`)
You can see the code here: https://godbolt.org/z/5beeYe77a https://godbolt.org/z/5beeYe77a
- voidstarcpp 3y agoThis is possible if the call site can see the implementation, but you can't count on it for separate translation units or larger functions. My goal was to not rely on site-specific optimization and instead have one separately compiled function body that can be improved for common cases. Certainly, once the compiler has a full view of everything it can take advantage of information as it pleases but this is less controllable. If I were really picky about optimizing for each use I would make it a template. >Doesn't even need the `inline` keyword for this at `-O2` The inline keyword means little in terms of actually causing inlining to happen. I would expect the majority of inlining compilers do happens automatically on functions that lack the "inline" keyword. Conversely, programmers probably add "inline" as an incantation all over the place not knowing that compilers often ignore it.
- chii 3y agowould it have made a difference if the function was static? The compiler would then be able to deduce that it isn't used anywhere else, and thus could do this inline optimization.
- thesnide 3y agoGcc actually totally does that. Even doing it while not static by copy/pasting the function and enabling further optims. to me modern compilers are like voodoo magic
- Thorrez 3y ago>Conversely, programmers probably add "inline" as an incantation all over the place not knowing that compilers often ignore it. Funnily, the inline keyword actually has a use, but that use isn't to tell the compiler to inline a function. The use is to allow a function (or variable) to be defined in multiple translations units without being an ODR violation.
- maccard 3y agoMSVC treats inline as a hint [0] , GCC is ambiguous [1] but I read it as utilising the hint. My understanding of clang [2] is that it tries to match GCC, which would imply that it applies. [0] https://learn.microsoft.com/en-us/cpp/build/reference/ob-inline-function-expansion?view=msvc-170 https://learn.microsoft.com/en-us/cpp/build/reference/ob-inl... [1] https://gcc.gnu.org/onlinedocs/gcc/Inline.html https://gcc.gnu.org/onlinedocs/gcc/Inline.html [2] https://clang.llvm.org/compatibility.html https://clang.llvm.org/compatibility.html
- rileymat2 3y agoWhat the OP is saying is that it has a use to satisfy (https://en.cppreference.com/w/cpp/language/definition https://en.cppreference.com/w/cpp/language/definition) and in that case it is not optional and not ignored by the compiler. Whether it will "actually" inline it is another matter, that is optional.
- lifthrasiir 3y agoYou can also force it by using extensions like `[[gnu::always_inline]]` or `__forceinline`. I've actually used this technique to generate an auto-vectorizable function whenever it's possible, without any code duplication [1]. [1] https://github.com/lifthrasiir/j40/blob/252e7987d36d50f617f29e94eeeb741198adc617/j40.h#L5764-L5856 https://github.com/lifthrasiir/j40/blob/252e7987d36d50f617f2...
- cxr 3y ago> the reason the compiler doesn't add special cases for the simplest version is that it doesn't know which of its _many_ special cases to use This is a fact claim. I'm having trouble parsing the "IMO" that's a preface to your comment.
- deredede 3y agoThe "IMO" here is used instead of "I think" or "I believe" to indicate that this is not a fact claim but a (presumably educated) guess. Not a very correct use of "IMO", technically, but a fairly common one nonetheless.
- cxr 3y agoAre you sure this is common? I've never seen it before.
- dahart 3y agoWhoa, that’s wild! IMO == “In My Opinion”, and it’s all over the internet, along with its cousin IMHO (“In My Humble Opinion”). Are you sure you haven’t seen it before? Try searching for it on HN or Google. HN search shows me almost 100k comments with IMO, and the first page of hits is all this meaning.
- cxr 3y ago"IMO" is common. "IMO" used this way, on the other hand, is uncommon enough that I've literally never encountered it before.
- bunderbunder 3y agoFor all sorts of reasons, I think that being pedantic about the meanings of acronyms probably does more to inhibit understanding each other than it does to facilitate it. In this particular case, regardless of how often anyone has personally seen the term used to mean something more like "I think that...", taking that to be the intended meaning is the most friendly interpretation, and therefore the preferable one. It's an informal forum, we type fast, and don't necessarily carefully proof-read comments before hitting reply. Nor should we -- this place is a more pleasant space to occupy for everyone involved if we give each other the benefit of the doubt about choice of words.
- yakubin 3y ago> Doesn't even need the `inline` keyword for this at `-O2` Not surprising, since inline has nothing to do with inlining. :)
- bunderbunder 3y agoOn modern hardware, it's also not clear that a special case that makes things faster from a purely CPU-oriented perspective makes things faster overall. Adding additional code to handle special cases makes the code larger, and making the code larger can make it slower because of the memory hierarchy. There's a CPPCast interview with one of the people who worked on the Intel C Compiler where he talks about the things they do to get it producing such fast binaries. At least from what he said, it sounds like the vast majority of the unique optimizations they put into the compiler were about minimizing cache misses, not CPU cycles.