StaticBitSet with a variable length

I am trying to create a set of StaticBitSets whose length is not known at compile time; stringLength is read from the command line, like this: std.bit_set.StaticBitSet(stringLength). This fails with

src/bitset_chromosome_generator.zig:18:75: error: unable to evaluate comptime expression
    const output = try generate(allocator, prng, std.bit_set.StaticBitSet(stringLength), numStrings);

Is there any workaround for this, other than generating the code and building it on the fly?

Then you should use a DynamicBitSet (or DynamicBitSetUnmanaged since you seem to store the allocator elsewhere).

1 Like

The issue here is that while StaticBitSet(number) is a type, DynamicBitSet is not. Some important refactoring is needed. I don’t know, however, if there will be a noticeable change in performance.
The problem is a bit more general, maybe, namely, how to work with variable types in run time. Might be an unsolvable problem in this context, since it clashes with Zig philosophy.

how to work with variable types in run time.

Well basically you have two options. Either you allocate the memory range you need with a DynamicBitSet, or if you know the maximum size, then you can also always use StaticBitSet of that maximum length.

I don’t know, however, if there will be a noticeable change in performance.

Generally, as long as you don’t allocate or free memory constantly, it shouldn’t make a measurable performance difference. However if you are worried about performance, then you should measure it. This is always better than speculating.

4 Likes