binary serch when size > 16

This commit is contained in:
pikastech 2022-10-15 23:32:34 +08:00
parent 2244e33943
commit 86686a99b9
3 changed files with 28 additions and 31 deletions

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test", "program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [ "args": [
"--gtest_filter=vm.c_module_get_set_attr" // "--gtest_filter=vm.c_module_get_set_attr"
], ],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",

View File

@ -331,23 +331,20 @@ Arg* _obj_getProp(PikaObj* obj, char* name) {
} }
} }
Hash method_hash = hash_time33(name); Hash method_hash = hash_time33(name);
#define BINARY_SEARCH_ENABLE 1
while (1) { while (1) {
if (prop == NULL) { if (prop == NULL) {
break; break;
} }
#if BINARY_SEARCH_ENABLE
/* binary search */
int size = prop->methodGroupCount; int size = prop->methodGroupCount;
/* binary search */
if (size == 0) { if (size == 0) {
goto next; goto next;
} }
if (size > 16) {
int left = 0; int left = 0;
int right = size - 1; int right = size - 1;
int mid = 0; int mid = 0;
int i = 0;
while (1) { while (1) {
i++;
if (left > right) { if (left > right) {
break; break;
} }
@ -362,7 +359,7 @@ Arg* _obj_getProp(PikaObj* obj, char* name) {
right = mid - 1; right = mid - 1;
} }
} }
#else } else {
for (int i = 0; i < (int)prop->methodGroupCount; i++) { for (int i = 0; i < (int)prop->methodGroupCount; i++) {
Arg* prop_this = (Arg*)(prop->methodGroup + i); Arg* prop_this = (Arg*)(prop->methodGroup + i);
if (prop_this->name_hash == method_hash) { if (prop_this->name_hash == method_hash) {
@ -370,8 +367,8 @@ Arg* _obj_getProp(PikaObj* obj, char* name) {
goto exit; goto exit;
} }
} }
}
goto next; goto next;
#endif
next: next:
prop = (NativeProperty*)prop->super; prop = (NativeProperty*)prop->super;
} }

View File

@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 11 #define PIKA_VERSION_MINOR 11
#define PIKA_VERSION_MICRO 4 #define PIKA_VERSION_MICRO 4
#define PIKA_EDIT_TIME "2022/10/10 21:37:18" #define PIKA_EDIT_TIME "2022/10/15 23:32:32"