Skip to content

Commit

Permalink
module_find_export: use binary search if exports are sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Aug 6, 2023
1 parent b5e17bc commit 824619b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ int
module_find_export(const struct module *m, const struct name *name,
uint32_t type, uint32_t *idxp)
{
#if defined(TOYWASM_SORT_EXPORTS)
uint32_t left = 0;
uint32_t right = m->nexports;
while (left < right) {
uint32_t mid = left + (right - left) / 2;
const struct export *ex = &m->exports[mid];
int cmp = compare_name(&ex->name, name);
if (cmp == 0) {
const struct exportdesc *exd = &ex->desc;
if (exd->type == type) {
*idxp = exd->idx;
return 0;
}
break;
} else if (cmp < 0) {
left = mid + 1;
} else {
right = mid;
}
}
return ENOENT;
#else
uint32_t i;
for (i = 0; i < m->nexports; i++) {
const struct export *ex = &m->exports[i];
Expand All @@ -68,6 +90,7 @@ module_find_export(const struct module *m, const struct name *name,
}
}
return ENOENT;
#endif
}

int
Expand Down

0 comments on commit 824619b

Please sign in to comment.