Jump to page: 1 2
Thread overview
Does D use zero-cost exceptions?
Apr 15, 2019
A
Apr 15, 2019
Nicholas Wilson
Apr 16, 2019
Russel Winder
Apr 16, 2019
Araq
Apr 16, 2019
Walter Bright
Apr 16, 2019
Mike Franklin
Apr 16, 2019
Radu
Apr 29, 2019
matheus
April 15, 2019
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?
April 15, 2019
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.
April 15, 2019
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).
April 16, 2019
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
April 16, 2019
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.
April 16, 2019
On Mon, 2019-04-15 at 16:24 -0400, Andrei Alexandrescu via Digitalmars-
d wrote:
[…]
> 
> This is a problem important enough for C++ that they added a keyword 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-fragmenting_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.)

-- 
Russel.
===========================================
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



April 16, 2019
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.
April 16, 2019
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
April 16, 2019
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.).
April 16, 2019
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.
« First   ‹ Prev
1 2