function / method inlining

Started by
2 comments, last by WitchLord 9 years, 6 months ago

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...

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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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).

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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement