Is empty functions optimized away whenever know?

113 views
Skip to first unread message

idleman

unread,
Oct 20, 2012, 4:05:04 PM10/20/12
to v8-u...@googlegroups.com
Hi,

Is empty functions in lined whenever the function is know? Example:

function do_nothing() { }

//somewhere later in the code:
var cb = do_nothing;
cb(null, "Will this call be inlined/optimized away?");

Will V8 actually call the function, even if it does nothing? I wonder because I want to know if it is smarter to create a do_nothing() function which will be reused over and over again (but not as obvious) or each time create an empty function { } directly in place and let the V8 more easily optimize away the call.

Thanks in advance!



Vyacheslav Egorov

unread,
Oct 21, 2012, 10:15:16 AM10/21/12
to v8-u...@googlegroups.com

V8 does inline functions at call sites where target is observed to be always the same. Inclined body is guarded by an identity check against identity of the call target. If guard fails code is deoptimized.

Thus what matters is whether each call site is monomorphic ( sees the same function all the time) or megamorphic (sees different functions).

Without seeing complete code it is hard to say whether you will help inlining by creating a single empty function (inlining definitely will not happen if you create new functions and send them to a single call site again and again). But you will definitely save space.

--
Vyacheslav Egorov

idleman

unread,
Oct 21, 2012, 1:10:53 PM10/21/12
to v8-u...@googlegroups.com
Thanks for your answer!

To make the question some more clear, I invoke huge number of asynchronous functions, but sometimes I just don´t care about the result, but the function itself require a callback to pass the result of the operation to. Would it in those cases be smarter to create a global do_nothing() function and pass it into all the asynchronous functions where I don´t care about the result, than on invocation just create a new empty function:

//new empty functions each time
async_http_get("http://statics.com?webpage=abc", function() { });
async_flush(function() { });

//or using a global do_nothing function
function do_nothing() { }

async_http_get("http://statics.com?webpage=abc", do_nothing);
async_flush(do_nothing);

My question regards if V8 would easier optimize away the "callback" call when using anonymous empty functions or not, because if it does, it would be worthless to create a global do_nothing on the first place.

But what I understood, V8 does no such optimizations? What would be better in that case, using a global do_nothing() or not?

Thanks in advance!

Michael Schwartz

unread,
Oct 21, 2012, 1:19:00 PM10/21/12
to v8-u...@googlegroups.com
How about:

async_flush(null);

or simler:

async_flush();

Vyacheslav Egorov

unread,
Oct 21, 2012, 1:58:00 PM10/21/12
to v8-u...@googlegroups.com

Well if you create a single global function you will at least save memory and allocation time (as function literal creates a new function  every time it is executed).

Additionally if you always pass empty function to async_http_get then its better to create a single function to help inlining as explained in my previous mail.

Anyway all this matters only on a very hot path.

Vyacheslav Egorov

idleman

unread,
Oct 21, 2012, 2:08:13 PM10/21/12
to v8-u...@googlegroups.com
Thanks, exactly what I wanted to hear. :-)

PS. async_http_get and async_flush was only used as an example, they does not exist.
Reply all
Reply to author
Forward
0 new messages