Raymond Chen's take on so-called zero cost exceptions
deadalnix
deadalnix at gmail.com
Tue Mar 1 13:31:42 UTC 2022
On Tuesday, 1 March 2022 at 06:50:54 UTC, Araq wrote:
> On Tuesday, 1 March 2022 at 06:44:05 UTC, H. S. Teoh wrote:
>> If error codes are somehow fundamentally more efficient, why
>> can't the compiler just rewrite throwing functions into
>> functions that return error codes, with the compiler inserting
>> error code checks into the caller as needed?
>
> Exactly. It's entirely feasible. And it is what Swift does:
> https://www.mikeash.com/pyblog/friday-qa-2017-08-25-swift-error-handling-implementation.html
>
> (It's not universally faster than table based exceptions but it
> seems preferable for embedded devices.)
Yes, this is doable and indeed done in some cases. The main
benefit is that the resulting binaries are smaller, which is
important on embeded devices.
Exceptions handling typically causes a ~20% increase of the
binaries.
Another benefit is that it is much cheaper in the exceptional
case. The flip side of this is that it imposes a greater cost in
the non throwing case.
More information about the Digitalmars-d
mailing list