This seems like it will clutter code. I wish it was more terse as I find modern C++ code bases to be way too verbose already. It starts to get straining when looking at new modules.
This is standardizing vendor-specific attributes that have existed for many years. The code that use these probably use some preprocessor macro to select the right builtins, and aren't going to gain much new clutter to replace those macros. I believe this is the proposal that added them: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p04... The "references" section has links to GCC and Clang builtins: https…
C++ Attribute: Likely, Unlikely
21–30 of 70 posts
Re: C++ Attribute: Likely, Unlikely
#22Re: C++ Attribute: Likely, Unlikely
#23A common use case for these is to prevent the compiler from inlining the unlikely case to avoid thrashing the instruction cache. if (unlikely_condition) { // Don’t inline this expensive_operation(); } It’s a good idea to check the generated assembly when using these as they can lead to weird reordering of the code.
The compiler can make sure that the body for the likely condition is inline with the rest of the code, while the unlikely condition (e.g. the else block of a likely if) can be outlined behind a forward branch
Keeping the unlikely code further aside and behind a branch helps the happy path stay hot and well-predicted
Re: C++ Attribute: Likely, Unlikely
#24This seems like it will clutter code. I wish it was more terse as I find modern C++ code bases to be way too verbose already. It starts to get straining when looking at new modules.
I suppose there is no reason you can’t profile your code and have a tool insert these hints based on actual statistics from execution.
Re: C++ Attribute: Likely, Unlikely
#25Re: C++ Attribute: Likely, Unlikely
#26Re: C++ Attribute: Likely, Unlikely
#27In GCC you can already use (both on functions¹ and labels²) __attribute__(hot) and __attribute(cold) 1. https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Common-Functio... >; Since GCC 4.3, released March 5, 2008 2. https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Label-Attribut... >; Since GCC 4.8, released March 3, 2013
Re: C++ Attribute: Likely, Unlikely
#28Any ideas how "likely" it needs to be benefit from likely? more than 50%? or 75%? or 90%? Can it be detrimental if it has higher changes but still close to 50%?
Re: C++ Attribute: Likely, Unlikely
#29See also: https://blog.aaronballman.com/2020/08/dont-use-the-likely-or... tl;dr: these attributes are absolutely full of footguns because the standard is not explicit about precedence and nesting, and you should probably avoid them and prefer to spend time investing in PGO. It’s very easy to make sane-looking code containing these attributes which does the exact opposite of what you intended. Note that this issue doe…
Re: C++ Attribute: Likely, Unlikely
#30See also: https://blog.aaronballman.com/2020/08/dont-use-the-likely-or... tl;dr: these attributes are absolutely full of footguns because the standard is not explicit about precedence and nesting, and you should probably avoid them and prefer to spend time investing in PGO. It’s very easy to make sane-looking code containing these attributes which does the exact opposite of what you intended. Note that this issue doe…