4 ms·
Right, and those destructors get called at the of the variable's scope. Is there a reason why it's not well-founded to consider scopes to be regions?
by miguelraz 5y ago
Right, and those destructors get called at the of the variable's scope.
Is there a reason why it's not well-founded to consider scopes to be regions?
- steveklabnik 5y ago> Is there a reason why it's not well-founded to consider scopes to be regions? You're running into a colloquial vs formal distinction, I think. Yes, a scope is a "region" in the sort of general sense, but in PLT, "region based memory management" is jargon: https://www.cs.umd.edu/projects/cyclone/papers/cyclone-regions.pdf https://www.cs.umd.edu/projects/cyclone/papers/cyclone-regio... If you check 2.1 of that paper, you'll see it's something sort of like "arenas with optional GC if you want to reclaim memory", which is quite different than what Rust does by default.
- miguelraz 5y agoThanks, for the paper Steve, I'll edit the post with due credit. What would be the appropriate description for Rust's memory model? Automatic scoped memory management with opt-in reference counting?
- Diggsey 5y ago"Memory model" is also an academic term which (among other things) describes how memory behaves WRT to concurrent opreations, so I would not use it in this context. Memory management is the correct term. Rust the language does not do any memory management: memory management is a feature of the standard library. I would say that the Rust standard library (the `alloc` crate specifically) provides types that safely encapsulate allocation and deallocation of memory. This is enabled by automatic destructor calls on stack-allocated variables when they go out of scope. (However, note that destructors need not deallocate memory). Again, this part works exactly the same in C++. In C++ when a type's constructor acquires some resource, and its destructor releases that resource (in this case the resource being memory) it is referred to as "Resource Acquisition Is Initialization" or RAII. https://en.wikipedia.org/wiki/Memory_model_(programming) https://en.wikipedia.org/wiki/Memory_model_(programming)
- oconnor663 5y agoYeah "exactly the same as C++" is really very accurate here, and there's really not much else to say. I find it a little odd that the Wikipedia article even mentions "reference counting is optional", because it makes Arc sound like a central feature of the language, but I don't think anyone thinks about shared_ptr that way.
- quietbritishjim 5y agoTo add to that, I can imagine implementing a sort of fully garbage collected smart pointer in both languages (but maybe not compacting), but surely no one would describe either of them as an "optionally garbage collected language".
- pjmlp 5y agoJust extending your explanation that Ada also does RAII, there they are called controlled types. And to certain extent, C++ has influenced Turbo Pascal/Delphi as well with similar approach.
- kibwen 5y agoThe objection is to the term "garbage collection", not the term "region". "Garbage collection" implies dynamic lifetime determination. Rust's ownership system is static lifetime determination. For those instances where dynamic lifetime determination is necessary, Rust provides types like Rc (which can be considered a very rudimentary garbage collector).
- miguelraz 5y agoOh, I see. Yes, that's a useful distinction then. Will edit. Thanks!
- moonchild 5y ago> "Garbage collection" implies dynamic lifetime determination. It does not. It refers to automatic memory reclamation. Generally, memory reclamation policies (whether automatic or manual) are closely tied to lifetimes—a sound program does not attempt to reuse objects which are still alive—but that is incidental in this context. In rust, programs generate garbage, do not explicitly clean it up or free it, and it is reused for new allocations; that is garbage collection. Reference counting, far from rudimentary, is a highly effective means of garbage collection which works quite well for certain types of programming languages and programs. (Notable examples include switch/objc, nim, and apl.) It has fallen out of favour in recent times because popular languages in industry and academia encourage execution patterns that perform much better with tracing garbage collectors, but that is not in itself a mark against reference counters.