Skip to content

Commit a3960c8

Browse files
authored
Refine looking up aot function with index (#3882)
* Refine looking up aot function with index * refine the code
1 parent 483c57d commit a3960c8

File tree

4 files changed

+95
-28
lines changed

4 files changed

+95
-28
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,11 +1096,11 @@ aot_get_default_memory(AOTModuleInstance *module_inst)
10961096
}
10971097

10981098
AOTMemoryInstance *
1099-
aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index)
1099+
aot_get_memory_with_idx(AOTModuleInstance *module_inst, uint32 mem_idx)
11001100
{
1101-
if ((index >= module_inst->memory_count) || !module_inst->memories)
1101+
if ((mem_idx >= module_inst->memory_count) || !module_inst->memories)
11021102
return NULL;
1103-
return module_inst->memories[index];
1103+
return module_inst->memories[mem_idx];
11041104
}
11051105

11061106
static bool
@@ -1282,21 +1282,78 @@ init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module,
12821282
return true;
12831283
}
12841284

1285+
static int
1286+
cmp_export_func_map(const void *a, const void *b)
1287+
{
1288+
uint32 func_idx1 = ((const ExportFuncMap *)a)->func_idx;
1289+
uint32 func_idx2 = ((const ExportFuncMap *)b)->func_idx;
1290+
return func_idx1 < func_idx2 ? -1 : (func_idx1 > func_idx2 ? 1 : 0);
1291+
}
1292+
12851293
AOTFunctionInstance *
1286-
aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx)
1294+
aot_lookup_function_with_idx(AOTModuleInstance *module_inst, uint32 func_idx)
12871295
{
1288-
AOTModule *module = (AOTModule *)module_inst->module;
12891296
AOTModuleInstanceExtra *extra = (AOTModuleInstanceExtra *)module_inst->e;
12901297
AOTFunctionInstance *export_funcs =
12911298
(AOTFunctionInstance *)module_inst->export_functions;
1299+
AOTFunctionInstance *func_inst = NULL;
1300+
ExportFuncMap *export_func_maps, *export_func_map, key;
1301+
uint64 size;
12921302
uint32 i;
12931303

1294-
/* export functions are pre-instantiated */
1295-
for (i = 0; i < module_inst->export_func_count; i++) {
1296-
if (export_funcs[i].func_index == func_idx)
1297-
return &export_funcs[i];
1304+
if (module_inst->export_func_count == 0)
1305+
return NULL;
1306+
1307+
exception_lock(module_inst);
1308+
1309+
/* create the func_idx to export_idx maps if it hasn't been created */
1310+
if (!extra->export_func_maps) {
1311+
size = sizeof(ExportFuncMap) * (uint64)module_inst->export_func_count;
1312+
if (!(export_func_maps = extra->export_func_maps =
1313+
runtime_malloc(size, NULL, 0))) {
1314+
/* allocate memory failed, lookup the export function one by one */
1315+
for (i = 0; i < module_inst->export_func_count; i++) {
1316+
if (export_funcs[i].func_index == func_idx) {
1317+
func_inst = &export_funcs[i];
1318+
break;
1319+
}
1320+
}
1321+
goto unlock_and_return;
1322+
}
1323+
1324+
for (i = 0; i < module_inst->export_func_count; i++) {
1325+
export_func_maps[i].func_idx = export_funcs[i].func_index;
1326+
export_func_maps[i].export_idx = i;
1327+
}
1328+
1329+
qsort(export_func_maps, module_inst->export_func_count,
1330+
sizeof(ExportFuncMap), cmp_export_func_map);
12981331
}
12991332

1333+
/* lookup the map to get the export_idx of the func_idx */
1334+
key.func_idx = func_idx;
1335+
export_func_map =
1336+
bsearch(&key, extra->export_func_maps, module_inst->export_func_count,
1337+
sizeof(ExportFuncMap), cmp_export_func_map);
1338+
if (export_func_map)
1339+
func_inst = &export_funcs[export_func_map->export_idx];
1340+
1341+
unlock_and_return:
1342+
exception_unlock(module_inst);
1343+
return func_inst;
1344+
}
1345+
1346+
AOTFunctionInstance *
1347+
aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx)
1348+
{
1349+
AOTModule *module = (AOTModule *)module_inst->module;
1350+
AOTModuleInstanceExtra *extra = (AOTModuleInstanceExtra *)module_inst->e;
1351+
AOTFunctionInstance *func_inst;
1352+
1353+
/* lookup from export functions first */
1354+
if ((func_inst = aot_lookup_function_with_idx(module_inst, func_idx)))
1355+
return func_inst;
1356+
13001357
exception_lock(module_inst);
13011358

13021359
/* allocate functions array if needed */
@@ -2168,6 +2225,9 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
21682225
if (module_inst->export_functions)
21692226
wasm_runtime_free(module_inst->export_functions);
21702227

2228+
if (extra->export_func_maps)
2229+
wasm_runtime_free(extra->export_func_maps);
2230+
21712231
#if WASM_ENABLE_MULTI_MEMORY != 0
21722232
if (module_inst->export_memories)
21732233
wasm_runtime_free(module_inst->export_memories);

core/iwasm/aot/aot_runtime.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ typedef struct AOTFunctionInstance {
109109
} u;
110110
} AOTFunctionInstance;
111111

112+
/* Map of a function index to the element ith in
113+
the export functions array */
114+
typedef struct ExportFuncMap {
115+
uint32 func_idx;
116+
uint32 export_idx;
117+
} ExportFuncMap;
118+
112119
typedef struct AOTModuleInstanceExtra {
113120
DefPointer(const uint32 *, stack_sizes);
114121
/*
@@ -120,6 +127,13 @@ typedef struct AOTModuleInstanceExtra {
120127
MemBound shared_heap_start_off;
121128

122129
WASMModuleInstanceExtraCommon common;
130+
131+
/**
132+
* maps of func indexes to export func indexes, which
133+
* is sorted by func index for a quick lookup and is
134+
* created only when first time used.
135+
*/
136+
ExportFuncMap *export_func_maps;
123137
AOTFunctionInstance **functions;
124138
uint32 function_count;
125139
#if WASM_ENABLE_MULTI_MODULE != 0
@@ -556,14 +570,21 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst);
556570
AOTFunctionInstance *
557571
aot_lookup_function(const AOTModuleInstance *module_inst, const char *name);
558572

573+
/**
574+
* Lookup an exported function in the AOT module instance with
575+
* the function index.
576+
*/
577+
AOTFunctionInstance *
578+
aot_lookup_function_with_idx(AOTModuleInstance *module_inst, uint32 func_idx);
579+
559580
AOTMemoryInstance *
560581
aot_lookup_memory(AOTModuleInstance *module_inst, char const *name);
561582

562583
AOTMemoryInstance *
563584
aot_get_default_memory(AOTModuleInstance *module_inst);
564585

565586
AOTMemoryInstance *
566-
aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index);
587+
aot_get_memory_with_idx(AOTModuleInstance *module_inst, uint32 mem_idx);
567588

568589
/**
569590
* Get a function in the AOT module instance.
@@ -574,7 +595,7 @@ aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index);
574595
* @return the function instance found
575596
*/
576597
AOTFunctionInstance *
577-
aot_get_function_instance(AOTModuleInstance *module_inst, uint32_t func_idx);
598+
aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx);
578599

579600
/**
580601
* Call the given AOT function of a AOT module instance with

core/iwasm/common/wasm_c_api.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3383,21 +3383,8 @@ wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params,
33833383
if (!(func_comm_rt = func->func_comm_rt)) {
33843384
AOTModuleInstance *inst_aot =
33853385
(AOTModuleInstance *)func->inst_comm_rt;
3386-
AOTModule *module_aot = (AOTModule *)inst_aot->module;
3387-
uint32 export_i = 0, export_func_j = 0;
3388-
3389-
for (; export_i < module_aot->export_count; ++export_i) {
3390-
AOTExport *export = module_aot->exports + export_i;
3391-
if (export->kind == EXPORT_KIND_FUNC) {
3392-
if (export->index == func->func_idx_rt) {
3393-
func_comm_rt =
3394-
aot_lookup_function(inst_aot, export->name);
3395-
((wasm_func_t *)func)->func_comm_rt = func_comm_rt;
3396-
break;
3397-
}
3398-
export_func_j++;
3399-
}
3400-
}
3386+
func_comm_rt = ((wasm_func_t *)func)->func_comm_rt =
3387+
aot_lookup_function_with_idx(inst_aot, func->func_idx_rt);
34013388
}
34023389
#endif
34033390
}

core/iwasm/common/wasm_memory.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,8 +1579,7 @@ wasm_runtime_get_memory(WASMModuleInstanceCommon *module_inst, uint32 index)
15791579

15801580
#if WASM_ENABLE_AOT != 0
15811581
if (module_inst->module_type == Wasm_Module_AoT)
1582-
return aot_get_memory_with_index((AOTModuleInstance *)module_inst,
1583-
index);
1582+
return aot_get_memory_with_idx((AOTModuleInstance *)module_inst, index);
15841583
#endif
15851584

15861585
return NULL;

0 commit comments

Comments
 (0)