| 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/megamorphic_cache_table.h" | 5 #include "vm/megamorphic_cache_table.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include "vm/object.h" | 8 #include "vm/object.h" |
| 9 #include "vm/object_store.h" |
| 9 #include "vm/stub_code.h" | 10 #include "vm/stub_code.h" |
| 10 #include "vm/symbols.h" | 11 #include "vm/symbols.h" |
| 11 | 12 |
| 12 namespace dart { | 13 namespace dart { |
| 13 | 14 |
| 14 MegamorphicCacheTable::MegamorphicCacheTable() | 15 RawMegamorphicCache* MegamorphicCacheTable::Lookup(Isolate* isolate, |
| 15 : miss_handler_function_(NULL), | 16 const String& name, |
| 16 miss_handler_code_(NULL), | 17 const Array& descriptor) { |
| 17 capacity_(0), | 18 ASSERT(name.IsSymbol()); |
| 18 length_(0), | 19 // TODO(rmacnak): ASSERT(descriptor.IsCanonical()); |
| 19 table_(NULL) { | |
| 20 } | |
| 21 | 20 |
| 22 | 21 // TODO(rmacnak): Make a proper hashtable a la symbol table. |
| 23 MegamorphicCacheTable::~MegamorphicCacheTable() { | 22 GrowableObjectArray& table = GrowableObjectArray::Handle( |
| 24 free(table_); | 23 isolate->object_store()->megamorphic_cache_table()); |
| 25 } | 24 if (table.IsNull()) { |
| 26 | 25 table = GrowableObjectArray::New(); |
| 27 | 26 ASSERT((table.Length() % kEntrySize) == 0); |
| 28 RawMegamorphicCache* MegamorphicCacheTable::Lookup(const String& name, | 27 isolate->object_store()->set_megamorphic_cache_table(table); |
| 29 const Array& descriptor) { | 28 } else { |
| 30 for (intptr_t i = 0; i < length_; ++i) { | 29 for (intptr_t i = 0; i < table.Length(); i += kEntrySize) { |
| 31 if ((table_[i].name == name.raw()) && | 30 if ((table.At(i + kEntryNameOffset) == name.raw()) && |
| 32 (table_[i].descriptor == descriptor.raw())) { | 31 (table.At(i + kEntryDescriptorOffset) == descriptor.raw())) { |
| 33 return table_[i].cache; | 32 return MegamorphicCache::RawCast(table.At(i + kEntryCacheOffset)); |
| 33 } |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 | 36 |
| 37 if (length_ == capacity_) { | |
| 38 capacity_ += kCapacityIncrement; | |
| 39 table_ = | |
| 40 reinterpret_cast<Entry*>(realloc(table_, capacity_ * sizeof(*table_))); | |
| 41 } | |
| 42 | |
| 43 ASSERT(length_ < capacity_); | |
| 44 const MegamorphicCache& cache = | 37 const MegamorphicCache& cache = |
| 45 MegamorphicCache::Handle(MegamorphicCache::New()); | 38 MegamorphicCache::Handle(MegamorphicCache::New()); |
| 46 Entry entry = { name.raw(), descriptor.raw(), cache.raw() }; | 39 table.Add(name); |
| 47 table_[length_++] = entry; | 40 table.Add(descriptor); |
| 41 table.Add(cache); |
| 42 ASSERT((table.Length() % kEntrySize) == 0); |
| 48 return cache.raw(); | 43 return cache.raw(); |
| 49 } | 44 } |
| 50 | 45 |
| 51 | 46 |
| 52 void MegamorphicCacheTable::InitMissHandler() { | 47 RawFunction* MegamorphicCacheTable::miss_handler(Isolate* isolate) { |
| 48 ASSERT(isolate->object_store()->megamorphic_miss_function() != |
| 49 Function::null()); |
| 50 return isolate->object_store()->megamorphic_miss_function(); |
| 51 } |
| 52 |
| 53 |
| 54 void MegamorphicCacheTable::InitMissHandler(Isolate* isolate) { |
| 53 // The miss handler for a class ID not found in the table is invoked as a | 55 // The miss handler for a class ID not found in the table is invoked as a |
| 54 // normal Dart function. | 56 // normal Dart function. |
| 55 const Code& code = | 57 const Code& code = |
| 56 Code::Handle(StubCode::Generate("_stub_MegamorphicMiss", | 58 Code::Handle(StubCode::Generate("_stub_MegamorphicMiss", |
| 57 StubCode::GenerateMegamorphicMissStub)); | 59 StubCode::GenerateMegamorphicMissStub)); |
| 58 const Class& cls = | 60 const Class& cls = |
| 59 Class::Handle(Type::Handle(Type::Function()).type_class()); | 61 Class::Handle(Type::Handle(Type::Function()).type_class()); |
| 60 const Function& function = | 62 const Function& function = |
| 61 Function::Handle(Function::New(Symbols::MegamorphicMiss(), | 63 Function::Handle(Function::New(Symbols::MegamorphicMiss(), |
| 62 RawFunction::kRegularFunction, | 64 RawFunction::kRegularFunction, |
| 63 false, // Not static. | 65 false, // Not static. |
| 64 false, // Not const. | 66 false, // Not const. |
| 65 false, // Not abstract. | 67 false, // Not abstract. |
| 66 false, // Not external. | 68 false, // Not external. |
| 67 false, // Not native. | 69 false, // Not native. |
| 68 cls, | 70 cls, |
| 69 0)); // No token position. | 71 0)); // No token position. |
| 70 function.set_is_debuggable(false); | 72 function.set_is_debuggable(false); |
| 71 function.set_is_visible(false); | 73 function.set_is_visible(false); |
| 72 miss_handler_code_ = code.raw(); | |
| 73 miss_handler_function_ = function.raw(); | |
| 74 function.AttachCode(code); | 74 function.AttachCode(code); |
| 75 |
| 76 isolate->object_store()->SetMegamorphicMissHandler(code, function); |
| 75 } | 77 } |
| 76 | 78 |
| 77 | 79 |
| 78 void MegamorphicCacheTable::VisitObjectPointers(ObjectPointerVisitor* v) { | 80 void MegamorphicCacheTable::PrintSizes(Isolate* isolate) { |
| 79 ASSERT(v != NULL); | |
| 80 v->VisitPointer(reinterpret_cast<RawObject**>(&miss_handler_code_)); | |
| 81 v->VisitPointer(reinterpret_cast<RawObject**>(&miss_handler_function_)); | |
| 82 for (intptr_t i = 0; i < length_; ++i) { | |
| 83 v->VisitPointer(reinterpret_cast<RawObject**>(&table_[i].name)); | |
| 84 v->VisitPointer(reinterpret_cast<RawObject**>(&table_[i].descriptor)); | |
| 85 v->VisitPointer(reinterpret_cast<RawObject**>(&table_[i].cache)); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 | |
| 90 void MegamorphicCacheTable::PrintSizes() { | |
| 91 StackZone zone(Thread::Current()); | 81 StackZone zone(Thread::Current()); |
| 92 intptr_t size = 0; | 82 intptr_t size = 0; |
| 93 MegamorphicCache& cache = MegamorphicCache::Handle(); | 83 MegamorphicCache& cache = MegamorphicCache::Handle(); |
| 94 Array& buckets = Array::Handle(); | 84 Array& buckets = Array::Handle(); |
| 95 for (intptr_t i = 0; i < length_; ++i) { | 85 const GrowableObjectArray& table = GrowableObjectArray::Handle( |
| 96 cache = table_[i].cache; | 86 isolate->object_store()->megamorphic_cache_table()); |
| 87 for (intptr_t i = 0; i < table.Length(); i += kEntrySize) { |
| 88 cache ^= table.At(i + kEntryCacheOffset); |
| 97 buckets = cache.buckets(); | 89 buckets = cache.buckets(); |
| 98 size += MegamorphicCache::InstanceSize(); | 90 size += MegamorphicCache::InstanceSize(); |
| 99 size += Array::InstanceSize(buckets.Length()); | 91 size += Array::InstanceSize(buckets.Length()); |
| 100 } | 92 } |
| 101 OS::Print("%" Pd " megamorphic caches using %" Pd "KB.\n", | 93 OS::Print("%" Pd " megamorphic caches using %" Pd "KB.\n", |
| 102 length_, size / 1024); | 94 table.Length() / kEntrySize, size / 1024); |
| 103 } | 95 } |
| 104 | 96 |
| 105 } // namespace dart | 97 } // namespace dart |
| OLD | NEW |