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

Side by Side Diff: runtime/vm/symbols.cc

Issue 1638143002: Precompilation: compact the symbol table. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 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
« runtime/vm/symbols.h ('K') | « runtime/vm/symbols.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 Zone* zone = Thread::Current()->zone();
336 intptr_t initial_size = -1;
337 intptr_t final_size = -1;
338
339 // 1. Build a collection of all the predefined symbols so they are
340 // strongly referenced (the read only handles are not traced).
341 GrowableObjectArray& predefined_symbols =
342 GrowableObjectArray::Handle(GrowableObjectArray::New());
siva 2016/01/28 00:04:00 Can this be moved inside the scope of if (Object::
rmacnak 2016/01/28 17:53:09 Done.
343 {
344 String& symbol = String::Handle();
siva 2016/01/28 00:03:59 This handle could also go inside the scope of if (
rmacnak 2016/01/28 17:53:09 Done.
345 SymbolTable table(zone, isolate->object_store()->symbol_table());
346 initial_size = table.NumOccupied();
347
348 if (Object::vm_isolate_snapshot_object_table().Length() == 0) {
349 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) {
350 const unsigned char* name =
351 reinterpret_cast<const unsigned char*>(names[i]);
352 symbol ^= table.GetOrNull(Latin1Array(name, strlen(names[i])));
353 ASSERT(!symbol.IsNull());
354 predefined_symbols.Add(symbol);
355 }
356 for (intptr_t c = 0; c < kNumberOfOneCharCodeSymbols; c++) {
357 intptr_t idx = (kNullCharId + c);
358 ASSERT(idx < kMaxPredefinedId);
359 ASSERT(Utf::IsLatin1(c));
360 uint8_t ch = static_cast<uint8_t>(c);
361 symbol ^= table.GetOrNull(Latin1Array(&ch, 1));
362 ASSERT(!symbol.IsNull());
363 predefined_symbols.Add(symbol);
364 }
365 }
366 table.Release();
367 }
368
369 // 2. Knock out the symbol table and do a full garbage collection.
370 isolate->object_store()->set_symbol_table(Object::empty_array());
371 isolate->heap()->CollectAllGarbage();
372
373 // 3. Walk the heap and built a new table from surviving symbols.
siva 2016/01/27 21:21:10 Walk the heap and build a new
rmacnak 2016/01/27 23:06:20 Done.
374 GrowableArray<String*> symbols;
375 class SymbolCollector : public ObjectVisitor {
376 public:
377 SymbolCollector(GrowableArray<String*>* symbols,
378 Isolate* isolate,
379 Zone* zone)
380 : ObjectVisitor(isolate),
381 symbols_(symbols),
382 zone_(zone) {}
siva 2016/01/28 00:04:00 Why not make the signature SymbolCollector(Thread*
rmacnak 2016/01/28 17:53:09 Done.
383
384 void VisitObject(RawObject* obj) {
385 if (obj->IsString() && obj->IsCanonical()) {
siva 2016/01/28 00:04:00 would if (obj->IsCanonical() && obj->IsString()) b
rmacnak 2016/01/28 17:53:09 Probably a little bit since canonical is a bit che
386 symbols_->Add(&String::ZoneHandle(zone_, String::RawCast(obj)));
387 }
388 }
389
390 private:
391 GrowableArray<String*>* symbols_;
392 Zone* zone_;
393 };
394
395 SymbolCollector visitor(&symbols, isolate, zone);
396 isolate->heap()->IterateObjects(&visitor);
397
398 {
399 Array& array =
400 Array::Handle(HashTables::New<SymbolTable>(kInitialSymtabSize,
siva 2016/01/27 21:21:10 Instead of kInitialSymtabSize you could count the
rmacnak 2016/01/27 23:06:20 Done. (plus load factor)
401 Heap::kOld));
402 SymbolTable table(zone, array.raw());
403 for (intptr_t i = 0; i < symbols.length(); i++) {
404 String& symbol = *symbols[i];
405 ASSERT(symbol.IsString());
406 ASSERT(symbol.IsCanonical());
407 bool present = table.Insert(symbol);
408 ASSERT(!present);
409 }
410 final_size = table.NumOccupied();
411 isolate->object_store()->set_symbol_table(table.Release());
siva 2016/01/27 21:21:10 Instead of creating a strong reference to the pred
rmacnak 2016/01/27 23:06:20 As discussed, would leave some read-only handles p
412 }
413
414 return initial_size - final_size;
415 }
416
417
334 void Symbols::GetStats(Isolate* isolate, intptr_t* size, intptr_t* capacity) { 418 void Symbols::GetStats(Isolate* isolate, intptr_t* size, intptr_t* capacity) {
335 ASSERT(isolate != NULL); 419 ASSERT(isolate != NULL);
336 SymbolTable table(isolate->object_store()->symbol_table()); 420 SymbolTable table(isolate->object_store()->symbol_table());
337 *size = table.NumOccupied(); 421 *size = table.NumOccupied();
338 *capacity = table.NumEntries(); 422 *capacity = table.NumEntries();
339 table.Release(); 423 table.Release();
340 } 424 }
341 425
342 426
343 RawString* Symbols::New(const char* cstr, intptr_t len) { 427 RawString* Symbols::New(const char* cstr, intptr_t len) {
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { 688 RawObject* Symbols::GetVMSymbol(intptr_t object_id) {
605 ASSERT(IsVMSymbolId(object_id)); 689 ASSERT(IsVMSymbolId(object_id));
606 intptr_t i = (object_id - kMaxPredefinedObjectIds); 690 intptr_t i = (object_id - kMaxPredefinedObjectIds);
607 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { 691 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) {
608 return symbol_handles_[i]->raw(); 692 return symbol_handles_[i]->raw();
609 } 693 }
610 return Object::null(); 694 return Object::null();
611 } 695 }
612 696
613 } // namespace dart 697 } // namespace dart
OLDNEW
« runtime/vm/symbols.h ('K') | « runtime/vm/symbols.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698