r/Zig

🔥 Hot ▲ 65 r/Zig

zig 0.16 almost 2x slower than 0.15.2 for my library

I'm working on audio processing library, nothing fancy, just some math optimized as well as I can.

Today I decided to try Zig 0.16 and noticed smth weird.

If I build my "harness" profiling client as Zig static binary -- performance difference is negligible, like 0.15'th version is 1.00 ± 0.03 times faster than 0.16'th.

But when I build it as shared library w/ gnu libc linking and run my Go client benchmark (intended usage), 0.15'th version is almost twice faster, 3ms vs 5.5ms per benchmark.

I'm investigating further, just wanted to ask if it's just me or somebody also noticed this?

reddit.com
u/gistart — 24 hours ago
▲ 22 r/Zig

On readonly/private members of structs

Andrew on readonly or private struct members

https://github.com/ziglang/zig/issues/2479

> This is one of those things where zig is going to not provide a language feature that, admittedly, could be useful, but ultimately is unnecessary to accomplish zig's goals.

I guess the goal of zig is to alienate fans of the language that write programs with complex state machines.

I’m writing this post solely to express my frustration.

Zig is basically solving every crappy thing about C++ with its amazing comptime, build system, very nice syntax, error handling, etc … that won me over easily after reading the (entire) documentation.

Several of its design decisions are IMHO is better than rust.

That said, the idealistic design decision to not have private members boggles the mind !

Any serious real world application that isn’t a small app or utility will have an internal state that is strictly accessed by core functionality which must be allowed to make assumptions about said state. Allowing a (possibly dumb) user to manipulate the state means one of the following:

  1. Add a huge amount of avoidable validation and internal error reporting (a broken state is rarely recoverable)

  2. Having to educate the user with naming, comments, meetings and 100s of closed get-good tickets to keep repeating STOP TOUCHING MY PRIVATE DATA

Finally, given that “good naming” and comments should do the trick. It begs the question, why this aspect of safe coding (on both the supplier and user side) didn’t get the safe-explicit-by-design no-footgun treatment.

Every deficit in C that zig is trying to fix can also be fixed in C with better naming and comments. What’s the point of zig then ?

The decision is arbitrary.

And for someone who has been using C++ for so long and desperately desires a performant, simple, powerful-meta-programming, reflective programming language. I find myself reading the release notes of every zig release and checking up on the subreddit regularly to hopefully see this “everything-public” anti-pattern revoked or sidestepped nicely.

There I said my peace.

I know I’m not the only one and this is not the first time this is discussed, but something really should move here.

u/mute_narrator — 1 day ago
▲ 16 r/Zig

How to get a file's content in zig 0.17.0?

i've just started using zig about 3 days ago. i'm trying to build a little program for practice. the program should take in a filename as a command line argument, then read the contents of that file and count how many words there are.

but i cannot figure out for the life of me how to read the contents of a file. all the information i'm seeing online seems to be wrong and outdated.

i'm completely new to low level programming so this has been pretty daunting. i thought this should be a pretty simple task.

reddit.com
u/NicBarr — 1 day ago
▲ 29 r/Zig

Just a minor thing I was wondering about (while learning Zig)


// If imports are done like this:

const std = @import("std");

// and structs are also done like this:

const Point = struct {
	x: f32, 
	y: f32
};

// then why is Zig not consistent so that we cannot also do:

const add = function (a: i32, b: i32) i32 {};

pub const main = function (init: std.process.Init) !void {};

const @"test 1" = test {

}
// and so on?
reddit.com
u/__parad0x59 — 1 day ago
🔥 Hot ▲ 58 r/Zig

0.15.1 -> 0.16.0 upgrade not as scary as I thought

I did have to change over a few things for my program:

  1. Change from @cImport to loading the c files through the build system

  2. the only Io changes needed were to switch from Timer to Timestamps

  3. Changing from the GeneralPurposeAllocator to the DebugAllocator (because I use enable_memory_limit)

  4. Dealing with vector indexes having to be known at comptime—so switching some vectors to arrays while they’re being built

The changelog was helpful for the cImport and comptime vector indexing changes. I had to hunt through the package docs to figure out how the memory allocator changed. For the Timer/Timestamps, I had to hunt through the zig std library source to find examples.

I was able to do the migration in one sitting and the program ran first time after it compiled.

reddit.com
u/quag — 2 days ago
▲ 14 r/Zig

Notifications/progress bar disable

I have this really annoying series of problems that stem from the fact the zig compiler has a progress animation and sends out progress notifications.

is it possible to disable it? i looked for a flag to use with zig build or zig run that would remove the progress bar or notifications and i couldn't find one.

reddit.com
u/NicBarr — 3 days ago
▲ 31 r/Zig

Is there any hobby contributor?

Nowadays I am gonna be more interested in Zig

And suddenly it hits me that can I also be a contributor to Zig while learning some

So my question is: Zig community is friendly to newbies for contributing?

And where could I find good issue to start

Because I can barely see GFI issues

reddit.com
u/MatterConscious382 — 4 days ago
▲ 17 r/Zig

Zig and Android

Couldn't get a clean way to work with zig on Android. i tried egl, raylib, SDL all of them hurting when try to export for Android

reddit.com
u/SuccessfulCrew6916 — 4 days ago
▲ 26 r/Zig

Inconsistency about unreachable code analysis

This code compiles:

test "can format empty document with template" {
    if (true) {
        return error.SkipZigTest;
    }

    // more code
}

While this one does not:

test "can format empty document with template" {
    return error.SkipZigTest;

    // more code
}

Zig complains about unreachable code in the second case, which is true. But I would have expected the same for the former.

Does this occur because Zig does not optimize if branches at all when testing?

reddit.com
u/Objective-Fox-9403 — 4 days ago
🔥 Hot ▲ 55 r/Zig

Zig or Rust along with Go?

I know this topic has been asked several times but since zig is constantly evolving and the learning circumstances are not same for me as others. I learn and use golang on weekdays. This is the main language that I am learning to get a job but I have decided to learn another low level language on weekend nights only for fun. There's nothing specific that I am aiming to build. From my research I have found out that Rust is mature but the learning curve is steeper compared to Zig which is a smaller language but the docs of Zig is pretty bad and the language itself is unstable and keep changing. What do you think I should learn?

P.S: I don't know why I have a bias towards Zig but please give me an unbiased opinion.

reddit.com
u/Humble-Gift-3649 — 6 days ago
▲ 30 r/Zig

Looking for feedback: what data‑format libraries does Zig still need? (CSV, TSV, structured text, etc.)

Hi folks,

I’m pretty new to Zig and I’d like to learn it by working on a long‑term project over the next few months (or even more if the project succeed). I’m not expecting to create anything groundbreaking, but I’d love to try building something that could eventually be useful to others.

I’m especially interested in projects that are:

  • easy to test thoroughly
  • deterministic
  • not web‑framework related
  • helpful for tooling, data processing, or general development

One idea I’m considering is a CSV/TSV/structured‑text parsing + writing library, mostly because it seems like a good way to learn Zig’s I/O, error handling, and API design. But I’m not sure what the ecosystem actually needs, and I don’t want to duplicate existing work or overlook something important.

So I’d love to hear from the community:

  • Would a structured‑text library (CSV/TSV/etc.) be useful?
  • Are there other beginner‑friendly but meaningful library ideas you’d recommend?
  • Any gaps in the ecosystem that you think would make good learning projects?

Thanks for any suggestions, I hope to become a useful member of the community

reddit.com
u/TynK-M- — 5 days ago
▲ 13 r/Zig

is there a way to force eager evaluation of a unused const on a type?

Hi there, sorry if this is not the correct place to ask, but with Stack Overflow kinda dead I really don't know where else to put this question.

A bit of context: I'm currently working on a library to simplify Zig-Lua interop. For that I created a marker strategy to represent the metadata of Zig structs, unions and enums that defines metatables and encoding/decoding strategies.

The problem is that it is fragile in the sense that forgetting pub on ZUA_META, or misspelling it, causes the encode/decode paths to fall back to the default strategy. That is intentional since the core idea is to allow interop with as little ceremony as possible, so small DTOs should not need any annotation. But the silent fallback causes two kinds of errors: types with non-table strategies get encoded and decoded as plain tables (the minor problem), and methods never get attached to the metatable (the major one, since Lua ends up calling nil and the error message gives no hint about what went wrong on the Zig side).

To catch this I added a comptime guard inside the metadata type constructor so that a misspelled or private ZUA_META would produce a @ compileError. It does not fire. After debugging with @ compileLog I confirmed the cause is that Zig lazily evaluates generic type instantiations, so the check never runs if the result is never actually used. I can confirm this by forcing evaluation manually:

const Vector2 = struct {
    const ZUA_META = zua.Meta.Table(Vector2, .{ .length = length });

    x: f64,
    y: f64,

    pub fn length(self: Vector2) f64 {
        _ = ZUA_META; // force evaluation
        return std.math.sqrt(self.x * self.x + self.y * self.y);
    }
};

_ = (Vector2{ .x = 0, .y = 0 }).length(); // only now the compileError fires

Is there any mechanism in Zig to force eager evaluation of a comptime block, or to guarantee a generic type instantiation is always evaluated even when its result is not directly used? I cannot require explicit registration calls because that would break the zero-ceremony DTO use case. I also cannot use a struct field with a default value because the marker needs to work for enums and unions too, not just structs.

btw here is the commit where I implemented the check that is not working: https://github.com/SolracHQ/zua/commit/ea6f4f083e795874c98d4a1ae55ca4236715fa8e

btw 2, it is a bit ironic that Zig is extremely strict about unused variables everywhere else but silently skips evaluation of unused type declarations with no warning at all. Even a warning would have saved me a lot of debugging time.

Thanks for any help.

u/zeronetdev — 5 days ago
🔥 Hot ▲ 53 r/Zig

Image processing library zignal 0.10.0 is out

Hi everyone!

Zignal 0.10.0 is out. compilable with Zig 0.16.0.

There are the usual performance improvements and bug fixes, but the main highlight is a simple CLI tool to perform various image operations via the terminal.

You can get an idea of what it can do with:

Usage: zignal [options] <command> [command-options]

Global Options:
--log-level <level>   Set the logging level (err, warn, info, debug)

Commands:
blur     Apply various blur effects to images.
diff     Compute the visual difference between two images.
display  Display an image in the terminal using supported graphics protocols.
edges    Perform edge detection on an image using Sobel, Canny, or Shen-Castan algorithms.
fdm      Apply Feature Distribution Matching (style transfer) from target to source image.
info     Display detailed information about one or more image files.
metrics  Compute quality metrics (PSNR, SSIM, Mean Error) between a reference and target images.
resize   Resize an image using various interpolation methods.
tile     Combine multiple images into a single tiled image.
version  Display version information.
help     Display this help message

Run 'zignal help <command>' for more information on a specific command.

For example:

zignal help display

Usage: zignal display <image> [options]
Display an image in the terminal using supported graphics protocols.

Options:
--width <N>     Target width in pixels
--height <N>    Target height in pixels
--protocol <p>  Force protocol: kitty, sixel, sgr, braille, auto

Full release notes here:

https://github.com/arrufat/zignal/releases/tag/0.10.0

u/archdria — 7 days ago
▲ 31 r/Zig

I made a little zig env library with the new 0.16.0 release

It uses libc which I don‘t really like but for now it makes my life easier and its up to date with zig version 0.16.0.

Also its really nothing fancy just a little helper, maybe it helps some :)

github.com
u/Blize0 — 7 days ago