OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/symbols.h" | 5 #include "vm/symbols.h" |
6 | 6 |
7 #include "vm/handles.h" | 7 #include "vm/handles.h" |
8 #include "vm/handles_impl.h" | 8 #include "vm/handles_impl.h" |
9 #include "vm/hash_table.h" | 9 #include "vm/hash_table.h" |
10 #include "vm/isolate.h" | 10 #include "vm/isolate.h" |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 ASSERT(str->HasHash()); | 300 ASSERT(str->HasHash()); |
301 ASSERT(str->IsCanonical()); | 301 ASSERT(str->IsCanonical()); |
302 predefined_[c] = str->raw(); | 302 predefined_[c] = str->raw(); |
303 symbol_handles_[idx] = str; | 303 symbol_handles_[idx] = str; |
304 } | 304 } |
305 | 305 |
306 vm_isolate->object_store()->set_symbol_table(table.Release()); | 306 vm_isolate->object_store()->set_symbol_table(table.Release()); |
307 } | 307 } |
308 | 308 |
309 | 309 |
310 void Symbols::AddPredefinedSymbolsToIsolate() { | |
311 // Should only be run by regular Dart isolates. | |
312 Thread* thread = Thread::Current(); | |
313 Isolate* isolate = thread->isolate(); | |
314 Zone* zone = thread->zone(); | |
315 ASSERT(isolate != Dart::vm_isolate()); | |
316 String& str = String::Handle(zone); | |
317 | |
318 SymbolTable table(zone, isolate->object_store()->symbol_table()); | |
319 | |
320 // Set up all the predefined string symbols and create symbols for | |
321 // language keywords. | |
322 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) { | |
323 str = OneByteString::New(names[i], Heap::kOld); | |
324 str.Hash(); | |
325 str ^= table.InsertOrGet(str); | |
326 str.SetCanonical(); // Make canonical once entered. | |
327 } | |
328 | |
329 // Add Latin1 characters as Symbols, so that Symbols::FromCharCode is fast. | |
330 for (intptr_t c = 0; c < kNumberOfOneCharCodeSymbols; c++) { | |
331 intptr_t idx = (kNullCharId + c); | |
332 ASSERT(idx < kMaxPredefinedId); | |
333 ASSERT(Utf::IsLatin1(c)); | |
334 uint8_t ch = static_cast<uint8_t>(c); | |
335 str = OneByteString::New(&ch, 1, Heap::kOld); | |
336 str.Hash(); | |
337 str ^= table.InsertOrGet(str); | |
338 str.SetCanonical(); // Make canonical once entered. | |
339 } | |
340 | |
341 isolate->object_store()->set_symbol_table(table.Release()); | |
342 } | |
343 | |
344 | |
345 void Symbols::SetupSymbolTable(Isolate* isolate) { | 310 void Symbols::SetupSymbolTable(Isolate* isolate) { |
346 ASSERT(isolate != NULL); | 311 ASSERT(isolate != NULL); |
347 | 312 |
348 // Setup the symbol table used within the String class. | 313 // Setup the symbol table used within the String class. |
349 const intptr_t initial_size = (isolate == Dart::vm_isolate()) ? | 314 const intptr_t initial_size = (isolate == Dart::vm_isolate()) ? |
350 kInitialVMIsolateSymtabSize : kInitialSymtabSize; | 315 kInitialVMIsolateSymtabSize : kInitialSymtabSize; |
351 Array& array = | 316 Array& array = |
352 Array::Handle(HashTables::New<SymbolTable>(initial_size, Heap::kOld)); | 317 Array::Handle(HashTables::New<SymbolTable>(initial_size, Heap::kOld)); |
353 isolate->object_store()->set_symbol_table(array); | 318 isolate->object_store()->set_symbol_table(array); |
354 } | 319 } |
355 | 320 |
356 | 321 |
357 intptr_t Symbols::Compact(Isolate* isolate) { | 322 RawArray* Symbols::UnifiedSymbolTable() { |
| 323 Thread* thread = Thread::Current(); |
| 324 Isolate* isolate = thread->isolate(); |
| 325 Zone* zone = thread->zone(); |
| 326 |
| 327 ASSERT(thread->IsMutatorThread()); |
| 328 ASSERT(isolate->background_compiler() == NULL); |
| 329 |
| 330 SymbolTable vm_table(zone, |
| 331 Dart::vm_isolate()->object_store()->symbol_table()); |
| 332 SymbolTable table(zone, isolate->object_store()->symbol_table()); |
| 333 intptr_t unified_size = vm_table.NumOccupied() + table.NumOccupied(); |
| 334 SymbolTable unified_table(zone, HashTables::New<SymbolTable>(unified_size, |
| 335 Heap::kOld)); |
| 336 String& symbol = String::Handle(zone); |
| 337 |
| 338 SymbolTable::Iterator vm_iter(&vm_table); |
| 339 while (vm_iter.MoveNext()) { |
| 340 symbol ^= vm_table.GetKey(vm_iter.Current()); |
| 341 ASSERT(!symbol.IsNull()); |
| 342 bool present = unified_table.Insert(symbol); |
| 343 ASSERT(!present); |
| 344 } |
| 345 vm_table.Release(); |
| 346 |
| 347 SymbolTable::Iterator iter(&table); |
| 348 while (iter.MoveNext()) { |
| 349 symbol ^= table.GetKey(iter.Current()); |
| 350 ASSERT(!symbol.IsNull()); |
| 351 bool present = unified_table.Insert(symbol); |
| 352 ASSERT(!present); |
| 353 } |
| 354 table.Release(); |
| 355 |
| 356 return unified_table.Release().raw(); |
| 357 } |
| 358 |
| 359 |
| 360 #if defined(DART_PRECOMPILER) |
| 361 void Symbols::Compact(Isolate* isolate) { |
358 ASSERT(isolate != Dart::vm_isolate()); | 362 ASSERT(isolate != Dart::vm_isolate()); |
| 363 Zone* zone = Thread::Current()->zone(); |
359 | 364 |
360 Zone* zone = Thread::Current()->zone(); | 365 // 1. Drop the symbol table and do a full garbage collection. |
361 intptr_t initial_size = -1; | |
362 intptr_t final_size = -1; | |
363 | |
364 // 1. Build a collection of all the predefined symbols so they are | |
365 // strongly referenced (the read only handles are not traced). | |
366 { | |
367 SymbolTable table(zone, isolate->object_store()->symbol_table()); | |
368 initial_size = table.NumOccupied(); | |
369 | |
370 if (Object::vm_isolate_snapshot_object_table().Length() == 0) { | |
371 GrowableObjectArray& predefined_symbols = GrowableObjectArray::Handle( | |
372 GrowableObjectArray::New(kMaxPredefinedId)); | |
373 String& symbol = String::Handle(); | |
374 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) { | |
375 const unsigned char* name = | |
376 reinterpret_cast<const unsigned char*>(names[i]); | |
377 symbol ^= table.GetOrNull(Latin1Array(name, strlen(names[i]))); | |
378 ASSERT(!symbol.IsNull()); | |
379 predefined_symbols.Add(symbol); | |
380 } | |
381 for (intptr_t c = 0; c < kNumberOfOneCharCodeSymbols; c++) { | |
382 intptr_t idx = (kNullCharId + c); | |
383 ASSERT(idx < kMaxPredefinedId); | |
384 ASSERT(Utf::IsLatin1(c)); | |
385 uint8_t ch = static_cast<uint8_t>(c); | |
386 symbol ^= table.GetOrNull(Latin1Array(&ch, 1)); | |
387 ASSERT(!symbol.IsNull()); | |
388 predefined_symbols.Add(symbol); | |
389 } | |
390 } | |
391 table.Release(); | |
392 } | |
393 | |
394 // 2. Knock out the symbol table and do a full garbage collection. | |
395 isolate->object_store()->set_symbol_table(Object::empty_array()); | 366 isolate->object_store()->set_symbol_table(Object::empty_array()); |
396 isolate->heap()->CollectAllGarbage(); | 367 isolate->heap()->CollectAllGarbage(); |
397 | 368 |
398 // 3. Walk the heap and build a new table from surviving symbols. | 369 // 2. Walk the heap to find surviving symbols. |
399 GrowableArray<String*> symbols; | 370 GrowableArray<String*> symbols; |
400 class SymbolCollector : public ObjectVisitor { | 371 class SymbolCollector : public ObjectVisitor { |
401 public: | 372 public: |
402 SymbolCollector(Thread* thread, | 373 SymbolCollector(Thread* thread, |
403 GrowableArray<String*>* symbols) | 374 GrowableArray<String*>* symbols) |
404 : symbols_(symbols), | 375 : symbols_(symbols), |
405 zone_(thread->zone()) {} | 376 zone_(thread->zone()) {} |
406 | 377 |
407 void VisitObject(RawObject* obj) { | 378 void VisitObject(RawObject* obj) { |
408 if (obj->IsCanonical() && obj->IsStringInstance()) { | 379 if (obj->IsCanonical() && obj->IsStringInstance()) { |
409 symbols_->Add(&String::ZoneHandle(zone_, String::RawCast(obj))); | 380 symbols_->Add(&String::ZoneHandle(zone_, String::RawCast(obj))); |
410 } | 381 } |
411 } | 382 } |
412 | 383 |
413 private: | 384 private: |
414 GrowableArray<String*>* symbols_; | 385 GrowableArray<String*>* symbols_; |
415 Zone* zone_; | 386 Zone* zone_; |
416 }; | 387 }; |
417 | 388 |
418 SymbolCollector visitor(Thread::Current(), &symbols); | 389 SymbolCollector visitor(Thread::Current(), &symbols); |
419 isolate->heap()->IterateObjects(&visitor); | 390 isolate->heap()->IterateObjects(&visitor); |
420 | 391 |
421 { | 392 // 3. Build a new table from the surviving symbols. |
422 Array& array = | 393 Array& array = |
423 Array::Handle(HashTables::New<SymbolTable>(symbols.length() * 4 / 3, | 394 Array::Handle(zone, HashTables::New<SymbolTable>(symbols.length() * 4 / 3, |
424 Heap::kOld)); | 395 Heap::kOld)); |
425 SymbolTable table(zone, array.raw()); | 396 SymbolTable table(zone, array.raw()); |
426 for (intptr_t i = 0; i < symbols.length(); i++) { | 397 for (intptr_t i = 0; i < symbols.length(); i++) { |
427 String& symbol = *symbols[i]; | 398 String& symbol = *symbols[i]; |
428 ASSERT(symbol.IsString()); | 399 ASSERT(symbol.IsString()); |
429 ASSERT(symbol.IsCanonical()); | 400 ASSERT(symbol.IsCanonical()); |
430 bool present = table.Insert(symbol); | 401 bool present = table.Insert(symbol); |
431 ASSERT(!present); | 402 ASSERT(!present); |
432 } | |
433 final_size = table.NumOccupied(); | |
434 isolate->object_store()->set_symbol_table(table.Release()); | |
435 } | 403 } |
436 | 404 isolate->object_store()->set_symbol_table(table.Release()); |
437 return initial_size - final_size; | |
438 } | 405 } |
| 406 #endif // DART_PRECOMPILER |
439 | 407 |
440 | 408 |
441 void Symbols::GetStats(Isolate* isolate, intptr_t* size, intptr_t* capacity) { | 409 void Symbols::GetStats(Isolate* isolate, intptr_t* size, intptr_t* capacity) { |
442 ASSERT(isolate != NULL); | 410 ASSERT(isolate != NULL); |
443 SymbolTable table(isolate->object_store()->symbol_table()); | 411 SymbolTable table(isolate->object_store()->symbol_table()); |
444 *size = table.NumOccupied(); | 412 *size = table.NumOccupied(); |
445 *capacity = table.NumEntries(); | 413 *capacity = table.NumEntries(); |
446 table.Release(); | 414 table.Release(); |
447 } | 415 } |
448 | 416 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 table.Release(); | 616 table.Release(); |
649 } | 617 } |
650 if (symbol.IsNull()) { | 618 if (symbol.IsNull()) { |
651 Isolate* isolate = thread->isolate(); | 619 Isolate* isolate = thread->isolate(); |
652 SafepointMutexLocker ml(isolate->symbols_mutex()); | 620 SafepointMutexLocker ml(isolate->symbols_mutex()); |
653 data ^= isolate->object_store()->symbol_table(); | 621 data ^= isolate->object_store()->symbol_table(); |
654 SymbolTable table(&key, &value, &data); | 622 SymbolTable table(&key, &value, &data); |
655 symbol ^= table.GetOrNull(str); | 623 symbol ^= table.GetOrNull(str); |
656 table.Release(); | 624 table.Release(); |
657 } | 625 } |
658 | |
659 ASSERT(symbol.IsNull() || symbol.IsSymbol()); | 626 ASSERT(symbol.IsNull() || symbol.IsSymbol()); |
660 ASSERT(symbol.IsNull() || symbol.HasHash()); | 627 ASSERT(symbol.IsNull() || symbol.HasHash()); |
661 return symbol.raw(); | 628 return symbol.raw(); |
662 } | 629 } |
663 | 630 |
664 | 631 |
665 RawString* Symbols::LookupFromConcat( | 632 RawString* Symbols::LookupFromConcat( |
666 Thread* thread, const String& str1, const String& str2) { | 633 Thread* thread, const String& str1, const String& str2) { |
667 if (str1.Length() == 0) { | 634 if (str1.Length() == 0) { |
668 return Lookup(thread, str2); | 635 return Lookup(thread, str2); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 // Now dump regular isolate symbol table stats. | 718 // Now dump regular isolate symbol table stats. |
752 GetStats(Isolate::Current(), &size, &capacity); | 719 GetStats(Isolate::Current(), &size, &capacity); |
753 OS::Print("Isolate: Number of symbols : %" Pd "\n", size); | 720 OS::Print("Isolate: Number of symbols : %" Pd "\n", size); |
754 OS::Print("Isolate: Symbol table capacity : %" Pd "\n", capacity); | 721 OS::Print("Isolate: Symbol table capacity : %" Pd "\n", capacity); |
755 // TODO(koda): Consider recording growth and collision stats in HashTable, | 722 // TODO(koda): Consider recording growth and collision stats in HashTable, |
756 // in DEBUG mode. | 723 // in DEBUG mode. |
757 } | 724 } |
758 } | 725 } |
759 | 726 |
760 | 727 |
761 intptr_t Symbols::LookupVMSymbol(RawObject* obj) { | 728 intptr_t Symbols::LookupPredefinedSymbol(RawObject* obj) { |
762 for (intptr_t i = 1; i < Symbols::kMaxPredefinedId; i++) { | 729 for (intptr_t i = 1; i < Symbols::kMaxPredefinedId; i++) { |
763 if (symbol_handles_[i]->raw() == obj) { | 730 if (symbol_handles_[i]->raw() == obj) { |
764 return (i + kMaxPredefinedObjectIds); | 731 return (i + kMaxPredefinedObjectIds); |
765 } | 732 } |
766 } | 733 } |
767 return kInvalidIndex; | 734 return kInvalidIndex; |
768 } | 735 } |
769 | 736 |
770 | 737 |
771 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 738 RawObject* Symbols::GetPredefinedSymbol(intptr_t object_id) { |
772 ASSERT(IsVMSymbolId(object_id)); | 739 ASSERT(IsPredefinedSymbolId(object_id)); |
773 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 740 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
774 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { | 741 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { |
775 return symbol_handles_[i]->raw(); | 742 return symbol_handles_[i]->raw(); |
776 } | 743 } |
777 return Object::null(); | 744 return Object::null(); |
778 } | 745 } |
779 | 746 |
780 } // namespace dart | 747 } // namespace dart |
OLD | NEW |