www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Does D use zero-cost exceptions?

reply A <email example.org> writes:
It is well known that exceptions are much slower than their 
alternatives, but is there a performance cost for using 
try/catch, even when no Exception/Error is being thrown?
Apr 15
next sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Monday, 15 April 2019 at 19:25:24 UTC, A wrote:
 It is well known that exceptions are much slower than their 
 alternatives, but is there a performance cost for using 
 try/catch, even when no Exception/Error is being thrown?
There is a space cost for the unwind tables, but exceptions are faster than checking error codes all over the place (you do check them right? ;) ). If an error is thrown you application is dead (or UB) anyway. Please use the learn forum for such questions in the future.
Apr 15
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 4/15/19 3:36 PM, Nicholas Wilson wrote:
 On Monday, 15 April 2019 at 19:25:24 UTC, A wrote:
 It is well known that exceptions are much slower than their 
 alternatives, but is there a performance cost for using try/catch, 
 even when no Exception/Error is being thrown?
There is a space cost for the unwind tables, but exceptions are faster than checking error codes all over the place (you do check them right? ;) ).
Sadly that's not quite the case... we've run a bunch of test at Facebook back in the day and the sheer presence of unwinding disables a bunch of optimizations, notably code motion and everything enabled by it. The bottom line effect is quite unpleasant, in the single digits. This is a problem important enough for C++ that they added a keyword dedicated to it (noexcept).
Apr 15
next sibling parent Russel Winder <russel winder.org.uk> writes:
On Mon, 2019-04-15 at 16:24 -0400, Andrei Alexandrescu via Digitalmars-
d wrote:
[=E2=80=A6]
=20
 This is a problem important enough for C++ that they added a keyword=20
 dedicated to it (noexcept).
Herb Sutter has a proposal for exception handling in C++ to make it better. His keynote at ACCU 2019 is reputedly the first public presentation. Slides are at: https://github.com/ACCUConf/PDFs_2019/blob/master/herb_sutter_-_de-fragment= ing_cpp__making_exceptions_more_affordable_and_usable.pdf video will be up on YouTube as soon as the videographers can put things up. (There has been an issue outwith the videographers' control that has meant things are not going up within 24 hours of being recorded.) --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
Apr 16
prev sibling next sibling parent Araq <rumpf_a web.de> writes:
On Monday, 15 April 2019 at 20:24:27 UTC, Andrei Alexandrescu 
wrote:

 Sadly that's not quite the case... we've run a bunch of test at 
 Facebook back in the day and the sheer presence of unwinding 
 disables a bunch of optimizations, notably code motion and 
 everything enabled by it. The bottom line effect is quite 
 unpleasant, in the single digits.

 This is a problem important enough for C++ that they added a 
 keyword dedicated to it (noexcept).
Did you compare it to if-based error handling or to calling terminate/abort on error? I would expect that excessive if statements for error handling prevent the code motion optimizations in the same way.
Apr 16
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 4/15/19 4:24 PM, Andrei Alexandrescu wrote:
 On 4/15/19 3:36 PM, Nicholas Wilson wrote:
 On Monday, 15 April 2019 at 19:25:24 UTC, A wrote:
 It is well known that exceptions are much slower than their 
 alternatives, but is there a performance cost for using try/catch, 
 even when no Exception/Error is being thrown?
There is a space cost for the unwind tables, but exceptions are faster than checking error codes all over the place (you do check them right? ;) ).
Sadly that's not quite the case... we've run a bunch of test at Facebook back in the day and the sheer presence of unwinding disables a bunch of optimizations, notably code motion and everything enabled by it. The bottom line effect is quite unpleasant, in the single digits.
It would be important to know what "the day" was. Because there have been a lot of improvements on LLVM. I wonder if some of this has been mitigated for LDC at least.
 This is a problem important enough for C++ that they added a keyword 
 dedicated to it (noexcept).
Don't we have nothrow for this? -Steve
Apr 16
next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 4/16/2019 1:52 PM, Steven Schveighoffer wrote:
 Don't we have nothrow for this?
Yes. The problem with EH is not in the throwing, it's in the unwinding. Each RAII object requires its own try-finally, and try-finally disables many optimizations (such as code motion, enregistering of variables, etc.).
Apr 16
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 4/16/19 4:52 PM, Steven Schveighoffer wrote:
 On 4/15/19 4:24 PM, Andrei Alexandrescu wrote:
 On 4/15/19 3:36 PM, Nicholas Wilson wrote:
 On Monday, 15 April 2019 at 19:25:24 UTC, A wrote:
 It is well known that exceptions are much slower than their 
 alternatives, but is there a performance cost for using try/catch, 
 even when no Exception/Error is being thrown?
There is a space cost for the unwind tables, but exceptions are faster than checking error codes all over the place (you do check them right? ;) ).
Sadly that's not quite the case... we've run a bunch of test at Facebook back in the day and the sheer presence of unwinding disables a bunch of optimizations, notably code motion and everything enabled by it. The bottom line effect is quite unpleasant, in the single digits.
It would be important to know what "the day" was. Because there have been a lot of improvements on LLVM. I wonder if some of this has been mitigated for LDC at least.
2015. I'll ask for an update.
 This is a problem important enough for C++ that they added a keyword 
 dedicated to it (noexcept).
Don't we have nothrow for this?
We do, but... see Walter's explanation.
Apr 16
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 4/16/19 5:46 PM, Andrei Alexandrescu wrote:
 On 4/16/19 4:52 PM, Steven Schveighoffer wrote:
 On 4/15/19 4:24 PM, Andrei Alexandrescu wrote:
 On 4/15/19 3:36 PM, Nicholas Wilson wrote:
 On Monday, 15 April 2019 at 19:25:24 UTC, A wrote:
 It is well known that exceptions are much slower than their 
 alternatives, but is there a performance cost for using try/catch, 
 even when no Exception/Error is being thrown?
There is a space cost for the unwind tables, but exceptions are faster than checking error codes all over the place (you do check them right? ;) ).
Sadly that's not quite the case... we've run a bunch of test at Facebook back in the day and the sheer presence of unwinding disables a bunch of optimizations, notably code motion and everything enabled by it. The bottom line effect is quite unpleasant, in the single digits.
It would be important to know what "the day" was. Because there have been a lot of improvements on LLVM. I wonder if some of this has been mitigated for LDC at least.
2015. I'll ask for an update.
OP delivers... got this: "RAII code introduces unwind code which can constrain optimizations and keep more values alive after a callsite (that can throw) but it’s possible to annotate noexcept functions, and LTO can do that too (I mean probably, I didn’t actually check). LTO, autoFDO, and hot/code layout have been getting better in gcc and clang, plus tools like bolt can move cold unwind code bloat to cold pages and reduce TLB misses in hot functions that have unwinding."
Apr 21
prev sibling next sibling parent Mike Franklin <slavo5150 yahoo.com> writes:
On Monday, 15 April 2019 at 19:25:24 UTC, A wrote:
 It is well known that exceptions are much slower than their 
 alternatives, but is there a performance cost for using 
 try/catch, even when no Exception/Error is being thrown?
I haven't benchmarked D, but I did do some benchmarking with C++ (arm-none-eabi-g++) on an ARM Cortex-M embedded application. What I found was that, compared with checking error codes every step of the way, exceptions slightly improved runtime performance when no exception was thrown. However, once an exception was thrown, the performance cost was heavy. For that application, I opted for using exceptions only where the condition causing an exception to be thrown was an unrecoverable failure and the system had to be gracefully shut down. At that point all bets were off anyway and it was just about logging the failure for investigators. I don't know how that translates to D. I suggest running some benchmarks yourself. It would probably be an interesting topic for many, so you could even make a blog post out of it, help others understand the issue more deeply, and get a little notoriety for your work. Mike
Apr 15
prev sibling next sibling parent Radu <void null.pt> writes:
On Monday, 15 April 2019 at 19:25:24 UTC, A wrote:
 It is well known that exceptions are much slower than their 
 alternatives, but is there a performance cost for using 
 try/catch, even when no Exception/Error is being thrown?
You can check this by looking at the generated assembly. I tried this https://godbolt.org/z/fl8qlv and looks that for non-exception path there is no cost associated with it, at least for LDC.
Apr 15
prev sibling parent matheus <matheus gmail.com> writes:
On Monday, 15 April 2019 at 19:25:24 UTC, A wrote:
 It is well known that exceptions are much slower than their 
 alternatives, but is there a performance cost for using 
 try/catch, even when no Exception/Error is being thrown?
Herb Sutter ACCU presentation about this Exception Handling x Zero-Overhead: https://youtu.be/os7cqJ5qlzo?t=860 Matheus.
Apr 29