Status: New
Owner: ----

New issue 3195 by [email protected]: Eager and Double parsing function closures.
http://code.google.com/p/v8/issues/detail?id=3195

I have noticed that a function closure is always eagerly parsed, and yet parts of it are lazily parsed again during execution.

For example,

var bar = {};
(function foo() { // this is parsed eagerly because of the paren
bar.baz = function() { // this is parsed eagerly too, though it will be compiled lazily
    // add some code here to make the function long enough (1024)
  };
})();
bar.baz();

When the parser spots an immediately executed closure, it will fully parse that function (since it knows is going to be executed immediately, so it may as well parse it eagerly).

V8 has the logic "everything inside an eagerly parsed function is
parsed eagerly".

However, with compilation the same logic doesn't hold. It's possible
that we compile a function eagerly, and compile the inner functions
lazily. Both parsing and compilation have the logic "if you see a
paren before the function, make it eager".

Now bar.baz is parsed eagerly and compiled lazily. Which means that
it's not actually compiled until its ran. And at that time, it's
parsed again.

I think this is why you see code being parsed during run time, even
though it has already been eagerly parsed.

The "everything inside an eagerly parsed function is parsed eagerly"
logic is needed for cases like this:

(function foo() {
  var a = 0;
  bar.baz = function() { return a; }
})();

Now bar.baz references "a" which is in the outer scope, and it cannot
be lazily compiled (thus, it should also be parsed eagerly). However,
when we're thinking about whether to parse a function lazily or
eagerly, we don't yet know in which case we are (whether the function
refers stuff in the outer scope or not). The only way to find out is
to parse eagerly.

The problem:
Despite that we eagerly parse everything in the closure, we discard the AST after the compilation. During execution, we find that the functions we decided to omit in compilation are needed, so they are lazy parsed again then compiled --> double parsing them. (Scope information are stored for the second parsing pass)

Suggested solutions:
1- Eagerly compile all the code (but might result in extra compilation that are never executed). 2- Try to avoid eagerly parsing the code in the first place, pre-parsing might be enough to extract the information needed. 3- Maintain and use the AST for the non-compiled code so that it does not get reparsed.


(The description has text taken from conversations with rmcilroy@ and marja@)





--
You received this message because this project is configured to send all issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to