Nice work. You did a field-study. I think your measurements seem correct and your analysis is good, too.
You say that you are moderately allergic to Linux. Well, I'm extremely allergic to Windows and I would rather change profession than work on Windows. Anyway, I'm saying this just because I know how this works on Linux so YMMV on Windows, but I think it shouldn't be too different.
Your twitter friends told you that page commit affect latency. They are correct. At least on Linux, overcommit is the default and pages will not be allocated until they are used. Essentially, when your program asks the kernel to allocate new memory, the kernel just says "sure here you go" and virtual memory is allocated. This virtual memory will not be mapped to physical memory (or any equivalent) until it's used.
But there's more to it. malloc() and free() do not directly talk with the kernel. Instead, they build bins of memory regions. There are various implementations, but the basic idea is that there's always a little extra bins to be given to the program. Only once suitable bins run out, the library ask the kernel for more memory.
The kernel interface is mmap(). With mmap() the kernel gives a new memory region which the library then further splits into suitable bins for the program to use. Note that because of this approach the regions can empty whereafter the library can deallocate the memory with munmap().
As you can see here, when the memory usage profile is not varying too much, the memory allocation library needs to do a lot less work. Likewise, the memory had already been linked to real physical memory and there will be no page faults for the CPU and kernel to handle.
I think this theory is well aligned with your results. When the game loads a new map, menu etc. the calls to malloc()/free() take a longer time to complete. This makes sense because there's more going on. When the actual game is running there's a similar amount of memory allocated and memory freed resulting in less work and hence less latency.
When the actual game is running there's a similar amount of memory allocated and memory freed resulting in less work and hence less latency.
There are 2 other important parts.
First of all, it's important to note that modern malloc implementations use allocation classes, for example: 1, 2, 4, 8, 12, 16, 24, 32, ... and that they typically keep a thread-local "slab" for each allocation class. This means that a malloc of 29 bytes can reuse a freed 25 bytes block (because it was effectively 32 bytes) if that block had been allocated from the same slab.
Essentially, allocation is:
Allocate from thread-local slab, if there is space.
Otherwise, obtain new slab for the allocation class from a global pool, if there is any.
Otherwise request memory from the OS.
The first is typically fast -- hence the 25ns -- and it gets progressively slower as you go down.
It's also important to note that freeing is complicated. Most importantly is the fact that the allocation-size and allocation-alignment are not passed to free: only the pointer. This makes recovering the slab from which the allocation comes potentially complicated.
Finally, in my experience, free performs consolidating work from time to time, leading to large latency spikes. That's annoying.
It's also important to note that freeing is complicated. Most importantly is the fact that the allocation-size and allocation-alignment are not passed to free: only the pointer. This makes recovering the slab from which the allocation comes potentially complicated.
This made me wonder if there is any sort of malloc/free replacement that uses some sort of fat pointer to provide more information to free... Would it even make sense? Obviously it wouldn't work as a drop-in replacement.
I’ve written memory allocators. One good trick is to put block size information (among other things) at the beginning of a page (or larger aligned block) so you can find it immediately by rounding a free’d block’s address down to the start of its page.
The problem, of course, is knowing how much to round by.
free will receive allocations of 16 bytes and allocations of 574 MB alike, no matter how big your "page" size is, you need to be able to handle larger allocations.
I like mimalloc's trick of encoding the allocation category in the alignment of the pointer, personally.
Large allocations (page or bigger) are easier to align on page boundaries (plus header size) than medium-sized ones. Avoiding wastage for sizes bigger than half a page is the challenge. Alignment can serve as a signal that the allocation has a per-allocation header.
jemalloc and tcmalloc both provide alternative APIs where one can specify size and alignment when freeing a pointer. Empirical tests didn't really show much performance benefit, that I recall.
I know the Rust's Allocator API specifically requires that both be specify when freeing in order to give allocator implementers the option to make use of the information.
Empirical tests didn't really show much performance benefit, that I recall.
On the contrary -- passing size information to the deallocation functions is probably the single biggest "one weird trick" you can get in an allocator implementation using caching and out-of-band metadata, to the extent that both jemalloc and tcmalloc sort of regard plain-old-free as a slow path these days. Something like a 30% improvement on deallocation in practice even for a heavily optimized size lookup pathway.
Interesting. Rust used to use jemalloc's special deallocation functions, but from memory the reason it was removed was that it didn't provide much and thus it was easier to just conform to the standard interface.
I suppose your 30% improvement is specifically about deallocation itself, and at the time the Rust benchmarks measured whole program performance... still in heavy allocation situations I'd have expected 30% to make a dent...
Interesting. Rust used to use jemalloc's special deallocation functions, but from memory the reason it was removed was that it didn't provide much and thus it was easier to just conform to the standard interface.
I think the last time I looked at it, the Rust sized deallocation function ended up being a call to free (I think because of NULL handling rules around sdallocx). And once people started looking into why a little more closely, Rust had stopped shipping with jemalloc by default, so using sized deallocation would have required a sort of complicated configuration detection step. (Avoiding complicated configuration checks was one of the motivations for N2699, which got adopted by C, so hopefully things will get easier in that department soon).
I suppose your 30% improvement is specifically about deallocation itself, and at the time the Rust benchmarks measured whole program performance... still in heavy allocation situations I'd have expected 30% to make a dent...
Right -- we (jemalloc/Facebook) measured it at something like 1-1.5% of fleet CPU end-to-end at the time we turned on C++ sized deallocation by default (with the TCMalloc/Google people reporting similar numbers). It might be possible to come up with screwy allocation patterns where having the size hint doesn't help at all, but I'd be surprised (like, really, the two paths are mostly the same, except that one has a "do a complicated lookup" step added in).
Right -- we (jemalloc/Facebook) measured it at something like 1-1.5% of fleet CPU end-to-end at the time we turned on C++ sized deallocation by default (with the TCMalloc/Google people reporting similar numbers).
That's a fairly sizeable improvement at large scale!
15
u/OCPetrus Jun 03 '22
Nice work. You did a field-study. I think your measurements seem correct and your analysis is good, too.
You say that you are moderately allergic to Linux. Well, I'm extremely allergic to Windows and I would rather change profession than work on Windows. Anyway, I'm saying this just because I know how this works on Linux so YMMV on Windows, but I think it shouldn't be too different.
Your twitter friends told you that page commit affect latency. They are correct. At least on Linux, overcommit is the default and pages will not be allocated until they are used. Essentially, when your program asks the kernel to allocate new memory, the kernel just says "sure here you go" and virtual memory is allocated. This virtual memory will not be mapped to physical memory (or any equivalent) until it's used.
But there's more to it. malloc() and free() do not directly talk with the kernel. Instead, they build bins of memory regions. There are various implementations, but the basic idea is that there's always a little extra bins to be given to the program. Only once suitable bins run out, the library ask the kernel for more memory.
The kernel interface is mmap(). With mmap() the kernel gives a new memory region which the library then further splits into suitable bins for the program to use. Note that because of this approach the regions can empty whereafter the library can deallocate the memory with munmap().
As you can see here, when the memory usage profile is not varying too much, the memory allocation library needs to do a lot less work. Likewise, the memory had already been linked to real physical memory and there will be no page faults for the CPU and kernel to handle.
I think this theory is well aligned with your results. When the game loads a new map, menu etc. the calls to malloc()/free() take a longer time to complete. This makes sense because there's more going on. When the actual game is running there's a similar amount of memory allocated and memory freed resulting in less work and hence less latency.