#Can I run something during compilation, but compile it first?

1 messages · Page 1 of 1 (latest)

ashen oracle
#

I am generating a large lookup table during comptime, but it's taking too long. When I compile the table-generation code and run it, it's much quicker. Is there a way I can make this lookup table during comptime, but make it run as fast as when I compile the table-generation separately?

Maybe I can do it as a separate compilation step?

#

Maybe I can have something like a generate_lut.zig file that gets compiled and ran separately to make a lut.bin, then I use @embedFile to put it into the binary at comptime?

hoary roost
#

do you need to use the table at comptime? If it's faster to parse the table at runtime, then you can just parse it at runtime. No need to force the parsing to be done at comptime because I think the compiler's execution environment is not that fast.

ashen oracle
#

I do not think I need it at compile time for anything yet, but I might in the future.

ashen oracle
hoary roost
ashen oracle
#

it's 4MB

#

Maybe not directly relevant to my original question, but while implementing the idea above I ran into this error which I do not understand:

error: dereference of '*const [16][512][64]u64' exceeds bounds of containing decl of type '[4431:0]u8'

where this is how I am "importing" the table:

pub const stack_change: *const [stack_max_height][1 << (max_amount + 1)][max_board_size]HashSize = @alignCast(@ptrCast(@embedFile("zobrist.zig")));
#

HashSize is u64 in this case

hoary roost
#

try having stack_change being a 1-dimension array

#

hmmmm

#

what does the file zobrist.zig contain?

#

it seems weird that you're embedding a zig file

ashen oracle
#

ohhh

#

should have been zobrist.bin

#

that fixed the issue

ashen oracle
#

I am still struggling with the build script. I am able to build and run the zig file to generate the LUT, and I can also include it in zig-out/, but I don't know how I would just make it available to me when compiling the rest of the code.
When I use temp_step.addOutputFileArg("zobrist.bin") I get passed a lazy path to which I write the LUT. How would I give this path to the rest of my code to use with @embedFile()?

#

Context or summary for those coming in later:

  • I am building a large LUT which is too slow to build using the comptime executor
  • My idea was to add a build step where I compile and run a zig program to write the LUT to a file, and then during the normal build I use @embedFile to load the LUT into the binary.
#

@hoary roost do you have any more helpful tips?

#

Nevermind, I figured it out

#

For future strangers:

Inside build.zig

    // create Zobirst LUT
    const zobrist = b.addExecutable(.{
        .name = "zobrist",
        .root_source_file = b.path("src/zobrist.zig"),
        .target = b.graph.host,
        .optimize = .Debug,
    });
    const zobrist_step = b.addRunArtifact(zobrist);
    const output = zobrist_step.addOutputFileArg("zobrist.bin");
    main_module.addAnonymousImport("zobrist_stack_change", .{
        .root_source_file = output,
    });

the main function from zobrist.zig

/// Generate the stack_change LUT
/// because the comptime execution is too slow.
pub fn main() !void {
    var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena_state.deinit();

    const args = try std.process.argsAlloc(arena_state.allocator());
    std.debug.assert(args.len == 2);

    var output_file = try std.fs.cwd().createFile(args[1], .{});
    defer output_file.close();
    const lut = makeStackChange();
    try output_file.writeAll(@ptrCast(&lut));
}

Elsewhere in my code:

const StackChangeType = [optimized_stack_max_height][1 << (max_amount + 1)][max_board_size]HashType;
pub const stack_change: *const StackChangeType = @ptrCast(@alignCast(@embedFile("zobrist_stack_change")));