From 5bb5c3902ed904a0ea0b971f2e83d4d52b7d3d43 Mon Sep 17 00:00:00 2001 From: Muayyad alsadi Date: Wed, 5 Apr 2023 03:14:59 +0300 Subject: [PATCH] FIXES #137: make calling wasm from python 7x faster --- examples/gcd_perf.py | 7 ++++++- examples/simd_i8x16.py | 4 +++- tests/test_func.py | 4 +++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/examples/gcd_perf.py b/examples/gcd_perf.py index f7b09879..816f4660 100644 --- a/examples/gcd_perf.py +++ b/examples/gcd_perf.py @@ -1,14 +1,19 @@ -import wasmtime.loader import time from math import gcd as math_gcd from gcd import gcd_func as wasm_gcd + def python_gcd(x, y): while y: x, y = y, x % y return abs(x) +a = 16516842 +b = 154654684 + +print(math_gcd(a, b), python_gcd(a, b), wasm_gcd(a, b)) + N = 1_000 by_name = locals() for name in "math_gcd", "python_gcd", "wasm_gcd": diff --git a/examples/simd_i8x16.py b/examples/simd_i8x16.py index 2b49eb90..407836b5 100644 --- a/examples/simd_i8x16.py +++ b/examples/simd_i8x16.py @@ -25,7 +25,9 @@ instance = Instance(store, module, []) vector_type = ctypes.c_uint8 * 16 -add_v128_f: Func = instance.exports(store)["add_v128"] +add_v128_f = instance.exports(store)["add_v128"] +if not isinstance(add_v128_f, Func): + raise TypeError("expecting Func") add_v128 = partial(add_v128_f, store) a = vector_type(*(i for i in range(16))) b = vector_type(*(40 + i for i in range(16))) diff --git a/tests/test_func.py b/tests/test_func.py index 23364453..478c68db 100644 --- a/tests/test_func.py +++ b/tests/test_func.py @@ -38,7 +38,9 @@ def test_simd_i8x16_add(self): instance = Instance(store, module, []) vector_type = ctypes.c_uint8 * 16 - add_v128_f: Func = instance.exports(store)["add_v128"] + add_v128_f = instance.exports(store)["add_v128"] + if not isinstance(add_v128_f, Func): + raise TypeError("expecting Func") add_v128 = partial(add_v128_f, store) a = vector_type(*(i for i in range(16))) b = vector_type(*(40 + i for i in range(16)))