Why many programmers don't like GC?

IGotD- nise at nise.com
Fri Jan 15 15:18:31 UTC 2021


On Friday, 15 January 2021 at 14:24:40 UTC, welkam wrote:
>
> No. And it will never will. Currently DMD uses custom allocator 
> for almost everything. It works as follows. Allocate a big 
> chunk(1MB) of memory using malloc. Have a internal pointer that 
> points to the beginning of unallocated memory. When someone ask 
> for memory return that pointer and increment internal pointer 
> with the 16 byte aligned size of allocation. Meaning the new 
> pointer is pointing to unused memory and everything behind the 
> pointer has been allocated. This simple allocation strategy is 
> called bump the pointer and it improved DMD performance by ~70%.
>
> You can use GC with D compiler by passing -lowmem flag. I didnt 
> measure but I heard it can increase compilation time by 3x.
>
> https://github.com/dlang/dmd/blob/master/src/dmd/root/rmem.d#L153

Actually druntime uses map (Linux) and VirtualAlloc (Windows) to 
break out more memory. C-lib malloc is an option but not used in 
most platforms and this option also is very inefficient in terms 
of waste of memory because of alignment requirements.

Bump the pointer is a very fast way to allocate memory but what 
is more interesting is what happens when you return the memory. 
What does the allocator do with chunks of free memory? Does it 
put it in a free list, does it merge chunks? I have a feeling 
that bump the pointer is not the complete algorithm that D uses 
because of that was the only one, D would waste a lot of memory.

As far as I can see, it is simply very difficult to create a 
completely lockless allocator. Somewhere down the line there will 
be a lock, even if you don't add one in druntime (the lock will 
be in the kernel instead when breaking out memory). Also merging 
chunks can be difficult without locks.



More information about the Digitalmars-d-learn mailing list