Tech Off Thread

6 posts

Inline routines

Back to Forum: Tech Off
  • User profile image
    Akaina

    Quick question,

    I'm not an expert with how .NET compiles, but I was wondering if there exists a feature anything like traditional C++ macros (#include, #whatever) whereby a function could be called with syntax not unlike a regular call, but allow the function code to be literally copied during compile time into an inline routine, thus removing repeated function calls.

    One could re-use code and get the speed advantage of hard-coding. Updates to the routine would not require modifying multiple instances of identicly hard-coded routines.


    e.g.

    inline function myInlineFunction
    {
      //do lots of CPU intensive things here...
      System.Console.Writeline("test function " + scopeVar + "\n");
      System.Console.Writeline("test function " + scopeVar + "\n");
      System.Console.Writeline("test function " + scopeVar + "\n");
      System.Console.Writeline("test function " + scopeVar + "\n");

    }//end of myInlineFunction

    //meanwhile back at the ranch...

    Class1
    {
      static void Main()
      {
        int scopeVar = 1;
        ulong max_count = 99999999;
        System.out.writeline("Welcome to my speedy program\n\n");
        for (int i = 0; i < max_count; i++)
        {
          //normally a function call would be used 9999999 times
          myInlineFunction;   //code is literally pasted here during compile-time
        }//end of for
      }//end of member Main
    }//end of Class1



    This kind of routine would not be able to return values, or accept values, and would exist (have access to variables) only within the scope 'this' of where it was called from.

    I could see this as a speed enhancement. Does it already exist? If this is a newb question, I appologize in advance.

  • User profile image
    Tom Servo

    IIRC .NET decides that on its own. I think some requirements for the JIT to perform inlining was max. 32bytes of IL code, no branching, and some other stuff I can't remember. Not sure about the branching though.

  • User profile image
    Cronan

    .Net inlines everything it can
    Eric (Gunnerson) has blogged on this
    As has Rico Mariani

  • User profile image
    Cronan

    For me, in a (good) modern language this sort of thing should always be left to the compiler or the jitter. Write good, clean code and rely on the quality of the compiler/jitter. Otherwise you may find that your optimisations themselves cause problems when a new version of the compiler (or jitter) is released.

    In .Net, inlining takes place in the JIT.

  • User profile image
    Akaina

    Cool, thanks for the info!

  • User profile image
    jonathanh

    Tom Servo wrote:
    IIRC .NET decides that on its own. I think some requirements for the JIT to perform inlining was max. 32bytes of IL code, no branching, and some other stuff I can't remember. Not sure about the branching though.


    I've seen posts saying that you can get away with *one* if-then-else construct in a function and still have it inlined. It's probably something to check on a case-by-case basis though .

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.