Article View: comp.lang.c++
Article #977Re: What does this code
From: Michael S
Date: Sat, 21 Nov 2026 16:40
Date: Sat, 21 Nov 2026 16:40
77 lines
4670 bytes
4670 bytes
On Sat, 21 Nov 2026 14:55:12 +0100
David Brown <david.brown@hesbynett.no> wrote:
> On 21/11/2026 13:10, Bonita Montero wrote:
> > Am 21.11.2026 um 12:38 schrieb David Brown:
> >
> >> That is very different code, and the compiler has different
> >> heuristics for generating object code. ...
> >
> > It doesn't fit with your statement.
> >
> > > That's the kind of hack "manual optimisation" code that most C
> > > and C++ programmers were able to leave behind at the turn of the
> > > century as compilers got better at optimising well-written and
> > > clear source code.
> >
> > If I did it more simple and readable from the beginning the code
> > wouldn't be as efficient - as you suggested.
> >
>
> I think the branchless code will often be more efficient in practice
> when the data is already in registers (but see Michael's comments
> about the code - and he is quite experienced in that kind of thing).
When prediction is correct close to 100% of time, branchy code will
be more efficient (lower latency) evn with values in registers.
Of course, it would be very hard to prove it by measurements, because
the difference will be small.
> When the data is in memory, not a register, I expect the branched
> code is better. gcc generates the best object code - if my
> predictions are correct - from the clearest source code, both in your
> struct method and for the stand-alone function example.
>
IMHO, it's not a matter of optimization.
Branchless code in thhis case is not allowed by semantics of the
language. In order to give compiler a chance, you'd have to modify a
source. For example, to something like that:
strct::operator int() const noexcept
{
bool bb = b;
if (!p )
return -1;
return (int)bb;
}
In C it's much more obvious.
I played a little with this variant using gcc and clang.
Once again, like in previous example, I was able to influence code
generation of clang by means of __builtin_expect() or of
__builtin_expect_with_probability().
Once again, like in previous example, gcc stubbornly generated the
same (this time branchy) code regardless of what I was trying to tell
it.
> In real usage, methods like this would be inlined, and the code
> generated would depend on the surrounding source code. In general,
> clearer source code gives the compiler better opportunities to
> optimise the inlined code well.
>
> The code you wrote is not "cool" or "smart" - it is a smart-arse hack
> that results in significantly poorer source code quality, and worse
> object code efficiency (though it probably makes no measurable
> difference in real-world usage). Before writing such ugly code, you
> should be benchmarking the results of clear source code in real
> usage, and determining if there is actually a bottleneck in your code
> performance caused by the function. Then after writing your hack,
> you should test again and determine if there is a significant enough
> gain for it to be worth keeping. I do not believe you have made the
> slightest attempt to do that.
>
Technical Details
Message-ID:
<20261121184041.00007f1e@yahoo.com>Path:
mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11dou16$2vlr8$1@raubtier-asyl.eternal-september.org> <20261120114427.00002e24@yahoo.com> <11dp7lk$330qr$1@dont-email.me> <11dp9fv$33khk$1@raubtier-asyl.eternal-september.org> <11dpbgr$34a3s$1@dont-email.me> <11draqa$3qusi$1@raubtier-asyl.eternal-september.org> <11drvr5$12fg$2@dont-email.me> <11ds1o6$20l4$1@raubtier-asyl.eternal-september.org> <11ds7s0$3uj9$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Michael S <already5chosen@yahoo.com> Newsgroups: comp.lang.c++ Subject: Re: What does this code Date: Sat, 21 Nov 2026 18:40:41 +0200 Organization: A noiseless patient Spider Lines: 77 Message-ID: <20261121184041.00007f1e@yahoo.com> References: <11dou16$2vlr8$1@raubtier-asyl.eternal-september.org> <20261120114427.00002e24@yahoo.com> <11dp7lk$330qr$1@dont-email.me> <11dp9fv$33khk$1@raubtier-asyl.eternal-september.org> <11dpbgr$34a3s$1@dont-email.me> <11draqa$3qusi$1@raubtier-asyl.eternal-september.org> <11drvr5$12fg$2@dont-email.me> <11ds1o6$20l4$1@raubtier-asyl.eternal-september.org> <11ds7s0$3uj9$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Injection-Date: Sat, 21 Nov 2026 16:40:40 +0000 (UTC) Injection-Info: dont-email.me; logging-data="225258"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OB0qCex5rjN+ULmpH6s5e4yNVmzZztPU="; posting-host="00c07e9ec34dc910f3ffb3cd1391c851" Cancel-Lock: sha1:xpkmbJtEtiDUvi8eCBhmP2xGe+g= sha256:BNNpxQ+0c2AQuIh9h68xZHOZEbitwUKnS0cxa7GEWRA= sha1:QF6VREsnsV89DEHix786/FMnjHE= sha256:qMZtAJjxmIC3cS1boIeqpD09SJhmABs4iW8TQssGLn8= X-Newsreader: Claws Mail 4.1.1 (GTK 3.24.34; x86_64-w64-mingw32)