4 ms·
why are heap allocations bad for performance? 1. You have to call to your allocator. Then do some type of search for free memory, of the approximate size. Then
by valarauca1 10y ago
why are heap allocations bad for performance?
1. You have to call to your allocator. Then do some type of search for free memory, of the approximate size. Then flag this memory as used. Then mark the remaining memory in that block as unused. Then mark the internal books to match the new layout.
This is done efficiently, but modifying these collections take time. AND IT STILL FASTER THEN:
2. Calling the kernel to MMAP in new virtual memory, adding that to the pool, and well restarting this process all over again.
Allocation time is a big cost, and there is work to make allocation lazy by default in the Rust Compiler at the minute.
- marcosdumay 10y agoAlso, make sure #1 is done atomically. Your request will either get in a queue, wait for a mutex, or create many more of #2 than needed because the pool isn't shared between threads.
- valarauca1 10y agoRust uses jemalloc which has per-thread-pooling (sort of). There is very little lock contention as there are multiple pools per-thread. Also per thread pooling doesn't use more memory then not. In some cases it actually uses less. Citation: https://people.freebsd.org/~jasone/jemalloc/bsdcan2006/jemalloc.pdf https://people.freebsd.org/~jasone/jemalloc/bsdcan2006/jemal...