Hi, people. I’m iterating over my previous solution for the Parallel Letter Frequency exercise on JS, experimenting with new things. I noticed that currently the description for the exercise has the following text, which seems to indicate that Exercism only has support to node.js Worker threads:
Parallelism in Javascript
Even though Javascript by default is single-threaded, there is a way to execute code in parallel fashion.
If your running javascript in the browser (e.g. in a web app), then the way to achieve parallelism is through the Web Worker API. As described by MDN:
Web Workers makes it possible to run a script operation in a background thread separate from the main execution thread of an application.
On the other hand, if your javascript is running in Node.js, which is Exercism’s target runtime, this same concept is known as Worker threads.
However, the web test runner doesn’t seem to support Worker threads, but the Web Worker API. And local solutions, on the opposite and according to the description for the exercise, only support node.js Worker threads.
So I’m thinking that maybe the description for the exercise should be updated to mention this difference?
If I understood correctly how the online editor works with the web test runner, one has to support both APIs to succesfully submit a solution using the online editor. The Web Worker API to get it to work in the browser and the Worker threads to pass the final submission tests running in the Exercism infrastructure. Sounds like a nightmare to debug.
@SleeplessByte Does the submission process work that way? Should we discourage actual Parallelism?
We should probably find a way for someone to write the node code and transpiles it so it works in the browser.
This is likely possible because the interface required to complete the exercise is nearly 1-to-1 what you’d need to write for webworkers. I’ll have to play with it.
There is no CSP other than report-only on the website so that should affect anything. As for cross-origin isolation: yeah I’ll check that out. Could be an issue. I’ll find out what’s up with those shared array buffers
Sidenote: I was actually involved turning the exercise into one with parallelism and I think it’s important to try to keep as there are no race conditions in the classical sense or multi threading bugs with JavaScript because of the single execution context and thread message passing.
@oxe-b I wrote up the instructions for this exercise before the web test-runner was a thing so it made sense at the time. ¯_(ツ)_/¯
The way I see it, maybe just removing the bit about Exercism's target runtime may be better, but I’m open for suggestions.
@mk-mxp The tests for this exercise don’t enforce actual parallelism in any way so you can pass with a concurrent solution that just returns a promise (and most people do so), so it wouldn’t be a high bar. I never found a way to enforce test for actual parallelism that didn’t feel clunky. Otherwize, I vote to keep this exercise as it highlights one of the unique aspects of JS nicely.
I was fighting for actual parallelism becoming possible in this exercise and wrote the demonstrating solution that it was not possible back then but could be without enforcing parallelism
I do not want anyone to test for students using parallelism and never did
I am very much for keeping parallelism in this exercise
I am also for describing precisely what can work to add parallelism to ones solution (which is hard now)
Which made me ask: Should we discourage it? Because I don’t not see a student friendly way to add parallelism now
I am very much in favor of adding transpiling, be it web worker to worker threads or the other way around
I have a slight tendendcy to keep the current node worker thread based solutions working while I also understand that writing node worker thread code in the browser feels very weird
Another possible way to improve the situation for now: We could add a note, that implementations using actual parallelism must be submitted from command line.
@mk-mxp yes let’s do that last one so I can have some time implementing the fix. I also need @dem4ron for that to do a deploy which may take time after I finish.
Well, I don’t know all the magic inside the tests, but I wrote many iterations for the exercise and the ones usin Web Workers pass on the web test runner and I’ve also submitted those solutions successfully. So I believe that as of now the web test runner has full support to Web Workers (as long as they don’t use Shared Array Buffers).
For reference, this was the last one using Web Workers: