inlining

dsimcha dsimcha at yahoo.com
Thu Jul 17 14:14:45 PDT 2008


If we're going to make the jump and expose these features to the programmer, a
noinline attribute to do the opposite might be nice also.  Here's a hypothetical
example of where that might be nice:

void myFunc(int foo) {
   foreach(i, 0..1_000_000) {
       if(foo < 5) {  //This is an unusual case, foo usually >5.
          bar(foo);
       }
       //Stuff that may change the value of foo.
   }
   //More stuff that may change the value of foo.
   foreach(i; 0..1_000_000) {
       if(foo < 5) {  //Again unusual
           bar(foo);
       }
       //More stuff that may change the value of foo.
   }
}

In this case the compiler, not understanding the high-level meaning of what is
being done, would likely not realize that foo is almost always >= 5 in real-world
scenarios.  It would likely inline bar at both places it's called from,
contributing to code bloat, especially since it's called from a loop that iterates
a lot of times.  However, since the programmer knows that the branch that calls
bar() will be taken very infrequently, the programmer might want to specify that
bar() should not be inlined.




More information about the Digitalmars-d mailing list