Can D optimize?

Amex Amex at gmail.com
Sun Jun 9 05:24:56 UTC 2019


Can dmd or ldc optimize the following cases:

foo(int x)
{
    if (x > 10 && x < 100) bar1; else bar2;
}


...

for(int i = 23; i < 55; i++)
    foo(i); // equivalent to calling bar1(i)

clearly i is within the range of the if in foo and so the checks 
are unnecessary.

I realize that this is generally a complex and possibly 
unsolvable problem... but in the example above, which comes up 
enough, it is not.

Many times when one iterates over an array the boundaries cause 
special cases... for maximum performance one has to break up the 
boundary cases which just increases code complexity and 
verbosity.  If the compiler can figure it out automatically then 
it is unnecessary.

When working with algorithms

Of course, if the limits are not known at compile time this 
method cannot be used...


So, I'm wondering if there is a simple way to achieve the same 
thing that is easy on the eyes.

Suppose we have a resampling function. The boundaries require 
different functionality.

So there are effectively 3 functions and the loop over the data 
would be split up in to three parts. Normally we have to do it by 
hand and maintain everything.

I imagine ranges might be able to help solve this in an optimal 
way but I'm not sure what might be the best approach. It should 
be equivalent to hand written code if not better.



More information about the Digitalmars-d-learn mailing list