-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
As mentioned in #2123 and #1392, parse() is currently quite slow.
SWC's Rust parser is of course extremely fast, but the bottleneck is passing ASTs back into JS.
I've done some experiments, and believe I may have the beginnings of a solution to speed it up by 5x or more, enough so it out-performs Babel at least.
https://github.com/overlookmotel/swc-parse-test
The problem
There are 2 bottlenecks involved in transferring AST from Rust to JS:
- Cost of passing AST from Rust to JS (as JSON string)
- Cost of parsing JSON in JS (using
JSON.parse())
Potential solution
I've attempted to alleviate these pinch points by outputting a buffer (JsBuffer) from Rust instead of a JSON string. The buffer is a binary serialization of the AST, and I've written a matching deserializer in JS.
This has 2 advantages:
JsBuffercan be created in main thread's memory so passing it from Rust to JS is zero cost.- AST can be encoded and deserialized more efficiently in binary form.
The results appear to be pretty good.
Provisos
The implementation is only a proof of concept. So far it can only deal with const x = 1; statements. I see no reason why it shouldn't maintain it's speed when support for all AST node types is included, but I could be wrong.
It may also be naive. Until this weekend, I've never written a line of Rust before, so I may be totally missing a reason why it's not possible to integrate this approach into SWC.
The Rust code I've written is, for the same reason, quite embarrassingly bad. The JS code is also hacked together very quickly. So potentially it could get significantly faster with some optimization.
Questions
-
Does this seem like a promising direction?
-
If
parse()(andprint()) can be made much faster, might this make it feasible to maintain support for plugins written in Javascript?
As mentioned above, I don't know Rust at all, so I have limited ability to help with an effort to flesh out these experiments into fully-fledged features. However, I would be very happy to help out on the JS side (the deserializer etc).