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

perf(webidl): optimize dictionary converters #16594

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
perf(webidl): optimize dictionary converters
  • Loading branch information
littledivy committed Nov 11, 2022
commit ac426af7c67e9bc5a7735bfd5540adbdc2ede8f8
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.

53 changes: 17 additions & 36 deletions ext/webidl/00_webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,11 +671,19 @@
}
}

return function (V, opts = {}) {
return new Function(
"makeException",
"type",
"defaultValues",
"allMembers",
`return function (V, opts = {}) {
Comment on lines +685 to +690
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause the process to fail if --disallow-code-generation-from-strings flag is used (#16535)

const typeV = type(V);
let useDefault = false;
switch (typeV) {
case "Undefined":
case "Null":
useDefault = true;
break;
case "Object":
break;
default:
Expand All @@ -685,43 +693,16 @@
opts,
);
}
const esDict = V;

const idlDict = ObjectAssign({}, defaultValues);

// NOTE: fast path Null and Undefined.
if ((V === undefined || V === null) && !hasRequiredKey) {
return idlDict;
return {
${
allMembers.map((member, idx) =>
`${member.key}: (useDefault || V.${member.key} === undefined) ? defaultValues.${member.key} : allMembers[${idx}].converter(V.${member.key}, opts)`
).join(",\n")
}

for (const member of allMembers) {
const key = member.key;

let esMemberValue;
if (typeV === "Undefined" || typeV === "Null") {
esMemberValue = undefined;
} else {
esMemberValue = esDict[key];
}

if (esMemberValue !== undefined) {
const context = `'${key}' of '${name}'${
opts.context ? ` (${opts.context})` : ""
}`;
const converter = member.converter;
const idlMemberValue = converter(esMemberValue, { ...opts, context });
idlDict[key] = idlMemberValue;
} else if (member.required) {
throw makeException(
TypeError,
`can not be converted to '${name}' because '${key}' is required in '${name}'.`,
opts,
);
}
}

return idlDict;
};
};
};`,
)(makeException, type, defaultValues, allMembers);
}

// https://heycam.github.io/webidl/#es-enumeration
Expand Down
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 = { version = "0.158.0", path = "../../core" }

[dev-dependencies]
deno_bench_util = { version = "0.70.0", path = "../../bench_util" }

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

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;
}
41 changes: 41 additions & 0 deletions ext/webidl/benches/dict.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2018-2022 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, Bencher};

use deno_core::Extension;

fn setup() -> Vec<Extension> {
vec![
deno_webidl::init(),
Extension::builder()
.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);