Reviewers: rossberg,

Message:
rossberg, ptal

Description:
AstValueFactory: make true, false, null, undefined and "the hole" unique values.

They were not, so we were creating several instances of them, one for each time
they occurred in the source code.

It's not known to have caused efficiency problems though, so this is a sanity
fix more than an efficiency fix.

Note that numbers are still not unique.

BUG=

Please review this at https://codereview.chromium.org/657893003/

Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files (+36, -50 lines):
  M src/ast.h
  M src/ast-value-factory.h
  M src/ast-value-factory.cc


Index: src/ast-value-factory.cc
diff --git a/src/ast-value-factory.cc b/src/ast-value-factory.cc
index 4df6ac01a03a7ddef1fa401c6b0707677bc1950b..84df5cc172a8faca69d640b234a379948807aebd 100644
--- a/src/ast-value-factory.cc
+++ b/src/ast-value-factory.cc
@@ -330,16 +330,6 @@ const AstValue* AstValueFactory::NewSmi(int number) {
 }


-const AstValue* AstValueFactory::NewBoolean(bool b) {
-  AstValue* value = new (zone_) AstValue(b);
-  if (isolate_) {
-    value->Internalize(isolate_);
-  }
-  values_.Add(value);
-  return value;
-}
-
-
 const AstValue* AstValueFactory::NewStringList(
     ZoneList<const AstRawString*>* strings) {
   AstValue* value = new (zone_) AstValue(strings);
@@ -351,36 +341,6 @@ const AstValue* AstValueFactory::NewStringList(
 }


-const AstValue* AstValueFactory::NewNull() {
-  AstValue* value = new (zone_) AstValue(AstValue::NULL_TYPE);
-  if (isolate_) {
-    value->Internalize(isolate_);
-  }
-  values_.Add(value);
-  return value;
-}
-
-
-const AstValue* AstValueFactory::NewUndefined() {
-  AstValue* value = new (zone_) AstValue(AstValue::UNDEFINED);
-  if (isolate_) {
-    value->Internalize(isolate_);
-  }
-  values_.Add(value);
-  return value;
-}
-
-
-const AstValue* AstValueFactory::NewTheHole() {
-  AstValue* value = new (zone_) AstValue(AstValue::THE_HOLE);
-  if (isolate_) {
-    value->Internalize(isolate_);
-  }
-  values_.Add(value);
-  return value;
-}
-
-
 const AstRawString* AstValueFactory::GetString(
     uint32_t hash, bool is_one_byte, Vector<const byte> literal_bytes) {
   // literal_bytes here points to whatever the user passed, and this is OK
Index: src/ast-value-factory.h
diff --git a/src/ast-value-factory.h b/src/ast-value-factory.h
index de8a44228c9d59716d62a22e79db6a4560af6ebc..0fa3b8f699c770d3f8bde117d3a0e2e357cfabab 100644
--- a/src/ast-value-factory.h
+++ b/src/ast-value-factory.h
@@ -238,7 +238,7 @@ class AstValue : public ZoneObject {
 };


-// For generating string constants.
+// For generating constants.
 #define STRING_CONSTANTS(F)                           \
   F(anonymous_function, "(anonymous function)")       \
   F(arguments, "arguments")                           \
@@ -268,6 +268,12 @@ class AstValue : public ZoneObject {
   F(use_strict, "use strict")                         \
   F(value, "value")

+#define OTHER_CONSTANTS(F)                \
+  F(true_value, true)                     \
+  F(false_value, false)                   \
+  F(null_value, AstValue::NULL_TYPE)      \
+  F(undefined_value, AstValue::UNDEFINED) \
+  F(the_hole_value, AstValue::THE_HOLE)

 class AstValueFactory {
  public:
@@ -280,6 +286,9 @@ class AstValueFactory {
     name##_string_ = NULL;
     STRING_CONSTANTS(F)
 #undef F
+#define F(name, value) name##_ = NULL;
+    OTHER_CONSTANTS(F)
+#undef F
   }

   Zone* zone() const { return zone_; }
@@ -312,16 +321,27 @@ class AstValueFactory {
   STRING_CONSTANTS(F)
 #undef F

+#define F(name, value)                       \
+  const AstValue* name() {                   \
+    if (name##_ == NULL) {                   \
+      name##_ = new (zone_) AstValue(value); \
+      if (isolate_) {                        \
+        name##_->Internalize(isolate_);      \
+      }                                      \
+      values_.Add(name##_);                  \
+    }                                        \
+    return name##_;                          \
+  }
+  OTHER_CONSTANTS(F)
+#undef F
+
+
   const AstValue* NewString(const AstRawString* string);
   // A JavaScript symbol (ECMA-262 edition 6).
   const AstValue* NewSymbol(const char* name);
   const AstValue* NewNumber(double number);
   const AstValue* NewSmi(int number);
-  const AstValue* NewBoolean(bool b);
   const AstValue* NewStringList(ZoneList<const AstRawString*>* strings);
-  const AstValue* NewNull();
-  const AstValue* NewUndefined();
-  const AstValue* NewTheHole();

  private:
   const AstRawString* GetString(uint32_t hash, bool is_one_byte,
@@ -342,6 +362,10 @@ class AstValueFactory {
   const AstRawString* name##_string_;
   STRING_CONSTANTS(F)
 #undef F
+
+#define F(name, value) AstValue* name##_;
+  OTHER_CONSTANTS(F)
+#undef F
 };


@@ -351,5 +375,6 @@ bool AstRawString::IsArguments(AstValueFactory* ast_value_factory) const {
 } }  // namespace v8::internal

 #undef STRING_CONSTANTS
+#undef OTHER_CONSTANTS

 #endif  // V8_AST_VALUE_FACTORY_H_
Index: src/ast.h
diff --git a/src/ast.h b/src/ast.h
index f997c44a82d418c76a9bf79b23d050727b860e81..67735639fc8fca8fd4fb3a7d4855900cc228f545 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -3401,8 +3401,9 @@ class AstNodeFactory FINAL BASE_EMBEDDED {
   }

   Literal* NewBooleanLiteral(bool b, int pos) {
-    Literal* lit =
-        new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
+    const AstValue* value = b ? ast_value_factory_->true_value()
+                              : ast_value_factory_->false_value();
+    Literal* lit = new (zone_) Literal(zone_, value, pos);
     VISIT_AND_RETURN(Literal, lit)
   }

@@ -3415,19 +3416,19 @@ class AstNodeFactory FINAL BASE_EMBEDDED {

   Literal* NewNullLiteral(int pos) {
     Literal* lit =
-        new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
+        new (zone_) Literal(zone_, ast_value_factory_->null_value(), pos);
     VISIT_AND_RETURN(Literal, lit)
   }

   Literal* NewUndefinedLiteral(int pos) {
     Literal* lit =
- new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos); + new (zone_) Literal(zone_, ast_value_factory_->undefined_value(), pos);
     VISIT_AND_RETURN(Literal, lit)
   }

   Literal* NewTheHoleLiteral(int pos) {
     Literal* lit =
-        new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
+ new (zone_) Literal(zone_, ast_value_factory_->the_hole_value(), pos);
     VISIT_AND_RETURN(Literal, lit)
   }



--
--
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/d/optout.

Reply via email to