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

Use SWC to parse and transform code instead of Babel #1365

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ewanharris
Copy link
Contributor

@ewanharris ewanharris commented Oct 10, 2023

This is a proof of concept in replacing Babel in the compilation step, it probably is not ready for merge given it's limited testing (hence the draft PR) but I think it's at a stage for potential discussion.

The positives here are mostly around the faster build time, I am limited on what I have to test but there appears to be a consistent speed up between the Babel based Alloy and SWC based Alloy (benchmarks and testing method below)

Default app

Command Mean [ms] Min [ms] Max [ms] Relative
alloy compile /Users/awam/Documents/titanium-workspace/test-app/app --config platform=ios,version=0,simtype=none,devicefamily=universal,deploytype=development,target=simulator 280.0 ± 5.7 273.6 293.1 1.52 ± 0.03
alloy2 compile /Users/awam/Documents/titanium-workspace/test-app --config platform=ios,version=0,simtype=none,devicefamily=universal,deploytype=development,target=simulator 183.9 ± 1.8 181.6 187.0 1.00
Benchmark command `hyperfine --prepare 'ti clean' --runs 10 'alloy compile /Users/awam/dev/git/tidev/kitchensink-v2/app --config platform=ios,version=0,simtype=none,devicefamily=universal,deploytype=development,target=simulator' 'alloy2 compile /Users/awam/dev/git/tidev/kitchensink-v2/app --config platform=ios,version=0,simtype=none,devicefamily=universal,deploytype=development,target=simulator'`

KitchenSink

Command Mean [ms] Min [ms] Max [ms] Relative
alloy compile /Users/awam/dev/git/tidev/kitchensink-v2/app --config platform=ios,version=0,simtype=none,devicefamily=universal,deploytype=development,target=simulator 879.0 ± 18.7 859.2 918.6 1.52 ± 0.03
alloy2 compile /Users/awam/dev/git/tidev/kitchensink-v2/app --config platform=ios,version=0,simtype=none,devicefamily=universal,deploytype=development,target=simulator 579.0 ± 2.3 575.7 582.3 1.00
Benchmark command hyperfine --prepare 'ti clean' --runs 10 'alloy compile /Users/awam/Documents/titanium-workspace/test-app/app --config platform=ios,version=0,simtype=none,devicefamily=universal,deploytype=development,target=simulator' 'alloy2 compile /Users/awam/Documents/titanium-workspace/test-app --config platform=ios,version=0,simtype=none,devicefamily=universal,deploytype=development,target=simulator'

However, there are some potential drawbacks here:

  • SWC is Rust based and ships native binaries, this would be a new thing for the Alloy package so might need some testing across our supported OS's
  • SWC has plans to deprecate the JS plugin method used currently and instead use Rust based plugins. Whilst it seems like there could be a further speed increase here, the plugin creation and distribution is tougher.
  • The source maps are entirely untested and this historically has proved difficult. Given these are crucial for debugging Alloy apps we can't really compromise here, I plan to test this in the near term

If this proves useful, and there is consensus we could potentially make this an opt-in thing via a config setting to allow testing over time before making it the default.

There are some areas also that could be investigated to improve:

  • If we've already copied all of the builtin files, don't run the builtins plugin
  • Given we do similar work in the SDK, we could potentially replace the usage of Babel there with SWC
  • Investigate allowing TypeScript usage (typechecking is not possible by SWC)
  • We're currently using synchronous code everywhere, maybe we could try and see if we could use some async APIs? (this applies to babel too)

Testing

If you're curious in testing this you can install it using npm i tidev/alloy#feat/use-swc -g. Please provide any feedback you may have!

Other things

One potential question here is "what about X?", and that would be a good one, I just went with SWC as it seemed a good choice. I think it would also be worthwhile to investigate whether it's feasible to do this with maybe esbuild or simply just acorn (maybe even though it's JS it is faster than babel?). I plan to give these a try if I can find some time.

@m1ga
Copy link
Contributor

m1ga commented Oct 10, 2023

Very nice! Saw the active branch before as github pushed it up in my fav list 😄

Just tested it with 3 times running the command and using the last number, not the average

Alloy compile:

  • Alloy compiled in 3.793s (old)
  • Alloy compiled in 2.65815s (new)

with 33 views, 15 widgets.

Full app:

  • Project built successfully in 15s 197m (old)
  • Project built successfully in 14s 211ms (new)

Linux, Ti 12.3.0, no issues building and the app looks fine at first sight.

Would be good for testing if you raise the version number. Then it's easier to see which version you are using 😄

Avoiding suffix as I dont wanna test how the editor plugins handle them
@ewanharris
Copy link
Contributor Author

ewanharris commented Oct 10, 2023

@m1ga ha that was fast! Thank you! Good idea on bumping the version, just pushed a commit to do that. Your numbers look inline with my tests so far, there's nothing groundbreaking in a speedup here it seems

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