Idea: norecover blocks

dsimcha dsimcha at yahoo.com
Sat Jan 17 09:24:33 PST 2009


I've been thinking about exception handling in D as it relates to nothrow.
The obvious benefit of nothrow is compiler-enforced documentation, but a
second benefit is performance with scope statements and RAII.  If the compiler
knows some function call can't throw, it can generate better code.

In some cases, it's possible to call a function that throws an exception, but
because of the nature of the client code, this exception would be deemed
unrecoverable anyhow, and the program should simply die.  However, the fact
that these lower-level functions still throw prevents the higher level
functions from being marked nothrow, hurting performance.  Catching the
exception is no help, because the try-catch block has its own performance
cost.  It would be nice if these function calls could be marked as norecover,
thus preventing the compiler from generating any finally blocks for scope
statements and RAII, and allowing the higher level functions to be marked as
nothrow.  For example:

void foo() nothrow {
    auto scratch = new uint[1_000];  // May throw outOfMemoryError.
    // Use scratch.
}

The above wouldn't work because new throws an outOfMemoryError, but:

void foo() nothrow {
    norecover auto scratch = new uint[1_000];
    // Use scratch.
}

would work. I honestly don't know much about how exception handling is
implemented at the ASM level, but I do know that scope statements and RAII are
in practice performance killers in the presence of throwing code.  Does this
sound like a reasonable strategy for mitigating this performance drain?



More information about the Digitalmars-d mailing list