Live data from Hacker News

C++ Attribute: Likely, Unlikely

en.cppreference.com

11–20 of 70 posts

Re: C++ Attribute: Likely, Unlikely

#11
In 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

#12
post #3

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://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

http://llvm.org/docs/BranchWeightMetadata.html#built-in-expe...

Re: C++ Attribute: Likely, Unlikely

#13
See 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 does not exist with the equivalent C macros — those generally behave as expected. But you should probably just invest in PGO instead of static hints there, too.

Re: C++ Attribute: Likely, Unlikely

#14
post #4
post #3

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.

I understand the overall feeling but I’m not sure I understand the specific reason why you say this is making code bases more terse. Are you comparing this with the alternative of using GCC specific extensions or no definition of likely/unlikely code paths at all?

They're saying the opposite - that it makes code bases more verbose.

Re: C++ Attribute: Likely, Unlikely

#15
post #10
post #6

Earlier quoted context omitted.

These annotations are really only of interest in performance-critical computations. It’s another knob for library writers to use to make the libraries you use magically faster for their users. And, should be quite rare outside of libraries.

And even then they should be handled with extreme care, as they can trigger UB if used incorrectly.

Do you have examples? It's not clear from the article how you could have a UB with them.

Re: C++ Attribute: Likely, Unlikely

#16
post #7
post #3

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.

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.

Sometimes the important performance critical path is the one least taken. You can't profile because the profiler has no way to know you don't care about the common path.

In general the profiler is a better tool, but there are rare exceptions and if those apply to you c++ gives you the control you need.

Re: C++ Attribute: Likely, Unlikely

#17
A 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.

Re: C++ Attribute: Likely, Unlikely

#18
post #10
post #6

Earlier quoted context omitted.

These annotations are really only of interest in performance-critical computations. It’s another knob for library writers to use to make the libraries you use magically faster for their users. And, should be quite rare outside of libraries.

And even then they should be handled with extreme care, as they can trigger UB if used incorrectly.

Compilers have always been making guesses about what the most likely code path is behind the scenes, but it still needs to behave correctly in the case where it was wrong (that will just be the less-optimal code path). All these attributes are doing is helping the compiler know instead of guess what the hot path is. if there is any way to confuse the compiler into giving undefined behavior with hints like this, that's a compiler bug. (not saying compiler bugs don't exist, but are you aware of a specific bug like this)?

Re: C++ Attribute: Likely, Unlikely

#19
post #3

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.

Any suggestions on how it could be terser while still being readable? If you're reading a new module using the functionality, would you prefer seeing

    [[likely]] return 2;
or

    @!l return 2;
? Which one is more understandable if you're reading and not familiar to the syntax?

Re: C++ Attribute: Likely, Unlikely

#20
post #3

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…

> This is standardizing vendor-specific attributes

Except the standard’s likely and unlikely attributes invented new syntax that is not drop-in compatible with clang’s and gcc’s attributes.

Where clang and gcc would use:

  if (__builtin_expect(x > 0, 1)) { … }
the standard uses:

  if (x > 0) [[likely]] { … }
Post reply on HN