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

[PoC] Experimental Rust parser + benchmark #5196

Closed
wants to merge 1 commit into from

Conversation

fpacanowski
Copy link
Contributor

@fpacanowski fpacanowski commented Dec 26, 2024

Ever since Aaron Patterson published his "Ruby outperforms C" article, I was curious about how Rust based parser would fare in this comparison. To satisfy my curiosity I wrote a gem that's yet another implementation of GraphQL parser.

There are 3 parts to this PR:

  • the parser gem itself
  • benchmark that compares Ruby, C, and Rust parser implementations
  • Dockerfile to make the result reproducible

The parser gem is a thin wrapper over graphql-parser crate. It first parses the input string to Rust structure, and then recursively traverses said structure to build ruby-graphql's AST. Note that this is a proof-of-concept, not production-ready code. Also note that's not the fastest Rust GraphQL parser out there, e.g. Stellate claims to be over 8x faster. It does offer a nice speed gain though (run on e2-standard-2 VM in Google Cloud):

Warming up --------------------------------------
      parsing - Rust   112.000  i/100ms
         parsing - C    83.000  i/100ms
      parsing - Ruby    76.000  i/100ms
Calculating -------------------------------------
      parsing - Rust      1.124k (± 1.8%) i/s -     33.712k in  30.004060s
         parsing - C    849.502  (± 1.8%) i/s -     25.481k in  30.005166s
      parsing - Ruby    766.000  (± 1.6%) i/s -     23.028k in  30.070330s

Comparison:
      parsing - Rust:     1123.9 i/s
         parsing - C:      849.5 i/s - 1.32x  slower
      parsing - Ruby:      766.0 i/s - 1.47x  slower

Whether this speed gain is worth adding another language to the project is up for debate. Parsing is rarely the bottleneck, but I imagine in a large deployment a faster parser can translate to some savings. Anyway, I've spent significant amount of time on this, so I thought I'd share the effects.

@rmosolgo FYI.

@rmosolgo
Copy link
Owner

Hi! Thanks for sharing your work here, very impressive. It looks like you've prepared this work in a separate gem at https://github.com/fpacanowski/rust_graphql_parser. Is there anything else I can help with?

@fpacanowski
Copy link
Contributor Author

Hi! Thanks for sharing your work here, very impressive. It looks like you've prepared this work in a separate gem at https://github.com/fpacanowski/rust_graphql_parser. Is there anything else I can help with?

Yes, the code in this PR is an exact copy of the code in the other repo. I shared this PR to highlight the possibility for improving parser performance further by employing Rust and its ecosystem of parser libraries. The code I have works, but would require some amount of work to make it robust enough for serious production use. To be honest I'm no longer incentivized to work on it as I moved to a company that doesn't use GraphQL. But maybe this PR will be useful to someone :)

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