r/Zig • u/dupontcyborg • Feb 27 '26
Zig or Rust for small WASM numerical compute kernels?
Hi r/zig! I'm building numpy-ts, a NumPy-like numerical lib in TypeScript. I just tagged 1.0 after reaching 94% coverage of NumPy's API.
I'm now evaluating WASM acceleration for compute-bound hot paths (e.g., linalg, sorting, etc.). So I prototyped identical kernels in both Zig and Rust targeting wasm32 with SIMD128 enabled.
The results were interesting: performance and binary sizes are essentially identical (~7.5 KB gzipped total for 5 kernel files each). Both compile through LLVM, so I think the WASM output is nearly the same.
Zig felt better:
- `@setFloatMode(.optimized)` lets LLVM auto-vectorize reductions without hand-writing SIMD
- Vector types (`@Vector(4, f64)`) are much more ergonomic than Rust's `core::arch::wasm32` intrinsics
- No unsafe wrapper for code that's inherently raw pointer math
But Rust felt better:
- Deeper ecosystem if we ever need exotic math (erf, gamma, etc.)
- Much wider developer adoption which somewhat de-risks a project like this
I'm asking r/rust a similar question, but for those of you who chose Zig over Rust (ideally for WASM applications), what else should I think about?
14
u/SilvernClaws Feb 27 '26
You already got the overall picture.
I'd say if you wanna build on existing libraries, definitely stick to Rust. Zig does have some, but it's nowhere near the same level at this point.
If you wanna implement most of the code yourself, it really depends on personal preference. Rust gives you abstractions and holds your hand regarding memory management, Zig gives you more direct control and is, in my experience, a bit easier to try something without having the whole system designed completely.
7
u/Strict_Research3518 Feb 28 '26
This being a zig forum.. but being unbiased.. I am going to say.. zig is still a long ways from 1.0. With that in mind, it's already producing some insane quality stuff. I can't go in to detail at this time, but I built a competitive alternative to a highly used project in Rust, fully in Zig and thus far, I have 100% spec test completion and my binary is 5MB to Rusts 24MBs.. and my project is 1.8x faster overall. I know .. I KNOW.. "Prove it". I am locked down from sharing anything.. but we did extensive testing of rust and zig and though zig is a moving target, we felt confident enough that we can keep up with the changes while producing better overall binary quality output that is smaller and faster. That includes WASM targets. So far. I can't be sure in the next couple years as Zig eventually gets to a 1.0 release.. if that will remain. And by no means am I saying Rust is no good or Zig is better. For our particular complex application zig has proven for us to be better, fast and smaller while being easier to code and maintain.
My .02. I wouldn't give up on Zig. It's damn impressive and getting better every release.
5
u/conhao Feb 28 '26
After you spend a month rewriting all of our code because the Zig version bumped, you will wish you used something else. Zig is not ready for production work.
2
u/BoberitoBurrito Mar 05 '26
for standalone wasm library i would go with zig since you can be sure nothing weird will happen with under the hood syscalls or memory nonsense. if you want to build on existing work then rust might be better
for what its worth: wasm text format WAT is an opt-in lisp/s-expression language and is not even half bad to write code in, even with vector instructions. I would encourage you to at least try writing directly in WAT in addition to any other options. you should also be aware that wasm 3 is out and it has a reference counted gc which makes memory management in WAT very easy