Jump to content
  • Advertisement
Sign in to follow this  
gjl

function / method inlining

This topic is 1485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

Since we were discussing function calls performance in another thread, I was wondering if it would be feasible to implement functions and methods inlining in the compiler. Not automatically (I think you Andreas suggested it a while ago when discussing JITs), but when declared by the programmer, typically with the "inline" keyword:

class MyClass
{
    inline void DoSomething()
    {
       // do something to be inlined
    };
}

This would allow scripter to write computational-intensive code with optimal performance and yet keep the code clean and easy to read (manually inlining results in lots of code duplication).

 

Since it implies changing a function call into duplicated code in another context, I am not sure if it is something that can be done without too much impact...

Share this post


Link to post
Share on other sites
Advertisement

Function inlining for optimization purpose is something I'm interested in adding, but it is a quite complicated thing to do. Manually identifying the functions that should be inlined would make it a little easier, but not much.

 

To properly inline a function it is not just a matter of merging the bytecode generated for the inlined function into the calling function. It also has to be able to properly handle the new code paths. For example a return instruction from the inlined function obviously must not cause the calling function to be exited. The function arguments should also be reevaluated so that the calling function doesn't have to push the arguments on the stack, instead the inlined function should access the original variables directly on the stack. Even the local variables should preferrably be rearranged so that the use of the stack space is optimal.

Share this post


Link to post
Share on other sites

Yes, it indeed looks complex to implement, for sure!

 

I am not sure how native compilers do it, but it sounds like inlining is done before code generation (it could be easier to pre-process these inline functions and then generate the bytecode, wouldn't it?). But this might cause lots of context change issues (local vs global variables etc.).

 

But if you know that the function being compiled is also being inlined, that may simplify a little bit, as you can generate different bytecode for function call and return statements,...

 

(Just thinking out loud).

Share this post


Link to post
Share on other sites

Indeed. It is quite possible to compile the inlined function from the source code instead. I'm not sure if that is any easier though, since the compiler still has to interpret the source code in a different way than when compiling it as a normal function, or else you're not really gaining a lot with inlining the function.

 

 

Regardless of which option is chosen is doable, it just isn't a trivial thing to implement. I estimate that it could be implemented in maybe a couple of weeks, if only I had the time to dedicate to this.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!