Reviewers: Søren Gjesse,
Description:
Don't lazily compile functions that are immediately receded by '('.
We heuristically expect those functions to be called immediately.
Please review this at http://codereview.chromium.org/6311005/
Affected files:
M src/parser.h
M src/parser.cc
M src/preparser.h
M src/preparser.cc
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index
5ea1c5e083ac44a15ea4ac100f1f6848d9d4fffd..3730a4577cf6f1e70440637b575f8547d96892a0
100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -600,7 +600,8 @@ Parser::Parser(Handle<Script> script,
extension_(extension),
pre_data_(pre_data),
fni_(NULL),
- stack_overflow_(false) {
+ stack_overflow_(false),
+ parenthesized_function_(false) {
AstNode::ResetIds();
}
@@ -2482,9 +2483,13 @@ Expression*
Parser::ParseLeftHandSideExpression(bool* ok) {
// The calls that need special treatment are the
// direct (i.e. not aliased) eval calls. These calls are all of the
// form eval(...) with no explicit receiver object where eval is
not
- // declared in the current scope chain. These calls are marked as
- // potentially direct eval calls. Whether they are actually direct
calls
- // to eval is determined at run time.
+ // declared in the current scope chain.
+ // These calls are marked as potentially direct eval calls. Whether
+ // they are actually direct calls to eval is determined at run
time.
+ // TODO(994): In ES5, it doesn't matter if the "eval" var is
declared
+ // in the local scope chain. It only matters that it's
called "eval",
+ // is called without a receiver and it refers to the original eval
+ // function.
VariableProxy* callee = result->AsVariableProxy();
if (callee != NULL && callee->IsVariable(Factory::eval_symbol())) {
Handle<String> name = callee->name();
@@ -2734,6 +2739,9 @@ Expression* Parser::ParsePrimaryExpression(bool* ok) {
case Token::LPAREN:
Consume(Token::LPAREN);
+ // Heuristically try to detect immediately called functions before
+ // seeing the call parentheses.
+ parenthesized_function_ = (peek() == Token::FUNCTION);
result = ParseExpression(true, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK);
break;
@@ -3225,8 +3233,11 @@ FunctionLiteral*
Parser::ParseFunctionLiteral(Handle<String> var_name,
// Determine if the function will be lazily compiled. The mode can
// only be PARSE_LAZILY if the --lazy flag is true.
- bool is_lazily_compiled =
- mode() == PARSE_LAZILY && top_scope_->HasTrivialOuterContext();
+ bool is_lazily_compiled = (mode() == PARSE_LAZILY &&
+
top_scope_->outer_scope()->is_global_scope() &&
+ top_scope_->HasTrivialOuterContext() &&
+ !parenthesized_function_);
+ parenthesized_function_ = false; // The bit was set for this function
only.
int function_block_pos = scanner().location().beg_pos;
int materialized_literal_count;
Index: src/parser.h
diff --git a/src/parser.h b/src/parser.h
index
1dfc153362c6831eb4c804614cb4cfb2bb0639cf..460f6647facb6bc7680f3dcf28c8ca583b081527
100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -682,6 +682,11 @@ class Parser {
ScriptDataImpl* pre_data_;
FuncNameInferrer* fni_;
bool stack_overflow_;
+ // If true, the next (and immediately following) function literal is
+ // preceded by a parenthesis.
+ // Heuristically that means that the function will be called immediately,
+ // so never lazily compile it.
+ bool parenthesized_function_;
};
Index: src/preparser.cc
diff --git a/src/preparser.cc b/src/preparser.cc
index
e05f903772e6147fad2da97ef808b06d80335787..c0dcc0b4a1df3bdc9512c22341276be220e2468a
100644
--- a/src/preparser.cc
+++ b/src/preparser.cc
@@ -1,3 +1,4 @@
+
// Copyright 2010 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
@@ -894,6 +895,7 @@ PreParser::Expression
PreParser::ParsePrimaryExpression(bool* ok) {
case i::Token::LPAREN:
Consume(i::Token::LPAREN);
+ parenthesized_function_ = (peek() == i::Token::FUNCTION);
result = ParseExpression(true, CHECK_OK);
Expect(i::Token::RPAREN, CHECK_OK);
if (result == kIdentifierExpression) result = kUnknownExpression;
@@ -1071,8 +1073,10 @@ PreParser::Expression
PreParser::ParseFunctionLiteral(bool* ok) {
// Determine if the function will be lazily compiled.
// Currently only happens to top-level functions.
// Optimistically assume that all top-level functions are lazily
compiled.
- bool is_lazily_compiled =
- (outer_scope_type == kTopLevelScope && !inside_with && allow_lazy_);
+ bool is_lazily_compiled = (outer_scope_type == kTopLevelScope &&
+ !inside_with && allow_lazy_ &&
+ !parenthesized_function_);
+ parenthesized_function_ = false;
if (is_lazily_compiled) {
log_->PauseRecording();
Index: src/preparser.h
diff --git a/src/preparser.h b/src/preparser.h
index
536e6d4f431050037970bd725126f8e9569a48eb..66fad3bcbf6b51456577a0219748f2013c4fb1e8
100644
--- a/src/preparser.h
+++ b/src/preparser.h
@@ -144,7 +144,8 @@ class PreParser {
scope_(NULL),
stack_limit_(stack_limit),
stack_overflow_(false),
- allow_lazy_(true) { }
+ allow_lazy_(true),
+ parenthesized_function_(false) { }
// Preparse the program. Only called in PreParseProgram after creating
// the instance.
@@ -268,6 +269,7 @@ class PreParser {
uintptr_t stack_limit_;
bool stack_overflow_;
bool allow_lazy_;
+ bool parenthesized_function_;
};
} } // v8::preparser
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev