Skip to content
zit Artifact [9bf3367b57]
Login

Artifact [9bf3367b57]

Artifact 9bf3367b574e076e2b38f31a289631b755c0f27292cd0e727501162b4c12e7a0:


//! The switch for checks too expensive for production.
//!
//! `std.debug.assert(f(x))` is not free in ReleaseFast: when the optimizer
//! cannot prove `f` finite and pure it keeps the call, so an assert that
//! re-hashes a buffer or walks a whole set runs in the shipped binary. Such
//! an assert is written `if (verify) assert(f(x))`, and the call compiles out.
//!
//! The idea is TigerBeetle's `constants.verify`, "extra assertions which are
//! too expensive for production" (Joran Dirk Greef and the TigerBeetle team,
//! TIGER_STYLE).
const std = @import("std");
const builtin = @import("builtin");

/// On in Debug, ReleaseSafe and every test binary (unit, simulation, e2e);
/// off in ReleaseFast and ReleaseSmall builds of the tool itself.
pub const verify: bool = enabledFor(builtin.mode, builtin.is_test);

fn enabledFor(mode: std.lang.Optimize, is_test: bool) bool {
    return is_test or switch (mode) {
        .debug, .safe => true,
        .fast, .small => false,
    };
}

test "verify: off only in a fast or small build of the tool" {
    try std.testing.expect(enabledFor(.debug, false));
    try std.testing.expect(enabledFor(.safe, false));
    try std.testing.expect(!enabledFor(.fast, false));
    try std.testing.expect(!enabledFor(.small, false));
    try std.testing.expect(enabledFor(.fast, true));
}