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

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

Issue 2687933003: [Parser] Cache and clone initial AstValueFactory string_table_. (Closed)
Patch Set: Address comments Created 3 years, 10 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') | 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 bool is_one_byte() const { return IsOneByteBits::decode(bit_field_); } 99 bool is_one_byte() const { return IsOneByteBits::decode(bit_field_); }
100 100
101 bool IsOneByteEqualTo(const char* data) const; 101 bool IsOneByteEqualTo(const char* data) const;
102 uint16_t FirstCharacter() const { 102 uint16_t FirstCharacter() const {
103 if (is_one_byte()) return literal_bytes_[0]; 103 if (is_one_byte()) return literal_bytes_[0];
104 const uint16_t* c = 104 const uint16_t* c =
105 reinterpret_cast<const uint16_t*>(literal_bytes_.start()); 105 reinterpret_cast<const uint16_t*>(literal_bytes_.start());
106 return *c; 106 return *c;
107 } 107 }
108 108
109 static bool Compare(void* a, void* b);
110
109 // For storing AstRawStrings in a hash map. 111 // For storing AstRawStrings in a hash map.
110 uint32_t hash() const { 112 uint32_t hash() const {
111 return hash_; 113 return hash_;
112 } 114 }
113 115
114 private: 116 private:
115 friend class AstRawStringInternalizationKey; 117 friend class AstRawStringInternalizationKey;
116 friend class AstStringConstants; 118 friend class AstStringConstants;
117 friend class AstValueFactory; 119 friend class AstValueFactory;
118 120
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 F(this_function, ".this_function") \ 331 F(this_function, ".this_function") \
330 F(throw, "throw") \ 332 F(throw, "throw") \
331 F(undefined, "undefined") \ 333 F(undefined, "undefined") \
332 F(use_asm, "use asm") \ 334 F(use_asm, "use asm") \
333 F(use_strict, "use strict") \ 335 F(use_strict, "use strict") \
334 F(value, "value") 336 F(value, "value")
335 337
336 class AstStringConstants final { 338 class AstStringConstants final {
337 public: 339 public:
338 AstStringConstants(Isolate* isolate, uint32_t hash_seed) 340 AstStringConstants(Isolate* isolate, uint32_t hash_seed)
339 : zone_(isolate->allocator(), ZONE_NAME), hash_seed_(hash_seed) { 341 : zone_(isolate->allocator(), ZONE_NAME),
342 string_table_(AstRawString::Compare),
343 hash_seed_(hash_seed) {
340 DCHECK(ThreadId::Current().Equals(isolate->thread_id())); 344 DCHECK(ThreadId::Current().Equals(isolate->thread_id()));
341 #define F(name, str) \ 345 #define F(name, str) \
342 { \ 346 { \
343 const char* data = str; \ 347 const char* data = str; \
344 Vector<const uint8_t> literal(reinterpret_cast<const uint8_t*>(data), \ 348 Vector<const uint8_t> literal(reinterpret_cast<const uint8_t*>(data), \
345 static_cast<int>(strlen(data))); \ 349 static_cast<int>(strlen(data))); \
346 uint32_t hash = StringHasher::HashSequentialString<uint8_t>( \ 350 uint32_t hash = StringHasher::HashSequentialString<uint8_t>( \
347 literal.start(), literal.length(), hash_seed_); \ 351 literal.start(), literal.length(), hash_seed_); \
348 name##_string_ = new (&zone_) AstRawString(true, literal, hash); \ 352 name##_string_ = new (&zone_) AstRawString(true, literal, hash); \
349 /* The Handle returned by the factory is located on the roots */ \ 353 /* The Handle returned by the factory is located on the roots */ \
350 /* array, not on the temporary HandleScope, so this is safe. */ \ 354 /* array, not on the temporary HandleScope, so this is safe. */ \
351 name##_string_->set_string(isolate->factory()->name##_string()); \ 355 name##_string_->set_string(isolate->factory()->name##_string()); \
356 base::HashMap::Entry* entry = \
357 string_table_.InsertNew(name##_string_, name##_string_->hash()); \
358 DCHECK(entry->value == nullptr); \
359 entry->value = reinterpret_cast<void*>(1); \
352 } 360 }
353 STRING_CONSTANTS(F) 361 STRING_CONSTANTS(F)
354 #undef F 362 #undef F
355 } 363 }
356 364
357 #define F(name, str) \ 365 #define F(name, str) \
358 AstRawString* name##_string() { return name##_string_; } 366 const AstRawString* name##_string() const { return name##_string_; }
359 STRING_CONSTANTS(F) 367 STRING_CONSTANTS(F)
360 #undef F 368 #undef F
361 369
362 uint32_t hash_seed() const { return hash_seed_; } 370 uint32_t hash_seed() const { return hash_seed_; }
371 const base::CustomMatcherHashMap* string_table() const {
372 return &string_table_;
373 }
363 374
364 private: 375 private:
365 Zone zone_; 376 Zone zone_;
377 base::CustomMatcherHashMap string_table_;
366 uint32_t hash_seed_; 378 uint32_t hash_seed_;
367 379
368 #define F(name, str) AstRawString* name##_string_; 380 #define F(name, str) AstRawString* name##_string_;
369 STRING_CONSTANTS(F) 381 STRING_CONSTANTS(F)
370 #undef F 382 #undef F
371 383
372 DISALLOW_COPY_AND_ASSIGN(AstStringConstants); 384 DISALLOW_COPY_AND_ASSIGN(AstStringConstants);
373 }; 385 };
374 386
375 #define OTHER_CONSTANTS(F) \ 387 #define OTHER_CONSTANTS(F) \
376 F(true_value) \ 388 F(true_value) \
377 F(false_value) \ 389 F(false_value) \
378 F(null_value) \ 390 F(null_value) \
379 F(undefined_value) \ 391 F(undefined_value) \
380 F(the_hole_value) 392 F(the_hole_value)
381 393
382 class AstValueFactory { 394 class AstValueFactory {
383 public: 395 public:
384 AstValueFactory(Zone* zone, AstStringConstants* string_constants, 396 AstValueFactory(Zone* zone, const AstStringConstants* string_constants,
385 uint32_t hash_seed) 397 uint32_t hash_seed)
386 : string_table_(AstRawStringCompare), 398 : string_table_(string_constants->string_table()),
387 values_(nullptr), 399 values_(nullptr),
388 strings_(nullptr), 400 strings_(nullptr),
389 strings_end_(&strings_), 401 strings_end_(&strings_),
390 string_constants_(string_constants), 402 string_constants_(string_constants),
391 zone_(zone), 403 zone_(zone),
392 hash_seed_(hash_seed) { 404 hash_seed_(hash_seed) {
393 #define F(name) name##_ = nullptr; 405 #define F(name) name##_ = nullptr;
394 OTHER_CONSTANTS(F) 406 OTHER_CONSTANTS(F)
395 #undef F 407 #undef F
396 DCHECK_EQ(hash_seed, string_constants->hash_seed()); 408 DCHECK_EQ(hash_seed, string_constants->hash_seed());
397 std::fill(smis_, smis_ + arraysize(smis_), nullptr); 409 std::fill(smis_, smis_ + arraysize(smis_), nullptr);
398 std::fill(one_character_strings_, 410 std::fill(one_character_strings_,
399 one_character_strings_ + arraysize(one_character_strings_), 411 one_character_strings_ + arraysize(one_character_strings_),
400 nullptr); 412 nullptr);
401 InitializeStringConstants();
402 } 413 }
403 414
404 Zone* zone() const { return zone_; } 415 Zone* zone() const { return zone_; }
405 416
406 const AstRawString* GetOneByteString(Vector<const uint8_t> literal) { 417 const AstRawString* GetOneByteString(Vector<const uint8_t> literal) {
407 return GetOneByteStringInternal(literal); 418 return GetOneByteStringInternal(literal);
408 } 419 }
409 const AstRawString* GetOneByteString(const char* string) { 420 const AstRawString* GetOneByteString(const char* string) {
410 return GetOneByteString(Vector<const uint8_t>( 421 return GetOneByteString(Vector<const uint8_t>(
411 reinterpret_cast<const uint8_t*>(string), StrLength(string))); 422 reinterpret_cast<const uint8_t*>(string), StrLength(string)));
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 void ResetStrings() { 466 void ResetStrings() {
456 strings_ = nullptr; 467 strings_ = nullptr;
457 strings_end_ = &strings_; 468 strings_end_ = &strings_;
458 } 469 }
459 V8_EXPORT_PRIVATE AstRawString* GetOneByteStringInternal( 470 V8_EXPORT_PRIVATE AstRawString* GetOneByteStringInternal(
460 Vector<const uint8_t> literal); 471 Vector<const uint8_t> literal);
461 AstRawString* GetTwoByteStringInternal(Vector<const uint16_t> literal); 472 AstRawString* GetTwoByteStringInternal(Vector<const uint16_t> literal);
462 AstRawString* GetString(uint32_t hash, bool is_one_byte, 473 AstRawString* GetString(uint32_t hash, bool is_one_byte,
463 Vector<const byte> literal_bytes); 474 Vector<const byte> literal_bytes);
464 475
465 void InitializeStringConstants() {
466 #define F(name, str) \
467 AstRawString* raw_string_##name = string_constants_->name##_string(); \
468 base::HashMap::Entry* entry_##name = string_table_.LookupOrInsert( \
469 raw_string_##name, raw_string_##name->hash()); \
470 DCHECK(entry_##name->value == nullptr); \
471 entry_##name->value = reinterpret_cast<void*>(1);
472 STRING_CONSTANTS(F)
473 #undef F
474 }
475
476 static bool AstRawStringCompare(void* a, void* b);
477
478 // All strings are copied here, one after another (no NULLs inbetween). 476 // All strings are copied here, one after another (no NULLs inbetween).
479 base::CustomMatcherHashMap string_table_; 477 base::CustomMatcherHashMap string_table_;
480 // For keeping track of all AstValues and AstRawStrings we've created (so that 478 // For keeping track of all AstValues and AstRawStrings we've created (so that
481 // they can be internalized later). 479 // they can be internalized later).
482 AstValue* values_; 480 AstValue* values_;
483 481
484 // We need to keep track of strings_ in order since cons strings require their 482 // We need to keep track of strings_ in order since cons strings require their
485 // members to be internalized first. 483 // members to be internalized first.
486 AstString* strings_; 484 AstString* strings_;
487 AstString** strings_end_; 485 AstString** strings_end_;
488 486
489 // Holds constant string values which are shared across the isolate. 487 // Holds constant string values which are shared across the isolate.
490 AstStringConstants* string_constants_; 488 const AstStringConstants* string_constants_;
491 489
492 // Caches for faster access: small numbers, one character lowercase strings 490 // Caches for faster access: small numbers, one character lowercase strings
493 // (for minified code). 491 // (for minified code).
494 AstValue* smis_[kMaxCachedSmi + 1]; 492 AstValue* smis_[kMaxCachedSmi + 1];
495 AstRawString* one_character_strings_[26]; 493 AstRawString* one_character_strings_[26];
496 494
497 Zone* zone_; 495 Zone* zone_;
498 496
499 uint32_t hash_seed_; 497 uint32_t hash_seed_;
500 498
501 #define F(name) AstValue* name##_; 499 #define F(name) AstValue* name##_;
502 OTHER_CONSTANTS(F) 500 OTHER_CONSTANTS(F)
503 #undef F 501 #undef F
504 }; 502 };
505 } // namespace internal 503 } // namespace internal
506 } // namespace v8 504 } // namespace v8
507 505
508 #undef STRING_CONSTANTS 506 #undef STRING_CONSTANTS
509 #undef OTHER_CONSTANTS 507 #undef OTHER_CONSTANTS
510 508
511 #endif // V8_AST_AST_VALUE_FACTORY_H_ 509 #endif // V8_AST_AST_VALUE_FACTORY_H_
OLDNEW
« no previous file with comments | « no previous file | src/ast/ast-value-factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698