Skip to content

Commit

Permalink
chore(ext/webidl): Add dictionary converter microbenchmark (#17435)
Browse files Browse the repository at this point in the history
This commits add a `webidl.createDictionaryConverter` converter
microbenchmark.

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

Closes #17436
  • Loading branch information
littledivy authored and bartlomieju committed Jan 16, 2023
1 parent 1a06380 commit 7f65a5d
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 7f65a5d

Please sign in to comment.