C++ Logo

std-proposals

Advanced search

Re: [std-proposals] No shortcut logical operands-functions

From: Jens Maurer <jens.maurer_at_[hidden]>
Date: Thu, 15 Aug 2024 21:22:43 +0200
On 15/08/2024 20.39, Robin Savonen Söderholm via Std-Proposals wrote:
> Hi!
>
> I saw a CPP-con talk a while ago about how the shortcutting logical operations may generate several branches that in some cases may hurt performance. E.g. if we have the function like:
> ```cpp
> inline int foo(int a, int b) {
> if(a > 0 && b != 0) { return 0; } else { return 1; }
> }
> ```
> , the compiler may be forced to create 2 rather than 1 branch here (a jump for the condition for "a" and a second jump for the condition of "b").
Only if "b" has side effects or if "a" checks preconditions
for "b" (e.g. that a pointer is not null). In both cases,
you actually want the two branches.

If "a" and "b" are simple enough, the optimizer can often
fold the two checks.

So, to make a proposal in that direction fly, you'd need
specific evidence that the pattern appears often enough,
can't be optimized properly without extra user help, and
the work-around is hard enough to craft for yourself in
modern C++ that it should be standardized.

Currently, I'm not convinced on any of those counts.

Jens

Received on 2024-08-15 19:22:47