DEV Community

Cover image for Running Rust in WebAssembly in a Pool of Concurrent Web Workers in JavaScript
Alessandro Genova
Alessandro Genova

Posted on • Originally published at alesgenova.github.io

5 2

Running Rust in WebAssembly in a Pool of Concurrent Web Workers in JavaScript

Your scientists were so preoccupied with whether or not they could, that they didn't stop to think whether they should.

I would like to share a little experiment I did for no other reason than to show I could.

In this proof of concept, the main application starts a pool of web workers that it later uses to offload a series of heavy tasks.

Alt Text

The task in question is to to render a single frame of a simple 3D scene using ray-tracing (path-tracing). The computationally intensive rendering is performed by a rust library compiled to WebAssembly.

These are the tools I used:

  • GitHub logo alesgenova / post-me

    📩 Use web Workers and other Windows through a simple Promise API

  • GitHub logo alesgenova / ray-tracer

    A simple ray tracing engine

  • GitHub logo rustwasm / wasm-bindgen

    Facilitating high-level interactions between Wasm modules and JavaScript

  • Small in-house task queue to dispatch tasks to workers when available.
  • react for the skeleton of the app.

If you would like to run this madness, an instance of this application is deployed here.

Alt Text

If you would like to see the details of the implementation, you can find the source code of the app on Github

Bonus

Using a similar approach, I also created an app that can detect the pitch of sounds being captured by the device's microphone.

Try it out

Alt Text

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (3)

Collapse
 
winstonpuckett profile image
Winston Puckett

This is really helpful. I'm trying to write my own game engine and having good examples of 3D graphics when I get there... In a few years lol... is awesome

Collapse
 
alesgenova profile image
Alessandro Genova

Glad you liked it!
Keep in mind that the ray tracing engine I implemented runs on the CPU, so it's way too slow for any practical application.
I just wrote it out of curiosity and to teach myself some rust while following "Ray tracing in a weekend" in C++

Collapse
 
winstonpuckett profile image
Winston Puckett

Ray tracing in general might be not what I'm looking for. I'm thinking even a basic "how to place 3D objects in webgl" would be useful for me :)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay