Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3: Using JsBuffer to transfer AssetGraph to Nodejs #313

Merged
merged 2 commits into from
Jan 23, 2025

Conversation

alshdavid
Copy link
Contributor

@alshdavid alshdavid commented Jan 22, 2025

To help with the determinism issue I am experimenting with changes to the serialization of the AssetGraph. This PR simplifies the transferral of the AssetGraph from JsString values to JsBuffer to make it easier to work with when experimenting with changes to the serialization approach.

It performs pretty much identically (slightly faster but not meaningfully) however does not have the same string size limitations and avoids the need to manually create a JSON string from Rust.

@alshdavid alshdavid force-pushed the alsh/v3-using-buffers-for-assetgraph-napi-transfer branch from a1ad2fa to 5c05361 Compare January 22, 2025 05:30
@alshdavid alshdavid force-pushed the alsh/v3-using-buffers-for-assetgraph-napi-transfer branch from 5c05361 to 75c10cd Compare January 22, 2025 05:31
};

let node_bytes = serde_json::to_vec(&serialized_node)?;
let js_buffer = env.create_buffer_with_data(node_bytes)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we do this instead of just returning an object?

if this is better why not serialise the entire thing as JSON?

Copy link
Contributor Author

@alshdavid alshdavid Jan 22, 2025

Choose a reason for hiding this comment

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

why do we do this instead of just returning an object?

If you're referring to a napi_derive::napi(object) it's because the macros are broken in the IDE (though the build will pass). We can either move to napi_v3 (prerelease) or manually cast the properties to an object.

If you mean why convert to JSON in the first place, it's because creating the objects takes in Rust takes much longer

if this is better why not serialise the entire thing as JSON?

Returning a single JSON exceeds the max string length limit in Nodejs. Having each node be its own json allows for the total JSON size to be broken up into smaller pieces. Using buffers isn't necessary but allows the values to be moved rather than copied

}

#[cfg(test)]
mod tests {
Copy link
Contributor

Choose a reason for hiding this comment

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

Deleted tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tests were checking the max string length limitation of Nodejs was respected by the manual creation of the original JsString in Rust.

Because the new function uses Env directly it cannot be unit tested without a Nodejs environment. This circuit is covered by the Nodejs integration tests in v3 because failure to transfer the AssetGraph would result in a critical failure

@alshdavid alshdavid merged commit 4cc2520 into main Jan 23, 2025
17 checks passed
@alshdavid alshdavid deleted the alsh/v3-using-buffers-for-assetgraph-napi-transfer branch January 23, 2025 00:14
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.

2 participants