Skip to content

Instantly share code, notes, and snippets.

@CJSmith-0141
Created March 25, 2022 15:28
Show Gist options
  • Save CJSmith-0141/c15d14924812a96bfb5af5c69fc3e1e1 to your computer and use it in GitHub Desktop.
Save CJSmith-0141/c15d14924812a96bfb5af5c69fc3e1e1 to your computer and use it in GitHub Desktop.

Rust

Rust has been touted as the most loved programming language many years in a row according to respondents in the Stack Overflow Annual Developer Survey , and if you write a blog post without mentioning this you are potentially commiting a crime. But . . . why? What productive things are being done with Rust, right now? Where, exactly, does rust shine? Having tinkered with the language on-again and off-again, I think it clearly shines where its killer application of garbage-collection-less memory safety can shine. That brings us to . . .

WebAssembly (wasm)

WebAssembly. wasm. You've probably heard murmerings of it, things like "it's faster!" or "adoption is coming!". Wasm evangelists haven't won me over, but I am curious about it. Since rust can compile with wasm as a target, rust and wasm feel tied together in my brain. How unfortunate for them both.

Svelte

Svelte is a JavaScript framework without a virtual DOM. But it's actually not that at all, it's an html templating language that compiles scripts and components together into vanilla JavaScript. Also, it's worth noting, svelte is the tech of choice at my day job. It doesn't do this all on it's own, however, and leans pretty heavily on a JS bundler. At the time of writing you can choose between Rollup and

Vite

Vite is the bundler of choice for SvelteKit (which is like the full stack version of Svelte), So I wanted to use it here since I would like to give SvelteKit a shot in the future. This also comes with a beast of a development server and hot module replacement.

Yarn

No comment.

Node

I guess?

wasm-vite-svelte-monorepo

You didn't think I did this on my own, did you? Shout out to Daniel Segovia for making an absolute beacon on the proverbial hill of a template project. I was slamming my head against the wall until I decided to really dig deep on google searches, and wasm-vite-svelte-monorepo is what came out the other end. Go give him a star for me, please.

Ok, you smashed some tutorials together. So what?

  1. Who asked you, buddy?
  2. I learned some pitfalls and would like to pass these learning onto the next person that likes the idea of this stack.

What pitfalls?

The Rust and WebAssembly book will emphasize that one should think about the memory structure of wasm and minimize structures crossing the boundary between JS and wasm. This is good advice, and the mechanism that is imparted upon the reader to accomplish this looks like:

https://gist.github.com/aa578b0597aa236b1d9bfd423d8bb03b

I was not able to get this to work inside of a svelte component. I have some ideas on how to get it working, passing a handle into the root component context on instantiation could work. After reading some Stack Overflow questions and answers I decided that this wasn't going to be worth it, and getting this correct would be quite difficult and fraught with undefined behavior since a new allocation in the web assembly memory could invalidate pointers that have been passed to JS. I instead decided to copy the universe state out of web assembly, directly going against the books advice. Oh well.

https://gist.github.com/4d6b3b43bac84904f36d7674bdbc14ab

Conclusion?

Without profiling a similarly naive implementation of Conway's game of life and trying to compare apples to apples, I don't know what benifit this particular toy gets from the wasm implementation. I think, with this brief exposure, I would reach for wasm only in a few specific circumstances:

  1. I need to do image or video or audio processing in the browser (or anything that needs fast linear algebra)
  2. I need to shift almost all of my processing browser side without bogging users down ( an application built on gun comes to mind)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment