Skip to content

perf(napi): JsStringUtf8 avoid memory copies in into_owned and take - #3711

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

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

Conversation

@overlookmotel

@overlookmotel overlookmotel commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

JsStringUtf8::into_owned and JsStringUtf8::take both take self by value, so move the buffer out instead of copying it. This saves an allocation and a copy of the whole string on every call, including when capturing error messages, which go through into_utf8().into_owned().

The returned String / Vec<u8> now has 1 byte of spare capacity, from the slot N-API wrote the NUL terminator into.

Based on top of #3710.

Summary by CodeRabbit

  • Bug Fixes

    • Improved JavaScript string conversions across UTF-8, UTF-16, and Latin-1, including empty strings, emoji, and long inputs.
    • UTF-8 string access now returns its stored string directly rather than reporting a conversion error during access.
  • Tests

    • Expanded string conversion checks, including byte-slice and ownership methods, across standard and WASI examples.

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

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

N-API string conversions now allocate buffers with capacity and set their lengths from N-API results. JsStringUtf8 stores a String. The example exports and tests UTF-8 access methods, including string, length, and byte results.

Changes: String conversion

Layer / File(s) Summary
Allocate and populate conversion buffers
crates/napi/src/js_values/string/mod.rs, crates/napi/src/bindgen_runtime/js_values/string.rs, crates/napi/src/error.rs
String conversion methods use capacity-allocated buffers and set their lengths from the units reported by N-API. The error string helper uses the reported byte count and retains its failure handling.
Store UTF-8 strings as String
crates/napi/src/js_values/string/utf8.rs
JsStringUtf8 stores a String. as_str returns its contents without UTF-8 validation. into_owned returns the stored string, and take consumes it into bytes.
Expose and test UTF-8 methods
examples/napi/src/string.rs, examples/napi/__tests__/*, examples/napi-compat-mode/__tests__/string.spec.ts, examples/napi/index.*, examples/napi/example.wasi*, examples/napi/example.wasip1*
The example returns UTF-8 string and byte access results through testUtf8Methods. Tests cover UTF-8 method results, lone surrogates, and string conversion edge cases. The example entry points and declarations expose the new method.

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

Change: Refactor

Possibly related PRs

Suggested reviewers: brooooooklyn

Merge Risk: Minimal · previous review f8da6

This change avoids a copy and an allocation when converting JS strings to owned Rust strings. I found no concrete behavioral or safety regression in the supplied changes, and the new tests cover the main edge cases. It looks ready to merge, subject to CI passing.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the change: JsStringUtf8 avoids memory copies in into_owned and take. The raw summary shows utf8.rs changing JsStringUtf8 to store a String, with into_owned returning the stored string…
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 change: JsStringUtf8 avoids memory copies in into_owned and take. The raw summary shows utf8.rs changing JsStringUtf8 to store a String, with into_owned returning the stored string and take consuming it into bytes. The PR objectives describe the same goal. The title uses the conventional perf(napi) prefix and is specific. The title does not mention the buffer-allocation changes in string.rs, error.rs, and js_values/string/mod.rs, or the new test_utf8_methods example and its tests. Those changes support the main change and do not need to be in the title.

  • 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: f86bc6e2-6c35-46fd-ab41-98e64e27ace1
Commits

Reviewing files that changed from the base of the PR and between 03502b8 and f8da669.

Files ignored due to path filters: 1

  • examples/napi/__tests__/__snapshots__/values.spec.ts.snap is excluded by !**/*.snap
Files selected for processing: 16

  • crates/napi/src/bindgen_runtime/js_values/string.rs
  • crates/napi/src/error.rs
  • crates/napi/src/js_values/string/mod.rs
  • crates/napi/src/js_values/string/utf8.rs
  • examples/napi-compat-mode/__tests__/string.spec.ts
  • examples/napi/__tests__/__snapshots__/values.spec.ts.md
  • examples/napi/__tests__/values.spec.ts
  • examples/napi/example.wasi-browser.js
  • examples/napi/example.wasi.cjs
  • examples/napi/example.wasi.d.cts
  • examples/napi/example.wasip1-browser.js
  • examples/napi/example.wasip1.cjs
  • examples/napi/example.wasip1.d.cts
  • examples/napi/index.cjs
  • examples/napi/index.d.cts
  • examples/napi/src/string.rs

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

@Brooooooklyn
Brooooooklyn force-pushed the om/11-16-perf_napi_jsstringutf8_avoid_memory_copies_in_into_owned_and_take_ branch 2 times, most recently from 03502b8 to f8da669 Compare November 19, 2026 06:48
…ake`

Both take `self` by value, so move the buffer out instead of copying it. This saves an allocation and a copy of the whole string on every call, including when capturing error messages, which go through `into_utf8().into_owned()`.

The returned `String` / `Vec<u8>` now has 1 byte of spare capacity, from the slot N-API wrote the NUL terminator into.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the om/11-16-perf_napi_jsstringutf8_avoid_memory_copies_in_into_owned_and_take_ branch from f8da669 to 5e1dd9e Compare November 19, 2026 15:05
@Brooooooklyn
Brooooooklyn merged commit 5f477dc 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