shared array?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 11 23:23:01 PDT 2015


On Friday, September 11, 2015 23:29:05 Laeeth Isharc via Digitalmars-d-learn wrote:
> On Friday, 11 September 2015 at 21:58:28 UTC, Adam D. Ruppe wrote:
> > On Friday, 11 September 2015 at 21:48:14 UTC, Prudence wrote:
> >> Oh really?!?! I thought slicing used the GC? Is this a recent
> >> development or always been that way?
> >
> > Always been that way. A D slice is just a C pointer + length
> > packed together. A slice simply increments the pointer and/or
> > decrements the length - no allocation needed.
> > (btw the garbage collector is actually pretty nice, why are you
> > avoiding it anyway?)
>
> Seems to be quite a lot of FUD wrt use of standard library and
> GC, which means also perhaps we don't communicate this point very
> well as a community.  Making Phobos GC-optional perhaps is an
> ultimate answer.  But people seem to think that you're back to C
> without the GC.

Aside from the few classes in Phobos, its GC usage is almost entirely
restricted to when it allocates arrays or when it has to allocate a closure
for a delegate, which can happen in some cases when passing predicates to
range-based algorithms. Avoiding functions that need to allocate arrays
avoids that source of allocation, and using functors or function pointers
as predicates avoids having to allocate closures. So, you _can_ end up with
GC allocations accidentally in Phobos if you're not careful, but on the
whole, the assertion that Phobos uses the GC heavily is FUD - or at least a
misunderstanding. But as we make more of the functions use lazy ranges
rather than arrays (particularly with regards to strings), and we make more
of the code @nogc, it becomes even clearer that the GC isn't involved. Also,
improvements to how lambdas are handled should reduce how often closures
have to be allocated for them.

Probably the may issue that actually requires a language change is that
exceptions currently pretty much have to be GC-allocated whereas they really
should be reference-counted (though possibly still GC-allocated, just not
left for a garbage collection cycle), but exceptions are thrown rarely,
meaning that the fact that they're GC allocated is really only a problem if
you're trying to avoid the GC completely rather than just minimize its use,
so it's rarely a problem. And there are plans to support reference-counted
types in the language, in which case, the exception types would be change to
use that Object hierarchy rather than the normal one.

So, we have improvements to make, but for the most part, I think that the
idea that Phobos requires a GC is FUD. It's not completely false, but folks
seem to think using Phobos means heavy GC usage, and for the most part,
that's not true at all.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list