new Zig maelstrom node framework / client

Hi,

I was curious recently about Zig as there is lots of bragging about it on Twitter. So I decided to take a look into it and wrote (almost finished) a Zig framework/client library for Maelstrom. RPC() and KVs() will be there soon.

The most awful part is working with JSON and serialization overall. You can take a look at the code. Zig does not have annotations of field tags so I had to do something about it. You can’t have fields named type

I have started with full async implementation but nothing was working in 0.10.x branch. I faced a couple of critical bugs that I could not overcome so I had to give up the async version and moved to just having a pool of Threads and simple multi-threading impl. You can also find different FIXME comments regarding that in the code.

In the end, I faced one compiler bug that prevented me from parsing the topology struct correctly and also memory corruption with arena allocators in JSON parser (that is already fixed in the upstream branch, 0.11.x).

Regardless of all that, the echo example already works, and the broadcast is on the way. For all interested folks, you are welcome at github.com/sitano/maelstrom-zig-node/.

Sample for echo workload:

const m = @import("maelstrom");
const std = @import("std");

pub const log = m.log.f;
pub const log_level = .debug;

pub fn main() !void {
    var runtime = try m.Runtime.init();
    try runtime.handle("echo", echo);
    try runtime.run();
}

fn echo(self: m.ScopedRuntime, req: *m.Message) m.Error!void {
    self.send_back_ok(req);
}

I wish I had !void in the handle fn ptr but it wants to be comptime known. I’ve got an idea it could be good to have a few additional error types there like OutOfMemory or JSON parsing ones included but probably I will not be investing in that… will see. You are welcome to contribute. Also, merge_to_json name is bad, but I don’t know at the moment what is the better name.

$ zig build && ~/maelstrom/maelstrom test -w echo --bin ./zig-out/bin/echo --node-count 1 --time-limit 10 --log-stderr
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.