News for hackers. Stuff that matters.
Re:C++ Attribute: Likely, Unlikely by gumby on Sunday October 22, 2023 @01:56PM (#37977568)
I often write my code pessimally in this regard so have a note in the back of my mind to someday use these in a few hot paths.

When I say “pessimally” I mean I usually check the unlikely cases right away and then put the normal case last:

    Blah foo (something& arg) {
    
      if (is_invalid  (arg)) return blah(0);
      if (is_inactive (arg)) return blah(1);
    
    // ok do all the normal stuff
    }
It makes the code clearer but slightly slower. I could always write a conditional for the hot path up front, but code is for human readers, right?

So when people say “this clutters the code” they are right, but most of the time you just don’t worry about it — it need only clutter a few functions in your hot loops, where you’re willing to rewrite it anyway regardless of how ugly it gets.

It’s like looking at the standard library source: super cluttered, but it has to handle all sorts of weird corner cases and is called a lot. Normal code can ignore all that in more than 99.99% of the cases.

Re:C++ Attribute: Likely, Unlikely by vore on Sunday October 22, 2023 @02:34PM (#37977879)
I'm so confused by this. How would you avoid doing these checks? Aren't they invariants for your function?
Re:C++ Attribute: Likely, Unlikely by bee_rider on Sunday October 22, 2023 @02:58PM (#37978057)
I don’t think they suggested avoiding the checks, unless I’ve missed something?
Re:C++ Attribute: Likely, Unlikely by vore on Sunday October 22, 2023 @03:22PM (#37978259)
The part that's confusing me is the part about avoiding clutter – if you have to do all the checks anyway, what clutter are you avoiding by changing the order of them?
Re:C++ Attribute: Likely, Unlikely by bee_rider on Sunday October 22, 2023 @03:30PM (#37978328)
I believe the likelihood annotations are the things they are talking about, for cluttering the code. Not the argument checks.
Re:C++ Attribute: Likely, Unlikely by gumby on Sunday October 22, 2023 @06:00PM (#37979479)
That conditional would be checked, then if it failed, checked again after the normal case has run, in order to choose the arm to follow.

Not a huge deal execution-time-wise, but from a reader’s PoV, the way I write it says, “ok, the special cases don’t apply to the body so I don’t have to worry that the index will be out of range (or whatever) and can just focus on the logic”.

Re:C++ Attribute: Likely, Unlikely by actionfromafar on Sunday October 22, 2023 @03:11PM (#37978170)
Maybe the implied question is that, the compiler can optimise the checks to the occur in whatever order it wants.
Re:C++ Attribute: Likely, Unlikely by gpderetta on Sunday October 22, 2023 @02:20PM (#37977757)
> I usually check the unlikely cases right away and then put the normal case last.

Don't we all?

Re:C++ Attribute: Likely, Unlikely by gumby on Sunday October 22, 2023 @06:46PM (#37979762)
If only I lived in such a paradise!
Re:C++ Attribute: Likely, Unlikely by kmac_ on Sunday October 22, 2023 @02:12PM (#37977700)
Names like 'is_invalid' and 'is_inactive' will lead to a double negation.
Re:C++ Attribute: Likely, Unlikely by latchkey on Sunday October 22, 2023 @02:38PM (#37977914)
Agreed, this reads so much easier...

  if (!is_valid)

  if (!is_active)
Re:C++ Attribute: Likely, Unlikely by bee_rider on Sunday October 22, 2023 @03:03PM (#37978107)
> I could always write a conditional for the hot path up front, but code is for human readers, right?

Does C or C++ actually make any promises that it’ll assume the “true” branch of the conditional will be taken? I always assumed that the compiler could make whatever weird decision it wants for that sort of thing.

TBH I’d probably just write normal stuff as a function, and then call that function directly in cases where performance is really crucial, if it can be done safely… if such a case exists…

Re:C++ Attribute: Likely, Unlikely by twoodfin on Sunday October 22, 2023 @03:36PM (#37978375)
No. The traditional compiler heuristic is to assume backwards branches are taken (loops) and forward branches are not.