Skip to content

Commit

Permalink
perf(wasmtime) avoid heap allocations in most calls
Browse files Browse the repository at this point in the history
Perform adjustment of arguments and return values in the stack
for the common case of functions with fewer than 10 arguments
and/or 10 returns.
  • Loading branch information
hishamhm committed Sep 25, 2023
1 parent d295645 commit 58f8960
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/wasm/wrt/ngx_wrt_wasmtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <ngx_wasi.h>


#define NGX_WRT_WASMTIME_STACK_ARGS 10
#define NGX_WRT_WASMTIME_STACK_RETS 10


typedef void (*wasmtime_config_set_int_pt)(wasm_config_t *config,
uint64_t value);
typedef void (*wasmtime_config_set_bool_pt)(wasm_config_t *config, bool value);
Expand Down Expand Up @@ -611,6 +615,9 @@ ngx_wasmtime_call(ngx_wrt_instance_t *instance, ngx_str_t *func_name,
wasmtime_extern_t item;
wasmtime_func_t *func;
wasmtime_val_t *wargs = NULL, *wrets = NULL;
wasmtime_val_t swargs[NGX_WRT_WASMTIME_STACK_ARGS];
wasmtime_val_t swrets[NGX_WRT_WASMTIME_STACK_RETS];


if (!wasmtime_instance_export_get(instance->store->context,
&instance->instance,
Expand All @@ -624,16 +631,28 @@ ngx_wasmtime_call(ngx_wrt_instance_t *instance, ngx_str_t *func_name,

func = &item.of.func;

wargs = ngx_pcalloc(instance->pool, sizeof(wasmtime_val_t) * args->size);
if (wargs == NULL) {
goto done;
if (args->size <= NGX_WRT_WASMTIME_STACK_ARGS) {
wargs = &swargs[0];

} else {
wargs = ngx_pcalloc(instance->pool,
sizeof(wasmtime_val_t) * args->size);
if (wargs == NULL) {
goto done;
}
}

ngx_wasm_valvec2wasmtime(wargs, args);

wrets = ngx_pcalloc(instance->pool, sizeof(wasmtime_val_t) * rets->size);
if (wrets == NULL) {
goto done;
if (rets->size <= NGX_WRT_WASMTIME_STACK_RETS) {
wrets = &swrets[0];

} else {
wrets = ngx_pcalloc(instance->pool,
sizeof(wasmtime_val_t) * rets->size);
if (wrets == NULL) {
goto done;
}
}

err->res = wasmtime_func_call(instance->store->context,
Expand All @@ -652,11 +671,11 @@ ngx_wasmtime_call(ngx_wrt_instance_t *instance, ngx_str_t *func_name,

done:

if (wargs) {
if (args->size > NGX_WRT_WASMTIME_STACK_ARGS && wargs) {
ngx_pfree(instance->pool, wargs);
}

if (wrets) {
if (rets->size > NGX_WRT_WASMTIME_STACK_RETS && wrets) {
ngx_pfree(instance->pool, wrets);
}

Expand Down

0 comments on commit 58f8960

Please sign in to comment.