I’m porting a serialization format to zig and want the serializer to be really fast. I’m having a hard time figuring out the best way to handle the temporary memory structures. The pattern is something like
recurse into all child nodes and serialize them, collect output as a linked list or something
write the header for this section and return that to my caller somehow (a linked list)?
Is a linked list the best way to generate the data? It’s generated out of order so I can’t just stream the output.
But once I do have the final linked list of chunks (or some other data structure) how do I quickly emit that? I think my library will write to some stream like stdout or a file descriptor.
Also if I have all this heap allocated memory and most of it will be used till the very end, I assume I want to use an arena allocator for the whole operation right? I’m don’t know how much total space I’ll need up though. Do arena allocators support this?
Also I just realized that Zig doesn’t have FAM (flexible array members). Isn’t it a lot of overhead to alloc twice (for the linked-list node and again for the payload)? In C I would do something like: