Repository navigation
perf(napi): avoid zero-initializing string conversion buffers - #3708
Conversation
|
Caution Review failedThe pull request is closed. No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
a55408e to
1ed0b19
Compare
Allocate the buffers that `napi_get_value_string_*` writes into with `Vec::with_capacity` and `set_len` to the count N-API reports, instead of `vec![0; len]` + `truncate`. N-API writes exactly that many elements plus a NUL terminator, so the zeroing was never observed. For mostly-ASCII strings of 1 KiB and up this saves about 12-20% of the string conversion cost. Short strings and non-ASCII strings see little or no difference. `JsString::into_utf16` now sizes `buf` to the units N-API wrote plus the NUL terminator, rather than the queried length plus one. These are equal in practice, but only the former is guaranteed to be initialized. A wrong count from N-API is now unsound rather than harmless, so each `set_len` is preceded by a `debug_assert!` on it. Add tests for empty, one-unit, non-BMP and long (> 1 KiB) strings through `String`, `Utf16String`, `Latin1String` and `JsString::into_utf8` / `into_utf16`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1ed0b19 to
8ef8254
Compare
Avoid zero-initializing buffers that
napi_get_value_string_*immediately overwrites with data.vec![0; len]+Vec::truncateafter the write.Vec::with_capacity+Vec::set_lenafter the buffer is filled.set_lenis passed the count N-API reports.For mostly-ASCII strings of 1 KiB and up this saves about 12-20% of the string conversion cost. Short strings and non-ASCII strings see little or no difference.
JsString::into_utf16now sizesbufto the units N-API wrote plus the NUL terminator, rather than the queried length plus one. These are equal in practice, but only the former is guaranteed to be initialized.A wrong count from N-API is now unsound rather than harmless, so each
set_lenis preceded by adebug_assert!on it. Adds tests for empty, one-unit, non-BMP and long (> 1 KiB) strings throughString,Utf16String,Latin1StringandJsString::into_utf8/into_utf16.Based on top of #3707.
Summary by CodeRabbit