@@ -1096,11 +1096,11 @@ aot_get_default_memory(AOTModuleInstance *module_inst)
10961096}
10971097
10981098AOTMemoryInstance *
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
11061106static 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+
12851293AOTFunctionInstance *
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 );
0 commit comments