Add js import enum benchmark (#3906)

This commit is contained in:
David Huculak
2024-04-02 03:32:22 -04:00
committed by GitHub
parent a9f06701c7
commit 9347af3b51
7 changed files with 51 additions and 11 deletions

1
.gitignore vendored
View File

@@ -10,3 +10,4 @@ yarn.lock
/publish.exe
.vscode
webdriver.json
benchmarks/pkg

View File

@@ -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']

View File

@@ -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

View File

@@ -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',
}

View File

@@ -275,6 +275,20 @@ table td {
<td class='bm' id='wbindgen_call_foo_bar_structural_n_times'></td>
</tr>
<tr>
<td>
Call a custom JS function with an enum value parameter
<a class='about-open' href='#'>(?)</a>
<p class='about'>
Benchmarks the speed of passing enum values to javascript
</p>
</td>
<td class='bm' id='wbindgen_call_use_baz_n_times'></td>
</tr>
<tr style='display:none' class='str-benchmark'>
<td>
Pass <span class='str'></span> to/from wasm-bindgen

View File

@@ -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 = {

View File

@@ -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 {