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

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

Issue 1965493004: Canonicalize generic types in an isolate specific hash table (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 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
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/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/cpu.h" 10 #include "vm/cpu.h"
(...skipping 4617 matching lines...) Expand 10 before | Expand all | Expand 10 after
4628 4628
4629 4629
4630 static uint32_t FinalizeHash(uint32_t hash) { 4630 static uint32_t FinalizeHash(uint32_t hash) {
4631 hash += hash << 3; 4631 hash += hash << 3;
4632 hash ^= hash >> 11; // Logical shift, unsigned hash. 4632 hash ^= hash >> 11; // Logical shift, unsigned hash.
4633 hash += hash << 15; 4633 hash += hash << 15;
4634 return hash; 4634 return hash;
4635 } 4635 }
4636 4636
4637 4637
4638 intptr_t TypeArguments::Hash() const { 4638 intptr_t TypeArguments::ComputeHash() const {
4639 if (IsNull()) return 0; 4639 if (IsNull()) return 0;
4640 const intptr_t num_types = Length(); 4640 const intptr_t num_types = Length();
4641 if (IsRaw(0, num_types)) return 0; 4641 if (IsRaw(0, num_types)) return 0;
4642 uint32_t result = 0; 4642 uint32_t result = 0;
4643 AbstractType& type = AbstractType::Handle(); 4643 AbstractType& type = AbstractType::Handle();
4644 for (intptr_t i = 0; i < num_types; i++) { 4644 for (intptr_t i = 0; i < num_types; i++) {
4645 type = TypeAt(i); 4645 type = TypeAt(i);
4646 // The hash may be calculated during type finalization (for debugging 4646 // The hash may be calculated during type finalization (for debugging
4647 // purposes only) while a type argument is still temporarily null. 4647 // purposes only) while a type argument is still temporarily null.
4648 result = CombineHashes(result, type.IsNull() ? 0 : type.Hash()); 4648 result = CombineHashes(result, type.IsNull() ? 0 : type.Hash());
4649 } 4649 }
4650 return FinalizeHash(result); 4650 result = FinalizeHash(result);
4651 SetHash(result);
4652 return result;
4651 } 4653 }
4652 4654
4653 4655
4654 RawString* TypeArguments::SubvectorName(intptr_t from_index, 4656 RawString* TypeArguments::SubvectorName(intptr_t from_index,
4655 intptr_t len, 4657 intptr_t len,
4656 NameVisibility name_visibility) const { 4658 NameVisibility name_visibility) const {
4657 Thread* thread = Thread::Current(); 4659 Thread* thread = Thread::Current();
4658 Zone* zone = thread->zone(); 4660 Zone* zone = thread->zone();
4659 ASSERT(from_index + len <= Length()); 4661 ASSERT(from_index + len <= Length());
4660 String& name = String::Handle(zone); 4662 String& name = String::Handle(zone);
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after
5091 } 5093 }
5092 TypeArguments& result = TypeArguments::Handle(); 5094 TypeArguments& result = TypeArguments::Handle();
5093 { 5095 {
5094 RawObject* raw = Object::Allocate(TypeArguments::kClassId, 5096 RawObject* raw = Object::Allocate(TypeArguments::kClassId,
5095 TypeArguments::InstanceSize(len), 5097 TypeArguments::InstanceSize(len),
5096 space); 5098 space);
5097 NoSafepointScope no_safepoint; 5099 NoSafepointScope no_safepoint;
5098 result ^= raw; 5100 result ^= raw;
5099 // Length must be set before we start storing into the array. 5101 // Length must be set before we start storing into the array.
5100 result.SetLength(len); 5102 result.SetLength(len);
5103 result.SetHash(0);
5101 } 5104 }
5102 // The zero array should have been initialized. 5105 // The zero array should have been initialized.
5103 ASSERT(Object::zero_array().raw() != Array::null()); 5106 ASSERT(Object::zero_array().raw() != Array::null());
5104 COMPILE_ASSERT(StubCode::kNoInstantiator == 0); 5107 COMPILE_ASSERT(StubCode::kNoInstantiator == 0);
5105 result.set_instantiations(Object::zero_array()); 5108 result.set_instantiations(Object::zero_array());
5106 return result.raw(); 5109 return result.raw();
5107 } 5110 }
5108 5111
5109 5112
5110 5113
(...skipping 11767 matching lines...) Expand 10 before | Expand all | Expand 10 after
16878 ASSERT(IsFinalized()); 16881 ASSERT(IsFinalized());
16879 if (IsCanonical() || IsMalformed()) { 16882 if (IsCanonical() || IsMalformed()) {
16880 ASSERT(IsMalformed() || TypeArguments::Handle(arguments()).IsOld()); 16883 ASSERT(IsMalformed() || TypeArguments::Handle(arguments()).IsOld());
16881 return this->raw(); 16884 return this->raw();
16882 } 16885 }
16883 Thread* thread = Thread::Current(); 16886 Thread* thread = Thread::Current();
16884 Zone* zone = thread->zone(); 16887 Zone* zone = thread->zone();
16885 Isolate* isolate = thread->isolate(); 16888 Isolate* isolate = thread->isolate();
16886 AbstractType& type = Type::Handle(zone); 16889 AbstractType& type = Type::Handle(zone);
16887 const Class& cls = Class::Handle(zone, type_class()); 16890 const Class& cls = Class::Handle(zone, type_class());
16891 intptr_t hash = Hash();
16892 USE(hash); // Will be used to store the type in a hash map.
16888 // Since void is a keyword, we never have to canonicalize the void type after 16893 // Since void is a keyword, we never have to canonicalize the void type after
16889 // it is canonicalized once by the vm isolate. The parser does the mapping. 16894 // it is canonicalized once by the vm isolate. The parser does the mapping.
16890 ASSERT((cls.raw() != Object::void_class()) || 16895 ASSERT((cls.raw() != Object::void_class()) ||
16891 (isolate == Dart::vm_isolate())); 16896 (isolate == Dart::vm_isolate()));
16892 // Since dynamic is not a keyword, the parser builds a type that requires 16897 // Since dynamic is not a keyword, the parser builds a type that requires
16893 // canonicalization. 16898 // canonicalization.
16894 if ((cls.raw() == Object::dynamic_class()) && 16899 if ((cls.raw() == Object::dynamic_class()) &&
16895 (isolate != Dart::vm_isolate())) { 16900 (isolate != Dart::vm_isolate())) {
16896 ASSERT(Object::dynamic_type().IsCanonical()); 16901 ASSERT(Object::dynamic_type().IsCanonical());
16897 return Object::dynamic_type().raw(); 16902 return Object::dynamic_type().raw();
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
17028 const Library& library = Library::Handle(zone, cls.library()); 17033 const Library& library = Library::Handle(zone, cls.library());
17029 pieces.Add(String::Handle(zone, library.url())); 17034 pieces.Add(String::Handle(zone, library.url()));
17030 pieces.Add(Symbols::NewLine()); 17035 pieces.Add(Symbols::NewLine());
17031 const TypeArguments& type_args = TypeArguments::Handle(zone, arguments()); 17036 const TypeArguments& type_args = TypeArguments::Handle(zone, arguments());
17032 pieces.Add(String::Handle(zone, type_args.EnumerateURIs())); 17037 pieces.Add(String::Handle(zone, type_args.EnumerateURIs()));
17033 } 17038 }
17034 return Symbols::FromConcatAll(thread, pieces); 17039 return Symbols::FromConcatAll(thread, pieces);
17035 } 17040 }
17036 17041
17037 17042
17038 intptr_t Type::Hash() const { 17043 intptr_t Type::ComputeHash() const {
17039 ASSERT(IsFinalized()); 17044 ASSERT(IsFinalized());
17040 uint32_t result = 1; 17045 uint32_t result = 1;
17041 if (IsMalformed()) return result; 17046 if (IsMalformed()) return result;
17042 result = CombineHashes(result, Class::Handle(type_class()).id()); 17047 result = CombineHashes(result, Class::Handle(type_class()).id());
17043 result = CombineHashes(result, TypeArguments::Handle(arguments()).Hash()); 17048 result = CombineHashes(result, TypeArguments::Handle(arguments()).Hash());
17044 if (IsFunctionType()) { 17049 if (IsFunctionType()) {
17045 const Function& sig_fun = Function::Handle(signature()); 17050 const Function& sig_fun = Function::Handle(signature());
17046 AbstractType& type = AbstractType::Handle(sig_fun.result_type()); 17051 AbstractType& type = AbstractType::Handle(sig_fun.result_type());
17047 result = CombineHashes(result, type.Hash()); 17052 result = CombineHashes(result, type.Hash());
17048 result = CombineHashes(result, sig_fun.NumOptionalPositionalParameters()); 17053 result = CombineHashes(result, sig_fun.NumOptionalPositionalParameters());
17049 const intptr_t num_params = sig_fun.NumParameters(); 17054 const intptr_t num_params = sig_fun.NumParameters();
17050 for (intptr_t i = 0; i < num_params; i++) { 17055 for (intptr_t i = 0; i < num_params; i++) {
17051 type = sig_fun.ParameterTypeAt(i); 17056 type = sig_fun.ParameterTypeAt(i);
17052 result = CombineHashes(result, type.Hash()); 17057 result = CombineHashes(result, type.Hash());
17053 } 17058 }
17054 if (sig_fun.NumOptionalNamedParameters() > 0) { 17059 if (sig_fun.NumOptionalNamedParameters() > 0) {
17055 String& param_name = String::Handle(); 17060 String& param_name = String::Handle();
17056 for (intptr_t i = sig_fun.num_fixed_parameters(); i < num_params; i++) { 17061 for (intptr_t i = sig_fun.num_fixed_parameters(); i < num_params; i++) {
17057 param_name = sig_fun.ParameterNameAt(i); 17062 param_name = sig_fun.ParameterNameAt(i);
17058 result = CombineHashes(result, param_name.Hash()); 17063 result = CombineHashes(result, param_name.Hash());
17059 } 17064 }
17060 } 17065 }
17061 } 17066 }
17062 return FinalizeHash(result); 17067 result = FinalizeHash(result);
17068 SetHash(result);
17069 return result;
17063 } 17070 }
17064 17071
17065 17072
17066 void Type::set_type_class(const Object& value) const { 17073 void Type::set_type_class(const Object& value) const {
17067 ASSERT(!value.IsNull() && (value.IsClass() || value.IsUnresolvedClass())); 17074 ASSERT(!value.IsNull() && (value.IsClass() || value.IsUnresolvedClass()));
17068 StorePointer(&raw_ptr()->type_class_, value.raw()); 17075 StorePointer(&raw_ptr()->type_class_, value.raw());
17069 } 17076 }
17070 17077
17071 17078
17072 void Type::set_arguments(const TypeArguments& value) const { 17079 void Type::set_arguments(const TypeArguments& value) const {
(...skipping 10 matching lines...) Expand all
17083 } 17090 }
17084 17091
17085 17092
17086 RawType* Type::New(const Object& clazz, 17093 RawType* Type::New(const Object& clazz,
17087 const TypeArguments& arguments, 17094 const TypeArguments& arguments,
17088 TokenPosition token_pos, 17095 TokenPosition token_pos,
17089 Heap::Space space) { 17096 Heap::Space space) {
17090 const Type& result = Type::Handle(Type::New(space)); 17097 const Type& result = Type::Handle(Type::New(space));
17091 result.set_type_class(clazz); 17098 result.set_type_class(clazz);
17092 result.set_arguments(arguments); 17099 result.set_arguments(arguments);
17100 result.SetHash(0);
17093 result.set_token_pos(token_pos); 17101 result.set_token_pos(token_pos);
17094 result.StoreNonPointer(&result.raw_ptr()->type_state_, RawType::kAllocated); 17102 result.StoreNonPointer(&result.raw_ptr()->type_state_, RawType::kAllocated);
17095 return result.raw(); 17103 return result.raw();
17096 } 17104 }
17097 17105
17098 17106
17099 void Type::set_token_pos(TokenPosition token_pos) const { 17107 void Type::set_token_pos(TokenPosition token_pos) const {
17100 ASSERT(!token_pos.IsClassifying()); 17108 ASSERT(!token_pos.IsClassifying());
17101 StoreNonPointer(&raw_ptr()->token_pos_, token_pos); 17109 StoreNonPointer(&raw_ptr()->token_pos_, token_pos);
17102 } 17110 }
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
17492 const Class& cls = Class::Handle(zone, parameterized_class()); 17500 const Class& cls = Class::Handle(zone, parameterized_class());
17493 pieces.Add(String::Handle(zone, cls.UserVisibleName())); 17501 pieces.Add(String::Handle(zone, cls.UserVisibleName()));
17494 pieces.Add(Symbols::SpaceIsFromSpace()); 17502 pieces.Add(Symbols::SpaceIsFromSpace());
17495 const Library& library = Library::Handle(zone, cls.library()); 17503 const Library& library = Library::Handle(zone, cls.library());
17496 pieces.Add(String::Handle(zone, library.url())); 17504 pieces.Add(String::Handle(zone, library.url()));
17497 pieces.Add(Symbols::NewLine()); 17505 pieces.Add(Symbols::NewLine());
17498 return Symbols::FromConcatAll(thread, pieces); 17506 return Symbols::FromConcatAll(thread, pieces);
17499 } 17507 }
17500 17508
17501 17509
17502 intptr_t TypeParameter::Hash() const { 17510 intptr_t TypeParameter::ComputeHash() const {
17503 ASSERT(IsFinalized()); 17511 ASSERT(IsFinalized());
17504 uint32_t result = Class::Handle(parameterized_class()).id(); 17512 uint32_t result = Class::Handle(parameterized_class()).id();
17505 // No need to include the hash of the bound, since the type parameter is fully 17513 // No need to include the hash of the bound, since the type parameter is fully
17506 // identified by its class and index. 17514 // identified by its class and index.
17507 result = CombineHashes(result, index()); 17515 result = CombineHashes(result, index());
17508 return FinalizeHash(result); 17516 result = FinalizeHash(result);
17517 SetHash(result);
17518 return result;
17509 } 17519 }
17510 17520
17511 17521
17512 RawTypeParameter* TypeParameter::New() { 17522 RawTypeParameter* TypeParameter::New() {
17513 RawObject* raw = Object::Allocate(TypeParameter::kClassId, 17523 RawObject* raw = Object::Allocate(TypeParameter::kClassId,
17514 TypeParameter::InstanceSize(), 17524 TypeParameter::InstanceSize(),
17515 Heap::kOld); 17525 Heap::kOld);
17516 return reinterpret_cast<RawTypeParameter*>(raw); 17526 return reinterpret_cast<RawTypeParameter*>(raw);
17517 } 17527 }
17518 17528
17519 17529
17520 RawTypeParameter* TypeParameter::New(const Class& parameterized_class, 17530 RawTypeParameter* TypeParameter::New(const Class& parameterized_class,
17521 intptr_t index, 17531 intptr_t index,
17522 const String& name, 17532 const String& name,
17523 const AbstractType& bound, 17533 const AbstractType& bound,
17524 TokenPosition token_pos) { 17534 TokenPosition token_pos) {
17525 const TypeParameter& result = TypeParameter::Handle(TypeParameter::New()); 17535 const TypeParameter& result = TypeParameter::Handle(TypeParameter::New());
17526 result.set_parameterized_class(parameterized_class); 17536 result.set_parameterized_class(parameterized_class);
17527 result.set_index(index); 17537 result.set_index(index);
17528 result.set_name(name); 17538 result.set_name(name);
17529 result.set_bound(bound); 17539 result.set_bound(bound);
17540 result.SetHash(0);
17530 result.set_token_pos(token_pos); 17541 result.set_token_pos(token_pos);
17531 result.StoreNonPointer(&result.raw_ptr()->type_state_, 17542 result.StoreNonPointer(&result.raw_ptr()->type_state_,
17532 RawTypeParameter::kAllocated); 17543 RawTypeParameter::kAllocated);
17533 return result.raw(); 17544 return result.raw();
17534 } 17545 }
17535 17546
17536 17547
17537 void TypeParameter::set_token_pos(TokenPosition token_pos) const { 17548 void TypeParameter::set_token_pos(TokenPosition token_pos) const {
17538 ASSERT(!token_pos.IsClassifying()); 17549 ASSERT(!token_pos.IsClassifying());
17539 StoreNonPointer(&raw_ptr()->token_pos_, token_pos); 17550 StoreNonPointer(&raw_ptr()->token_pos_, token_pos);
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
17756 return BoundedType::New(bounded_type, upper_bound, type_param); 17767 return BoundedType::New(bounded_type, upper_bound, type_param);
17757 } 17768 }
17758 17769
17759 17770
17760 RawString* BoundedType::EnumerateURIs() const { 17771 RawString* BoundedType::EnumerateURIs() const {
17761 // The bound does not appear in the user visible name. 17772 // The bound does not appear in the user visible name.
17762 return AbstractType::Handle(type()).EnumerateURIs(); 17773 return AbstractType::Handle(type()).EnumerateURIs();
17763 } 17774 }
17764 17775
17765 17776
17766 intptr_t BoundedType::Hash() const { 17777 intptr_t BoundedType::ComputeHash() const {
17767 uint32_t result = AbstractType::Handle(type()).Hash(); 17778 uint32_t result = AbstractType::Handle(type()).Hash();
17768 // No need to include the hash of the bound, since the bound is defined by the 17779 // No need to include the hash of the bound, since the bound is defined by the
17769 // type parameter (modulo instantiation state). 17780 // type parameter (modulo instantiation state).
17770 result = CombineHashes(result, 17781 result = CombineHashes(result,
17771 TypeParameter::Handle(type_parameter()).Hash()); 17782 TypeParameter::Handle(type_parameter()).Hash());
17772 return FinalizeHash(result); 17783 result = FinalizeHash(result);
17784 SetHash(result);
17785 return result;
17773 } 17786 }
17774 17787
17775 17788
17776 RawBoundedType* BoundedType::New() { 17789 RawBoundedType* BoundedType::New() {
17777 RawObject* raw = Object::Allocate(BoundedType::kClassId, 17790 RawObject* raw = Object::Allocate(BoundedType::kClassId,
17778 BoundedType::InstanceSize(), 17791 BoundedType::InstanceSize(),
17779 Heap::kOld); 17792 Heap::kOld);
17780 return reinterpret_cast<RawBoundedType*>(raw); 17793 return reinterpret_cast<RawBoundedType*>(raw);
17781 } 17794 }
17782 17795
17783 17796
17784 RawBoundedType* BoundedType::New(const AbstractType& type, 17797 RawBoundedType* BoundedType::New(const AbstractType& type,
17785 const AbstractType& bound, 17798 const AbstractType& bound,
17786 const TypeParameter& type_parameter) { 17799 const TypeParameter& type_parameter) {
17787 const BoundedType& result = BoundedType::Handle(BoundedType::New()); 17800 const BoundedType& result = BoundedType::Handle(BoundedType::New());
17788 result.set_type(type); 17801 result.set_type(type);
17789 result.set_bound(bound); 17802 result.set_bound(bound);
17803 result.SetHash(0);
17790 result.set_type_parameter(type_parameter); 17804 result.set_type_parameter(type_parameter);
17791 return result.raw(); 17805 return result.raw();
17792 } 17806 }
17793 17807
17794 17808
17795 const char* BoundedType::ToCString() const { 17809 const char* BoundedType::ToCString() const {
17796 const char* format = "BoundedType: type %s; bound: %s; type param: %s of %s"; 17810 const char* format = "BoundedType: type %s; bound: %s; type param: %s of %s";
17797 const char* type_cstr = String::Handle(AbstractType::Handle( 17811 const char* type_cstr = String::Handle(AbstractType::Handle(
17798 type()).Name()).ToCString(); 17812 type()).Name()).ToCString();
17799 const char* bound_cstr = String::Handle(AbstractType::Handle( 17813 const char* bound_cstr = String::Handle(AbstractType::Handle(
(...skipping 4652 matching lines...) Expand 10 before | Expand all | Expand 10 after
22452 return UserTag::null(); 22466 return UserTag::null();
22453 } 22467 }
22454 22468
22455 22469
22456 const char* UserTag::ToCString() const { 22470 const char* UserTag::ToCString() const {
22457 const String& tag_label = String::Handle(label()); 22471 const String& tag_label = String::Handle(label());
22458 return tag_label.ToCString(); 22472 return tag_label.ToCString();
22459 } 22473 }
22460 22474
22461 } // namespace dart 22475 } // namespace dart
OLDNEW
« runtime/vm/object.h ('K') | « runtime/vm/object.h ('k') | runtime/vm/raw_object.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698