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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
386 if (str1.Length() == 0) { | 386 if (str1.Length() == 0) { |
387 return New(str2); | 387 return New(str2); |
388 } else if (str2.Length() == 0) { | 388 } else if (str2.Length() == 0) { |
389 return New(str1); | 389 return New(str1); |
390 } else { | 390 } else { |
391 return NewSymbol(ConcatString(str1, str2)); | 391 return NewSymbol(ConcatString(str1, str2)); |
392 } | 392 } |
393 } | 393 } |
394 | 394 |
395 | 395 |
396 RawString* Symbols::FromConcatAll(const GrowableArray<const String*>& strs) { | |
397 intptr_t len_sum = 0; | |
398 for (intptr_t i = 0; i < strs.length(); i++) { | |
399 len_sum += OS::SNPrint(NULL, 0, "%s", strs[i]->ToCString()); | |
koda
2015/08/26 17:37:06
There's a lot of temporary allocations and duplica
srdjan
2015/08/26 19:43:55
1. Overestimate the buffer length: there is always
koda
2015/08/26 20:02:03
I don't mean "a large enough constant".
I mean co
srdjan
2015/08/26 20:23:42
I'll leave it as it is.
| |
400 } | |
401 Zone* zone = Thread::Current()->zone(); | |
402 intptr_t buffer_len = len_sum + 1; // Space for terminating character. | |
403 char* buffer = zone->Alloc<char>(buffer_len); | |
404 char* orig_buffer = buffer; | |
koda
2015/08/26 17:37:06
const char* const orig_buffer
koda
2015/08/26 20:02:03
Ditto?
srdjan
2015/08/26 20:23:42
Done.
| |
405 for (intptr_t i = 0; i < strs.length(); i++) { | |
406 intptr_t len = OS::SNPrint(buffer, buffer_len, "%s", strs[i]->ToCString()); | |
407 buffer += len; | |
408 buffer_len -= len; | |
409 ASSERT(buffer_len >= 0); | |
410 } | |
411 return Symbols::New(orig_buffer, len_sum); | |
koda
2015/08/26 17:37:06
What kind of hit ratio do we expect here? (If it's
koda
2015/08/26 17:37:06
If you decide to overestimate the buffer size, you
srdjan
2015/08/26 19:43:55
Added a TODO in case this turns out to be hot code
| |
412 } | |
413 | |
414 | |
396 // StringType can be StringSlice, ConcatString, or {Latin1,UTF16,UTF32}Array. | 415 // StringType can be StringSlice, ConcatString, or {Latin1,UTF16,UTF32}Array. |
397 template<typename StringType> | 416 template<typename StringType> |
398 RawString* Symbols::NewSymbol(const StringType& str) { | 417 RawString* Symbols::NewSymbol(const StringType& str) { |
399 Thread* thread = Thread::Current(); | 418 Thread* thread = Thread::Current(); |
400 Isolate* isolate = thread->isolate(); | 419 Isolate* isolate = thread->isolate(); |
401 Zone* zone = thread->zone(); | 420 Zone* zone = thread->zone(); |
402 String& symbol = String::Handle(zone); | 421 String& symbol = String::Handle(zone); |
403 { | 422 { |
404 Isolate* vm_isolate = Dart::vm_isolate(); | 423 Isolate* vm_isolate = Dart::vm_isolate(); |
405 SymbolTable table(zone, vm_isolate->object_store()->symbol_table()); | 424 SymbolTable table(zone, vm_isolate->object_store()->symbol_table()); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
469 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 488 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
470 ASSERT(IsVMSymbolId(object_id)); | 489 ASSERT(IsVMSymbolId(object_id)); |
471 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 490 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
472 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { | 491 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { |
473 return symbol_handles_[i]->raw(); | 492 return symbol_handles_[i]->raw(); |
474 } | 493 } |
475 return Object::null(); | 494 return Object::null(); |
476 } | 495 } |
477 | 496 |
478 } // namespace dart | 497 } // namespace dart |
OLD | NEW |