by pizza234 · 5y ago · 0 comments
> Scope-based memory management (destructors are injected after the scope) - generally reduces RAM usage of the programs and improves performance.
How does Nim handle references? How similar is this to Rust's lifetimes GC-ed instead of manually managed?
Is this essentially like Rust, with the main difference that lifetimes (references) are handled via reference counting?
> Shared heap - different threads have access to the same memory and you don’t need to copy variables to pass them between threads - you can instead move them
I think this is the same as Rust thread move semantics?
That's what I understood at least. So ya, it does look like some kind of hybrid between statically inferable ownership semantics, reference counting, ARC and GC.
I read that Nim's GC is optional, does this mean it can be deactivated and when the static memory management fails, I can do it manually for the edge cases that would otherwise done by the GC?
Hope this makes sense, haha.
I think that's an okay way to put it. Though it might be best to think of it the other way around. It'll default to GC and copying data around (pass by value) and will try to optimize things if it can to more deterministic memory management and pass by reference.
> I read that Nim's GC is optional, does this mean it can be deactivated and when the static memory management fails, I can do it manually for the edge cases that would otherwise done by the GC?
It isn't optional like that. It's more that by default all data is passed by value and allocated on the stack. And if you want to create things on the heap, you need to be explicit about it, at which point you can choose if you want the pointer to be managed by Nim's GC or manually. That's done by having two type of pointers, ref will be under GC, and ptr won't.
Sort of. You either deactivate it completely with --gc:none ; or, you do it on per-variable basis by using a "ptr" (pointer) type, which is outside of the GC's realm. Any variable declared "ref" (reference) is within the realm of Nim's GC, and will be tracked by the gc you chose (none, boehm, mark&sweep, arc, orc, ...).
Additionally, you can define type bound operators to help the GC do its job, but IIRC (and I'm not sure I understand correctly), that doesn't turn off arc/orc - rather, it tells them how to apply some things.
You're talking accross threads I believe. But accros scopes they are passed by reference I believe.
ORC provides performance comparable to Rust but without having to write the boilerplate.
Environments like Mesa/Cedar at Xerox PARC already made use of it, back in 1985.
https://bitsavers.informatik.uni-stuttgart.de/pdf/xerox/parc...
https://archive.org/details/bitsavers_xeroxparctddingGarbage...