Revision: 6309
Author: [email protected]
Date: Fri Jan 14 02:50:13 2011
Log: 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
http://code.google.com/p/v8/source/detail?r=6309

Modified:
 /branches/bleeding_edge/src/parser.cc
 /branches/bleeding_edge/src/parser.h
 /branches/bleeding_edge/src/preparser.cc
 /branches/bleeding_edge/src/preparser.h

=======================================
--- /branches/bleeding_edge/src/parser.cc       Fri Jan  7 04:35:42 2011
+++ /branches/bleeding_edge/src/parser.cc       Fri Jan 14 02:50:13 2011
@@ -600,7 +600,8 @@
       extension_(extension),
       pre_data_(pre_data),
       fni_(NULL),
-      stack_overflow_(false) {
+      stack_overflow_(false),
+      parenthesized_function_(false) {
   AstNode::ResetIds();
 }

@@ -2482,9 +2483,13 @@
         // 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 @@

     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 @@

     // 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;
=======================================
--- /branches/bleeding_edge/src/parser.h        Fri Jan  7 04:35:42 2011
+++ /branches/bleeding_edge/src/parser.h        Fri Jan 14 02:50:13 2011
@@ -682,6 +682,11 @@
   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_;
 };


=======================================
--- /branches/bleeding_edge/src/preparser.cc    Tue Jan  4 04:07:16 2011
+++ /branches/bleeding_edge/src/preparser.cc    Fri Jan 14 02:50:13 2011
@@ -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 @@

     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 @@
   // 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();
=======================================
--- /branches/bleeding_edge/src/preparser.h     Tue Jan  4 04:07:16 2011
+++ /branches/bleeding_edge/src/preparser.h     Fri Jan 14 02:50:13 2011
@@ -144,7 +144,8 @@
         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 @@
   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

Reply via email to