Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: src/ast/ast-value-factory.h

Issue 2225423002: Make the AstValueFactory more efficient and less memory hungry (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Undo union since windows Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/ast/ast.cc ('k') | src/ast/ast-value-factory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 25 matching lines...) Expand all
36 // independent of the V8 heap and internalizing them later. During parsing, 36 // independent of the V8 heap and internalizing them later. During parsing,
37 // AstStrings and AstValues are created and stored outside the heap, in 37 // AstStrings and AstValues are created and stored outside the heap, in
38 // AstValueFactory. After parsing, the strings and values are internalized 38 // AstValueFactory. After parsing, the strings and values are internalized
39 // (moved into the V8 heap). 39 // (moved into the V8 heap).
40 namespace v8 { 40 namespace v8 {
41 namespace internal { 41 namespace internal {
42 42
43 class AstString : public ZoneObject { 43 class AstString : public ZoneObject {
44 public: 44 public:
45 explicit AstString(bool is_raw) 45 explicit AstString(bool is_raw)
46 : bit_field_(IsRawStringBits::encode(is_raw)) {} 46 : next_(nullptr), bit_field_(IsRawStringBits::encode(is_raw)) {}
47
48 ~AstString() {}
49 47
50 int length() const; 48 int length() const;
51 bool IsEmpty() const { return length() == 0; } 49 bool IsEmpty() const { return length() == 0; }
52 50
53 // Puts the string into the V8 heap. 51 // Puts the string into the V8 heap.
54 void Internalize(Isolate* isolate); 52 void Internalize(Isolate* isolate);
55 53
56 // This function can be called after internalizing. 54 // This function can be called after internalizing.
57 V8_INLINE Handle<String> string() const { 55 V8_INLINE Handle<String> string() const {
58 DCHECK(!string_.is_null()); 56 DCHECK(!string_.is_null());
59 return string_; 57 return string_;
60 } 58 }
61 59
60 AstString** next_location() { return &next_; }
61 AstString* next() const { return next_; }
62
62 protected: 63 protected:
63 // This is null until the string is internalized. 64 // Handle<String>::null() until internalized.
64 Handle<String> string_; 65 Handle<String> string_;
66 AstString* next_;
65 // Poor-man's virtual dispatch to AstRawString / AstConsString. Takes less 67 // Poor-man's virtual dispatch to AstRawString / AstConsString. Takes less
66 // memory. 68 // memory.
67 class IsRawStringBits : public BitField<bool, 0, 1> {}; 69 class IsRawStringBits : public BitField<bool, 0, 1> {};
68 int bit_field_; 70 int bit_field_;
69 }; 71 };
70 72
71 73
72 class AstRawString final : public AstString { 74 class AstRawString final : public AstString {
73 public: 75 public:
74 int length() const { 76 int length() const {
75 if (is_one_byte()) return literal_bytes_.length(); 77 if (is_one_byte()) return literal_bytes_.length();
76 return literal_bytes_.length() / 2; 78 return literal_bytes_.length() / 2;
77 } 79 }
78 80
79 int byte_length() const { return literal_bytes_.length(); } 81 int byte_length() const { return literal_bytes_.length(); }
80 82
81 void Internalize(Isolate* isolate); 83 void Internalize(Isolate* isolate);
82 84
83 bool AsArrayIndex(uint32_t* index, HandleDereferenceMode deref_mode = 85 bool AsArrayIndex(uint32_t* index) const;
84 HandleDereferenceMode::kAllowed) const;
85 86
86 // The string is not null-terminated, use length() to find out the length. 87 // The string is not null-terminated, use length() to find out the length.
87 const unsigned char* raw_data() const { 88 const unsigned char* raw_data() const {
88 return literal_bytes_.start(); 89 return literal_bytes_.start();
89 } 90 }
90 91
91 bool is_one_byte() const { return IsOneByteBits::decode(bit_field_); } 92 bool is_one_byte() const { return IsOneByteBits::decode(bit_field_); }
92 93
93 bool IsOneByteEqualTo(const char* data) const; 94 bool IsOneByteEqualTo(const char* data) const;
94 uint16_t FirstCharacter() const { 95 uint16_t FirstCharacter() const {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 175
175 Smi* AsSmi() const { 176 Smi* AsSmi() const {
176 CHECK_EQ(SMI, type_); 177 CHECK_EQ(SMI, type_);
177 return Smi::FromInt(smi_); 178 return Smi::FromInt(smi_);
178 } 179 }
179 180
180 bool EqualsString(const AstRawString* string) const { 181 bool EqualsString(const AstRawString* string) const {
181 return type_ == STRING && string_ == string; 182 return type_ == STRING && string_ == string;
182 } 183 }
183 184
184 bool IsPropertyName( 185 bool IsPropertyName() const;
185 HandleDereferenceMode deref_mode = HandleDereferenceMode::kAllowed) const;
186 186
187 bool BooleanValue() const; 187 bool BooleanValue() const;
188 188
189 bool IsSmi() const { return type_ == SMI; } 189 bool IsSmi() const { return type_ == SMI; }
190 bool IsFalse() const { return type_ == BOOLEAN && !bool_; } 190 bool IsFalse() const { return type_ == BOOLEAN && !bool_; }
191 bool IsTrue() const { return type_ == BOOLEAN && bool_; } 191 bool IsTrue() const { return type_ == BOOLEAN && bool_; }
192 bool IsUndefined() const { return type_ == UNDEFINED; } 192 bool IsUndefined() const { return type_ == UNDEFINED; }
193 bool IsTheHole() const { return type_ == THE_HOLE; } 193 bool IsTheHole() const { return type_ == THE_HOLE; }
194 bool IsNull() const { return type_ == NULL_TYPE; } 194 bool IsNull() const { return type_ == NULL_TYPE; }
195 195
196 void Internalize(Isolate* isolate); 196 void Internalize(Isolate* isolate);
197 197
198 // Can be called after Internalize has been called. 198 // Can be called after Internalize has been called.
199 V8_INLINE Handle<Object> value() const { 199 V8_INLINE Handle<Object> value() const {
200 if (type_ == STRING) { 200 if (type_ == STRING) {
201 return string_->string(); 201 return string_->string();
202 } 202 }
203 DCHECK(!value_.is_null()); 203 DCHECK(!value_.is_null());
204 return value_; 204 return value_;
205 } 205 }
206 AstValue* next() const { return next_; }
207 void set_next(AstValue* next) { next_ = next; }
206 208
207 private: 209 private:
208 friend class AstValueFactory; 210 friend class AstValueFactory;
209 211
210 enum Type { 212 enum Type {
211 STRING, 213 STRING,
212 SYMBOL, 214 SYMBOL,
213 NUMBER, 215 NUMBER,
214 NUMBER_WITH_DOT, 216 NUMBER_WITH_DOT,
215 SMI, 217 SMI,
216 BOOLEAN, 218 BOOLEAN,
217 NULL_TYPE, 219 NULL_TYPE,
218 UNDEFINED, 220 UNDEFINED,
219 THE_HOLE 221 THE_HOLE
220 }; 222 };
221 223
222 explicit AstValue(const AstRawString* s) : type_(STRING) { string_ = s; } 224 explicit AstValue(const AstRawString* s) : type_(STRING), next_(nullptr) {
225 string_ = s;
226 }
223 227
224 explicit AstValue(const char* name) : type_(SYMBOL) { symbol_name_ = name; } 228 explicit AstValue(const char* name) : type_(SYMBOL), next_(nullptr) {
229 symbol_name_ = name;
230 }
225 231
226 explicit AstValue(double n, bool with_dot) { 232 explicit AstValue(double n, bool with_dot) : next_(nullptr) {
227 if (with_dot) { 233 if (with_dot) {
228 type_ = NUMBER_WITH_DOT; 234 type_ = NUMBER_WITH_DOT;
229 number_ = n; 235 number_ = n;
230 } else { 236 } else {
231 int int_value; 237 int int_value;
232 if (DoubleToSmiInteger(n, &int_value)) { 238 if (DoubleToSmiInteger(n, &int_value)) {
233 type_ = SMI; 239 type_ = SMI;
234 smi_ = int_value; 240 smi_ = int_value;
235 } else { 241 } else {
236 type_ = NUMBER; 242 type_ = NUMBER;
237 number_ = n; 243 number_ = n;
238 } 244 }
239 } 245 }
240 } 246 }
241 247
242 AstValue(Type t, int i) : type_(t) { 248 AstValue(Type t, int i) : type_(t), next_(nullptr) {
243 DCHECK(type_ == SMI); 249 DCHECK(type_ == SMI);
244 smi_ = i; 250 smi_ = i;
245 } 251 }
246 252
247 explicit AstValue(bool b) : type_(BOOLEAN) { bool_ = b; } 253 explicit AstValue(bool b) : type_(BOOLEAN), next_(nullptr) { bool_ = b; }
248 254
249 explicit AstValue(Type t) : type_(t) { 255 explicit AstValue(Type t) : type_(t), next_(nullptr) {
250 DCHECK(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE); 256 DCHECK(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE);
251 } 257 }
252 258
253 Type type_; 259 Type type_;
254 260
255 // Uninternalized value. 261 // Uninternalized value.
256 union { 262 union {
257 const AstRawString* string_; 263 const AstRawString* string_;
258 double number_; 264 double number_;
259 int smi_; 265 int smi_;
260 bool bool_; 266 bool bool_;
261 ZoneList<const AstRawString*>* strings_; 267 const AstRawString* strings_;
262 const char* symbol_name_; 268 const char* symbol_name_;
263 }; 269 };
264 270
265 // Internalized value (empty before internalized). 271 // Handle<String>::null() until internalized.
266 Handle<Object> value_; 272 Handle<Object> value_;
273 AstValue* next_;
267 }; 274 };
268 275
269 276
270 // For generating constants. 277 // For generating constants.
271 #define STRING_CONSTANTS(F) \ 278 #define STRING_CONSTANTS(F) \
272 F(anonymous_function, "(anonymous function)") \ 279 F(anonymous_function, "(anonymous function)") \
273 F(arguments, "arguments") \ 280 F(arguments, "arguments") \
274 F(async, "async") \ 281 F(async, "async") \
275 F(await, "await") \ 282 F(await, "await") \
276 F(constructor, "constructor") \ 283 F(constructor, "constructor") \
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 F(true_value) \ 316 F(true_value) \
310 F(false_value) \ 317 F(false_value) \
311 F(null_value) \ 318 F(null_value) \
312 F(undefined_value) \ 319 F(undefined_value) \
313 F(the_hole_value) 320 F(the_hole_value)
314 321
315 class AstValueFactory { 322 class AstValueFactory {
316 public: 323 public:
317 AstValueFactory(Zone* zone, uint32_t hash_seed) 324 AstValueFactory(Zone* zone, uint32_t hash_seed)
318 : string_table_(AstRawStringCompare), 325 : string_table_(AstRawStringCompare),
326 values_(nullptr),
327 strings_end_(&strings_),
319 zone_(zone), 328 zone_(zone),
320 isolate_(NULL), 329 isolate_(NULL),
321 hash_seed_(hash_seed) { 330 hash_seed_(hash_seed) {
331 ResetStrings();
322 #define F(name, str) name##_string_ = NULL; 332 #define F(name, str) name##_string_ = NULL;
323 STRING_CONSTANTS(F) 333 STRING_CONSTANTS(F)
324 #undef F 334 #undef F
325 #define F(name) name##_ = NULL; 335 #define F(name) name##_ = NULL;
326 OTHER_CONSTANTS(F) 336 OTHER_CONSTANTS(F)
327 #undef F 337 #undef F
328 } 338 }
329 339
330 Zone* zone() const { return zone_; } 340 Zone* zone() const { return zone_; }
331 341
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 const AstValue* NewSymbol(const char* name); 376 const AstValue* NewSymbol(const char* name);
367 const AstValue* NewNumber(double number, bool with_dot = false); 377 const AstValue* NewNumber(double number, bool with_dot = false);
368 const AstValue* NewSmi(int number); 378 const AstValue* NewSmi(int number);
369 const AstValue* NewBoolean(bool b); 379 const AstValue* NewBoolean(bool b);
370 const AstValue* NewStringList(ZoneList<const AstRawString*>* strings); 380 const AstValue* NewStringList(ZoneList<const AstRawString*>* strings);
371 const AstValue* NewNull(); 381 const AstValue* NewNull();
372 const AstValue* NewUndefined(); 382 const AstValue* NewUndefined();
373 const AstValue* NewTheHole(); 383 const AstValue* NewTheHole();
374 384
375 private: 385 private:
386 AstValue* AddValue(AstValue* value) {
387 if (isolate_) {
388 value->Internalize(isolate_);
389 } else {
390 value->set_next(values_);
391 values_ = value;
392 }
393 return value;
394 }
395 AstString* AddString(AstString* string) {
396 if (isolate_) {
397 string->Internalize(isolate_);
398 } else {
399 *strings_end_ = string;
400 strings_end_ = string->next_location();
401 }
402 return string;
403 }
404 void ResetStrings() {
405 strings_ = nullptr;
406 strings_end_ = &strings_;
407 }
376 AstRawString* GetOneByteStringInternal(Vector<const uint8_t> literal); 408 AstRawString* GetOneByteStringInternal(Vector<const uint8_t> literal);
377 AstRawString* GetTwoByteStringInternal(Vector<const uint16_t> literal); 409 AstRawString* GetTwoByteStringInternal(Vector<const uint16_t> literal);
378 AstRawString* GetString(uint32_t hash, bool is_one_byte, 410 AstRawString* GetString(uint32_t hash, bool is_one_byte,
379 Vector<const byte> literal_bytes); 411 Vector<const byte> literal_bytes);
380 412
381 static bool AstRawStringCompare(void* a, void* b); 413 static bool AstRawStringCompare(void* a, void* b);
382 414
383 // All strings are copied here, one after another (no NULLs inbetween). 415 // All strings are copied here, one after another (no NULLs inbetween).
384 base::HashMap string_table_; 416 base::HashMap string_table_;
385 // For keeping track of all AstValues and AstRawStrings we've created (so that 417 // For keeping track of all AstValues and AstRawStrings we've created (so that
386 // they can be internalized later). 418 // they can be internalized later).
387 List<AstValue*> values_; 419 AstValue* values_;
388 List<AstString*> strings_; 420 // We need to keep track of strings_ in order, since cons strings require
421 // their members to be internalized first.
422 AstString* strings_;
423 AstString** strings_end_;
389 Zone* zone_; 424 Zone* zone_;
390 Isolate* isolate_; 425 Isolate* isolate_;
391 426
392 uint32_t hash_seed_; 427 uint32_t hash_seed_;
393 428
394 #define F(name, str) const AstRawString* name##_string_; 429 #define F(name, str) const AstRawString* name##_string_;
395 STRING_CONSTANTS(F) 430 STRING_CONSTANTS(F)
396 #undef F 431 #undef F
397 432
398 #define F(name) AstValue* name##_; 433 #define F(name) AstValue* name##_;
399 OTHER_CONSTANTS(F) 434 OTHER_CONSTANTS(F)
400 #undef F 435 #undef F
401 }; 436 };
402 } // namespace internal 437 } // namespace internal
403 } // namespace v8 438 } // namespace v8
404 439
405 #undef STRING_CONSTANTS 440 #undef STRING_CONSTANTS
406 #undef OTHER_CONSTANTS 441 #undef OTHER_CONSTANTS
407 442
408 #endif // V8_AST_AST_VALUE_FACTORY_H_ 443 #endif // V8_AST_AST_VALUE_FACTORY_H_
OLDNEW
« no previous file with comments | « src/ast/ast.cc ('k') | src/ast/ast-value-factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698