Lemmixology (002) Some tips needed on the global data

Some tips on data needed…

I have game data which is loaded from a file and after that immutable (except when freeing the used memory in some of the structs, when the program closes).

In my “local debug” environment I create / import the below Metadata from a bunch of files on my local disk. I have some homemade on-disk-structure which works ok.
(using a save_to_file(metadata) and load_from_file(): Metadata function).

Now I was wondering if it would be worth the effort to change this:

pub const Metadata = struct {
    images: std.ArrayList(Image),
    styles: std.ArrayList(Style),
    lemmings: std.ArrayList(Lemming),
    // etc.
};

into this:

pub const Metadata = struct {
    images: []Image,
    // etc.
}

or even this:

pub const Metadata = struct {
    images: []const Image,
    // etc.
}

And when creating the file in my local environment use ArrayList just when building the stuff.
When ready convert all Lists to slices with ArrayList.toOwnedSlice().

Or just keep the lists.
Any tips? Ideas?

Why would you not want to use []T and toOwnedSlice()? I don’t see how it could be much more effort, and there are no logic changes at all.

If you need immutable data that you don’t want to change, then there is no point in using ArrayList(T). You can just use []const T. There’s no reason to use ArrayList if you don’t plan on mutating it at all.

2 Likes

True. It’s all const slices now in my experiment. Much better but I needed to write a “builder” in between thingy. I think I will keep these.
Serialiazing structs with std.Io.Reader and Writer is not fun…