Optimize individual (standard) library components

In my application I’ve found that in debug builds the speed of the blake3 module is very noticeable (3GB/s vs 60MB/s) so I’d like to speed that part up while not ruining the debugging experience in other parts.
My current solution is a small wrapper module with .optimize = .ReleaseFast that I link against as a static library. It’s a bit cumbersome and it’s not the only think where I’d like to do that. Is there a more convenient way of achieving what I want here?

@setRuntimeSafety(false) should do the trick. You want to use this in as small a scope as possible though. It’s scope bound as the langref says.

2 Likes

Also relevant: replace @setRuntimeSafety with @optimizeFor · Issue #978 · ziglang/zig · GitHub

3 Likes

Does not help at all in my usecase. I’ve tacked it in the loop scope, the entire function and the caller.
This is pretty much the entire offending code:

pub const Hasher = std.crypto.hash.Blake3;

pub const ContentHash = struct {
    bytes: [Hasher.digest_length]u8,

    pub fn fromFileStreaming(io: std.Io, dir: std.Io.Dir, basename: []const u8) !ContentHash {
        @setRuntimeSafety(false);
        var file = try dir.openFile(io, basename, .{});
        defer file.close(io);

        var hasher = Hasher.init(.{});
        var buf: [256 * 1024]u8 = undefined;
        while (true) {
            @setRuntimeSafety(false);
            var iov = [1][]u8{&buf};
            const n = file.readStreaming(io, &iov) catch |err| switch (err) {
                error.EndOfStream => break,
                else => return err,
            };
            if (n == 0) break;
            hasher.update(buf[0..n]);
        }
        var out: [Hasher.digest_length]u8 = undefined;
        hasher.final(&out);
        return .{ .bytes = out };
    }
}