Skip to content

perf(napi): owned_string_without_coercion use unchecked string conversion - #3709

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

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

Conversation

@overlookmotel

@overlookmotel overlookmotel commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

napi_get_value_string_utf8 only writes valid UTF-8, and String::from_napi_value already relies on that.

Use String::from_utf8_unchecked in owned_string_without_coercion too, instead of validating the bytes again. The None result for invalid UTF-8 could never happen.

Adds a test that captures lone surrogates (the one input where the skipped check could matter) through both paths in owned_reason_without_coercion: an Error message and a string primitive.

Based on top of #3708.

Summary by CodeRabbit

  • Bug Fixes
    • Improved string conversion reliability for empty and long strings, emoji, and other non-ASCII characters.
    • Improved handling of UTF-8, UTF-16, and Latin-1 strings, including strings containing lone surrogates.

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

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

UTF-8, UTF-16, and Latin-1 string conversions now allocate vector capacity without initializing the full buffer. After successful N-API calls, they set vector length to the written data. Tests cover string concatenation and lone-surrogate handling.

Changes: String buffer conversions

Layer / File(s) Summary
String conversion buffers
crates/napi/src/js_values/string/mod.rs, crates/napi/src/bindgen_runtime/js_values/string.rs, crates/napi/src/error.rs
Conversion methods and the no-coercion error-string helper reserve vector capacity and set vector length to the written data. UTF-16 retains its terminator; UTF-8 and Latin-1 exclude it.
String conversion tests
examples/napi-compat-mode/__tests__/string.spec.ts, examples/napi/__tests__/values.spec.ts
Tests cover empty, short, non-BMP, and long string inputs. Additional captured-value tests check lone-surrogate handling.

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

Change: Refactor

Possibly related PRs

  • napi-rs/napi-rs#3708: Changes buffer allocation in the same N-API string conversion paths.
  • napi-rs/napi-rs#3710: Changes UTF-8 string storage and validation based on the same N-API UTF-8 guarantee.
  • napi-rs/napi-rs#3707: Removes raw-parts vector reconstruction in related JsString::into conversion paths.

Suggested reviewers: brooooooklyn

Merge Risk: Minimal · previous review 41a73

This change avoids zero-filling and re-validating string buffers in N-API string conversions, which should improve performance without changing behavior. The inspected paths show no concrete defect, and the added tests cover the edge cases. The remaining risk is low and mainly depends on CI confirming the runtime tests.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: owned_string_without_coercion switches to an unchecked UTF-8 conversion. The raw summary confirms this. The error.rs file now builds the String with no validation, and…
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 names the main change: owned_string_without_coercion switches to an unchecked UTF-8 conversion. The raw summary confirms this. The error.rs file now builds the String with no validation, and the PR objectives describe the same change. The title does not mention the related buffer changes in string.rs and js_values/string/mod.rs, which is acceptable because the title need not cover every detail.

  • 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: 4a075cc8-b6eb-4d24-a4b2-5199b6064d85
Commits

Reviewing files that changed from the base of the PR and between 5293b42 and 41a7389.

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; 4 remain after this review.

@Brooooooklyn
Brooooooklyn force-pushed the om/11-16-perf_napi_owned_string_without_coercion_use_unchecked_string_conversion branch 2 times, most recently from 5293b42 to 41a7389 Compare November 19, 2026 06:48
…ersion

`napi_get_value_string_utf8` only writes valid UTF-8, and `String::from_napi_value` already relies on that. Use `String::from_utf8_unchecked` here too, instead of validating the bytes again. The `None` result for invalid UTF-8 could never happen.

Add a test that captures lone surrogates, the one input where the skipped check could matter, through both paths in `owned_reason_without_coercion` (an `Error` message and a string primitive).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the om/11-16-perf_napi_owned_string_without_coercion_use_unchecked_string_conversion branch from 41a7389 to 34a5237 Compare November 19, 2026 15:04
@Brooooooklyn
Brooooooklyn merged commit 6b6e75c 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