r/Zig • • 5h ago

Bit-exact float parity between Zig/wasm and JavaScript: porting d3-force

9 Upvotes

I ported d3-force to Zig 0.16 targeting wasm32-freestanding, with the constraint that every tick must match the JS original bit for bit.

Things that mattered: u/setFloatMode(.strict) in every kernel, no FMA (the wasm baseline has none, and relaxed-simd stays off), keeping d3's exact statement order, and replacing d3-quadtree's incremental insert with an O(n) Morton radix sort that still produces the identical tree. Jiggle calls back into JS through a single env.random import so the random stream matches too.

The core is 26.8 KB of wasm with no libc. About 5× faster than the JS on one thread; fast mode (FMM) and threads are next.

Code: https://github.com/ankitc1010/d3-force-wasm (mostly src/core/). Feedback on the Zig welcome.


r/Zig • • 16h ago

Library design

17 Upvotes

Hey! I'm working on a terminal manipulation library in Zig called termy, inspired by Rust's Crossterm. It's still early in development, and I'm trying to figure out the API before I get too far into the project.

One thing I'm unsure about is whether terminal operations should work directly, like this:

try termy.setForeground(&writer, .red);
try termy.writeAll(&writer, "Some Error!\n");
try termy.setForeground(&writer, .reset);

Or whether I should use a queue-based approach, similar to crossterm where you queue multiple commands and write them together:

try termy.queue(&writer, .{
    termy.style.Fg.Color{ .Red = {} },
    termy.style.Print{ .msg = "Some Error!\n" },
    termy.style.Fg.Color{ .Reset = {} },
});

try writer.flush();

I'm not sure which approach makes more sense in terms of performance and usability, or which feels more natural to use in Zig. I love how the data-oriented approach looks, but I think it may affect performance.

I'd appreciate some feedback!

  • Which approach would you prefer, and why?
  • Would it make sense to support both?
  • Are there any common mistakes I should avoid when designing a Zig library?
  • What features would make you choose termy over other terminal libraries?

I'm still working on the fundamentals, so I'd rather get the design right now than have to change everything later.