Skip to content

Commit

Permalink
FIXES bytecodealliance#137: make calling wasm from python 7x faster
Browse files Browse the repository at this point in the history
  • Loading branch information
muayyad-alsadi committed Mar 25, 2023
1 parent c724ce1 commit d0dcca2
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions wasmtime/_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d0dcca2

Please sign in to comment.