Menu

#114 Improved forward looking for function inlining

open
None
5
2010-05-31
2010-05-31
Haravikk
No

One thing that can be a bit inefficient when inlining functions is the fact that additional variables are often created to unroll the parameter passing to a function. However, this is only required in cases where a function actually changes the value of a variable that is then used later on.

For example, take the following script:

---------------------------------
// pragma inline
myFunction(integer arg) {
llOwnerSay("The number is " + (string)(++arg) + "!");
}

default {
state_entry() {
integer i = 19;
myFunction(i);
llOwnerSay("The number is now " + (string)i + "!");
}
}
---------------------------------

This is inlined correctly with arg becoming a new variable copying i.
However, if we remove the llOwnerSay() from state_entry(), then a duplicate variable is not required, as i can simply be used since it never appears again, however a variable for arg is still produced.

Now, in the example this isn't a big deal as it's only an integer, but for lists or strings you can end up with several copies of a list that aren't actually used, meaning that a function has to be inlined manually to keep the memory usage down.

I realise it's extra processing, but it should be possible to look to see where a variable in scope is last used, if an inline function appears before that line then parameters that use that variable need to receive a copy, if it appears after the last modifying line, then the parameter can be replaced with direct references to the original variable. On top this you just need to adjust line numbers as functions are inserted to account for the shift they will create, and of course global variables always need to be copied (though the ability to define an argument as being by reference would be a nice addition for function inlining when it comes to global variables.

Discussion


Log in to post a comment.

Monday.com Logo