Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_IMTABLE_INL_H_ |
| 18 | #define ART_RUNTIME_IMTABLE_INL_H_ |
| 19 | |
| 20 | #include "imtable.h" |
| 21 | |
| 22 | #include "art_method-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 23 | #include "dex/dex_file.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 24 | #include "dex/utf.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Andreas Gampe | a1ff30f | 2016-09-27 12:19:45 -0700 | [diff] [blame] | 28 | static constexpr bool kImTableHashUseName = true; |
| 29 | static constexpr bool kImTableHashUseCoefficients = true; |
| 30 | |
| 31 | // Magic configuration that minimizes some common runtime calls. |
| 32 | static constexpr uint32_t kImTableHashCoefficientClass = 427; |
| 33 | static constexpr uint32_t kImTableHashCoefficientName = 16; |
| 34 | static constexpr uint32_t kImTableHashCoefficientSignature = 14; |
| 35 | |
| 36 | inline void ImTable::GetImtHashComponents(ArtMethod* method, |
| 37 | uint32_t* class_hash, |
| 38 | uint32_t* name_hash, |
| 39 | uint32_t* signature_hash) { |
| 40 | if (kImTableHashUseName) { |
| 41 | if (method->IsProxyMethod()) { |
| 42 | *class_hash = 0; |
| 43 | *name_hash = 0; |
| 44 | *signature_hash = 0; |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | const DexFile* dex_file = method->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 49 | const dex::MethodId& method_id = dex_file->GetMethodId(method->GetDexMethodIndex()); |
Andreas Gampe | a1ff30f | 2016-09-27 12:19:45 -0700 | [diff] [blame] | 50 | |
| 51 | // Class descriptor for the class component. |
| 52 | *class_hash = ComputeModifiedUtf8Hash(dex_file->GetMethodDeclaringClassDescriptor(method_id)); |
| 53 | |
| 54 | // Method name for the method component. |
| 55 | *name_hash = ComputeModifiedUtf8Hash(dex_file->GetMethodName(method_id)); |
| 56 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 57 | const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a1ff30f | 2016-09-27 12:19:45 -0700 | [diff] [blame] | 58 | |
| 59 | // Read the proto for the signature component. |
| 60 | uint32_t tmp = ComputeModifiedUtf8Hash( |
| 61 | dex_file->GetTypeDescriptor(dex_file->GetTypeId(proto_id.return_type_idx_))); |
| 62 | |
| 63 | // Mix in the argument types. |
| 64 | // Note: we could consider just using the shorty. This would be faster, at the price of |
| 65 | // potential collisions. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 66 | const dex::TypeList* param_types = dex_file->GetProtoParameters(proto_id); |
Andreas Gampe | a1ff30f | 2016-09-27 12:19:45 -0700 | [diff] [blame] | 67 | if (param_types != nullptr) { |
| 68 | for (size_t i = 0; i != param_types->Size(); ++i) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 69 | const dex::TypeItem& type = param_types->GetTypeItem(i); |
Andreas Gampe | a1ff30f | 2016-09-27 12:19:45 -0700 | [diff] [blame] | 70 | tmp = 31 * tmp + ComputeModifiedUtf8Hash( |
| 71 | dex_file->GetTypeDescriptor(dex_file->GetTypeId(type.type_idx_))); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | *signature_hash = tmp; |
| 76 | return; |
| 77 | } else { |
| 78 | *class_hash = method->GetDexMethodIndex(); |
| 79 | *name_hash = 0; |
| 80 | *signature_hash = 0; |
| 81 | return; |
| 82 | } |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | inline uint32_t ImTable::GetImtIndex(ArtMethod* method) { |
Andreas Gampe | a1ff30f | 2016-09-27 12:19:45 -0700 | [diff] [blame] | 86 | uint32_t class_hash, name_hash, signature_hash; |
| 87 | GetImtHashComponents(method, &class_hash, &name_hash, &signature_hash); |
| 88 | |
| 89 | uint32_t mixed_hash; |
| 90 | if (!kImTableHashUseCoefficients) { |
| 91 | mixed_hash = class_hash + name_hash + signature_hash; |
| 92 | } else { |
| 93 | mixed_hash = kImTableHashCoefficientClass * class_hash + |
| 94 | kImTableHashCoefficientName * name_hash + |
| 95 | kImTableHashCoefficientSignature * signature_hash; |
| 96 | } |
| 97 | |
| 98 | return mixed_hash % ImTable::kSize; |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } // namespace art |
| 102 | |
| 103 | #endif // ART_RUNTIME_IMTABLE_INL_H_ |
| 104 | |