Earlier quoted context omitted.
there's a lot of sunlight between the class of things that are idiomatic and the class of things that are anti-patterns; and there are likely things that are idiomatic but still anti-patterns (yes, you can do this, and if you did this it would look like this, and it causes no regression in this particular case, but don't get in the habit of doing it this way because it can cause a hard-to-spot regression in the gener…
> anti-patterns I agree with all that, but patternic is not a word.
The curious case of a memory leak in a Zig program
61–70 of 80 posts
Re: The curious case of a memory leak in a Zig program
#62> If you are hell-bent on using FixedBufferAllocator only and you want to avoid copies, there is a way. Using two buffers (and separate allocators backed by them), it is possible to keep swapping between them after every iteration. I found this bit lovely: the author has independently reinvented the core idea of semispace copying garbage collectors (see eg https://wingolog.org/archives/2022/12/10/a-simple-semi-space.…
Re: The curious case of a memory leak in a Zig program
#63Earlier quoted context omitted.
What an awful API design choice. It’s a stack allocator that leaks your memory if you don’t free in reverse order. Why would anybody ever want that behavior, let alone as the default?
It's not the 'default' it's the behaviour of this allocator. This makes this allocator fast, but it should clearly be named/described I agree.
Personally, I would have called this a StackAllocator, that way the alloc/free order requirement is obvious. I would have made the default behavior to 'panic()' if you don't satisfy the precondition of freeing the most recently allocated buffer.
If somebody really wanted to make free a no-op, I'd offer a feature flag to turn that on.
Re: The curious case of a memory leak in a Zig program
#64Earlier quoted context omitted.
> anti-patternic patternic is not a word.
Why? All words are coined at some point.
Re: The curious case of a memory leak in a Zig program
#65Earlier quoted context omitted.
Thanks for the pointers! > use a GeneralPurposeAllocator and setting "enable_memory_limit" and "requested_memory_limit" Interesting! I hadn't looked at GeneralPurposeAllocator too closely, but yes these seem like the right way to do things instead of abusing FixedBufferAllocator as I did. > If the purpose is to "only use the stack"... Not really, I just had to decide on some arbitrary upper bound on the mem usage, an…
TigerBeetle writes to disk for long-term storage. Data over time is the part you can't fit into memory (eventually). :)
But how does it determine when it should write to disk? Does every write to a potentially OOM operation get preceeded by a check? Take the case of a HashAggregate. The DB clearly cannot know at compile time how many unique keys will be present in the hashtable; it needs to resize at runtime. So does that mean all the hashtables are still using some form of Bump/Arena allocators backed by the pre-allocated memory?
Maybe I should just read the source code :)
Re: The curious case of a memory leak in a Zig program
#66Earlier quoted context omitted.
TigerBeetle writes to disk for long-term storage. Data over time is the part you can't fit into memory (eventually). :)
> TigerBeetle writes to disk for long-term storage But how does it determine when it should write to disk? Does every write to a potentially OOM operation get preceeded by a check? Take the case of a HashAggregate. The DB clearly cannot know at compile time how many unique keys will be present in the hashtable; it needs to resize at runtime. So does that mean all the hashtables are still using some form of Bump/Arena…
You write fixed sized number of key-value pairs to the file at a time. This is how LSM trees work, you chunk your data up into N sorted keys per chunk. I don't myself understand all the specifics but this is the gist.
> Does every write to a potentially OOM operation get preceeded by a check?
If you allocate memory upfront and don't allocate any more memory, you can't OOM after the initial allocation. That's what TigerBeetle does.
Zig has some nice standard library containers for adding items while asserting that there's capacity. If we miscalculate, it is caught during tests because assertions fail.
Re: The curious case of a memory leak in a Zig program
#67Perhaps that allocator could print a warning message if you're not deleting the last element (when built in debug mode). That would make it a lot more clear how that kind of allocator should be used.
Re: The curious case of a memory leak in a Zig program
#68Re: The curious case of a memory leak in a Zig program
#69Earlier quoted context omitted.
>Is there a better way? Just let the kernel handle it. The virtual memory and mapped memory abstraction the kernel has makes your program's implementation simpler.
Zig is a low-level language. You might not have an MMU. You might not have a kernel. You might be the kernel.
Re: The curious case of a memory leak in a Zig program
#70Earlier quoted context omitted.
> anti-patterns I agree with all that, but patternic is not a word.
To you. English has a variety of suffixes that allow for exactly this kind of constructive generation of new words. Thus 'patternic' is a word precisely because it has just been coined. Or do you only ever approve of "dictionary" words without pondering where they might come from?