Helping the compiler with assumptions while using exceptions

Jonathan M Davis jmdavisProg at gmx.com
Mon May 30 15:17:32 PDT 2011


On 2011-05-30 15:03, Brad Roberts wrote:
> 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.

A valid point. But as long as enforce isn't inlineable, it can't be optimized. 
And with assert compiled out, I wouldn't expect it to have any affect on 
optimizations. If it did, depending on what was optimized, it could make bugs 
caused by values that would have made the assertion fail were it compiled in 
that much worse. Generally, assert is supposed to have no effect beyond 
throwing if it's compiled in and fails. Now, maybe Walter has a somewhat 
different take on that and maybe assertions could be used for optimizations, 
but the fact that they can be compiled out makes it a bit iffy.

But sure, as long as it all gets reduced down to if's and flow control, then 
the optimizer can have a field day. But at present, that can't happen with 
enforce (if it ever will), and I question that it can ever be done with assert 
thanks to the guarantees that are associated with it.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list