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

Parse AST in worker #5211

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

Parse AST in worker #5211

wants to merge 3 commits into from

Conversation

lukastaegert
Copy link
Member

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:
cf. #5202

Description

This will move regular parsing into a worker thread. It would be interesting to compare this approach with #5202. The advantage of using a worker thread as opposed to native threads in Rust is that this approach also parallelizes the @rollup/wasm-node build.
As parsing is currently faster than analyzing and tree-shaking, I do not think it makes sense to use more than one additional thread. However, I set it up in a way so that we could easily switch to a worker pool with a configurable number of workers.
Originally, I wanted to use a singleton worker. However, then it became difficult to decide when to terminate the worker, as a running worker would prevent the process from completing. Instead, the worker will now terminate itself if inactive for one macro task tick. Another approach could have been to terminate the worker on build end. However in the long run, I hope to extend the plugin API to allow async parsing in generate hooks, in which case we would still need the worker.

@vercel
Copy link

vercel bot commented Oct 17, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rollup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 20, 2023 3:19pm

@lukastaegert lukastaegert marked this pull request as draft October 17, 2023 19:39
@github-actions
Copy link

github-actions bot commented Oct 17, 2023

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install rollup/rollup#parse-web-worker

Notice: Ensure you have installed Rust nightly. If you haven't installed it yet, please first see https://www.rust-lang.org/tools/install to learn how to download Rustup and install Rust, then see https://rust-lang.github.io/rustup/concepts/channels.html to learn how to install Rust nightly.

or load it into the REPL:
https://rollup-asz8v50az-rollup-js.vercel.app/repl/?pr=5211

@codecov
Copy link

codecov bot commented Oct 17, 2023

Codecov Report

Merging #5211 (a509b7b) into master (2bb2a64) will decrease coverage by 0.07%.
The diff coverage is 86.84%.

❗ Current head a509b7b differs from pull request most recent head 265836d. Consider uploading reports for the commit 265836d to get more accurate results

@@            Coverage Diff             @@
##           master    #5211      +/-   ##
==========================================
- Coverage   98.82%   98.76%   -0.07%     
==========================================
  Files         231      233       +2     
  Lines        8849     8884      +35     
  Branches     2314     2319       +5     
==========================================
+ Hits         8745     8774      +29     
- Misses         43       49       +6     
  Partials       61       61              
Files Coverage Δ
src/Graph.ts 100.00% <100.00%> (ø)
src/Module.ts 99.61% <100.00%> (-0.20%) ⬇️
src/ModuleLoader.ts 99.59% <100.00%> (ø)
src/utils/logs.ts 97.66% <ø> (ø)
src/utils/parseAstWorkerEntry.ts 100.00% <100.00%> (ø)
src/utils/parseAstAsync.ts 83.33% <83.33%> (ø)

@jerome-benoit
Copy link
Contributor

worker_threads are not as efficient as native POSIX threads by design. Since the current use case is delightfully parallellisable, a unique native POSIX thread in rust with JS bindings and parsing task concurrency limit and a task FIFO will very likely out perform any worker_threads pool implementations available (I co-maintain one and be warned that: worker_threads communication are dead slow under load, worker_threads high memory usage needs tweaking to avoid OOM conditions, worker_threads startup time is quite slow compared to native POSIX thread, worker_threads memory sharing is way less convenient to work with than with native POSIX threads).

@lukastaegert
Copy link
Member Author

Yes, this confirms my observations at #5202 (comment), which is why I just merged and released @sapphi-red's approach #5202. This PR currently is mostly for reference and as inspiration if we want to parallelize the WASM builds after all.

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.

None yet

2 participants