Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add js import enum benchmark #3906

Merged
merged 7 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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