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

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: 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 | « no previous file | src/ast/ast-value-factory.cc » ('j') | src/ast/ast-value-factory.cc » ('J')
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 // This is a pointer to the next uninternalized string until internalized.
64 Handle<String> string_; 65 union {
66 Handle<String> string_;
67 AstString* next_;
68 };
65 // Poor-man's virtual dispatch to AstRawString / AstConsString. Takes less 69 // Poor-man's virtual dispatch to AstRawString / AstConsString. Takes less
66 // memory. 70 // memory.
67 class IsRawStringBits : public BitField<bool, 0, 1> {}; 71 class IsRawStringBits : public BitField<bool, 0, 1> {};
68 int bit_field_; 72 int bit_field_;
69 }; 73 };
70 74
71 75
72 class AstRawString final : public AstString { 76 class AstRawString final : public AstString {
73 public: 77 public:
74 int length() const { 78 int length() const {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 void Internalize(Isolate* isolate); 198 void Internalize(Isolate* isolate);
195 199
196 // Can be called after Internalize has been called. 200 // Can be called after Internalize has been called.
197 V8_INLINE Handle<Object> value() const { 201 V8_INLINE Handle<Object> value() const {
198 if (type_ == STRING) { 202 if (type_ == STRING) {
199 return string_->string(); 203 return string_->string();
200 } 204 }
201 DCHECK(!value_.is_null()); 205 DCHECK(!value_.is_null());
202 return value_; 206 return value_;
203 } 207 }
208 AstValue* next() const { return next_; }
209 void set_next(AstValue* next) { next_ = next; }
204 210
205 private: 211 private:
206 friend class AstValueFactory; 212 friend class AstValueFactory;
207 213
208 enum Type { 214 enum Type {
209 STRING, 215 STRING,
210 SYMBOL, 216 SYMBOL,
211 NUMBER, 217 NUMBER,
212 NUMBER_WITH_DOT, 218 NUMBER_WITH_DOT,
213 SMI, 219 SMI,
214 BOOLEAN, 220 BOOLEAN,
215 NULL_TYPE, 221 NULL_TYPE,
216 UNDEFINED, 222 UNDEFINED,
217 THE_HOLE 223 THE_HOLE
218 }; 224 };
219 225
220 explicit AstValue(const AstRawString* s) : type_(STRING) { string_ = s; } 226 explicit AstValue(const AstRawString* s) : type_(STRING), next_(nullptr) {
227 string_ = s;
228 }
221 229
222 explicit AstValue(const char* name) : type_(SYMBOL) { symbol_name_ = name; } 230 explicit AstValue(const char* name) : type_(SYMBOL), next_(nullptr) {
231 symbol_name_ = name;
232 }
223 233
224 explicit AstValue(double n, bool with_dot) { 234 explicit AstValue(double n, bool with_dot) : next_(nullptr) {
225 if (with_dot) { 235 if (with_dot) {
226 type_ = NUMBER_WITH_DOT; 236 type_ = NUMBER_WITH_DOT;
227 number_ = n; 237 number_ = n;
228 } else { 238 } else {
229 int int_value; 239 int int_value;
230 if (DoubleToSmiInteger(n, &int_value)) { 240 if (DoubleToSmiInteger(n, &int_value)) {
231 type_ = SMI; 241 type_ = SMI;
232 smi_ = int_value; 242 smi_ = int_value;
233 } else { 243 } else {
234 type_ = NUMBER; 244 type_ = NUMBER;
235 number_ = n; 245 number_ = n;
236 } 246 }
237 } 247 }
238 } 248 }
239 249
240 AstValue(Type t, int i) : type_(t) { 250 AstValue(Type t, int i) : type_(t), next_(nullptr) {
241 DCHECK(type_ == SMI); 251 DCHECK(type_ == SMI);
242 smi_ = i; 252 smi_ = i;
243 } 253 }
244 254
245 explicit AstValue(bool b) : type_(BOOLEAN) { bool_ = b; } 255 explicit AstValue(bool b) : type_(BOOLEAN), next_(nullptr) { bool_ = b; }
246 256
247 explicit AstValue(Type t) : type_(t) { 257 explicit AstValue(Type t) : type_(t), next_(nullptr) {
248 DCHECK(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE); 258 DCHECK(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE);
249 } 259 }
250 260
251 Type type_; 261 Type type_;
252 262
253 // Uninternalized value. 263 // Uninternalized value.
254 union { 264 union {
255 const AstRawString* string_; 265 const AstRawString* string_;
256 double number_; 266 double number_;
257 int smi_; 267 int smi_;
258 bool bool_; 268 bool bool_;
259 ZoneList<const AstRawString*>* strings_; 269 const AstRawString* strings_;
260 const char* symbol_name_; 270 const char* symbol_name_;
261 }; 271 };
262 272
263 // Internalized value (empty before internalized). 273 // Internalized value (pointer to the next uninternalized value until
264 Handle<Object> value_; 274 // internalized).
275 union {
276 Handle<Object> value_;
277 AstValue* next_;
278 };
265 }; 279 };
266 280
267 281
268 // For generating constants. 282 // For generating constants.
269 #define STRING_CONSTANTS(F) \ 283 #define STRING_CONSTANTS(F) \
270 F(anonymous_function, "(anonymous function)") \ 284 F(anonymous_function, "(anonymous function)") \
271 F(arguments, "arguments") \ 285 F(arguments, "arguments") \
272 F(async, "async") \ 286 F(async, "async") \
273 F(await, "await") \ 287 F(await, "await") \
274 F(constructor, "constructor") \ 288 F(constructor, "constructor") \
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 F(true_value) \ 321 F(true_value) \
308 F(false_value) \ 322 F(false_value) \
309 F(null_value) \ 323 F(null_value) \
310 F(undefined_value) \ 324 F(undefined_value) \
311 F(the_hole_value) 325 F(the_hole_value)
312 326
313 class AstValueFactory { 327 class AstValueFactory {
314 public: 328 public:
315 AstValueFactory(Zone* zone, uint32_t hash_seed) 329 AstValueFactory(Zone* zone, uint32_t hash_seed)
316 : string_table_(AstRawStringCompare), 330 : string_table_(AstRawStringCompare),
331 values_(nullptr),
332 strings_end_(&strings_),
317 zone_(zone), 333 zone_(zone),
318 isolate_(NULL), 334 isolate_(NULL),
319 hash_seed_(hash_seed) { 335 hash_seed_(hash_seed) {
336 ResetStrings();
320 #define F(name, str) name##_string_ = NULL; 337 #define F(name, str) name##_string_ = NULL;
321 STRING_CONSTANTS(F) 338 STRING_CONSTANTS(F)
322 #undef F 339 #undef F
323 #define F(name) name##_ = NULL; 340 #define F(name) name##_ = NULL;
324 OTHER_CONSTANTS(F) 341 OTHER_CONSTANTS(F)
325 #undef F 342 #undef F
326 } 343 }
327 344
328 Zone* zone() const { return zone_; } 345 Zone* zone() const { return zone_; }
329 346
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 const AstValue* NewSymbol(const char* name); 381 const AstValue* NewSymbol(const char* name);
365 const AstValue* NewNumber(double number, bool with_dot = false); 382 const AstValue* NewNumber(double number, bool with_dot = false);
366 const AstValue* NewSmi(int number); 383 const AstValue* NewSmi(int number);
367 const AstValue* NewBoolean(bool b); 384 const AstValue* NewBoolean(bool b);
368 const AstValue* NewStringList(ZoneList<const AstRawString*>* strings); 385 const AstValue* NewStringList(ZoneList<const AstRawString*>* strings);
369 const AstValue* NewNull(); 386 const AstValue* NewNull();
370 const AstValue* NewUndefined(); 387 const AstValue* NewUndefined();
371 const AstValue* NewTheHole(); 388 const AstValue* NewTheHole();
372 389
373 private: 390 private:
391 AstValue* AddValue(AstValue* value) {
392 if (isolate_) {
393 value->Internalize(isolate_);
394 } else {
395 value->set_next(values_);
396 values_ = value;
397 }
398 return value;
399 }
400 AstString* AddString(AstString* string) {
401 if (isolate_) {
402 string->Internalize(isolate_);
403 } else {
404 *strings_end_ = string;
405 strings_end_ = string->next_location();
406 }
407 return string;
408 }
409 void ResetStrings() {
410 strings_ = nullptr;
411 strings_end_ = &strings_;
412 }
374 AstRawString* GetOneByteStringInternal(Vector<const uint8_t> literal); 413 AstRawString* GetOneByteStringInternal(Vector<const uint8_t> literal);
375 AstRawString* GetTwoByteStringInternal(Vector<const uint16_t> literal); 414 AstRawString* GetTwoByteStringInternal(Vector<const uint16_t> literal);
376 AstRawString* GetString(uint32_t hash, bool is_one_byte, 415 AstRawString* GetString(uint32_t hash, bool is_one_byte,
377 Vector<const byte> literal_bytes); 416 Vector<const byte> literal_bytes);
378 417
379 static bool AstRawStringCompare(void* a, void* b); 418 static bool AstRawStringCompare(void* a, void* b);
380 419
381 // All strings are copied here, one after another (no NULLs inbetween). 420 // All strings are copied here, one after another (no NULLs inbetween).
382 base::HashMap string_table_; 421 base::HashMap string_table_;
383 // For keeping track of all AstValues and AstRawStrings we've created (so that 422 // For keeping track of all AstValues and AstRawStrings we've created (so that
384 // they can be internalized later). 423 // they can be internalized later).
385 List<AstValue*> values_; 424 AstValue* values_;
386 List<AstString*> strings_; 425 // We need to keep track of strings_ in order, since cons strings require
426 // their members to be internalized first.
427 AstString* strings_;
428 AstString** strings_end_;
387 Zone* zone_; 429 Zone* zone_;
388 Isolate* isolate_; 430 Isolate* isolate_;
389 431
390 uint32_t hash_seed_; 432 uint32_t hash_seed_;
391 433
392 #define F(name, str) const AstRawString* name##_string_; 434 #define F(name, str) const AstRawString* name##_string_;
393 STRING_CONSTANTS(F) 435 STRING_CONSTANTS(F)
394 #undef F 436 #undef F
395 437
396 #define F(name) AstValue* name##_; 438 #define F(name) AstValue* name##_;
397 OTHER_CONSTANTS(F) 439 OTHER_CONSTANTS(F)
398 #undef F 440 #undef F
399 }; 441 };
400 } // namespace internal 442 } // namespace internal
401 } // namespace v8 443 } // namespace v8
402 444
403 #undef STRING_CONSTANTS 445 #undef STRING_CONSTANTS
404 #undef OTHER_CONSTANTS 446 #undef OTHER_CONSTANTS
405 447
406 #endif // V8_AST_AST_VALUE_FACTORY_H_ 448 #endif // V8_AST_AST_VALUE_FACTORY_H_
OLDNEW
« no previous file with comments | « no previous file | src/ast/ast-value-factory.cc » ('j') | src/ast/ast-value-factory.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698