Commit 3800b24f authored by lrn@chromium.org's avatar lrn@chromium.org

Don't lazily compile functions that are immediately receded by '('.

We heuristically expect those functions to be called immediately.

Review URL: http://codereview.chromium.org/6311005

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@6309 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 2cccc400
...@@ -600,7 +600,8 @@ Parser::Parser(Handle<Script> script, ...@@ -600,7 +600,8 @@ Parser::Parser(Handle<Script> script,
extension_(extension), extension_(extension),
pre_data_(pre_data), pre_data_(pre_data),
fni_(NULL), fni_(NULL),
stack_overflow_(false) { stack_overflow_(false),
parenthesized_function_(false) {
AstNode::ResetIds(); AstNode::ResetIds();
} }
...@@ -2482,9 +2483,13 @@ Expression* Parser::ParseLeftHandSideExpression(bool* ok) { ...@@ -2482,9 +2483,13 @@ Expression* Parser::ParseLeftHandSideExpression(bool* ok) {
// The calls that need special treatment are the // The calls that need special treatment are the
// direct (i.e. not aliased) eval calls. These calls are all of 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 // form eval(...) with no explicit receiver object where eval is not
// declared in the current scope chain. These calls are marked as // declared in the current scope chain.
// potentially direct eval calls. Whether they are actually direct calls // These calls are marked as potentially direct eval calls. Whether
// to eval is determined at run time. // 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(); VariableProxy* callee = result->AsVariableProxy();
if (callee != NULL && callee->IsVariable(Factory::eval_symbol())) { if (callee != NULL && callee->IsVariable(Factory::eval_symbol())) {
Handle<String> name = callee->name(); Handle<String> name = callee->name();
...@@ -2734,6 +2739,9 @@ Expression* Parser::ParsePrimaryExpression(bool* ok) { ...@@ -2734,6 +2739,9 @@ Expression* Parser::ParsePrimaryExpression(bool* ok) {
case Token::LPAREN: case Token::LPAREN:
Consume(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); result = ParseExpression(true, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK); Expect(Token::RPAREN, CHECK_OK);
break; break;
...@@ -3225,8 +3233,11 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name, ...@@ -3225,8 +3233,11 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name,
// Determine if the function will be lazily compiled. The mode can // Determine if the function will be lazily compiled. The mode can
// only be PARSE_LAZILY if the --lazy flag is true. // only be PARSE_LAZILY if the --lazy flag is true.
bool is_lazily_compiled = bool is_lazily_compiled = (mode() == PARSE_LAZILY &&
mode() == PARSE_LAZILY && top_scope_->HasTrivialOuterContext(); 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 function_block_pos = scanner().location().beg_pos;
int materialized_literal_count; int materialized_literal_count;
......
...@@ -682,6 +682,11 @@ class Parser { ...@@ -682,6 +682,11 @@ class Parser {
ScriptDataImpl* pre_data_; ScriptDataImpl* pre_data_;
FuncNameInferrer* fni_; FuncNameInferrer* fni_;
bool stack_overflow_; 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_;
}; };
......
// Copyright 2010 the V8 project authors. All rights reserved. // Copyright 2010 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are // modification, are permitted provided that the following conditions are
...@@ -894,6 +895,7 @@ PreParser::Expression PreParser::ParsePrimaryExpression(bool* ok) { ...@@ -894,6 +895,7 @@ PreParser::Expression PreParser::ParsePrimaryExpression(bool* ok) {
case i::Token::LPAREN: case i::Token::LPAREN:
Consume(i::Token::LPAREN); Consume(i::Token::LPAREN);
parenthesized_function_ = (peek() == i::Token::FUNCTION);
result = ParseExpression(true, CHECK_OK); result = ParseExpression(true, CHECK_OK);
Expect(i::Token::RPAREN, CHECK_OK); Expect(i::Token::RPAREN, CHECK_OK);
if (result == kIdentifierExpression) result = kUnknownExpression; if (result == kIdentifierExpression) result = kUnknownExpression;
...@@ -1071,8 +1073,10 @@ PreParser::Expression PreParser::ParseFunctionLiteral(bool* ok) { ...@@ -1071,8 +1073,10 @@ PreParser::Expression PreParser::ParseFunctionLiteral(bool* ok) {
// Determine if the function will be lazily compiled. // Determine if the function will be lazily compiled.
// Currently only happens to top-level functions. // Currently only happens to top-level functions.
// Optimistically assume that all top-level functions are lazily compiled. // Optimistically assume that all top-level functions are lazily compiled.
bool is_lazily_compiled = bool is_lazily_compiled = (outer_scope_type == kTopLevelScope &&
(outer_scope_type == kTopLevelScope && !inside_with && allow_lazy_); !inside_with && allow_lazy_ &&
!parenthesized_function_);
parenthesized_function_ = false;
if (is_lazily_compiled) { if (is_lazily_compiled) {
log_->PauseRecording(); log_->PauseRecording();
......
...@@ -144,7 +144,8 @@ class PreParser { ...@@ -144,7 +144,8 @@ class PreParser {
scope_(NULL), scope_(NULL),
stack_limit_(stack_limit), stack_limit_(stack_limit),
stack_overflow_(false), stack_overflow_(false),
allow_lazy_(true) { } allow_lazy_(true),
parenthesized_function_(false) { }
// Preparse the program. Only called in PreParseProgram after creating // Preparse the program. Only called in PreParseProgram after creating
// the instance. // the instance.
...@@ -268,6 +269,7 @@ class PreParser { ...@@ -268,6 +269,7 @@ class PreParser {
uintptr_t stack_limit_; uintptr_t stack_limit_;
bool stack_overflow_; bool stack_overflow_;
bool allow_lazy_; bool allow_lazy_;
bool parenthesized_function_;
}; };
} } // v8::preparser } } // v8::preparser
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment