Skip to content

Commit

Permalink
chore(ext/webidl): Add dictionary converter microbenchmark (denoland#…
Browse files Browse the repository at this point in the history
…17435)

This commits add a `webidl.createDictionaryConverter` converter
microbenchmark.

There are 2 PRs currently open that need a microbenchmark for webidl
dictionary converter. See denoland#16594
and denoland#16407

Closes denoland#17436
  • Loading branch information
littledivy committed Jan 15, 2023
1 parent a4c98e3 commit 8f321a8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions ext/webidl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ path = "lib.rs"

[dependencies]
deno_core.workspace = true

[dev-dependencies]
deno_bench_util.workspace = true

[[bench]]
name = "dict"
harness = false
35 changes: 35 additions & 0 deletions ext/webidl/benches/dict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

// deno-lint-ignore-file

const { createDictionaryConverter, converters } = globalThis.__bootstrap.webidl;

const TextDecodeOptions = createDictionaryConverter(
"TextDecodeOptions",
[
{
key: "stream",
converter: converters.boolean,
defaultValue: false,
},
],
);

// Sanity check
{
const o = TextDecodeOptions(undefined);
if (o.stream !== false) {
throw new Error("Unexpected stream value");
}
}

function handwrittenConverter(V) {
const defaultValue = { stream: false };
if (V === undefined || V === null) {
return defaultValue;
}
if (V.stream !== undefined) {
defaultValue.stream = !!V.stream;
}
return defaultValue;
}
42 changes: 42 additions & 0 deletions ext/webidl/benches/dict.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use deno_bench_util::bench_js_sync;
use deno_bench_util::bench_or_profile;
use deno_bench_util::bencher::benchmark_group;
use deno_bench_util::bencher::Bencher;

use deno_core::Extension;

fn setup() -> Vec<Extension> {
vec![
deno_webidl::init(),
Extension::builder("deno_webidl_bench")
.js(vec![("setup", include_str!("dict.js"))])
.build(),
]
}

fn converter_undefined(b: &mut Bencher) {
bench_js_sync(b, r#"TextDecodeOptions(undefined);"#, setup);
}

fn handwritten_baseline_undefined(b: &mut Bencher) {
bench_js_sync(b, r#"handwrittenConverter(undefined)"#, setup);
}

fn converter_object(b: &mut Bencher) {
bench_js_sync(b, r#"TextDecodeOptions({});"#, setup);
}

fn handwritten_baseline_object(b: &mut Bencher) {
bench_js_sync(b, r#"handwrittenConverter({})"#, setup);
}

benchmark_group!(
benches,
converter_undefined,
handwritten_baseline_undefined,
converter_object,
handwritten_baseline_object,
);
bench_or_profile!(benches);

0 comments on commit 8f321a8

Please sign in to comment.