Hacker News new | ask | show | jobs
by jimbob45 14 days ago
Nim still has the GC, right? It always seemed like a slightly better Golang, much worse Dlang in that respect.
3 comments

Optionally but not by default ... things have changed a lot:

https://nim-lang.org/docs/mm.html

Reference counting by default is GC.
IIRC, ref counting is at least more deterministic than traditional GC because it only runs when you destruct the object (it goes out scope, object gets reassigned whatnot).

In Comparison, traditional GCs like JVM runs on a background thread, it's hard to determine when they run and generally blocks the whole program while collecting (stop-the-world).

So at least they are a little better in that regard?

I can’t speak of Nim’s implementation but reference counting can block the whole program as large object graphs have to decrement their counts. But you are right it is more deterministic.

Javas modern GCs also have extremely low pauses. ZGC is effectively pauseless, you’re likely to experience kernel jitter/traps that last longer than a ZGC pause.

Refcounting only cause problem when using `ref` counted type, which is also discouraged most of the time when not necessary.
Does it suffer from the same trap as Dlang, in that their GC is also optional, but you lose the many of the conveniences from the standard library if you forgo completely with it?
ref counting is only performed if you use `ref` types, e.g.

  type 
    RefObj = ref object
      foo: string
      bar: int

    ValueObj = object
      foo: string
      bar: int

`RefObj` is ref counted equivalent to a shared_ptr in C++ land (non-atomic counter unless using --mm:atomicArc or Nim3/Nimony has this as a default I believe). `ValueObj` is equivalent to struct in C, C++, etc.
Nim's better than dlang in this respect. Default memory management is similar to C++ and Rust. See https://nim-lang.org/docs/destructors.html
Nim has opt-in RC using the `ref` keyword; the default is stack-managed single-owner lifetimes, including for heap types. Passing is borrowing by default, while moving must be explicit; the vice-versa to Rust's approach. Data is technically cleaned up by the RC's procedures, but the destroy hooks are always called at the end of scope. And like I said, `ref` or `ptr` short-circuits that.