## digitalmars.D.learn - Can D optimize?

• Amex (31/31) Jun 08 2019 Can dmd or ldc optimize the following cases:
• Johan Engelen (5/15) Jun 09 2019 This is a simple case of inlining for the optimizer, so works out
```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.
```
Jun 08 2019
```On Sunday, 9 June 2019 at 05:24:56 UTC, Amex wrote:
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.

This is a simple case of inlining for the optimizer, so works out
well with LDC and GDC:
https://d.godbolt.org/z/pLy8Yy

-Johan
```
Jun 09 2019