Does SpiderMonkey "inline" function calls if possible?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Does SpiderMonkey "inline" function calls if possible?

john.chionglo
I have a JavaScript program:

var x1 = 0;
var x2 = 0;

function f1() {
  x1 = 5;
  x2 = 6;
}

function f2() {
  f1();
  x1 = x1 + x2;
}

Will or can SpiderMonkey "inline" f1 within f2 such that there is no function called within f2? The function f2 may also be called in some other context. Will or can SpiderMonkey "inline" f2 too? If so, which version(s) of SpiderMonkey does this?

Regards,
John
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Does SpiderMonkey "inline" function calls if possible?

Donny Viszneki
Hi John,

Inline is an optimization which removes function call overhead. At minimum
a function call will involve a jump to another address and probably a
return to a previous address, and in native compilations of procedural
languages, this also involves pushing a bunch of registers onto the stack,
and popping them later.

Spidermonkey does something even more interesting than this, of course,
since it is compiled to machine code "just in time" (JIT) and make
decisions at runtime about the most efficient machine code representation
of a given code path.

Assuming Spidermonkey still contains a tracing JIT, this code can cross
function call boundaries, meaning that once the JIT compiles/optimizes a
part of your Javascript program, the function call may only exist
conceptually, but nothing in the code being executed resembles necessarily
a function call.

Since these compilations are done "just in time," as opposed to ahead of
time (AOT,) Spidermonkey has to weigh the costs of doing the work against
the potential reward. In your example, if that was your entire program, I
am quite sure Spidermonkey would not make any optimizations, since the
optimizations themselves would actually take longer than just running your
code on the interpreter.

Hope this helps, and I didn't get too many facts completely wrong!


On Thu, Dec 5, 2013 at 7:39 PM, <[hidden email]> wrote:

> I have a JavaScript program:
>
> var x1 = 0;
> var x2 = 0;
>
> function f1() {
>   x1 = 5;
>   x2 = 6;
> }
>
> function f2() {
>   f1();
>   x1 = x1 + x2;
> }
>
> Will or can SpiderMonkey "inline" f1 within f2 such that there is no
> function called within f2? The function f2 may also be called in some other
> context. Will or can SpiderMonkey "inline" f2 too? If so, which version(s)
> of SpiderMonkey does this?
>
> Regards,
> John
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>



--
http://codebad.com/
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Does SpiderMonkey "inline" function calls if possible?

Nicholas Nethercote
On Sat, Dec 7, 2013 at 9:58 AM, Donny Viszneki <[hidden email]> wrote:
>
> Assuming Spidermonkey still contains a tracing JIT

It does not.  The tracing JIT was removed some time ago.

I haven't worked on IonMonkey, but I'm pretty sure it does inlining.
I just grepped for "inlining" in the js/src/jit/ directory and there
are 80 hits.

Nick
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Does SpiderMonkey "inline" function calls if possible?

john.chionglo
In reply to this post by john.chionglo
Hi Donny and Nick,

Thanks for the responses.

For my "program", I could avoid function calls by declaring all variables at the same "context" or "level", and manually code the body of each function where they are "called". [Of course I may need some kind of "pre-processor" so that I can #define and #include elements in my program if and when it grows to keep it manageable].

However, if I know that the SpiderMonkey compiler and/or interpreter will avoid these function calls, then I won't have to do any "manual optimization -- avoid function calls". [My "program" will be included in PDF files and the files would be read by someone using a PDF reader that can interpret JavaScript which would most likely be Adobe Acrobat or Adobe Reader.]

The "program" will actually be called several times. The following "program" would more accurately reflect how I will be using the functions.

----- Begin of "Program" -----

var state = 0;
var iv = setInterval("main()", 1);

function fa() {
  // some processing and perhaps more function calls
}

function fb(0 {
  // some processing and perhaps more function calls
}

function f0() {
  fa();
  // some more processing and may include a change in state
}

function f1() {
  fb();
  // some more processing and may include a change in state
}

function f2() {
  fa();
  fb();
  // some more processig and may include a change in state
}

function fd() {
  if (iv) {
    state = -1;
    clearInterval(iv);
    iv = undefined;
  }
}

function main() {
  if (state==0) {
      f0();
  } else if (state==1) {
      f1();
  } else if (state==2) {
      f2();
  } else {
      fd();
  }
}

----- End of Program -----

Regards. - john

References

- JavaScript for Acrobat. Retrieved Dec. 7, 2013 from http://www.adobe.com/devnet/acrobat/javascript.html.
- SpiderMonkey Internals. Retrieved on Dec. 7, 2013 from https://developer.mozilla.org/en-US/docs/SpiderMonkey/Internals.
- SpiderMonkey (JavaScript engine). In Wikipedia, The Free Encyclopedia. Retrieved on Dec. 7, 2013 from http://en.wikipedia.org/w/index.php?title=SpiderMonkey_(JavaScript_engine)&oldid=567896174 .

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Does SpiderMonkey "inline" function calls if possible?

Kannan Vijayan
Hi John,

I would advise against doing manual optimization for this case. In
situations where function call overhead matters (hot code which is
hitting the function call frequently), the Ion JIT will eventually
compile the caller function and inline the call.

Manual inlining as you are suggesting reduces the readability of code,
and leads to worse program structure, generally for little to no
performance gain.

In the example you provided, if "main()" is hot, Ion will almost
certainly inline f0, f1, f2, and fd into main, and subsequently inline
fa, and fb into the their call sites.  Furthermore, you gain the
additional benefit that if you later use tools like the Firefox Profiler
to profile the performance of your code, you'll get accurate
measurements of how much of total execution time fa and fb are taking.  
If you manually inline the code, you lose that ability.

Your best bet is to let the engine do its thing, and keep focused on
writing clear, readable code that separates logic into the naturally
appropriate function call boundaries.

Cheers.
Kannan

On 12/7/2013, 4:09 PM, [hidden email] wrote:

> Hi Donny and Nick,
>
> Thanks for the responses.
>
> For my "program", I could avoid function calls by declaring all variables at the same "context" or "level", and manually code the body of each function where they are "called". [Of course I may need some kind of "pre-processor" so that I can #define and #include elements in my program if and when it grows to keep it manageable].
>
> However, if I know that the SpiderMonkey compiler and/or interpreter will avoid these function calls, then I won't have to do any "manual optimization -- avoid function calls". [My "program" will be included in PDF files and the files would be read by someone using a PDF reader that can interpret JavaScript which would most likely be Adobe Acrobat or Adobe Reader.]
>
> The "program" will actually be called several times. The following "program" would more accurately reflect how I will be using the functions.
>
> ----- Begin of "Program" -----
>
> var state = 0;
> var iv = setInterval("main()", 1);
>
> function fa() {
>    // some processing and perhaps more function calls
> }
>
> function fb(0 {
>    // some processing and perhaps more function calls
> }
>
> function f0() {
>    fa();
>    // some more processing and may include a change in state
> }
>
> function f1() {
>    fb();
>    // some more processing and may include a change in state
> }
>
> function f2() {
>    fa();
>    fb();
>    // some more processig and may include a change in state
> }
>
> function fd() {
>    if (iv) {
>      state = -1;
>      clearInterval(iv);
>      iv = undefined;
>    }
> }
>
> function main() {
>    if (state==0) {
>        f0();
>    } else if (state==1) {
>        f1();
>    } else if (state==2) {
>        f2();
>    } else {
>        fd();
>    }
> }
>
> ----- End of Program -----
>
> Regards. - john
>
> References
>
> - JavaScript for Acrobat. Retrieved Dec. 7, 2013 from http://www.adobe.com/devnet/acrobat/javascript.html.
> - SpiderMonkey Internals. Retrieved on Dec. 7, 2013 from https://developer.mozilla.org/en-US/docs/SpiderMonkey/Internals.
> - SpiderMonkey (JavaScript engine). In Wikipedia, The Free Encyclopedia. Retrieved on Dec. 7, 2013 from http://en.wikipedia.org/w/index.php?title=SpiderMonkey_(JavaScript_engine)&oldid=567896174 .
>
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine