|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2004-10-31 07:27:25
Darren Cook <darren_at_[hidden]> writes:
>>>I'm hoping Thorsten Ottosen's contract programming will make it into
>>>the standard so the compiler will understand asserts and be able to
>>>optimize away the redundant ones.
>> Compilers are already allowed to do that, and I predict no proposal
>> that requires it to be done will ever be accepted into the standard.
>
> "be able" falls somewhere between "allowed to" and "required to". An
> optimizer will have a hard job analyzing all the code in a function so
> see if any can be skipped. But if it can see a precondition is always
> satisfied then it can jump directly to the body of the function and
> skip the precondition.
>
> E.g.
> void f(int x){
> precondition{ x>=0 && x<=99; } //1
> ... //2
> }
>
> f(atoi(argv[2])); //Jumps to f, at 1
> f(34); //Jumps to f, at 2
If f is inlined, I'm positive that compilers today can do that.
> void g(int z){
> precondition{ z>=10 && z<50; }
> f(z*2); //Can also jump to f at 2, as range is 20..98
> }
And I'm pretty sure that compilers today can do that one.
> I don't think Thorsten's proposal requires optimizations like
> this. But I don't know enough about compiler writing to know if the
> above is ever likely to be implemented.
It is just as implementable for assert as it is with Thorsten's
proposal.
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk