Zig's std.debug.assert behaves differently from C's assert macro. In Zig, assert is a regular function that evaluates to `if (!ok) unreachable`, meaning the expression is always evaluated even in release builds. The optimizer won't remove code with side effects or memory dependencies, so calls like `assert(try q.put(...))` remain safe. However, the optimizer may not always eliminate dead code in complex assertions, potentially causing performance issues. Unlike C's preprocessor-based assert that can remove entire expressions, Zig's approach prevents bugs from side effects inside assertions while still providing optimization hints through unreachable.

5m read timeFrom cryptocode.github.io
Post cover image
Table of contents
Does this mean asserts can be expensive even in ReleaseFast mode? #When the optimizer will definitely not remove code #

Sort: