Raymond Chen's take on so-called zero cost exceptions

deadalnix deadalnix at gmail.com
Tue Mar 1 01:13:22 UTC 2022


On Monday, 28 February 2022 at 20:33:17 UTC, Walter Bright wrote:
> "The presence of exceptions means that the code generation is 
> subject to constraints that don’t show up explicitly in the 
> code generation: Before performing any operation that could 
> potentially throw an exception, the compiler must spill any 
> object state back into memory if the object is observable from 
> an exception handler. (Any object with a destructor is 
> observable, since the exception handler may have to run the 
> destructor.)
> Similarly, potentially-throwing operations limit the compiler’s 
> ability to reorder or eliminate loads from or stores to 
> observable objects because the exception removes the guarantee 
> of mainline execution."
>
> https://devblogs.microsoft.com/oldnewthing/20220228-00/?p=106296
>
> This is what I've been saying.

There are a bit more to this though. First the exception ABI on 
windows is 100% atrocious and impose way more constraints than 
the usual libunwind based ones.

But most importantly, second, the argument is fallacious, as 
comparing code with and without exception makes no sense. What 
you want to compare is the code that uses exception vs the code 
that uses *something else* for error handling.

While that something else is almost always more expensive than 
exception on the non throwing path. The numbers from the original 
paper show this.


More information about the Digitalmars-d mailing list