-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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(web): optimize single pass utf8 decoding #16593
Changes from all commits
8c772e0
b5dde43
60b940f
3d2db48
ea9bd20
8a8af85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,14 +16,14 @@ | |||||||||
const ops = core.ops; | ||||||||||
const webidl = window.__bootstrap.webidl; | ||||||||||
const { | ||||||||||
ArrayBufferIsView, | ||||||||||
ObjectPrototypeIsPrototypeOf, | ||||||||||
PromiseReject, | ||||||||||
PromiseResolve, | ||||||||||
StringPrototypeCharCodeAt, | ||||||||||
StringPrototypeSlice, | ||||||||||
TypedArrayPrototypeSubarray, | ||||||||||
Uint8Array, | ||||||||||
ObjectPrototypeIsPrototypeOf, | ||||||||||
ArrayBufferIsView, | ||||||||||
Uint32Array, | ||||||||||
} = window.__bootstrap.primordials; | ||||||||||
|
||||||||||
|
@@ -34,6 +34,8 @@ | |||||||||
#fatal; | ||||||||||
/** @type {boolean} */ | ||||||||||
#ignoreBOM; | ||||||||||
/** @type {boolean} */ | ||||||||||
#utf8SinglePass; | ||||||||||
|
||||||||||
/** @type {number | null} */ | ||||||||||
#rid = null; | ||||||||||
|
@@ -56,6 +58,7 @@ | |||||||||
this.#encoding = encoding; | ||||||||||
this.#fatal = options.fatal; | ||||||||||
this.#ignoreBOM = options.ignoreBOM; | ||||||||||
this.#utf8SinglePass = encoding === "utf-8" && !options.fatal; | ||||||||||
this[webidl.brand] = webidl.brand; | ||||||||||
} | ||||||||||
|
||||||||||
|
@@ -81,7 +84,7 @@ | |||||||||
* @param {BufferSource} [input] | ||||||||||
* @param {TextDecodeOptions} options | ||||||||||
*/ | ||||||||||
decode(input = new Uint8Array(), options = {}) { | ||||||||||
decode(input = new Uint8Array(), options = undefined) { | ||||||||||
webidl.assertBranded(this, TextDecoderPrototype); | ||||||||||
const prefix = "Failed to execute 'decode' on 'TextDecoder'"; | ||||||||||
if (input !== undefined) { | ||||||||||
|
@@ -91,40 +94,46 @@ | |||||||||
allowShared: true, | ||||||||||
}); | ||||||||||
} | ||||||||||
options = webidl.converters.TextDecodeOptions(options, { | ||||||||||
prefix, | ||||||||||
context: "Argument 2", | ||||||||||
}); | ||||||||||
let stream = false; | ||||||||||
if (options !== undefined) { | ||||||||||
options = webidl.converters.TextDecodeOptions(options, { | ||||||||||
prefix, | ||||||||||
context: "Argument 2", | ||||||||||
}); | ||||||||||
stream = options.stream; | ||||||||||
} | ||||||||||
|
||||||||||
try { | ||||||||||
try { | ||||||||||
if (ArrayBufferIsView(input)) { | ||||||||||
input = new Uint8Array( | ||||||||||
input.buffer, | ||||||||||
input.byteOffset, | ||||||||||
input.byteLength, | ||||||||||
); | ||||||||||
} else { | ||||||||||
input = new Uint8Array(input); | ||||||||||
} | ||||||||||
} catch { | ||||||||||
// If the buffer is detached, just create a new empty Uint8Array. | ||||||||||
input = new Uint8Array(); | ||||||||||
} | ||||||||||
// Note from spec: implementations are strongly encouraged to use an implementation strategy that avoids this copy. | ||||||||||
// When doing so they will have to make sure that changes to input do not affect future calls to decode(). | ||||||||||
if ( | ||||||||||
ObjectPrototypeIsPrototypeOf( | ||||||||||
SharedArrayBuffer.prototype, | ||||||||||
Comment on lines
110
to
111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we please defer these primordials changes? I don't want to make the code any slower There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure; I'll introduce these next month, myself. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets see what others think. i personally don't like primordials in fast paths for mitigating a hypothetical case. Node.js has also removed primoridals in hot paths previously nodejs/node#38248 |
||||||||||
input.buffer, | ||||||||||
input || input.buffer, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is polymorphic |
||||||||||
) | ||||||||||
) { | ||||||||||
// We clone the data into a non-shared ArrayBuffer so we can pass it | ||||||||||
// to Rust. | ||||||||||
// `input` is now a Uint8Array, and calling the TypedArray constructor | ||||||||||
// with a TypedArray argument copies the data. | ||||||||||
input = new Uint8Array(input); | ||||||||||
if (ArrayBufferIsView(input)) { | ||||||||||
input = new Uint8Array( | ||||||||||
input.buffer, | ||||||||||
input.byteOffset, | ||||||||||
input.byteLength, | ||||||||||
Comment on lines
+121
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well... now these are all polymorphic... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wasn't this already the case before this patch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed! ...No one fixed it |
||||||||||
); | ||||||||||
} else { | ||||||||||
input = new Uint8Array(input); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
if (!options.stream && this.#rid === null) { | ||||||||||
// Fast path for single pass encoding. | ||||||||||
if (!stream && this.#rid === null) { | ||||||||||
// Fast path for utf8 single pass encoding. | ||||||||||
if (this.#utf8SinglePass) { | ||||||||||
return ops.op_encoding_decode_utf8(input, this.#ignoreBOM); | ||||||||||
} | ||||||||||
|
||||||||||
return ops.op_encoding_decode_single( | ||||||||||
input, | ||||||||||
this.#encoding, | ||||||||||
|
@@ -140,9 +149,9 @@ | |||||||||
this.#ignoreBOM, | ||||||||||
); | ||||||||||
} | ||||||||||
return ops.op_encoding_decode(input, this.#rid, options.stream); | ||||||||||
return ops.op_encoding_decode(input, this.#rid, stream); | ||||||||||
} finally { | ||||||||||
if (!options.stream && this.#rid !== null) { | ||||||||||
if (!stream && this.#rid !== null) { | ||||||||||
core.close(this.#rid); | ||||||||||
this.#rid = null; | ||||||||||
} | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should come up with a general improvement in webidl for cases like this. I think the optimal way would require
new Function
codegen.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what it's worth, Evan (Discord) had been saying that for over a year. The entire WebIDL bindings setup should be via JiT-compiled codegen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah :(
never too late though