(Obligatory preface: it doesn’t matter what folks on the Internet say, do what makes sense for your use-case)
There are two somewhat related, but very different ideas here: what I call Reserve First, and what we at TigerBeetle call Static Memory Allocation, but which is perhaps better understood under its original catchy moniker of “no dynamic memory allocation after startup”.
Reserve First is a local (a single function body), positive advice: if you need to add an item to two array lists, don’t write code like
try xs.append(gpa, item);
try ys.append(gpa, item);
instead, write something like
try xs.ensureUnusedCapacity(gpa, 1);
try ys.ensureUnusedCapacity(gpa, 1);
xs.appendAssumingCapacity(item);
ys.appendAssumingCapacity(item);
The latter pattern deals much better with partial failure: if only some allocations succeed, you still end up in logically consistent situation. Eg, if there’s an invariant that xs and ys have the same length, you don’t need to worry that one would be longer than the other if the second allocation fails.
The magic here is that reservation operations acquire physical resources, but don’t change logical state of the program, are idempotent, and make subsequent operations infallible.
This rule-of-thumb you can apply pretty mechanically in any function that takes a gpa as an argument.
Static Memory Allocation is a different beast. It’s a global (whole program), negative advice: just never allocate after startup at all. Don’t minimize the amount of allocs after startup allocation, make it the exactly zero.
Unlike Reserve First, this is not a sprinkle of magic dust you can add anywhere to your program to taste. This generally requires ground-up design of the application/library around the idea of known, explicit, static limits for everything. You do a data-oriented analysis of the problem you are solving, and then you change the definition of the problem such that the solution allows for static allocation.
E.g, TigerBeetle is a database, where the bulk of data are bytes on disk. The data gets into and out of a database via network request. It doesn’t make sense to make network requests arbitrarily large, as that increases latency, and makes it more likely that a request fails. Therefore, there could be a physical size limit on how large a single request could be. For TigerBeetle, that is 1MiB.
Now, if you always add to your ocean of data by 1MiB data drop at a time, you could actually design the whole thing such that you need only O(1MiB) extra memory. If you manage to limit the input, the output/intermediate state is usually limited, even if discovering the limits takes a lot of effort. Eg. because we know the input is 1MiB, and we know that one financial transfer is 128 bytes, that means we are doing at most 8K transfers at a time. Each transfer touches two accounts, so it’s enough to have 16k accounts in memory at a time. … 1000 similar reasoning steps later … And, therefore, TigerBeetle allocates all memory at a startup.
Note that the reasoning is basically repeated in code. We don’t allocate memory for 16k accounts. There’s only one constant, message_size_max = 1MiB, and everything else is derived in code. We compute transfers_count_size_max as divCeil(message_size_max, @sizeOf(transfers)), etc. It’s really quite amazing how compositional the whole thing turns out to be at the end, you never quite know the global limit in bytes, you derive local limits in the number of logical objects, you allocate those dynamically (at startup), and the not-known-up-front total is your allocation.
As another example, we’ve recently implemented a new CI system for TigerBeetle, and that also follows static allocation. CI system basically tracks job. To make static allocation work, we set a constraint that there can be at most N jobs at a time. What if we are very productive and need more than N? We just drop the oldest, the least relevant jobs. The universe of jobs is potentially infinite, but we take the N highest priority ones.
Regardless of these considerations, I highly recommend the following exercise: look at every function that both:
- takes an
Allocator argument,
- contains a
try statement.
and try to find memory management errors on paths that throw errors. In my experience, its highly likely that you will find a lot of bugs, which could teach you about error-prone patterns of managing memory.