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,
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;
......
......@@ -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_;
};
......
// 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();
......
......@@ -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
......
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