Nim has a new garbage collector called Orc (enabled with --gc:orc). It’s a 
reference counting mechanism with cycle direction. Most important feature of 
--gc:orc is much better support for threads by sharing the heap between them.

Now you can just pass deeply nested ref objects between threads and it all 
works. My threading needs are pretty pedestrian. I basically have a work queue 
with several work threads and I need work done. I need to pass large nested 
objects to the workers and the workers produce large nested data back. The old 
way to do that is with channels, but channels copy their data. Copying data can 
actually be better and faster with “share nothing” concurrency. But it’s really 
bad for my use case of passing around large nested structures. Another way was 
to use pointers but then I was basically writing C with manual allocations and 
deallocations not nim! This is why the new --gc:orc works so much better for me.

You still need to use and understand locks. But it’s not that bad. I just use 
two locks for input queue and output queue. They try to acquire and release - 
hold the locks - for as little as possible. No thread holds more than 1 lock at 
a time.

See my threaded work example here: 
[https://gist.github.com/treeform/3e8c3be53b2999d709dadc2bc2b4e097](https://gist.github.com/treeform/3e8c3be53b2999d709dadc2bc2b4e097)
 (Feedback on how to make it better welcome.)

Before creating objects and passing them between threads was a big issue. 
Default garbage collector (--gc:refc) gives each thread its own heap. With the 
old model objects allocated on one thread had to be deallocated on the same 
thread. This restriction is gone now!

Another big difference is that it’s more deterministic and supports 
distructors. Compilers can also infer where the frees will happen and optimize 
many allocations and deallocations with move semantics (similar to Rust). Sadly 
it can’t optimize all of them a way that is why reference counting exists. Also 
the cycle detector will try to find garbage cycles and free them as well.

This means I do not have to change the way I write code. I don’t have to mark 
my code in any special way and I don’t really have to worry about cycles. The 
new Orc GC is simply better.

This makes the new garbage collector--gc:orc a joy to use.

(If there are any factual errors about the GC let me know.) 

Reply via email to