From ba30006e1d7e91ea38f814ad2b867269c4cccc36 Mon Sep 17 00:00:00 2001 From: David Huculak Date: Tue, 2 Apr 2024 03:32:22 -0400 Subject: [PATCH] Add js import enum benchmark (#3906) --- .gitignore | 1 + benchmarks/Cargo.toml | 5 +++-- benchmarks/README.md | 11 ++--------- benchmarks/globals.js | 11 +++++++++++ benchmarks/index.html | 14 ++++++++++++++ benchmarks/index.js | 2 ++ benchmarks/src/lib.rs | 18 ++++++++++++++++++ 7 files changed, 51 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 8cdf73b9e4f..01d711fac1a 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ yarn.lock /publish.exe .vscode webdriver.json +benchmarks/pkg diff --git a/benchmarks/Cargo.toml b/benchmarks/Cargo.toml index ecb52e96ddd..cc526bd55db 100644 --- a/benchmarks/Cargo.toml +++ b/benchmarks/Cargo.toml @@ -5,8 +5,9 @@ authors = ["The wasm-bindgen Developers"] rust-version = "1.57" [dependencies] -wasm-bindgen = "0.2.43" -web-sys = { version = "0.3.20", features = ['Node'] } +wasm-bindgen = { path = '../' } +web-sys = { path = '../crates/web-sys', features = ['Node'] } +js-sys = { path = '../crates/js-sys' } [lib] crate-type = ['cdylib'] diff --git a/benchmarks/README.md b/benchmarks/README.md index 697074addfe..ca1619cd3cd 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -11,17 +11,10 @@ performance suite for WebAssembly for Rust. ## Building and Running -First, copy the benchmarks to a temporary directory: - -``` -$ cp ./benchmarks /some/other/directory -``` - -Next, `cd` into that directory and execute: - ``` +$ cd benchmarks $ cargo build --release --target wasm32-unknown-unknown -$ wasm-bindgen --out-dir pkg --target web ./target/wasm32-unknown-unknown/release/wasm_bindgen_benchmark.wasm +$ cargo run --package wasm-bindgen-cli -- --out-dir pkg --target web ../target/wasm32-unknown-unknown/release/wasm_bindgen_benchmark.wasm ``` Next, use your favorite static file server to host the current directory. For diff --git a/benchmarks/globals.js b/benchmarks/globals.js index 0412158e34a..13c8fe14b4a 100644 --- a/benchmarks/globals.js +++ b/benchmarks/globals.js @@ -1,5 +1,16 @@ export function jsthunk() {} export function add(a, b) { return a + b; } +export function use_baz(baz) { + if (baz !== Baz['variant-2']) { + throw new Error("Passed wrong variant"); + } + } export class Foo { bar() {} } + +export const Baz = { + 'variant-1': 'variant-1', + 'variant-2': 'variant-2', + 'variant-3': 'variant-3', +} diff --git a/benchmarks/index.html b/benchmarks/index.html index 7c3736e9da1..020839f85e8 100644 --- a/benchmarks/index.html +++ b/benchmarks/index.html @@ -275,6 +275,20 @@

wasm-bindgen benchmarks

+ + + Call a custom JS function with an enum value parameter + + (?) + +

+ Benchmarks the speed of passing enum values to javascript +

+ + + + + Pass to/from wasm-bindgen diff --git a/benchmarks/index.js b/benchmarks/index.js index ac603b63c76..e74d8781da5 100644 --- a/benchmarks/index.js +++ b/benchmarks/index.js @@ -16,6 +16,7 @@ import wbindgen_init, { call_first_child_final_n_times as wbindgen_call_first_child_final_n_times, call_first_child_structural_n_times as wbindgen_call_first_child_structural_n_times, call_foo_bar_final_n_times as wbindgen_call_foo_bar_final_n_times, + call_use_baz_n_times as wbindgen_call_use_baz_n_times, call_foo_bar_structural_n_times as wbindgen_call_foo_bar_structural_n_times, str_roundtrip as wbindgen_str_roundtrip, } from './pkg/wasm_bindgen_benchmark.js'; @@ -80,6 +81,7 @@ function makeBenchmarks() { const foo = new globals.Foo(); benchmarks.wbindgen_call_foo_bar_final_n_times = () => wbindgen_call_foo_bar_final_n_times(10000, foo); benchmarks.wbindgen_call_foo_bar_structural_n_times = () => wbindgen_call_foo_bar_structural_n_times(10000, foo); + benchmarks.wbindgen_call_use_baz_n_times = () => wbindgen_call_use_baz_n_times(10000); const strings = { diff --git a/benchmarks/src/lib.rs b/benchmarks/src/lib.rs index d5caa22b653..c5abf1c560c 100644 --- a/benchmarks/src/lib.rs +++ b/benchmarks/src/lib.rs @@ -11,6 +11,9 @@ extern "C" { #[wasm_bindgen(js_name = add)] fn js_add(a: i32, b: i32) -> i32; + #[wasm_bindgen(js_name = use_baz)] + fn js_use_baz(val: Baz); + pub type Foo; #[wasm_bindgen(method, final, js_name = bar)] fn bar_final(this: &Foo); @@ -23,6 +26,14 @@ extern "C" { fn doesnt_throw_catch() -> Result<(), JsValue>; } +#[wasm_bindgen] +#[derive(Copy, Clone)] +pub enum Baz { + Variant1 = "variant-1", + Variant2 = "variant-2", + Variant3 = "variant-3", +} + #[wasm_bindgen] pub fn call_js_thunk_n_times(n: usize) { for _ in 0..n { @@ -81,6 +92,13 @@ pub fn call_foo_bar_structural_n_times(n: usize, js_foo: &Foo) { } } +#[wasm_bindgen] +pub fn call_use_baz_n_times(n: usize) { + for _ in 0..n { + js_use_baz(Baz::Variant2); + } +} + #[wasm_bindgen] pub fn call_doesnt_throw_n_times(n: usize) { for _ in 0..n {