perf(ext/web): optimize TextEncoder/TextDecoder hot paths#32735
Merged
bartlomieju merged 1 commit intomainfrom Mar 17, 2026
Merged
perf(ext/web): optimize TextEncoder/TextDecoder hot paths#32735bartlomieju merged 1 commit intomainfrom
bartlomieju merged 1 commit intomainfrom
Conversation
- TextEncoder.encode(): skip webidl.converters.DOMString when input is
already a string (typeof check instead of function call + type checks)
- TextDecoder.decode(): fast path for Uint8Array input, skip full
BufferSource validation (avoids ArrayBufferIsView + getBuffer +
isSharedArrayBuffer chain)
- TextDecoder constructor: fast path for common UTF-8 labels ("utf-8",
"utf8", "unicode-1-1-utf-8") to avoid op_encoding_normalize_label
Rust op call
Benchmark improvements:
- TextEncoder.encode(): +9-13%
- TextDecoder.decode(): +8-11% (medium/path sizes)
- new TextDecoder(): +25-27%
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Optimizes the JS-side hot paths for
TextEncoder.encode(),TextDecoder.decode(), andnew TextDecoder()by skipping unnecessary webidl conversion overhead for the most common argument types.Benchmark results (2M iterations, macOS ARM64)
TextEncoder.encode()(13 chars)TextEncoder.encode()(85 chars)TextEncoder.encode()(1020 chars)TextDecoder.decode()(85 bytes)TextDecoder.decode()(49 bytes)new TextDecoder()new TextDecoder("utf-8")Changes
TextEncoder.encode(): Skipwebidl.converters.DOMStringwhen input is already a string (typeofcheck instead of function call + type dispatch). The converter ultimately returns the string unchanged for string inputs, so the check is redundant.TextDecoder.decode(): Add fast path forUint8Arrayinput (the most common case). ChecksTypedArrayPrototypeGetSymbolToStringTagdirectly instead of going through the fullwebidl.converters.BufferSourcevalidation chain (ArrayBufferIsView→getBuffer→isSharedArrayBuffer). Falls through to the full validation for other BufferSource types.new TextDecoder()constructor: Short-circuitop_encoding_normalize_labelfor the three standard UTF-8 labels ("utf-8","utf8","unicode-1-1-utf-8"). This avoids crossing the JS→Rust boundary entirely for the most common encoding.Test plan
🤖 Generated with Claude Code