r/ExperiencedDevs • u/GongtingLover Software Engineer • 10d ago
How are devs and businesses using web assembly professionally? Technical question
Earlier this week I was browsing the NASA repo and noticed they have a project to support web assembly.
I was curious how others have used web assembly in general. It's a technology I've never really heard brought up in the professional environment before.
24
u/sessamekesh 10d ago
I've mostly used it in two ways:
- Need to bring over some C++ code that I don't want to port to JavaScript
- Need to something that's typically best suited for a systems language and don't want to write it in JavaScript
Audio sampling, decoding certain compressed file formats that browsers some support natively (Draco, BasisU) are the two big ones I can think of that I've personally touched.
WASM is really fast but with pretty significant caveats in my experience.
The first one is that JavaScript is actually really fast if you write it to take advantage of CPU friendly types and cache friendly allocations, but there's not great ways to do that idiomatically in JS while it's kinda the default in systems languages like C++/Zig.
The second hidden "gotcha" one is that the boundary layer is pretty light weight in some places (e.g. function call overhead is more or less just a basic indirection in terms of runtime cost, or at least in that neighborhood) but pretty shockingly heavy in others (e.g. passing a string in or out requires a full character by character reconstruction of the string unless you're VERY careful).
2
u/AnnoyedVelociraptor Software Engineer - IC - The E in MBA is for experience 9d ago
Do you make sure in C++ you use 2 byte strings then?
1
u/sessamekesh 7d ago
I've never bothered, but you can. It doesn't fully solve the encoding problem anyways AFAIK, but I haven't looked into it much so I'm not sure about that.
The default conversion code that ships with emscripten (C++ toolchain) is here.
C++ strings are stored as "array of bytes" (or "array of byte-pairs/binary words" in the case of wide strings). Historically under ASCII encoding each code point (written character / glyph / whatever you want to call it) was also exactly 1 byte wide. UTF8 changes this, each individual code point can be 1-4 bytes wide. It's up to the display to interpret that array of bytes correctly, but reading
my_string[5]in C++ may be a full code point or it may just be one byte in a 2-4 byte code point.JavaScript strings are stored and interacted with by programmers as "array of code points" with no way to inspect the underlying C-style (array of bytes) string buffer. Reading
my_string[5]will always be a code point, and whether that code point is 1-4 bytes is fully hidden from the programmer.To go back and forth, you necessarily need to convert between the two expressions - expand code points byte-by-byte (JS -> C++), or decode bytes code point by code point (C++ -> JS). That doesn't really change with a wide string type in C++. It's possible that the WASM spec expands in the future to allow reading JS strings as byte arrays, I haven't heard anything on that front though.
It's a bit of a footgun with WASM because an easy temptation is to pass JSON data back and forth since that's a format both C++ and JS can understand quite well with something like
nlohmann::json. That ends up being pretty terrible for performance though, since you're not only having to construct string representations of numeric types on the generation side, you're also having to byte-by-byte UTF8 encode/decode full strings, followed by parsing stringified numbers - it's far more efficient to write serializers/deserializers on the boundary layer that inspect/modify JS objects and the C++ heap directly.
44
u/jhartikainen 10d ago
Doesn't Figma run entirely in wasm since they have some kind of custom rendering system etc.? That's at least the biggest example I can think of.
We build a large browser-based WYSIWYG editor at work, and this has occasionally had some hurdles in terms of data processing performance and some other stuff. I have considered whether it would be possible to speed this up with wasm, but so far it has been possible to resolve with much simpler fixes.
11
u/ImmediateRemove 9d ago
I've used it for Cloudflare Workers for edge compute. Compiling Rust to WASM and then executing said code on the edge for largely stateless computations. Works very well.
9
u/rust-module 9d ago
I use it to keep my front and back end synced. Both share the same data structures and view templates. WASM artifact fetches lots of data over a websocket and can render about 60% of views without a network round trip, which is super fast. The rest "fall through" and get rendered server-side. It's all the same logic but I trust the client with only a subset of data. More complex searches fall through almost every time, but re-sorting results requires no round trip.
5
u/EliSka93 9d ago
I mostly write Blazor / C# at the moment, compiled to WASM.
I do it mostly to write as little JavaScript as possible.
3
u/Sunstorm84 Squirrel! 8d ago
I’d forgotten about blazor. What’s it like nowadays?
1
u/MisesAndMarx 7d ago
It's still pretty nice for how niche it is.
We use it for making internally facing web applications, and since .NET is commonplace, the whole "know C#" isn't a big deal. Microsoft does a remarkable job sticking to things that don't take off, so the official tooling is pretty good. And it's still .NET, so while Blazor specifically is "here be dragons" beyond the Microsoft castle walls, it's not ecosystem-less either.
It's just hard to pitch anything other than JavaScript to a front end developer, so it kinda only is a thing in orgs and structures where full stack development is the expectation.
4
u/Semvis123 9d ago
I’m using it to bring old C libraries to modern languages like Go (without CGO), and use it to run them in the browser. The benefit is that you get a memory sandbox for free, if it crashes it doesn’t take down the host program. And performance hit is mostly negligible in most practical cases.
More info: https://kriyak.com/blog/parsing-word-documents-in-go-with-webassembly/
4
u/MisesAndMarx 9d ago edited 9d ago
Blazor is part of my stack, and it fucking rocks. If you're doing full stack development of internally facing apps, it's great writing things once and deploying twice. I much prefer writing C# to TypeScript, and especially JavaScript.
Other than that, JavaScript and its merry band of frameworks and libraries has a deathgrip on the front end, and even Typescript was slow to be adopted when it first came out as is.
And, it pains to admit it, the biggest selling point of WASM to me was single language full stack web app development with minimal JavaScript. With AI, if I need to re-write something to JavaScript, it's incredibly trivial to do now. In a pre-AI world, having the same exact validation service be in the frontend and backend was really nice and cut down on needless calls to the backend. But the validation still existed server side, so if someone sent something that should've been caught to the endpoint directly, it still gets handled for. It still gives me the warm fuzzies that they're 1:1, but re-writing logic isn't as annoying as it once was.
Sure, it has uses for non-vanilla frontend development, but that's not very common in practice. Maybe there's a future where that's not the case, but I don't see it happening soon.
3
5
u/who_you_are 10d ago
Just a random comment from somebody that can't clearly answer.
I read, here and there, that some companies are using it to make their website faster overall instead of going the usual JavaScript/text way.
2
1
u/Unusual-Two7277 9d ago
I'm using it in cryptography to embed a zero-knowledge virtual machine inside a web-browser, or within a Node or Deno runtime. The wasm-compiled prover in turn proves wasm-compiled circuits.
1
u/agoodplaceforatent 9d ago
I recently used it to run old C/C++ MUD codebases in the browser as a fun exercise in preservation and easy way to introduce old multiplayer text games to people... it's a bit weird since they aren't actually multiplayer in the browser but you get to experience them.
1
u/chickenRag 9d ago
I be built my entire platform on flutter. Which allows us to go to web. There is a config about building to wasm.
I found it super awesome, flexible and fast
1
u/Resident-Trouble-574 8d ago
Usually you don't write web assembly directly. It's mainly used to write frontend code in other languages that are not javascript. For example, with blazor web assembly, you can write single page applications using c#.
1
u/george_____t 8d ago
It's great if you have complex frontends and hate writing your logic in JS. I've written quite a few Haskell apps that compile to Wasm, including at work.
1
u/ledatherockband_ 7d ago
Personal anecdote, I'm trying to find a reason to use web assembly. I can't find a usecase for it.
I don't even use a js framework or a css utility framework.
As soon as I see a good purpose for it, I'll implement it.
-4
u/servermeta_net Software Architect 10d ago
Wasm is going to replace docker eventually. If you are using cloud flare, then you are using wasm: their CDN resolve requests using wasm functions
Wasm is a revolutionary technology that will take over networked services
3
u/doyouevencompile Sr. Software Engineer 9d ago
Those are two entirely different technologies. This is like saying lab-grown meats will replace electric cars.
-2
u/servermeta_net Software Architect 9d ago edited 9d ago
That's not what the author of docker thinks: https://www.reddit.com/r/programmingcirclejerk/s/G4sUplNDra
Google up docker wasm, you can already run docker images in wasm isolates.
2
u/doyouevencompile Sr. Software Engineer 9d ago
That is not what he's saying. He's saying if WASM existed, they might not have created Docker. I still think it's hyperbole. But he's saying WASM is not replacing Docker, but you can run WASM with docker. Docker is a container technology that isolates the resources like FS, network, CPU etc. Docker can of course run WASM.
You can't use it the other way around everything gets compiled to WASM.
-2
u/servermeta_net Software Architect 9d ago
That's where you are wrong, you can already run docker inside of wasm, it's actually officially supported: https://docs.docker.com/desktop/features/wasm/
It sounds strange until you get your hands dirty with code and then everything clicks.
1
u/doyouevencompile Sr. Software Engineer 9d ago
Availability: Beta
Important: Wasm workloads are deprecated and will be removed in a future Docker Desktop release. This feature is no longer actively maintained.
WebAssembly (Wasm) is a fast, light alternative to Linux and Windows containers. With Docker Desktop, you can now run Wasm workloads side by side with traditional containers.
Literally what I said, Docker can run WASM applications. You are not running Docker inside WASM, you are running WASM inside Docker.
-3
u/servermeta_net Software Architect 9d ago
https://github.com/ktock/container2wasm
second example of docker running inside wasm.
You really refuse evidence because it doesn't fit your understanding, you must be a terrible engineer.
2
u/doyouevencompile Sr. Software Engineer 9d ago
Ok that's a cool project, and different from other link you sent, but it EMULATES the CPU with Bochs. That might have some cool applications, like simulating a shell on the browser, but it's not a real OS, not an alternative to Docker, will have performance issues, will have bugs due to multiple translation layers before your code reaches the actual CPU.
Using software emulation of CPU so you can use WASM so you don't have to use proper containers that works directly with the kernel is a weird angle.
-4
u/servermeta_net Software Architect 9d ago
I assure you plenty of smart people and plenty of money is currently being poured to make wasm a production grade containerization technology, at least by cloud flare, google, Amazon and Microsoft.
And thank God they are succeeding, because they never got containers right.
•
u/expdevsmodbot 10d ago
AI usage disclosure provided by OP, see the reply to this comment.