Allow binary search for open methods. (#132)

This commit is contained in:
Nikolay Igotti
2016-12-13 13:24:50 +03:00
committed by GitHub
parent a6a6c677a3
commit 093409a1af
2 changed files with 46 additions and 3 deletions
+1 -1
View File
@@ -11,7 +11,7 @@
// Generic guideline is that global hash is being used in global persistent
// context, while local hashes are more local in scope.
// Local hash.
typedef uint64_t LocalHash;
typedef int64_t LocalHash;
// Hash of field name.
typedef LocalHash FieldNameHash;
// Hash of open method name.
+45 -2
View File
@@ -1,10 +1,52 @@
#include "Assert.h"
#include "TypeInfo.h"
// If one shall use binary search when looking up methods and fields.
// TODO: maybe select strategy basing on number of elements.
#define USE_BINARY_SEARCH 0
extern "C" {
#if USE_BINARY_SEARCH
int LookupFieldOffset(const TypeInfo* info, FieldNameHash nameSignature) {
int bottom = 0;
int top = info->fieldsCount_ - 1;
while (bottom <= top) {
int middle = (bottom + top) / 2;
if (info->fields_[middle].nameSignature_ < nameSignature)
bottom = middle + 1;
else if (info->fields_[middle].nameSignature_ == nameSignature)
return info->fields_[middle].fieldOffset_;
else
top = middle - 1;
}
RuntimeAssert(false, "Unknown field");
return -1;
}
void* LookupOpenMethod(const TypeInfo* info, MethodNameHash nameSignature) {
int bottom = 0;
int top = info->openMethodsCount_ - 1;
while (bottom <= top) {
int middle = (bottom + top) / 2;
if (info->openMethods_[middle].nameSignature_ < nameSignature)
bottom = middle + 1;
else if (info->openMethods_[middle].nameSignature_ == nameSignature)
return info->openMethods_[middle].methodEntryPoint_;
else
top = middle - 1;
}
RuntimeAssert(false, "Unknown open method");
return nullptr;
}
#else
int LookupFieldOffset(const TypeInfo* info, FieldNameHash nameSignature) {
// TODO: make it binary search?
for (int i = 0; i < info->fieldsCount_; ++i) {
if (info->fields_[i].nameSignature_ == nameSignature) {
return info->fields_[i].fieldOffset_;
@@ -15,7 +57,6 @@ int LookupFieldOffset(const TypeInfo* info, FieldNameHash nameSignature) {
}
void* LookupOpenMethod(const TypeInfo* info, MethodNameHash nameSignature) {
// TODO: make it binary search?
for (int i = 0; i < info->openMethodsCount_; ++i) {
if (info->openMethods_[i].nameSignature_ == nameSignature) {
return info->openMethods_[i].methodEntryPoint_;
@@ -25,4 +66,6 @@ void* LookupOpenMethod(const TypeInfo* info, MethodNameHash nameSignature) {
return nullptr;
}
#endif
}