News for hackers. Stuff that matters.
Re:Garbage Collection by vidarh on Saturday November 30, 2019 @02:14PM (#21671651)
This is great. Garbage collection is one of those things that tends to scare people, even though a basic garbage collector can be very simple (though a naive approach is usually also horribly slow..). Great to see the amount of illustrations to make it easier to grasp.
Re:Garbage Collection by munificent on Saturday November 30, 2019 @02:49PM (#21671822)
> Garbage collection is one of those things that tends to scare people

That's exactly why I was excited to write this chapter. They have a reputation for being hard, but the core algorithm is really just a simple graph traversal.

Re:Garbage Collection by amelius on Saturday November 30, 2019 @02:46PM (#21671811)
Designing and implementing a low latency concurrent garbage collector is actually at least as difficult as writing an optimizing compiler. Many languages today still struggle with concurrency+GC (see e.g. python, ocaml). Good to see this chapter on GC, but I think the subject deserves an entire book!
Re:Garbage Collection by stevekemp on Saturday November 30, 2019 @03:14PM (#21671930)
I recently read this piece, which I think has been posted here before, which walks through the process in an isolated fashion:

https://journal.stuffwithstuff.com/2013/12/08/babys-first-ga...

I've implemented a couple of scripting languages, but thus far I've ignored GC. It is something I'd like to experiment with in the future though.

Re:Garbage Collection by cracauer on Saturday November 30, 2019 @02:54PM (#21671849)
A GCed piece of nontrivial code can also be faster than a malloc/free solution.

The reason being that allocation can be much faster. The GCed code can allocate memory with as much as an increment of a pointer (and that is atomic, so no thread locking needed).

malloc/free always do full function calls, and they might/will descent into dealing with fragmentation (aka finding free space). Likewise, free() isn't free and cross-thread allocation/deallocation can further complicate things.

Re:Garbage Collection by YorickPeterse on Saturday November 30, 2019 @08:15PM (#21673161)
> increment of a pointer (and that is atomic, so no thread locking needed).

Nitpick (and mostly for others reading): incrementing the address of a pointer if done locally (as in, using a local variable of sorts) may be atomic, but storing the updated pointer somewhere may not. If a bump allocator wants to support concurrent allocations, it should make sure to use the right atomic operations (typically this just a compare-and-swap of the old pointer with the locally incremented one).

Re:Garbage Collection by jashmatthews on Saturday November 30, 2019 @06:44PM (#21672793)
Is this universally true? Redis for example vendors jemalloc so it’s entirely possible for malloc to mostly inline? IIUC malloc isn’t a syscall like the underlying sbrk and mmap calls that malloc implementations use to get memory from the kernel?
Re:Garbage Collection by cracauer on Saturday November 30, 2019 @07:57PM (#21673090)
Sure, you can change any of the individual properties.

But if you cannot move memory (adjust pointers like most GCs do) then you will have to deal with fragmentation, which slows down allocation (or causes other drawbacks).

Moving GC can also make the program faster due to memory compactation and hence being more efficient wrt CPU cache and TLB.

Re:Garbage Collection by vidarh on Sunday December 01, 2019 @02:21AM (#21674318)
Fragmentation issues depends greatly on language. E.g I'm (perpetually, it seems) working on a Ruby compiler, and I slotted in a naive gc as a starting point and instrumented it.

Turned out the vast majority of allocations are tiny object instances of a handful of different sizes. As a result minimizing fragmentation is as easy as allocating pools of a fixed size for small objects, and round up larger objects to a multiple of block size. There are still truly pathological patterns possible, and a compacting / generational gc may still be worth it later both to deal with that and to reduce the cost of a collection, but for a lot of uses you can get away with simple options like that.

Re:Garbage Collection by umanwizard on Saturday November 30, 2019 @09:45PM (#21673480)
Malloc is not a syscall, you're right. But it's a quite complicated funtion, so it wouldn't make sense for the compiler (or linker) to inline it wherever it's called.
Re:Garbage Collection by jashmatthews on Sunday December 01, 2019 @12:57AM (#21674096)
jemalloc is deliberately structured so that the fast path is all inlined and all unlikely paths which would cause it to blow the inlining complexity budget get pushed out.
Re:Garbage Collection by umanwizard on Sunday December 01, 2019 @03:09AM (#21674485)
Interesting. I didn't know that, but it sounds plausible.
Re:Garbage Collection by z92 on Saturday November 30, 2019 @02:33PM (#21671741)
Garbage collection isn't that slow. I find allocating off the fragmented memory after collection slower.
Re:Garbage Collection by vidarh on Sunday December 01, 2019 @02:10AM (#21674294)
A good gc isn't slow. But a naive one that tends to be awful. E.g common problems involve thrashing cache by touching the entire heap regularly.