Allocation strategy

^ Up

2026 July

While implementing the "list pool" that offers packed allocations of small arrays for maximum memory density and cache locality for AST nodes, I've made the realization:

I can't necessarily rely on a platform's allocator actually aligning an allocation of 4K exactly to one page. Some allocators add various metadata right next to the pointer returned to the user, so you can't always be sure that you're making optimal use of natural boundaries like pages or cache lines.

Furthermore, since I'll be using 32-bit heap indexes instead of raw pointers, I'll need to implement some kind of custom allocator that stays within a given region of memory.

For these reasons, and also because it's fun, I've decided that I'll completely ditch platform provided allocators and implement entirely custom memory allocation based purely on mmap() or equivalents.

I'll need a number of different allocators, based on purpose:

And maybe more.

The sort-of general-purpose one may actually serve as the core underlying allocator for everything else; I'm not yet sure.

In any case, I came up with the following interesting design for this internal allocator:

I'm not sure if I've missed anything significant. Maybe once I start implementing this, I'll notice some glaring problems, but so far it sounds quite good.

The biggest size class could actually be used for VM thread stacks; there should probably be a sane upper limit on the number of native threads, like 1024 or something, and beyond that the user is expected to use some kind of green thread thing.

The list heap, which uses the list pool sub-allocator, should use a modified version of this core allocator, since it needs to be limited to 32 GiB total, and doesn't need so many different underlying size classes; it has its own size class logic.

As for the generic heap, I'm not sure. We will probably need a large number of size classes, but also need to stick to 32 GiB total, while it's also not acceptable to limit each size class to a small cap like 2 GiB, since some user applications could make very heavy use of one or two specific size classes. So I guess the main heap will need a totally different strategy. The main heap will also be the main one needing a sophisticated garbage collector, so I guess it's OK to not use this super simple core allocator for it.

Addendum A

To prevent false sharing issues, it may be best to have 64 bytes be the smallest size class. Objects smaller than this should generally reside on the stack, or be allocated from some kind of pool; remember again this isn't for regular language-domain arbitrary heap objects.

Sticking to 16 size classes, this makes the largest 2 MiB, of which there can only be 2048, which is fine. Reducing to 8 size classes would make the biggest 8 KiB, which is no good.

The 32-bit index, by the way, should use a scaling factor (say 8, so it's zero-cost via the LEA instruction) so it can't overflow, even if the 4 GiB slab for a size class is saturated. This means we could go up to e.g. 16 GiB slabs (for a total of 256 GiB of vmem) which fully eliminates concerns about ever saturating a size class.

This also enables a remarkably clean strategy for checking for OOM: Use a signed 32-bit integer for the watermark, and let it overflow to negative when the last slot of a size class is used. Now we only need to check if it's negative before an allocation.

The new 16 GiB slab size means we can have 8192 active slots of the biggest 2 MiB size class, which is even finer than 2048.

New Summary:

The previously discussed "low-cost high-yield" optimization of simply decrementing the watermark if the last allocation is freed does not actually make sense in a multi-threaded context with thread-local caches, so scrap that. However, the following are possible:

If either or both of these is implemented, we could then also free memory back to the OS if a slab watermark drops significantly.

Addendum B

Gemini just taught me a little trick: Threads can reserve N slots per atomic operation. This works as follows:

This would also help against false sharing on Apple Silicon with its 128 byte cache lines. Although it can still happen if a 64-byte slot is released into the global free list, and another thread picks it up while its neighboring 64-byte slot within the same cache line remains in use by another thread.

A small amount of false sharing on Apple Silicon isn't too bad, but there's ways to mitigate it if we ever find it to be an issue. (This would be ridiculously far in the future of Xisp.)

One could make the chunk size N dependent on the size class, so that small size classes reserve chunks of 128 or so, while huge classes don't reserve any extras because you don't want a thread to reserve from the sparse number of available slots only to never use it.

Since the requested size class is generally comptime-known, we don't cause any additional computation for the "calculation" of the chunk size N for a size class.