3 ms·
Why does jemalloc need to lock in userspace in order to allocate memory? Isn't this an implementation detail of how the kernel allocates memory? I'm assuming
by jupp0r 4y ago
Why does jemalloc need to lock in userspace in order to allocate memory?
Isn't this an implementation detail of how the kernel allocates memory?
I'm assuming jemalloc is maintaining some sort of cross thread allocation pool?
- saagarjha 4y agoKernels allocate memory and hand pages back. Allocators in userspace have the job of carving these up for application use, and must do so across multiple threads, necessitating locking or similar primitives.
- jupp0r 4y agoMy understanding was that allocators nowadays have cache pools per cpu or thread and only rarely need to lock on global state (when imbalances between pool sizes necessitate shifting resources around).
- saagarjha 4y agoYou are correct, but there are still places where synchronization is necessary.
- gsvelto 4y agoThey do and so does jemalloc in Firefox, however that lowers contention, it doesn't let you do away with synchronization. Consider this simple scenario: a thread allocates a chunk of memory from its per-thread pool, but the object is later released by another thread. You can't do that w/o synchronization around the pool. Per-CPU pools are even trickier because a thread might be moved around between different CPUs by the scheduler while it's allocating memory. So you need special facilities to implement those in user-space like restartable sequences: https://lwn.net/Articles/650333/ https://lwn.net/Articles/650333/
- saagarjha 4y agoAmusingly macOS also has this as SPI, it's used for the Objective-C runtime.