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