perf(ext/node): write UTF-16LE buffers with V8 - #36804
Open
joepjoosten wants to merge 1 commit into
Open
Conversation
This was referenced Sep 8, 2026
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.
Fixes #36803.
What and why
Buffer.from(text, "utf16le")currently converts the string through a JS per-code-unit loop into a temporary Uint8Array, then copies that into the Buffer. This is slower than a straightforward DataView loop on medium/large inputs.This change adds a native op using V8's UTF-16 string write API and routes the normal
ucs2Writepath through it:Buffer.alloc(3).write("A", 0, 1, "utf16le")now returns 0, matching Node.Related: #35182 also ports internal binding helpers to Rust. This PR is narrower: direct destination writes for Buffer UTF-16LE encoding, with focused compatibility tests and benchmarks. It is based on current main, not that branch.
Benchmarks
Apple M2 Max, macOS ARM64, Deno 2.9.6 / V8 15.0.245.2-rusty. Baseline:
336da420f4343cbb1dcbd5eed9d075ff555ed6ee. Both baseline and candidate were built locally with Rust 1.95.0 using the samerelease-liteprofile,CARGO_INCREMENTAL=0, andCARGO_PROFILE_RELEASE_LITE_DEBUG=0.Ran the committed
tests/bench/buffer_utf16le.jsunchanged on both binaries, three times each, sequentially in alternating order (baseline/candidate, candidate/baseline, baseline/candidate), after builds/lint finished. Each value below is the median of three Deno.bench run averages, not a latency percentile. Sizes are UTF-16LE output bytes.For the 64 KiB mixed UTF-16 case:
Buffer.writeinto a reused destination: 232.054 → 1.440 µs (~161×).Buffer.write: 232.153 → 72.349 µs (~3.2×).Buffer.fromrun averages were 232.900 / 231.448 / 233.129 µs before and 2.554 / 2.584 / 2.657 µs after.These are warm, encoding-only microbenchmarks, not application-wide speedups. Full output equality is checked before timing. Timed
Buffer.fromcases allocate output and consume a byte; write-only cases reuse a destination and consume the returned byte count. Performance for other string representations, architectures, and sizes may differ.Reproduce on baseline and candidate binaries:
Validation
cargo build --locked --profile release-lite -p deno --bin denocargo build --locked --profile release-lite -p test_server --bin test_servercargo test --locked --profile release-lite -p unit_node_tests --test unit_node -- buffer_testcargo test --locked --profile release-lite -p node_compat_tests --test node_compat -- test-buffer-write.jsagainst the candidate binarytools/format.jsandtools/format.js --checktools/lint.js(full Rust + JS) and a finaltools/lint.js --jsrerungit diff --checkNew tests cover every UTF-16 code unit across all four encoding aliases, empty strings, NULs, lone/split surrogates, odd lengths/offsets, sliced destination boundaries, large unaligned writes spanning scratch-buffer iterations, and shared backing stores. The new test cases also pass against Node. Big-endian hardware was not available locally.
AI assistance: investigation, implementation, tests, and benchmark analysis were completed with OpenAI Codex.