News for hackers. Stuff that matters.
Re:Undefined behavior in C is a reading error by dooglius on Thursday May 20, 2021 @03:29PM (#27225913)
> minority use-case

The amount of code that looks like this in a big enough hot loop to make a difference is negligible. Can you provide even one real-world example where this makes a difference, i.e. not some microbenchmark? The amount of code that can break as a result of signed overflows being UB, on the other hand, is huge.

> programmers would have to do a lot more fragile hand-unrolling of operations to get that performance back

Much easier ways to do this, e.g. by using an assert wrapper around __builtin_unreachable. Alternatively, an unsafe_int_t could be defined that gives the optimize-able behavior. The important thing is to make it opt-in; sensible defaults matter.

Re:Undefined behavior in C is a reading error by msbarnett on Thursday May 20, 2021 @03:40PM (#27226088)
> Can you provide even one real-world example where this makes a difference, i.e. not some microbenchmark

Sure. I don't even have to leave this thread to find one: https://news.ycombinator.com/item?id=27223954 reports a measurable speed impact to PostgreSQL when compiled with -fwrapv, which rules out the exact optimization in question.

This shouldn't be surprising; loops are extremely common and superscalar processors benefit enormously from almost anything other than a naïve translation of them.

Here's -fwrapv cutting performance of a function in half in Cython vs the non-fwrapv compilation: https://stackoverflow.com/questions/46496295/poor-performanc...

Re:Undefined behavior in C is a reading error by vyodaiken on Thursday May 20, 2021 @10:14PM (#27229795)
Sure, if you leave out error checks code runs faster. Compile mutexes to no-ops and get an even better speedup.
Re:Undefined behavior in C is a reading error by msbarnett on Friday May 21, 2021 @04:41AM (#27231773)
The optimizations impacted by -fwrapv have nothing whatsoever to do with “leaving out error checks”
Re:Undefined behavior in C is a reading error by vyodaiken on Friday May 21, 2021 @03:43PM (#27238965)
I don't know how you can say that

z = x+y; if(z< x )overflowerror(); //important computation and

for(i=start, i >= start && i < n; i++)docomputation(x[i]); etc.

Re:Undefined behavior in C is a reading error by MauranKilom on Thursday May 20, 2021 @06:30PM (#27228248)
> The amount of code that can break as a result of signed overflows being UB, on the other hand, is huge

C++ recently decided to not make signed overflow defined, despite having the explicit opportunity to do so. Here is the reasoning:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p090...

> Performance concerns, whereby defining the behavior prevents optimizers from assuming that overflow never occurs;

> Implementation leeway for tools such as sanitizers;

> Data from Google suggesting that over 90% of all overflow is a bug, and defining wrapping behavior would not have solved the bug.

Presumably, data from Google disproves your assertion that the amount of code that breaks due to signed overflow being UB is huge.