Refine wasm_runtime_call_wasm_a/v (#1326)

Refine wasm_runtime_call_wasm_a/v by adding cache buf
for arguments/results to avoid allocating memory frequently.
This commit is contained in:
Wenyong Huang 2022-07-25 13:53:42 +08:00 committed by GitHub
parent dd62b32b20
commit 8de5168cea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1789,10 +1789,11 @@ wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
uint32 num_results, wasm_val_t results[],
uint32 num_args, wasm_val_t args[])
{
uint32 argc, *argv, cell_num, total_size, module_type;
uint32 argc, argv_buf[16] = { 0 }, *argv = argv_buf, cell_num, module_type;
#if WASM_ENABLE_REF_TYPES != 0
uint32 i, param_size_in_double_world = 0, result_size_in_double_world = 0;
#endif
uint64 total_size;
WASMType *type;
bool ret = false;
@ -1836,12 +1837,12 @@ wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
}
total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
if (!(argv = runtime_malloc((uint32)total_size, exec_env->module_inst, NULL,
0))) {
wasm_runtime_set_exception(exec_env->module_inst,
"allocate memory failed");
if (total_size > sizeof(argv_buf)) {
if (!(argv =
runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
goto fail1;
}
}
parse_args_to_uint32_array(type, args, argv);
if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv)))
@ -1850,6 +1851,7 @@ wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
parse_uint32_array_to_results(type, argv, results);
fail2:
if (argv != argv_buf)
wasm_runtime_free(argv);
fail1:
return ret;
@ -1861,9 +1863,10 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
uint32 num_results, wasm_val_t results[],
uint32 num_args, ...)
{
wasm_val_t *args = NULL;
wasm_val_t args_buf[8] = { 0 }, *args = args_buf;
WASMType *type = NULL;
bool ret = false;
uint64 total_size;
uint32 i = 0, module_type;
va_list vargs;
@ -1881,12 +1884,14 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
"function declaration.");
goto fail1;
}
total_size = sizeof(wasm_val_t) * (uint64)num_args;
if (total_size > sizeof(args_buf)) {
if (!(args =
runtime_malloc(sizeof(wasm_val_t) * num_args, NULL, NULL, 0))) {
wasm_runtime_set_exception(exec_env->module_inst,
"allocate memory failed");
runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
goto fail1;
}
}
va_start(vargs, num_args);
for (i = 0; i < num_args; i++) {
@ -1927,8 +1932,10 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
}
}
va_end(vargs);
ret = wasm_runtime_call_wasm_a(exec_env, function, num_results, results,
num_args, args);
if (args != args_buf)
wasm_runtime_free(args);
fail1: