Repository navigation
refactor(napi): parse with Source instead of Parser - #29970
Draft
overlookmotel wants to merge 1 commit into
Conversation
This was referenced Nov 21, 2026
Member
Author
Contributor
Merging this PR will not alter performance
Comparing Footnotes
|
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
force-pushed
the
om/11-20-refactor_napi_parse_with_source_instead_of_parser_
branch
from
November 21, 2026 02:28
b140c26 to
f0a1479
Compare
overlookmotel
force-pushed
the
om/11-20-feat_parser_add_source_from_napi_js_string_method
branch
from
November 21, 2026 02:28
c347fcc to
12b827d
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Use
Sourcein 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 useSource::from_napi_js_stringto use NAPI calls to write the string direct from the JS engine into the arena, without going via an intermediaryString, 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-minifyoxc-transform-reactoxc-transform-relaySync functions
Create an
Allocator, and callSource::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
Sourceborrows itsAllocator, so the two cannot be moved into the task together.Add a
SendableSourcetype tooxc_napito handle this. It owns anAllocator, and holds the raw parts of theSource.SendableSourcefrom theJsStringon the main thread, and moves it into the task. That is all it does. Everything else (e.g. working out theSourceType) happens off the main thread, incompute.computetakes theSendableSourceout of the task, and gets theSourcefrom it withSendableSource::source.Taking the
SendableSourceout of the task means theAllocatoris dropped at the end ofcompute, so its memory is freed on the worker thread. This reduces work that blocks JS main thread to a minimum.Playground
napi/playgroundonly switches fromParsertoSource::from_str. ItsOxcclass has to keep its own copy of the source text as aStringanyway, to render diagnostics afterrunreturns.Changes in behavior
sourceTextis not a string, the error has a different message: "Failed to convert JavaScript value intoSource". Itscodeis stillStringExpected.moduleRunnerTransformSync: If the file extension is not supported andsourceTextis not a string, it returns the error for the unsupported extension. Previously it threw.SourceTypeis 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.