Skip to content

Commit

Permalink
Add js import enum benchmark (#3906)
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidster committed Apr 2, 2024
1 parent a9f0670 commit 9347af3
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ yarn.lock
/publish.exe
.vscode
webdriver.json
benchmarks/pkg
5 changes: 3 additions & 2 deletions benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
11 changes: 2 additions & 9 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions benchmarks/globals.js
Original file line number Diff line number Diff line change
@@ -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',
}
14 changes: 14 additions & 0 deletions benchmarks/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,20 @@ <h1>wasm-bindgen benchmarks</h1>
<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
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = {
Expand Down
18 changes: 18 additions & 0 deletions benchmarks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9347af3

Please sign in to comment.