Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

buildMerkleTreeLayerParallel

blake3.buildMerkleTreeLayerParallel
fn buildMerkleTreeLayerParallel(
    input_cvs: [][8]u32,
    output_cvs: [][8]u32,
    key: [8]u32,
    flags: Flags,
    io: Io,
) Io.Cancelable!void

File

lib/std/crypto/blake3.zig:766

Code

fn buildMerkleTreeLayerParallel(
    input_cvs: [][8]u32,
    output_cvs: [][8]u32,
    key: [8]u32,
    flags: Flags,
    io: Io,
) Io.Cancelable!void {
    const num_parents = input_cvs.len / 2;

    // Process sequentially with SIMD for smaller tree layers to avoid thread overhead
    // Tree layers shrink quickly, so only parallelize the first few large layers
    if (num_parents <= 1024) {
        processParentBatchSIMD(ParentBatchContext{
            .input_cvs = input_cvs,
            .output_cvs = output_cvs,
            .start_idx = 0,
            .end_idx = num_parents,
            .key = key,
            .flags = flags,
        });
        return;
    }

    const num_workers = Thread.getCpuCount() catch 1;
    const parents_per_worker = (num_parents + num_workers - 1) / num_workers;
    var group: Io.Group = .init;
    defer group.cancel(io);

    for (0..num_workers) |worker_id| {
        const start_idx = worker_id * parents_per_worker;
        if (start_idx >= num_parents) break;

        group.async(io, processParentBatchSIMD, .{ParentBatchContext{
            .input_cvs = input_cvs,
            .output_cvs = output_cvs,
            .start_idx = start_idx,
            .end_idx = @min(start_idx + parents_per_worker, num_parents),
            .key = key,
            .flags = flags,
        }});
    }
    try group.await(io);
}