How expensive is integer-overflow trapping in C++?
1–10 of 198 posts
Re: How expensive is integer-overflow trapping in C++?
#2While 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>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++?
#4Note 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…
Re: How expensive is integer-overflow trapping in C++?
#5Note 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
Also the wrapping types aren't unsafe, they just make it explicit.
Re: How expensive is integer-overflow trapping in C++?
#6Note 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…
[0] https://play.rust-lang.org/?version=stable&mode=release&edit...
Re: How expensive is integer-overflow trapping in C++?
#7Re: How expensive is integer-overflow trapping in C++?
#8I 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++?
#9GCC 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.
Re: How expensive is integer-overflow trapping in C++?
#10https://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.