Skip to content

feat(ast/estree)!: replace serde with custom ESTree serializer#9256

Merged
Boshen merged 1 commit intomainfrom
02-20-feat_ast_estree_replace_serde_with_custom_estree_serializer
Feb 21, 2025
Merged

feat(ast/estree)!: replace serde with custom ESTree serializer#9256
Boshen merged 1 commit intomainfrom
02-20-feat_ast_estree_replace_serde_with_custom_estree_serializer

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Feb 20, 2025

Replace serde with a custom serializer specialized for writing JSON.

impl Serialize -> impl ESTree. I've called it ESTree in case we want to add e.g. impl Babel later on.

The main motivation is that with control of the serializer, we can make it stateful, so we can extend it to, for example, omit TS fields based on a flag. This is pretty much impossible with serde's interface.

But a side-effect of the simpler implementation is that it's also more performant.

The usage of ESTree trait is very similar to serde::Serialize. Main differences:

  • serialize method is infallible. Does not return a Result.
  • state = serializer.serialize_struct(); instead of map = serializer.serialize_map();.
  • state.serialize_field(...) instead of map.serialize_entry(...).
  • Struct keys must be strings, and JSON-safe (not including " or other characters that require escaping in JSON).

This is a breaking change, as it's possible downstream consumers are using serde to serialize our AST to other formats. I'd guess unlikely, but possible. We can always implement serde::Serialize as well as ESTree further down the line if there's a demand.

Copy link
Member Author

overlookmotel commented Feb 20, 2025


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 hot fixes, skip the queue and merge this PR next

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.

@overlookmotel overlookmotel changed the title feat(ast/estree): replace serde with custom ESTree serializer feat(ast/estree)!: replace serde with custom ESTree serializer Feb 20, 2025
@github-actions github-actions bot added A-parser Area - Parser A-ast Area - AST A-ast-tools Area - AST tools C-enhancement Category - New feature or request labels Feb 20, 2025
@overlookmotel overlookmotel force-pushed the 02-20-feat_ast_estree_replace_serde_with_custom_estree_serializer branch from 8fb55bf to 7df04c9 Compare February 20, 2025 02:26
@Boshen Boshen changed the base branch from 02-20-refactor_ast_estree_simplify_serializer_for_bindingproperty_ to graphite-base/9256 February 20, 2025 02:34
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 20, 2025

CodSpeed Performance Report

Merging #9256 will improve performances by 24.22%

Comparing 02-20-feat_ast_estree_replace_serde_with_custom_estree_serializer (53d9342) with main (464762e)

Summary

⚡ 1 improvements
✅ 32 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
estree[checker.ts] 189.9 ms 152.9 ms +24.22%

@Boshen Boshen force-pushed the graphite-base/9256 branch from f81c8b3 to 46b9151 Compare February 20, 2025 02:40
@Boshen Boshen force-pushed the 02-20-feat_ast_estree_replace_serde_with_custom_estree_serializer branch from 7df04c9 to 4232680 Compare February 20, 2025 02:40
@Boshen Boshen changed the base branch from graphite-base/9256 to main February 20, 2025 02:41
@Boshen Boshen force-pushed the 02-20-feat_ast_estree_replace_serde_with_custom_estree_serializer branch from 4232680 to beea0cc Compare February 20, 2025 02:41
@overlookmotel
Copy link
Member Author

overlookmotel commented Feb 20, 2025

#9259 adapted @oxc-parser/wasm NPM package to transfer AST to JS as a JSON string. So this PR now plays nicely with that NPM package.

oxc-wasm is still transferring AST as a wasm_bindgen::JsValue, and the code to do that in this PR is very unperformant. But that'd be easier to fix after this PR, rather than trying to change it first. As oxc-wasm package isn't designed for public consumption anyway, I think it should be OK to leave it in a dodgy state temporarily.

So I think we can merge this now.

@overlookmotel overlookmotel marked this pull request as ready for review February 20, 2025 17:40
@overlookmotel overlookmotel requested a review from Boshen February 20, 2025 17:40
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Feb 21, 2025
Copy link
Member

Boshen commented Feb 21, 2025

Merge activity

  • Feb 20, 10:24 PM EST: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.

@Boshen Boshen merged commit 216b33f into main Feb 21, 2025
32 checks passed
@Boshen Boshen deleted the 02-20-feat_ast_estree_replace_serde_with_custom_estree_serializer branch February 21, 2025 04:20
@overlookmotel overlookmotel removed the request for review from leaysgur February 21, 2025 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

0-merge Merge with Graphite Merge Queue A-ast Area - AST A-ast-tools Area - AST tools A-parser Area - Parser C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants