Comptime evaluation takes forever

I am writing a program in Zig that I want to use to figure out something for fun but I am stuck with a part where I cannot find a nice implementation.

I have a fixed list of 3408 Unicode coded words (all 5 letter words in the Finnish language). I want to translate this into a list of [5]u16’s that I will call word_list. I will use subsets of the word_list with bit_sets. So the word_list is a constant.

The problematic piece of code is below; Word is [5]u16.

const WordList = struct {
     const number_of_words: usize = 3408;
     const all_words: [number_of_words][ ] const u8 = @import("all_words.zig").words; 
 
     word_list: [number_of_words]Word,
 
     fn create() WordList {
         var words: [number_of_words]Word = undefined;
         @setEvalBranchQuota(200000);
         for (0..number_of_words) |i| {
             words[i] = Word.fromUtf8(all_words[i]) catch .dummy();
         }
         return .{ .word_list = words };
     }
     fn get(self: WordList, index: usize) Word {
         return self.word_list[index];
     }
 };
 
const word_list = WordList.create();

The problem is that this code takes over 80 seconds to compile. Am I missing something or is it just a fact of life that comptime code is much much slower than runtime?

2 Likes

comptime is currently slower than python, there is a goal to get it roughly equivalent to cpython.

For heavy pre computations you’re better off creating a separate program to compute it, ran by the build system, and embedding the result into the final binary.

17 Likes

tracking issue:

4 Likes