As an example, here’s a main.zig
and a lib.zig
that should contain a cached comptime evaluation:
main.zig:
const std = @import("std");
const lib = @import("lib.zig");
pub fn main() void {
std.log.info("{}\n", .{lib.cachedConstant});
}
lib.zig:
const std = @import("std");
pub const cachedConstant: u32 = slowFn();
// This takes a few seconds to evaluate
fn slowFn() u32 {
@setEvalBranchQuota(std.math.maxInt(u32));
var x: u32 = 1;
var i: u32 = 0;
// @compileLog("x1");
while (i < (1 << 19)) : (i += 1) {
x ^= (x << 3) +% 1;
x ^= x >> 1;
}
// @compileLog("x2");
return x;
}
Changing just main.zig
always results in a reevaluation of cachedConstant
in lib.zig
(zig 0.9.1).
Is there a way to cache comptime evals with the zig build system?