Skip to content

perf(napi): avoid zero-initializing string conversion buffers - #3708

Merged
Brooooooklyn merged 1 commit into
napi-rs:mainfrom
overlookmotel:om/11-16-perf_napi_avoid_zero-initializing_string_conversion_buffers
Nov 19, 2026
Merged

Brooooooklyn merged 1 commit into
napi-rs:mainfrom
overlookmotel:om/11-16-perf_napi_avoid_zero-initializing_string_conversion_buffers

Conversation

@overlookmotel

@overlookmotel overlookmotel commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Avoid zero-initializing buffers that napi_get_value_string_* immediately overwrites with data.

  • Before: vec![0; len] + Vec::truncate after the write.
  • After: Vec::with_capacity + Vec::set_len after the buffer is filled.

set_len is 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_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. Adds tests for empty, one-unit, non-BMP and long (> 1 KiB) strings through String, Utf16String, Latin1String and JsString::into_utf8 / into_utf16.

Based on top of #3707.

Summary by CodeRabbit

  • Improvements
    • String conversions for UTF-8, UTF-16, and Latin-1 now handle empty, short, long, and Unicode strings with more efficient buffer allocation.
  • Tests
    • Added coverage for string concatenation across empty inputs, non-ASCII characters, and longer strings in both compatibility and standard modes. These checks verify that converted strings retain their original content and receive the expected suffix.

Copilot AI balanced review requested due to automatic review settings November 16, 2026 14:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

No actionable comments were generated in the recent review.

Walkthrough

String conversions now allocate buffers without initializing their contents. They set vector lengths from N-API write counts and retain the required encoding-specific terminator handling. Tests add coverage for empty, Unicode, and long strings.

Changes: String buffer conversions

Layer / File(s) Summary
N-API string reads
crates/napi/src/bindgen_runtime/js_values/string.rs, crates/napi/src/error.rs, examples/napi-compat-mode/__tests__/string.spec.ts
The UTF-8, UTF-16, and Latin-1 conversions and owned_string_without_coercion use capacity-only vectors and set their lengths from the N-API write counts. Tests cover empty, short, Unicode, and long inputs.
JsString output buffers
crates/napi/src/js_values/string/mod.rs, examples/napi/__tests__/values.spec.ts
into_utf8, into_utf16, and into_latin1 use capacity-only vectors and set their lengths from the reported counts, with encoding-specific terminator handling. Tests cover empty, short, non-BMP, and long inputs.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related PRs

  • napi-rs/napi-rs#3710: Changes JsStringUtf8 to store a String and uses the UTF-8 buffer contents and written-length contract changed here.
  • napi-rs/napi-rs#3709: Changes owned_string_without_coercion to use unchecked UTF-8 conversion, relying on this PR’s buffer length handling.
  • napi-rs/napi-rs#3707: Changes the same JsString::into_utf8 and into_latin1 buffer code.

Suggested reviewers: brooooooklyn

Merge Risk: Minimal · previous review 1ed0b

This change avoids zero-initializing string conversion buffers, which should reduce conversion cost for long strings. I found no concrete behavioral regression, and the new tests cover the main edge cases. It looks safe to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "perf(napi): avoid zero-initializing string conversion buffers" matches the main change. The PR replaces zero-filled buffers (vec![0; len]) with Vec::with_capacity and Vec::set_len in the UT…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "perf(napi): avoid zero-initializing string conversion buffers" matches the main change. The PR replaces zero-filled buffers (vec![0; len]) with Vec::with_capacity and Vec::set_len in the UTF-8, UTF-16, and Latin-1 string conversions in crates/napi, which is the stated objective of PR #3708. The title is concise, specific, and follows a conventional commit style.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ed3f826a-c352-46c4-99b4-c50be19dfdab
Commits

Reviewing files that changed from the base of the PR and between a55408e and 1ed0b19.

Files selected for processing: 5

  • crates/napi/src/bindgen_runtime/js_values/string.rs
  • crates/napi/src/error.rs
  • crates/napi/src/js_values/string/mod.rs
  • examples/napi-compat-mode/__tests__/string.spec.ts
  • examples/napi/__tests__/values.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

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]>
@Brooooooklyn
Brooooooklyn force-pushed the om/11-16-perf_napi_avoid_zero-initializing_string_conversion_buffers branch from 1ed0b19 to 8ef8254 Compare November 19, 2026 15:04
@Brooooooklyn
Brooooooklyn merged commit d1bfe66 into napi-rs:main Nov 19, 2026
This was referenced Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants