From d0dcca215fd46886e96bbe9a33646595db57e2f9 Mon Sep 17 00:00:00 2001 From: Muayyad alsadi Date: Sun, 26 Mar 2023 01:53:21 +0300 Subject: [PATCH] FIXES #137: make calling wasm from python 7x faster --- wasmtime/_func.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/wasmtime/_func.py b/wasmtime/_func.py index af276bf5..084458b8 100644 --- a/wasmtime/_func.py +++ b/wasmtime/_func.py @@ -31,7 +31,19 @@ def __init__(self, store: Storelike, ty: FuncType, func: Callable, access_caller raise TypeError("expected a Store") if not isinstance(ty, FuncType): raise TypeError("expected a FuncType") - self._func_call_init(ty) + # init signature properties used by call + self._ty = ty + ty_params = ty.params + ty_results = ty.results + self._params_str = (str(i) for i in ty_params) + self._results_str = (str(i) for i in ty_results) + params_n = len(ty_params) + results_n = len(ty_results) + self._params_n = params_n + self._results_n = results_n + n = max(params_n, results_n) + self._vals_raw_type = wasmtime_val_raw_t*n + idx = FUNCTIONS.allocate((func, ty.results, access_caller)) _func = ffi.wasmtime_func_t() ffi.wasmtime_func_new( @@ -56,19 +68,6 @@ def type(self, store: Storelike) -> FuncType: ptr = ffi.wasmtime_func_type(store._context, byref(self._func)) return FuncType._from_ptr(ptr, None) - def _func_call_init(self, ty): - self._ty = ty - ty_params = ty.params - ty_results = ty.results - self._params_str = (str(i) for i in ty_params) - self._results_str = (str(i) for i in ty_results) - params_n = len(ty_params) - results_n = len(ty_results) - self._params_n = params_n - self._results_n = results_n - n = max(params_n, results_n) - self._vals_raw_type = wasmtime_val_raw_t*n - def _create_raw_vals(self, *params: IntoVal) -> ctypes.Array[wasmtime_val_raw_t]: raw = self._vals_raw_type() for i, param_str in enumerate(self._params_str): @@ -109,7 +108,7 @@ def __call__(self, store: Storelike, *params: IntoVal) -> Union[IntoVal, Sequenc # according to https://docs.wasmtime.dev/c-api/func_8h.html#a3b54596199641a8647a7cd89f322966f # it's safe to call wasmtime_func_call_unchecked because # - we allocate enough space to hold all the parameters and all the results - # - we set proper types + # - we set proper types by reading types from ty # - but not sure about "Values such as externref and funcref are valid within the store being called" with enter_wasm(store) as trap: error = None