Live data from Hacker News

C++ Attribute: Likely, Unlikely

en.cppreference.com

1–10 of 70 posts

Re: C++ Attribute: Likely, Unlikely

#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?

Re: C++ Attribute: Likely, Unlikely

#5
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 think C++ really just has bad defaults for many of its features. It's understandable given the age of the language, but I wish compiler developers would agree on a set of new default attributes for various language features and make a flag to enable them. That way, older style code can still compile but newer code isn't cursed with explicit attribute hell.

Re: C++ Attribute: Likely, Unlikely

#6
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.

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.

Re: C++ Attribute: Likely, Unlikely

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

Re: C++ Attribute: Likely, Unlikely

#8
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.

If you are relying on profile-directed optimization, the hints are almost surely redundant.

These are useful when there’s static knowledge about control flow that could assist the optimizer, e.g. with inlining decisions.

For example: It’s not uncommon to have “bi-modal” functions, where simple checks guard simple actions, followed by much more complex logic to handle everything else.

Are those checks for exceptional cases like an invariant violation? Think of an I/O write function confirming the device is open.

Or are they the “fast path” for the most common invocations? Think of std::vector::push_back() checking for available capacity.

The answer helps the optimizer immensely in deciding whether to “partially inline” the simple code into callers or not.

Re: C++ Attribute: Likely, Unlikely

#9
post #5
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 think C++ really just has bad defaults for many of its features. It's understandable given the age of the language, but I wish compiler developers would agree on a set of new default attributes for various language features and make a flag to enable them. That way, older style code can still compile but newer code isn't cursed with explicit attribute hell.

That will never happen given how many compilers exist, each with its own set of use cases.

They can agree at ISO level but even then it isn't enough, as proven by the whole set of issues that are currently being ignored on platforms where breaking the ABI is tabu.

Re: C++ Attribute: Likely, Unlikely

#10
post #6
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.

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.
Post reply on HN