Skip to content

Conversation

jp-knj
Copy link
Member

@jp-knj jp-knj commented Aug 2, 2025

Related PR: #14080, I'm planning to close this later,

Summary

This PR introduces an experimental Rust-based MDX compiler for Astro and maintain compatibility with existing remark and rehype plugins.

Key Features

Performance

  • 2-3x faster MDX compilation compared to the JavaScript implementation
  • Rust-based parsing and code generation for maximum speed
  • Seamless fallback to JS compiler when Rust binary is not available

Plugin Compatibility

  • compatible with existing remark and rehype plugins
  • Uses an AST bridge pattern: Parse (Rust) → Transform (JS plugins) → Generate (Rust)
  • No changes required to existing MDX configurations or plugins

User Experience

  • Opt-in via experimental.mdxCompiler: 'rs' configuration

Implementation Details

Architecture

The implementation uses an "AST bridge" pattern that combines the best of both worlds:

This is achieved through the AST Bridge architecture where:

  1. Rust handles the heavy parsing (MDX → AST)
  2. JavaScript runs the plugins (AST transformations)
  3. Rust handles the code generation (AST → JavaScript)

The performance gains are consistent and scalable, making it an excellent choice for projects that need both speed and plugin flexibility.

  packages/
    integrations/
      mdx/
        src/
          processors/
            js.ts         # JavaScript MDX processor
            rust.ts       # Rust MDX processor interface
          rust-parser/    # Rust MDX parser implementation
            Cargo.toml
            src/
              lib.rs
            package.json
            index.js      # Generated by napi-rs

Configuration

  // astro.config.mjs
  export default defineConfig({
    integrations: [mdx()],
    experimental: {
      mdxCompiler: 'rs' // 'js' (default) | 'rs'
    }
  });

Performance Metrics

The Rust-based MDX compiler with plugins delivers:

  • Small files (1KB): 3.68x faster
  • Medium files (10KB): 2.56x faster
  • Large files (100KB): 2.88x faster

Average performance improvement: ~2.5-3.7x faster than the JavaScript compiler while maintaining full compatibility with all remark and rehype plugins.

Technical Considerations

Pros

  • Significant performance improvement for content-heavy sites
  • No breaking changes or plugin incompatibilities
  • Clean abstraction allows future optimizations

Cons

  • Requires building native binaries
  • Adds complexity to the build process
  • Initial setup requires Rust toolchain for contributors

Copy link

changeset-bot bot commented Aug 2, 2025

⚠️ No Changeset found

Latest commit: 47d0283

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Aug 2, 2025
@jp-knj jp-knj changed the title feat(mdx): add experimental mdx-hybrid compiler option PoC(WIP): add experimental mdx-hybrid compiler option Aug 2, 2025
@jp-knj jp-knj marked this pull request as draft August 2, 2025 16:09
@jp-knj jp-knj closed this Aug 2, 2025
@jp-knj jp-knj reopened this Aug 2, 2025
@jp-knj jp-knj closed this Aug 3, 2025
@jp-knj jp-knj force-pushed the poc/rust-based-mdx branch from 6f45091 to 0d5b690 Compare August 3, 2025 00:11
@jp-knj jp-knj reopened this Aug 3, 2025
@github-actions github-actions bot added pkg: example Related to an example package (scope) pkg: astro Related to the core `astro` package (scope) docs pr labels Aug 3, 2025
Copy link

codspeed-hq bot commented Aug 3, 2025

CodSpeed Performance Report

Merging #14181 will not alter performance

Comparing jp-knj:poc/rust-based-mdx (9b30b53) with main (b36e72f)1

Summary

✅ 6 untouched benchmarks

Footnotes

  1. No successful run was found on main (9fe883e) during the generation of this report, so b36e72f was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@jp-knj jp-knj force-pushed the poc/rust-based-mdx branch from 5fe7fbb to 99e1b64 Compare August 3, 2025 04:54
@jp-knj jp-knj closed this Aug 3, 2025
@jp-knj jp-knj reopened this Aug 4, 2025
@jp-knj jp-knj changed the title PoC(WIP): add experimental mdx-hybrid compiler option PoC(WIP): add MDX AST Bridge Implementation for High-Performance Compilation Aug 4, 2025
@jp-knj jp-knj force-pushed the poc/rust-based-mdx branch 2 times, most recently from 15a90ad to 41c4475 Compare August 4, 2025 15:05
@github-actions github-actions bot removed the pkg: example Related to an example package (scope) label Aug 4, 2025
@jp-knj jp-knj force-pushed the poc/rust-based-mdx branch 5 times, most recently from 8b2a3de to 7a6dba6 Compare August 4, 2025 15:46
@jp-knj jp-knj force-pushed the poc/rust-based-mdx branch from 7a6dba6 to fee1f5d Compare August 4, 2025 16:45
@github-actions github-actions bot added the 🚨 action Modifies GitHub Actions label Aug 5, 2025
@jp-knj jp-knj force-pushed the poc/rust-based-mdx branch from 15bab68 to 7c6d19c Compare August 5, 2025 02:58
@github-actions github-actions bot removed the 🚨 action Modifies GitHub Actions label Aug 5, 2025
@github-actions github-actions bot added the 🚨 action Modifies GitHub Actions label Aug 5, 2025
@jp-knj jp-knj force-pushed the poc/rust-based-mdx branch from 88e1069 to 9b30b53 Compare August 5, 2025 13:24
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not ignore this file and put it in a dist?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right! I'll remove this!

@@ -0,0 +1,316 @@
/* eslint-disable */
Copy link
Contributor

Choose a reason for hiding this comment

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

@florian-lefebvre
Copy link
Member

Hey, thanks a lot for the research! I don't think this should live in Astro, but rather in the upstream package. I'm fine with leaving the PR open if that allows you to do more testing or whatever, otherwise feel free to close it

@Eveeifyeve
Copy link
Contributor

Hey, thanks a lot for the research! I don't think this should live in Astro, but rather in the upstream package. I'm fine with leaving the PR open if that allows you to do more testing or whatever, otherwise feel free to close it

I agree with this, please make the mdx-rs-parser package seperate from the astro repo.

@jp-knj
Copy link
Member Author

jp-knj commented Sep 19, 2025

Sorry, I'll close.
just creating the package right now

https://github.com/jp-knj/jetmd

@jp-knj jp-knj closed this Sep 19, 2025
@florian-lefebvre
Copy link
Member

No need to apologize, I just wanted to set the expectation there. Thanks for the work on this, I think that can be very powerful!

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

Labels

🚨 action Modifies GitHub Actions docs pr pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants