3 ms·
The reason GHC allocates at such an aggressive rate is the fact that memory allocation is really cheap in GHC. Allocating memory on the GHC heap is literally ju
by merijnv 12y ago
The reason GHC allocates at such an aggressive rate is the fact that memory allocation is really cheap in GHC. Allocating memory on the GHC heap is literally just bumping the "top of heap ptr", so writing one word.
The idea is that 90% of allocations are very short-lived, so allocating a ton doesn't matter as long as you can throw it away cheaply. Suppose we have an inner loop over an array, summing values. We may allocate at 1.6 GB/s doing the computation, but everything except the last value is garbage.
When we GC we simply copy this last value from the current heap and reset the "top of heap pointer". Yet another cheap operation.
There's a lot more going on (generational GC, nursery's and what not), but GHC's GC heavily abuse some facts that, for example, CRuby cannot, such as the fact that older generations can never reference data in newer generations, so GCing the current generation never has to perform "major GC" (i.e. checking the older data whether it still needs the current generation).