Skip to content

refactor(napi): parse with Source instead of Parser - #29970

Draft
overlookmotel wants to merge 1 commit into
om/11-20-feat_parser_add_source_from_napi_js_string_methodfrom
om/11-20-refactor_napi_parse_with_source_instead_of_parser_
Draft

overlookmotel wants to merge 1 commit into
om/11-20-feat_parser_add_source_from_napi_js_string_methodfrom
om/11-20-refactor_napi_parse_with_source_instead_of_parser_

Conversation

@overlookmotel

@overlookmotel overlookmotel commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Use Source in the NAPI packages, and copy source text from a JS string directly into the arena, using the primitives added in #29969.

All the entry points take source text as a JsString, and use Source::from_napi_js_string to use NAPI calls to write the string direct from the JS engine into the arena, without going via an intermediary String, which would result in copying the string twice.

This changes applied to:

  • oxc-parser (parseSync, parse)
  • oxc-transform (transform, isolatedDeclaration, moduleRunnerTransform, and their sync versions)
  • oxc-minify
  • oxc-transform-react
  • oxc-transform-relay

Sync functions

Create an Allocator, and call Source::from_napi_js_string.

Async functions

A JS string can only be read on the main thread, but the work happens on another thread. A Source borrows its Allocator, so the two cannot be moved into the task together.

Add a SendableSource type to oxc_napi to handle this. It owns an Allocator, and holds the raw parts of the Source.

  • The exported function creates a SendableSource from the JsString on the main thread, and moves it into the task. That is all it does. Everything else (e.g. working out the SourceType) happens off the main thread, in compute.
  • compute takes the SendableSource out of the task, and gets the Source from it with SendableSource::source.

Taking the SendableSource out of the task means the Allocator is dropped at the end of compute, so its memory is freed on the worker thread. This reduces work that blocks JS main thread to a minimum.

Playground

napi/playground only switches from Parser to Source::from_str. Its Oxc class has to keep its own copy of the source text as a String anyway, to render diagnostics after run returns.

Changes in behavior

  • When sourceText is not a string, the error has a different message: "Failed to convert JavaScript value into Source". Its code is still StringExpected.
  • moduleRunnerTransformSync: If the file extension is not supported and sourceText is not a string, it returns the error for the unsupported extension. Previously it threw. SourceType is now determined before the string is read. This is minor change which only affects when both the extension is invalid and source text is not a string, and only affects which error triggers first.

This was referenced Nov 21, 2026

overlookmotel commented Nov 21, 2026 •

Copy link
Copy Markdown
Member Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions Bot added A-parser Area - JS/TS parsing and syntax errors, oxc_parser A-minifier Area - Minification, compression and name mangling, oxc_minifier / oxc_mangler A-transformer Area - JS/TS transforms and downleveling, oxc_transformer A-react-compiler Area - React Compiler transforms and lint rules, oxc_react_compiler A-napi Area - Node.js bindings and WASM builds of the napi/ packages, oxc_napi labels Nov 21, 2026
@overlookmotel overlookmotel self-assigned this Nov 21, 2026
@codspeed

codspeed Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 91 untouched benchmarks
⏩ 19 skipped benchmarks1


Comparing om/11-20-refactor_napi_parse_with_source_instead_of_parser_ (f0a1479) with om/11-20-feat_parser_add_source_from_napi_js_string_method (12b827d)

Open in CodSpeed

Footnotes

  1. 19 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Use `Source` in the NAPI packages, and copy source text from a JS string directly into the arena.

Previously source text was copied twice. NAPI-RS copied the JS string into a `String`, and then `Source::from_str` copied that into the arena. Now functions take a `JsString`, and it's copied into the arena once.

This applies to `oxc-parser` (`parseSync`, `parse`), `oxc-transform` (`transform`, `isolatedDeclaration`, `moduleRunnerTransform`, and their sync versions), `oxc-minify`, `oxc-transform-react` and `oxc-transform-relay`.

Create an `Allocator`, and call `Source::from_napi_js_string`.

A JS string can only be read on the main thread, but the work happens on another thread. A `Source` borrows its `Allocator`, so the two cannot be moved into the task together.

Add a `SendableSource` type to `oxc_napi` to handle this. It owns an `Allocator`, and holds the raw parts of the `Source`.

- The exported function creates a `SendableSource` from the `JsString` on the main thread, and moves it into the task. That is all it does. Everything else (e.g. working out the `SourceType`) happens off the main thread, in `compute`.
- `compute` takes the `SendableSource` out of the task, and gets the `Source` from it with `SendableSource::source`.

Taking the `SendableSource` out of the task means the `Allocator` is dropped at the end of `compute`, so its memory is freed on the other thread. If it was left in the task, it'd be freed on the main thread when NAPI-RS drops the task.

`SendableSource` contains the only `unsafe impl Send`. The tasks themselves are `Send` without any unsafe code.

`napi/playground` only switches from `Parser` to `Source::from_str`. Its `Oxc` class has to keep its own copy of the source text as a `String` anyway, to render diagnostics after `run` returns.

- When `sourceText` is not a string, the error has a different message: "Failed to convert JavaScript value into `Source`". Its `code` is still `StringExpected`.
- `transform`: An error in the options (e.g. a syntax error in a `define` value) is no longer rendered with a code frame. The labels of such an error are offsets into the option's value, not the source text, so the code frame pointed at unrelated code in the file.
- `moduleRunnerTransformSync`: If the file extension is not supported and `sourceText` is not a string, it returns the error for the unsupported extension. Previously it threw. `SourceType` is now determined before the string is read.
@overlookmotel
overlookmotel force-pushed the om/11-20-refactor_napi_parse_with_source_instead_of_parser_ branch from b140c26 to f0a1479 Compare November 21, 2026 02:28
@overlookmotel
overlookmotel force-pushed the om/11-20-feat_parser_add_source_from_napi_js_string_method branch from c347fcc to 12b827d Compare November 21, 2026 02:28

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-minifier Area - Minification, compression and name mangling, oxc_minifier / oxc_mangler A-napi Area - Node.js bindings and WASM builds of the napi/ packages, oxc_napi A-parser Area - JS/TS parsing and syntax errors, oxc_parser A-react-compiler Area - React Compiler transforms and lint rules, oxc_react_compiler A-transformer Area - JS/TS transforms and downleveling, oxc_transformer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant