| 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/isolate.h" | 9 #include "vm/isolate.h" |
| 10 #include "vm/object.h" | 10 #include "vm/object.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 static const char* names[] = { | 27 static const char* names[] = { |
| 28 NULL, | 28 NULL, |
| 29 | 29 |
| 30 #define DEFINE_SYMBOL_LITERAL(symbol, literal) \ | 30 #define DEFINE_SYMBOL_LITERAL(symbol, literal) \ |
| 31 literal, | 31 literal, |
| 32 PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_LITERAL) | 32 PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_LITERAL) |
| 33 #undef DEFINE_SYMBOL_LITERAL | 33 #undef DEFINE_SYMBOL_LITERAL |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 intptr_t Symbols::num_of_grows_; |
| 37 intptr_t Symbols::collision_count_[kMaxCollisionBuckets]; |
| 38 |
| 39 DEFINE_FLAG(bool, dump_symbol_stats, false, "Dump symbol table statistics"); |
| 40 |
| 36 | 41 |
| 37 const char* Symbols::Name(SymbolId symbol) { | 42 const char* Symbols::Name(SymbolId symbol) { |
| 38 ASSERT((symbol > kIllegal) && (symbol < kMaxPredefinedId)); | 43 ASSERT((symbol > kIllegal) && (symbol < kMaxPredefinedId)); |
| 39 return names[symbol]; | 44 return names[symbol]; |
| 40 } | 45 } |
| 41 | 46 |
| 42 | 47 |
| 43 void Symbols::InitOnce(Isolate* isolate) { | 48 void Symbols::InitOnce(Isolate* isolate) { |
| 44 // Should only be run by the vm isolate. | 49 // Should only be run by the vm isolate. |
| 45 ASSERT(isolate == Dart::vm_isolate()); | 50 ASSERT(isolate == Dart::vm_isolate()); |
| 46 | 51 |
| 52 if (FLAG_dump_symbol_stats) { |
| 53 num_of_grows_ = 0; |
| 54 for (intptr_t i = 0; i < kMaxCollisionBuckets; i++) { |
| 55 collision_count_[i] = 0; |
| 56 } |
| 57 } |
| 58 |
| 47 // Create and setup a symbol table in the vm isolate. | 59 // Create and setup a symbol table in the vm isolate. |
| 48 SetupSymbolTable(isolate); | 60 SetupSymbolTable(isolate); |
| 49 | 61 |
| 50 // Create all predefined symbols. | 62 // Create all predefined symbols. |
| 51 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kMaxPredefinedId); | 63 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kMaxPredefinedId); |
| 52 ObjectStore* object_store = isolate->object_store(); | 64 ObjectStore* object_store = isolate->object_store(); |
| 53 Array& symbol_table = Array::Handle(); | 65 Array& symbol_table = Array::Handle(); |
| 54 dart::String& str = String::Handle(); | 66 dart::String& str = String::Handle(); |
| 55 | 67 |
| 56 for (intptr_t i = 1; i < Symbols::kMaxPredefinedId; i++) { | 68 for (intptr_t i = 1; i < Symbols::kMaxPredefinedId; i++) { |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 } | 273 } |
| 262 return predefined_[kNullCharId + char_code]; | 274 return predefined_[kNullCharId + char_code]; |
| 263 } | 275 } |
| 264 | 276 |
| 265 | 277 |
| 266 bool Symbols::IsPredefinedHandle(uword address) { | 278 bool Symbols::IsPredefinedHandle(uword address) { |
| 267 return predefined_handles_.IsValidScopedHandle(address); | 279 return predefined_handles_.IsValidScopedHandle(address); |
| 268 } | 280 } |
| 269 | 281 |
| 270 | 282 |
| 283 void Symbols::DumpStats() { |
| 284 if (FLAG_dump_symbol_stats) { |
| 285 intptr_t table_size = 0; |
| 286 dart::Smi& used = Smi::Handle(); |
| 287 Array& symbol_table = Array::Handle(Array::null()); |
| 288 |
| 289 // First dump VM symbol table stats. |
| 290 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); |
| 291 table_size = symbol_table.Length() - 1; |
| 292 used ^= symbol_table.At(table_size); |
| 293 OS::Print("VM Isolate: Number of symbols : %"Pd"\n", used.Value()); |
| 294 OS::Print("VM Isolate: Symbol table capacity : %"Pd"\n", table_size); |
| 295 |
| 296 // Now dump regular isolate symbol table stats. |
| 297 symbol_table = Isolate::Current()->object_store()->symbol_table(); |
| 298 table_size = symbol_table.Length() - 1; |
| 299 used ^= symbol_table.At(table_size); |
| 300 OS::Print("Isolate: Number of symbols : %"Pd"\n", used.Value()); |
| 301 OS::Print("Isolate: Symbol table capacity : %"Pd"\n", table_size); |
| 302 |
| 303 // Dump overall collision and growth counts. |
| 304 OS::Print("Number of symbol table grows = %"Pd"\n", num_of_grows_); |
| 305 OS::Print("Collision counts on add and lookup :\n"); |
| 306 intptr_t i = 0; |
| 307 for (i = 0; i < (kMaxCollisionBuckets - 1); i++) { |
| 308 OS::Print(" %"Pd" collisions => %"Pd"\n", i, collision_count_[i]); |
| 309 } |
| 310 OS::Print(" > %"Pd" collisions => %"Pd"\n", i, collision_count_[i]); |
| 311 } |
| 312 } |
| 313 |
| 314 |
| 271 void Symbols::GrowSymbolTable(const Array& symbol_table) { | 315 void Symbols::GrowSymbolTable(const Array& symbol_table) { |
| 272 // TODO(iposva): Avoid exponential growth. | 316 // TODO(iposva): Avoid exponential growth. |
| 317 num_of_grows_ += 1; |
| 273 intptr_t table_size = symbol_table.Length() - 1; | 318 intptr_t table_size = symbol_table.Length() - 1; |
| 274 intptr_t new_table_size = table_size * 2; | 319 intptr_t new_table_size = table_size * 2; |
| 275 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); | 320 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); |
| 276 // Copy all elements from the original symbol table to the newly allocated | 321 // Copy all elements from the original symbol table to the newly allocated |
| 277 // array. | 322 // array. |
| 278 String& element = String::Handle(); | 323 String& element = String::Handle(); |
| 279 dart::Object& new_element = Object::Handle(); | 324 dart::Object& new_element = Object::Handle(); |
| 280 for (intptr_t i = 0; i < table_size; i++) { | 325 for (intptr_t i = 0; i < table_size; i++) { |
| 281 element ^= symbol_table.At(i); | 326 element ^= symbol_table.At(i); |
| 282 if (!element.IsNull()) { | 327 if (!element.IsNull()) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 | 363 |
| 319 | 364 |
| 320 template<typename T> | 365 template<typename T> |
| 321 intptr_t Symbols::FindIndex(const Array& symbol_table, | 366 intptr_t Symbols::FindIndex(const Array& symbol_table, |
| 322 const T* characters, | 367 const T* characters, |
| 323 intptr_t len, | 368 intptr_t len, |
| 324 intptr_t hash) { | 369 intptr_t hash) { |
| 325 // Last element of the array is the number of used elements. | 370 // Last element of the array is the number of used elements. |
| 326 intptr_t table_size = symbol_table.Length() - 1; | 371 intptr_t table_size = symbol_table.Length() - 1; |
| 327 intptr_t index = hash % table_size; | 372 intptr_t index = hash % table_size; |
| 373 intptr_t num_collisions = 0; |
| 328 | 374 |
| 329 String& symbol = String::Handle(); | 375 String& symbol = String::Handle(); |
| 330 symbol ^= symbol_table.At(index); | 376 symbol ^= symbol_table.At(index); |
| 331 while (!symbol.IsNull() && !symbol.Equals(characters, len)) { | 377 while (!symbol.IsNull() && !symbol.Equals(characters, len)) { |
| 332 index = (index + 1) % table_size; // Move to next element. | 378 index = (index + 1) % table_size; // Move to next element. |
| 333 symbol ^= symbol_table.At(index); | 379 symbol ^= symbol_table.At(index); |
| 380 num_collisions += 1; |
| 381 } |
| 382 if (FLAG_dump_symbol_stats) { |
| 383 if (num_collisions >= kMaxCollisionBuckets) { |
| 384 num_collisions = (kMaxCollisionBuckets - 1); |
| 385 } |
| 386 collision_count_[num_collisions] += 1; |
| 334 } | 387 } |
| 335 return index; // Index of symbol if found or slot into which to add symbol. | 388 return index; // Index of symbol if found or slot into which to add symbol. |
| 336 } | 389 } |
| 337 | 390 |
| 338 | 391 |
| 339 template intptr_t Symbols::FindIndex(const Array& symbol_table, | 392 template intptr_t Symbols::FindIndex(const Array& symbol_table, |
| 340 const uint8_t* characters, | 393 const uint8_t* characters, |
| 341 intptr_t len, | 394 intptr_t len, |
| 342 intptr_t hash); | 395 intptr_t hash); |
| 343 template intptr_t Symbols::FindIndex(const Array& symbol_table, | 396 template intptr_t Symbols::FindIndex(const Array& symbol_table, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 } | 432 } |
| 380 | 433 |
| 381 | 434 |
| 382 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 435 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 383 ASSERT(IsVMSymbolId(object_id)); | 436 ASSERT(IsVMSymbolId(object_id)); |
| 384 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 437 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 385 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); | 438 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); |
| 386 } | 439 } |
| 387 | 440 |
| 388 } // namespace dart | 441 } // namespace dart |
| OLD | NEW |