I Made Zig Compute 33 Million Satellite Positions in 3 Seconds. No GPU Required

65 points by badtuple


badtuple

Submitting this because it was a super clear explanation of applying SIMD concepts to optimize an algorithm. SIMD advice often feels like it's asking you to "draw the rest of the owl," whereas this felt like a very natural working through of the problem. Also satellites are just plain cool.

I almost wish Zig wasn't so emphasized in the title since it may give people the impression that it's another "I rewrote this in a lower level language and now it's faster!" post. On the other hand, the code reads super naturally compared to equivalent Rust or C++...so Zig definitely deserves props.

Thanks to Anthony for writing it. It was a joy to read!

mitchellh

The main limitation to Zig's SIMD intrinsics is there is no easy way to compile a function to multiple targets and choose it cheaply at runtime. This issue is the long-standing one: https://github.com/ziglang/zig/issues/1018 So unless your target user is compiling from source with -Dtarget=native (the default, usually), they're going to instead get a -Dtarget=baseline package probably that doesn't have the latest and greatest SIMD support.

There are various tricks, and I tried em: pulling SIMD functions out into a separate Zig module, compiling it multiple times, embedding all of them into your binary, manually implementing CPU fingerprinting in your main, swapping dlopen your embedded bins, swap pointers, etc. etc. etc. You see, the complexity?

Ultimately, I personally opted into C++ (the HORROR) (https://github.com/google/highway) which I then call from Zig. Highway handles all the fingerprinting, multicompilation, etc. Here is Ghostty's SIMD routines and their Zig wrappers: https://github.com/ghostty-org/ghostty/tree/main/src/simd

I'd really prefer to use Zig, though. :)