Live data from Hacker News

How expensive is integer-overflow trapping in C++?

lemire.me

1–10 of 198 posts

Re: How expensive is integer-overflow trapping in C++?

#2
Note that Rust also traps on integer overflow, and it is trivial to „hack“ the compiler to disable this, and many have done so.

While on this microbenchmark you see a 3x slowdown with the LLVM backend, on large Rust projects like servo, Firefox, the rust compiler, etc. the slowdown is not even measurable.

Also, Rust provides you with unsafe intrinsics to opt-out of trapping in the particular line of code in which it causes an issue.

Re: How expensive is integer-overflow trapping in C++?

#3
From GCC documentation,

>The compiler will attempt to use hardware instructions to implement these built-in functions where possible, like conditional jump on overflow after addition, conditional jump on carry etc.

I suspect something's not exactly right with compiler flags.

Then again, how much of your program is integer arithmetic? 12x slowdown on 0.1% is 1.2%.

Re: How expensive is integer-overflow trapping in C++?

#4
post #2

Note that Rust also traps on integer overflow, and it is trivial to „hack“ the compiler to disable this, and many have done so. While on this microbenchmark you see a 3x slowdown with the LLVM backend, on large Rust projects like servo, Firefox, the rust compiler, etc. the slowdown is not even measurable. Also, Rust provides you with unsafe intrinsics to opt-out of trapping in the particular line of code in which it…

I dont think Rust traps on overflow in release builds

Re: How expensive is integer-overflow trapping in C++?

#5
post #4
post #2

Note that Rust also traps on integer overflow, and it is trivial to „hack“ the compiler to disable this, and many have done so. While on this microbenchmark you see a 3x slowdown with the LLVM backend, on large Rust projects like servo, Firefox, the rust compiler, etc. the slowdown is not even measurable. Also, Rust provides you with unsafe intrinsics to opt-out of trapping in the particular line of code in which it…

I dont think Rust traps on overflow in release builds

That's correct, it's not a zero-cost abstraction.

Also the wrapping types aren't unsafe, they just make it explicit.

Re: How expensive is integer-overflow trapping in C++?

#6
post #2

Note that Rust also traps on integer overflow, and it is trivial to „hack“ the compiler to disable this, and many have done so. While on this microbenchmark you see a 3x slowdown with the LLVM backend, on large Rust projects like servo, Firefox, the rust compiler, etc. the slowdown is not even measurable. Also, Rust provides you with unsafe intrinsics to opt-out of trapping in the particular line of code in which it…

Rust wraps in release mode and traps in debug mode[0][1]. All large projects that I know of ship in release mode. Though I'd be interested to see a source for it not pessimizing badly.

[0] https://play.rust-lang.org/?version=stable&mode=release&edit...

[1] https://github.com/rust-lang/rfcs/pull/560

Re: How expensive is integer-overflow trapping in C++?

#8
Aborting the program on integer overflow seems so drastic. Say you do that in a server program. Then convincing it to overflow an integer somewhere (possibly somewhere where it is harmless) causes denial of service and all unrelated connections in the process die?

I guess languages with support for exceptions have a more natural action for this, that could be caught somewhere in a server's processing loop to not affect other clients.

Re: How expensive is integer-overflow trapping in C++?

#9
I wish the author had dug a little deeper to see why this slowdown occurs.

GCC seems to be generating an out-of-line function call to implement the trapped addition, which is going to inhibit a lot of other optimizations like vectorization:

https://gcc.godbolt.org/z/zj6qbn

By contrast, Clang is not:

https://gcc.godbolt.org/z/r7avno

Btw if you only need overflow checking in a few places, you can use overflow detecting intrinsics:

https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins...

In C++, this is going to be way more useful than -ftrapv anyway, since in a lot of situations you're dealing with user input and you're probably going to want to throw, not abort.

https://gcc.godbolt.org/z/5qvTf9

Re: How expensive is integer-overflow trapping in C++?

#10
This blog post has a much more in-depth analysis of the cost of overflow checking:

https://blog.regehr.org/archives/1384

The tl;dr is that (on x86-64) the additional branch to trap has almost negligible cost (processor already sets the flag on overflow, jump is always predicted), but it completely breaks vectorization and loop optimizations, which significantly impacts certain benchmarks.

Post reply on HN