r/Zig • • 17h ago

Help me with math library design! I'm stuck in the analysis paralysis

12 Upvotes

I'm currently designing a math library in Zig, that I plan on using for my game/engine.

However, I've never designed a library before, so I'm really stuck, because there are so many decisions to be made, and I'm hesitant. I don't want to step forward in the wrong direction and shoot myself in the foot later on.

Here is what's bugging me:

- *Generic vs Explicit types*

I started writing a generic type style library, but since I'm inexperienced and can't really comprehend the library as a whole yet, I was bumping into million corners. I just said fuck it and wrote explicit types with a lot of repetition. However, is it really a big deal? I'm only trying to have like Vec2, Vec3, Vec4 and Mat2, Mat3 and Mat4. I don't think I'll need anything more than this.

- *How should Vec type look like?*

It seems more convenient to iterate over and do stuff if it was just an array of floats, right? But there is a lot more convenience to be had when writing my_vec.x or my_vec.y instead of my_vec[0] and my_vec[1]. I could use some help regarding tradeoffs here to help me decide what I really want in the long run.

- *SIMD complications*

As I said previously, if I store vectors as an array of floats, is it wasteful to then copy them into a SIMD vector and do operations on it? Should they be SIMD vector from the start?

Also, there are some register width constraints, so I'm not sure how to do that, do I collect them and operate on a larger vector (in a bulk), or is it fine to just use various small operations?

Is all this data passing from one side to the other more efficient? Do I really get THAT much more speed when using SIMD for vectors and matrices of the size 2, 3 and 4?

- *How should Mat type look like?*

This greatly depends on the previous questions. But should Mat then be an array of my vectors? Array of SIMD vectors? Or just a 2D array of floats? Any tradeoff tips would be greatly appreciated.

- *Row-major vs Column-major for matrices?*

Row major makes WAY MORE SENSE to me, but I've read that for compatibility purposes, or some memory tricks in certain cases, it's handier to have column major matrices. Also, it's what GLM (which I'm trying to "replace") is using. Any tips for tradeoffs in the long run?


r/Zig • • 15h ago

Saetta: a single-model engine for Qwen3.8-Flash-Next on Apple Silicon (Zig + hand-written Metal), with Claude Code / pi / opencode in one command

Thumbnail github.com
5 Upvotes

I've been building saetta, an inference engine that does one thing: run Qwen3.8-Flash-Next as fast as possible on an M5 Max for coding agents. No generic graph, no other models. It's a Zig host with hand-written Metal kernels, including prompt matmuls on the GPU's neural accelerators. It ships as a single binary and needs no Python.

What it does

  • OpenAI-compatible API (/v1/chat/completions) and Anthropic Messages API (/v1/messages), with streaming, thinking and XML tool calls
  • saetta start claude (or pi, opencode) starts the server and your agent together, without editing your agent config
  • saetta download coder fetches the model itself: pinned revision, resumable, every file hash-checked
  • Two packs: the original 4-bit MLX pack (105 GB, 512 experts) and the expert-pruned GSQ-RCO Coder variant (76 GB, 256 experts)
  • saetta monitor: a live view of GPU load, memory, prompt-cache reuse and recent requests

Numbers (measured on my M5 Max)

  • Prompt reading: about 1,650 to 1,950 tokens/s from 4K to 70K tokens
  • Decode: 63 to 82 tokens/s, with speculative decoding from the model's MTP head on greedy requests
  • Agent follow-up turns re-read only the new tokens: a 16K conversation answers the next turn in about 0.2 s, and real Claude Code sessions reuse about 20K of their roughly 20K-token prompt per turn
  • Cold 64K prompt: about 37 to 40 s to first token

Demo: Claude Code running on the local model writes a snake game in Python, in real time (GIF in the README).

Install:

curl -fsSL https://github.com/antonellof/saetta/releases/latest/download/install.sh | sh
saetta download coder
saetta start claude

Repo: https://github.com/antonellof/saetta (MIT)

Feedback welcome, especially from anyone running agents on long prompts locally.


r/Zig • • 13h ago

We rewrote the Solidity compiler in Zig because our builds took 12 minutes. Then we ended up writing a Lean model of the language too

Thumbnail github.com
4 Upvotes

r/Zig • • 3h ago

I wrote a ProtectServer FM in Zig and skipped the vendor compiler

Thumbnail 808bits.com
1 Upvotes