Helping the compiler with assumptions while using exceptions

Brad Roberts braddr at puremagic.com
Mon May 30 15:03:51 PDT 2011


On 5/30/2011 2:55 PM, Jonathan M Davis wrote:

> I'd be very surprised to see the compiler ever optimize code based on assert 
> or enforce statement. It's unlikely to do so based on assert simply because 
> the assertion is going to be compiled out. I think that there's a high chance 
> that optimizing based on an assertion that is removed in release mode would 
> actually be violating the guarantees of assert, since then the code without it 
> wouldn't act the same. But you'd have to ask Walter on that one. As for 
> enforce, it'll never happen because enforce is a library function, and the 
> compiler doesn't know anything about it. So, I wouldn't expect to ever seen 
> any compiler optimzations based on assert or enforce.
> 
> - Jonathan M Davis

I think you're underselling the possibilities.  The only thing preventing enforce from helping optimize in DMD is the
lack of inlining of it.  That will be improved at some point.  Once it's inlined, the rest of the optimizations can
easily take advantage of it.  There's nothing particularly magic about either assert or enforce.  They're just if's and
flow control which are an optimizer's bread and butter.

Later,
Brad


More information about the Digitalmars-d-learn mailing list