| 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 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 | 324 |
| 325 // Setup the symbol table used within the String class. | 325 // Setup the symbol table used within the String class. |
| 326 const intptr_t initial_size = (isolate == Dart::vm_isolate()) ? | 326 const intptr_t initial_size = (isolate == Dart::vm_isolate()) ? |
| 327 kInitialVMIsolateSymtabSize : kInitialSymtabSize; | 327 kInitialVMIsolateSymtabSize : kInitialSymtabSize; |
| 328 Array& array = | 328 Array& array = |
| 329 Array::Handle(HashTables::New<SymbolTable>(initial_size, Heap::kOld)); | 329 Array::Handle(HashTables::New<SymbolTable>(initial_size, Heap::kOld)); |
| 330 isolate->object_store()->set_symbol_table(array); | 330 isolate->object_store()->set_symbol_table(array); |
| 331 } | 331 } |
| 332 | 332 |
| 333 | 333 |
| 334 intptr_t Symbols::Compact(Isolate* isolate) { |
| 335 ASSERT(isolate != Dart::vm_isolate()); |
| 336 |
| 337 Zone* zone = Thread::Current()->zone(); |
| 338 intptr_t initial_size = -1; |
| 339 intptr_t final_size = -1; |
| 340 |
| 341 // 1. Build a collection of all the predefined symbols so they are |
| 342 // strongly referenced (the read only handles are not traced). |
| 343 { |
| 344 SymbolTable table(zone, isolate->object_store()->symbol_table()); |
| 345 initial_size = table.NumOccupied(); |
| 346 |
| 347 if (Object::vm_isolate_snapshot_object_table().Length() == 0) { |
| 348 GrowableObjectArray& predefined_symbols = GrowableObjectArray::Handle( |
| 349 GrowableObjectArray::New(kMaxPredefinedId)); |
| 350 String& symbol = String::Handle(); |
| 351 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) { |
| 352 const unsigned char* name = |
| 353 reinterpret_cast<const unsigned char*>(names[i]); |
| 354 symbol ^= table.GetOrNull(Latin1Array(name, strlen(names[i]))); |
| 355 ASSERT(!symbol.IsNull()); |
| 356 predefined_symbols.Add(symbol); |
| 357 } |
| 358 for (intptr_t c = 0; c < kNumberOfOneCharCodeSymbols; c++) { |
| 359 intptr_t idx = (kNullCharId + c); |
| 360 ASSERT(idx < kMaxPredefinedId); |
| 361 ASSERT(Utf::IsLatin1(c)); |
| 362 uint8_t ch = static_cast<uint8_t>(c); |
| 363 symbol ^= table.GetOrNull(Latin1Array(&ch, 1)); |
| 364 ASSERT(!symbol.IsNull()); |
| 365 predefined_symbols.Add(symbol); |
| 366 } |
| 367 } |
| 368 table.Release(); |
| 369 } |
| 370 |
| 371 // 2. Knock out the symbol table and do a full garbage collection. |
| 372 isolate->object_store()->set_symbol_table(Object::empty_array()); |
| 373 isolate->heap()->CollectAllGarbage(); |
| 374 |
| 375 // 3. Walk the heap and build a new table from surviving symbols. |
| 376 GrowableArray<String*> symbols; |
| 377 class SymbolCollector : public ObjectVisitor { |
| 378 public: |
| 379 SymbolCollector(Thread* thread, |
| 380 GrowableArray<String*>* symbols) |
| 381 : ObjectVisitor(thread->isolate()), |
| 382 symbols_(symbols), |
| 383 zone_(thread->zone()) {} |
| 384 |
| 385 void VisitObject(RawObject* obj) { |
| 386 if (obj->IsString() && obj->IsCanonical()) { |
| 387 symbols_->Add(&String::ZoneHandle(zone_, String::RawCast(obj))); |
| 388 } |
| 389 } |
| 390 |
| 391 private: |
| 392 GrowableArray<String*>* symbols_; |
| 393 Zone* zone_; |
| 394 }; |
| 395 |
| 396 SymbolCollector visitor(Thread::Current(), &symbols); |
| 397 isolate->heap()->IterateObjects(&visitor); |
| 398 |
| 399 { |
| 400 Array& array = |
| 401 Array::Handle(HashTables::New<SymbolTable>(symbols.length() * 4 / 3, |
| 402 Heap::kOld)); |
| 403 SymbolTable table(zone, array.raw()); |
| 404 for (intptr_t i = 0; i < symbols.length(); i++) { |
| 405 String& symbol = *symbols[i]; |
| 406 ASSERT(symbol.IsString()); |
| 407 ASSERT(symbol.IsCanonical()); |
| 408 bool present = table.Insert(symbol); |
| 409 ASSERT(!present); |
| 410 } |
| 411 final_size = table.NumOccupied(); |
| 412 isolate->object_store()->set_symbol_table(table.Release()); |
| 413 } |
| 414 |
| 415 return initial_size - final_size; |
| 416 } |
| 417 |
| 418 |
| 334 void Symbols::GetStats(Isolate* isolate, intptr_t* size, intptr_t* capacity) { | 419 void Symbols::GetStats(Isolate* isolate, intptr_t* size, intptr_t* capacity) { |
| 335 ASSERT(isolate != NULL); | 420 ASSERT(isolate != NULL); |
| 336 SymbolTable table(isolate->object_store()->symbol_table()); | 421 SymbolTable table(isolate->object_store()->symbol_table()); |
| 337 *size = table.NumOccupied(); | 422 *size = table.NumOccupied(); |
| 338 *capacity = table.NumEntries(); | 423 *capacity = table.NumEntries(); |
| 339 table.Release(); | 424 table.Release(); |
| 340 } | 425 } |
| 341 | 426 |
| 342 | 427 |
| 343 RawString* Symbols::New(const char* cstr, intptr_t len) { | 428 RawString* Symbols::New(const char* cstr, intptr_t len) { |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 689 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 605 ASSERT(IsVMSymbolId(object_id)); | 690 ASSERT(IsVMSymbolId(object_id)); |
| 606 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 691 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 607 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { | 692 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { |
| 608 return symbol_handles_[i]->raw(); | 693 return symbol_handles_[i]->raw(); |
| 609 } | 694 } |
| 610 return Object::null(); | 695 return Object::null(); |
| 611 } | 696 } |
| 612 | 697 |
| 613 } // namespace dart | 698 } // namespace dart |
| OLD | NEW |