Smart pointers instead of GC?

Sven Over dlang at svenover.de
Mon Dec 31 04:36:14 PST 2012


On Wednesday, 26 December 2012 at 11:43:55 UTC, Benjamin Thaut 
wrote:
> In my experience its quite some work to remove the GC entierly, 
> but it is possible.

Hi Benjamin! I've seen your druntime and thBase repos on GitHub. 
Very interesting stuff. As I have little (or to be honest: no) 
experience in D it's difficult for me to judge how much work 
needs to be done. Is the idea to replace Phobos completely?

> Also it makes writing Code also easier ans more effektive in 
> some places
> because:
> -You don't have to register manually allocated blocks with the 
> gc
> -You control the exact order of destruction
> -You control the time of destruction
> -You control in which thread a object gets destructed
> -You will be encouraged to not write code that does do 100 
> allocations just to format a string (because it will leak)

I personally agree with all your points, but I must admit that 
their relevance depends on the programming patterns you are 
using. Experienced GC programmers certainly favour patterns that 
work well with GC. I personally prefer a style of programming 
that makes use of destructors and relies on them being executed 
at the right time.

> But obviously it has an impact on productivity.

I'm not sure about this at all. I write software that is designed 
to offer a certain performance, and latency is a big issue. I 
don't believe that I can achieve that with a "just allocate 
memory and don't ever care" attitude. I would need some 
background knowledge about how the GC works, and write code that 
doesn't screw up badly. Manual memory management also requires me 
to have an understanding of the implications behind it. But there 
are well-tested helpers (e.g. smart pointers) that simplify the 
task a lot.

I do admit that, when performance (both in the sense of being 
fast and offering reliable performance) is not such a critical 
issue, then GC won't hurt and "just allocating" is fine and of 
course reduces development time. However, I tend to just use 
Python for this class problems.

It comes down to a matter of choice and personal preference. I'm 
not saying that GC is bad and shouldn't be used. Many people are 
very happy with it and it does a great job for them. But my 
personal preference is manual memory management (then again 
greatly automated by use of reference counting and smart 
pointers).


More information about the Digitalmars-d mailing list