On Thu, Jul 9, 2020 at 9:40 AM Markus Heukelom <markus.heuke...@gain.pro> wrote:
>
> Many data structures are implemented using pointers. How does this perform 
> wrt garbage collecting? For example take any data structure that uses some 
> tree of nodes, or even simpler just a singly linked list: there is one 
> pointer from the left node to the next. Wouldn't this incur huge GC 
> (unavoidable) cycles for large linked lists that will look at all pointers? I 
> can imagine that a large AVL tree would then cause potentially large hick-ups 
> in the GC cycle?
>
> Of course, luckily we have map where you can just store the values. But if 
> people start using generics for their own map types, or trees, wouldn't this 
> potentially be a bigger problem? Wouldn't this defy the use of generics for 
> data structures aimed to manage a lot of data?
>
> I don't know how Java does this for example, or if it is no practical issue 
> at all. In C++ you can you ref counting for the nodes, in C you would use a 
> arena allocator probably for the nodes etc. I was wondering how this relates 
> to Go.

There is no special connection between the builtin map type and the
garbage collector.  The builtin map type uses pointers too.  The
garbage collector has to trace those pointers.

Go uses a concurrent garbage collector, so a large linked list won't
cause any sort of program pause.  The garbage collector does use CPU
time, and it will use more CPU time for data structures that use more
pointers.  But I don't see that as being particularly tied to
generics.  If a program needs to build data structures with lots of
pointers, it will build those data structures whether or not the
language has generics.

In fact, there is a good argument that generics will reduce the number
of pointers overall.  Without generics, a general purpose data
structure will use interface types, and in the current implementation
interface types always contain two pointers.  With generics, a general
purpose data structure will store the value directly; if the value
does not contain any pointers, then the overall effect will be fewer
pointers and less GC work.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUQLF38t_R1LYkqk2TGDM3d6vtwE6oZZQGb1Fw6-%3DLUhg%40mail.gmail.com.

Reply via email to