Skip to content

Commit d0dcca2

Browse files
FIXES #137: make calling wasm from python 7x faster
1 parent c724ce1 commit d0dcca2

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

wasmtime/_func.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,19 @@ def __init__(self, store: Storelike, ty: FuncType, func: Callable, access_caller
3131
raise TypeError("expected a Store")
3232
if not isinstance(ty, FuncType):
3333
raise TypeError("expected a FuncType")
34-
self._func_call_init(ty)
34+
# init signature properties used by call
35+
self._ty = ty
36+
ty_params = ty.params
37+
ty_results = ty.results
38+
self._params_str = (str(i) for i in ty_params)
39+
self._results_str = (str(i) for i in ty_results)
40+
params_n = len(ty_params)
41+
results_n = len(ty_results)
42+
self._params_n = params_n
43+
self._results_n = results_n
44+
n = max(params_n, results_n)
45+
self._vals_raw_type = wasmtime_val_raw_t*n
46+
3547
idx = FUNCTIONS.allocate((func, ty.results, access_caller))
3648
_func = ffi.wasmtime_func_t()
3749
ffi.wasmtime_func_new(
@@ -56,19 +68,6 @@ def type(self, store: Storelike) -> FuncType:
5668
ptr = ffi.wasmtime_func_type(store._context, byref(self._func))
5769
return FuncType._from_ptr(ptr, None)
5870

59-
def _func_call_init(self, ty):
60-
self._ty = ty
61-
ty_params = ty.params
62-
ty_results = ty.results
63-
self._params_str = (str(i) for i in ty_params)
64-
self._results_str = (str(i) for i in ty_results)
65-
params_n = len(ty_params)
66-
results_n = len(ty_results)
67-
self._params_n = params_n
68-
self._results_n = results_n
69-
n = max(params_n, results_n)
70-
self._vals_raw_type = wasmtime_val_raw_t*n
71-
7271
def _create_raw_vals(self, *params: IntoVal) -> ctypes.Array[wasmtime_val_raw_t]:
7372
raw = self._vals_raw_type()
7473
for i, param_str in enumerate(self._params_str):
@@ -109,7 +108,7 @@ def __call__(self, store: Storelike, *params: IntoVal) -> Union[IntoVal, Sequenc
109108
# according to https://docs.wasmtime.dev/c-api/func_8h.html#a3b54596199641a8647a7cd89f322966f
110109
# it's safe to call wasmtime_func_call_unchecked because
111110
# - we allocate enough space to hold all the parameters and all the results
112-
# - we set proper types
111+
# - we set proper types by reading types from ty
113112
# - but not sure about "Values such as externref and funcref are valid within the store being called"
114113
with enter_wasm(store) as trap:
115114
error = None

0 commit comments

Comments
 (0)