AstValueFactory: make true, false, null, undefined and "the hole" unique values.
authormarja@chromium.org <marja@chromium.org>
Fri, 24 Oct 2014 13:02:23 +0000 (13:02 +0000)
committermarja@chromium.org <marja@chromium.org>
Fri, 24 Oct 2014 13:12:11 +0000 (13:12 +0000)
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=
R=rossberg@chromium.org

Review URL: https://codereview.chromium.org/657893003

Cr-Commit-Position: refs/heads/master@{#24873}
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24873 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/ast-value-factory.cc
src/ast-value-factory.h

index 4df6ac0..0a1949a 100644 (file)
@@ -330,13 +330,23 @@ const AstValue* AstValueFactory::NewSmi(int number) {
 }
 
 
+#define GENERATE_VALUE_GETTER(value, initializer) \
+  if (!value) {                                   \
+    value = new (zone_) AstValue(initializer);    \
+    if (isolate_) {                               \
+      value->Internalize(isolate_);               \
+    }                                             \
+    values_.Add(value);                           \
+  }                                               \
+  return value;
+
+
 const AstValue* AstValueFactory::NewBoolean(bool b) {
-  AstValue* value = new (zone_) AstValue(b);
-  if (isolate_) {
-    value->Internalize(isolate_);
+  if (b) {
+    GENERATE_VALUE_GETTER(true_value_, true);
+  } else {
+    GENERATE_VALUE_GETTER(false_value_, false);
   }
-  values_.Add(value);
-  return value;
 }
 
 
@@ -352,35 +362,22 @@ 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;
+  GENERATE_VALUE_GETTER(null_value_, AstValue::NULL_TYPE);
 }
 
 
 const AstValue* AstValueFactory::NewUndefined() {
-  AstValue* value = new (zone_) AstValue(AstValue::UNDEFINED);
-  if (isolate_) {
-    value->Internalize(isolate_);
-  }
-  values_.Add(value);
-  return value;
+  GENERATE_VALUE_GETTER(undefined_value_, AstValue::UNDEFINED);
 }
 
 
 const AstValue* AstValueFactory::NewTheHole() {
-  AstValue* value = new (zone_) AstValue(AstValue::THE_HOLE);
-  if (isolate_) {
-    value->Internalize(isolate_);
-  }
-  values_.Add(value);
-  return value;
+  GENERATE_VALUE_GETTER(the_hole_value_, AstValue::THE_HOLE);
 }
 
 
+#undef GENERATE_VALUE_GETTER
+
 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 de8a442..774e534 100644 (file)
@@ -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)            \
+  F(false_value)           \
+  F(null_value)            \
+  F(undefined_value)       \
+  F(the_hole_value)
 
 class AstValueFactory {
  public:
@@ -276,10 +282,12 @@ class AstValueFactory {
         zone_(zone),
         isolate_(NULL),
         hash_seed_(hash_seed) {
-#define F(name, str) \
-    name##_string_ = NULL;
+#define F(name, str) name##_string_ = NULL;
     STRING_CONSTANTS(F)
 #undef F
+#define F(name) name##_ = NULL;
+    OTHER_CONSTANTS(F)
+#undef F
   }
 
   Zone* zone() const { return zone_; }
@@ -299,15 +307,15 @@ class AstValueFactory {
     return isolate_ != NULL;
   }
 
-#define F(name, str) \
-  const AstRawString* name##_string() { \
-    if (name##_string_ == NULL) { \
-      const char* data = str; \
-      name##_string_ = GetOneByteString( \
+#define F(name, str)                                                    \
+  const AstRawString* name##_string() {                                 \
+    if (name##_string_ == NULL) {                                       \
+      const char* data = str;                                           \
+      name##_string_ = GetOneByteString(                                \
           Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), \
-                                static_cast<int>(strlen(data)))); \
-    } \
-    return name##_string_; \
+                                static_cast<int>(strlen(data))));       \
+    }                                                                   \
+    return name##_string_;                                              \
   }
   STRING_CONSTANTS(F)
 #undef F
@@ -338,10 +346,13 @@ class AstValueFactory {
 
   uint32_t hash_seed_;
 
-#define F(name, str) \
-  const AstRawString* name##_string_;
+#define F(name, str) const AstRawString* name##_string_;
   STRING_CONSTANTS(F)
 #undef F
+
+#define F(name) AstValue* name##_;
+  OTHER_CONSTANTS(F)
+#undef F
 };
 
 
@@ -351,5 +362,6 @@ bool AstRawString::IsArguments(AstValueFactory* ast_value_factory) const {
 } }  // namespace v8::internal
 
 #undef STRING_CONSTANTS
+#undef OTHER_CONSTANTS
 
 #endif  // V8_AST_VALUE_FACTORY_H_