News for hackers. Stuff that matters.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by jraph on Thursday December 23, 2021 @03:46AM (#29659940)
If you add a garbage collector to Rust, you neutralize one of its strength: the controlled lifetime of objects. You also make memory leaks easier.

I'm convinced Rust is easier to write without the management of lifetimes in a short time span. However, what about long term maintenance, and application stability?

Rust with a GC probably has its uses (compiled language with strong error checking), but one has to know what ship they are boarding.

It also makes it quite closer to D, an "easier C++", compiled language with a GC.

(I'm saying this as someone who tried Rust, and mostly uses GC'd languages)

edit: thanks for the replies below. I hadn't though about this. I guess one should better read my message as "If you remove Rust's lifetime management, …"

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by klodolph on Thursday December 23, 2021 @03:56AM (#29660000)
Rust’s type system doesn’t guarantee that object lifetimes are bounded from above (guaranteed to be destroyed), just that they are bounded from below (guaranteed to exist when used). This is why you have stuff like crossbeam—elaborate workarounds for a lack of linear types in Rust.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by staticassertion on Thursday December 23, 2021 @01:00PM (#29664469)
It doesn't guarantee it under all circumstances, like if your computer shuts off obviously destructors don't get called. But you can rely on the behavior to be deterministic.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by klodolph on Thursday December 23, 2021 @01:25PM (#29664757)
You can rely on the behavior to be deterministic, but the Rust type system provides no way on relying on this determinism. For example, if you have a stack variable and pass a reference to a short-lived thread, Rust will just throw an error, because there is no way for Rust to prove that the lifetime of the borrow is short enough (bounded from above) because Rust does not have linear types.

I want it to be exactly clear what I'm saying... While you (the programmer) may understand when an object is destroyed, and Rust (the compiler) will agree with you, that information is not present in the type system and you cannot use it in your programs. In this aspect, Rust has the same problems as C++... but where C++ still lets you write code, Rust will just throw an error and force you to use "unsafe" here.

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by zozbot234 on Thursday December 23, 2021 @01:40PM (#29664941)
> For example, if you have a stack variable and pass a reference to a short-lived thread, Rust will just throw an error, because there is no way for Rust to prove that the lifetime of the borrow is short enough (bounded from above) because Rust does not have linear types.

Rust has scoped threads for this use case. Generally this uses closures as opposed to lifetimes to "bound borrows from above" in a safe way.

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by klodolph on Thursday December 23, 2021 @01:43PM (#29664990)
Rust has crossbeam, but it is not a complete solution to the problem. Scoped threads were removed.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by ssokolow on Thursday December 23, 2021 @02:29PM (#29665520)
Would you mind elaborating?

It was removed from the standard library ages ago, but `crossbeam::scope` appears to still be there in the newest release of crossbeam and the git repo.

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by klodolph on Thursday December 23, 2021 @10:55PM (#29669987)
Using a scope for spawning threads is a solution to a narrow slice of a much larger problem.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by staticassertion on Thursday December 23, 2021 @01:27PM (#29664782)
Yes, that's correct. I just want to be clear that it's not like destructors in rust are unreliable in the way that finalizers are in Java.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by klodolph on Thursday December 23, 2021 @01:34PM (#29664865)
To be clear, I definitely wasn't talking about that.

Since you can't use the type system to guarantee that an object is destroyed, there are a few things in Rust that are a major pain to get working. Like passing a reference to a short-lived thread.

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by staticassertion on Thursday December 23, 2021 @03:52PM (#29666323)
It's not that hard in my experience. You generally do it by using a guarded scope where the user provides a callback and doesn't manage the resource (like a thread) themselves.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by klodolph on Thursday December 23, 2021 @10:58PM (#29669992)
Sure, if that works for you, it's easy. Stuff like crossbeam::scope only solves a subset of the problem, though. When that solution doesn't work for you, it's a pain.

Using a scope is, strictly speaking, less powerful and more annoying to work with than a more general system of linear types. It's like Go's defer, or C#'s using/IDisposable... they work in certain scenarios, but there's a percentage of the time where a lexical scope or function scope doesn't match the lifetime of your object, or can't match the lifetime of your object.

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by staticassertion on Friday December 24, 2021 @11:41AM (#29675022)
Yep, agreed.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by IshKebab on Thursday December 23, 2021 @05:28AM (#29660495)
> I'm convinced Rust is easier to write without the management of lifetimes in a short time span.

Yeah that's the problem with all these studies that use students writing new code for a few hours.

99% of coding isn't like that - it's reading other people's code (including code you wrote ages ago and can't remember), trying to figure out how it works, following complex call chains, debugging etc.

If you only look at Advent of Code type problems you'll come up with all sorts of strange conclusions, like static types don't make a difference, comments aren't useful at all, it doesn't matter if you exclusively use single character variable names, goto is totally harmless, etc. etc.

Though in this particular case I guess they didn't have much choice because there are no existing large programs written using Bronze.

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by Farmadupe on Thursday December 23, 2021 @05:56AM (#29660636)
I'm not 100% sure, but I think the OP was talking about the overhead of lifetime management for objects that don't live for a very long time. Or maybe objects that don't have "interesting" lives.

One example in my head individual strings that need to be constructed dynamically but live for the lifetime of the application (better to leak it with Box::leak or lazy_static! than pollute all code with lifetimes)

Another is writing in lifetimes for single purpose objects that live on the stack and might only ever get passed by ref into a single function and then get destroyed soon after.

Lifetimes are super important in rust and are a core part of the language, but in such degenerate cases they take up a lot of programmer effort for little benefit. In my head a "automatic" solution such as GC /could/ have a home in the language. Perhaps this would make rust a slightly better fit for really complex monolithic GUI apps (word processing, spreadsheets, CAD) where full GC would be performance-onerous but data lifetimes would be too complex for rust's strictly ordered lifetime concept

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by mcoblenz on Thursday December 23, 2021 @01:50PM (#29665075)
We were particularly interested in learnability for this study, which CAN be assessed in a study with students.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by rwmj on Thursday December 23, 2021 @03:53AM (#29659976)
Garbage collection and controlled object lifetimes are important and orthogonal concerns. You can have both at the same time. C# for instance lets you add a Dispose() method for the times when you want to free the resources now. There are even ways to ensure statically that objects aren't used after the close/dispose is called. (Of which I would find the paper, but my google-fu is failing me right now ... https://okmij.org/ftp/Haskell/regions.html and https://arxiv.org/abs/1803.02796 I think)
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by torginus on Thursday December 23, 2021 @06:59AM (#29661000)
Tbh, I'm not sure about this GC-purism. I've encountered tons of high-end C++ that's absolutely full of smart pointers, which is a form of GC. Rust has Rc<> and Arc<> as well. Having a a ref-counted GC instead of a tracing one doesn't seem like a step up in 'purity' to me.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by jeremyjh on Thursday December 23, 2021 @07:26AM (#29661165)
Ref-counting provides consistent performance - the costs are amortized smoothly over the operations. They aren't doing all this work for "purity".
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by bjourne on Thursday December 23, 2021 @11:24AM (#29663368)
Performance of ref-counting is proportional to the size of the dead objects. Performance of tracing gc is proportional to the size of the alive objects. In other words, there are many real-world situations in which ref-counting pauses are much longer than tracing gc pauses.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by louthy on Thursday December 23, 2021 @07:45AM (#29661301)
Ref counting can also lead to memory leaks with circular references.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by petre on Thursday December 23, 2021 @05:30AM (#29660508)
> It also makes it quite closer to D, an "easier C++", compiled language with a GC.

That's exactly why I loved D over C++ and Rust and had the feeling that I was not fighthing the compiler from day one: the GC, auto types, lack of annoying C preprocessor.

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by jraph on Thursday December 23, 2021 @08:11AM (#29661488)
D also has the good characteristic of not needing to download the entire Earth and compile half of it before starting a project. You just apt install ldc2 or apt install gdc and there you go (pun not intended).

This actually made me chose D over Rust for my last project.

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by p0nce on Thursday December 23, 2021 @01:28PM (#29664798)
> those who [used the GC] required only about a third as much time (4 hours vs. 12 hours).

This makes me happy to use D instead of Rust. D has a @safe subset.

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by KptMarchewa on Thursday December 23, 2021 @07:07AM (#29661045)
Can you use lifetimes for most objects, and use GC where it's "hard"? Like, canonical example with double linked lists.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by rtoway on Thursday December 23, 2021 @11:01AM (#29663098)
You can do this in Rust today, with the standard library's reference counter
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by klodolph on Thursday December 23, 2021 @01:36PM (#29664896)
Also, one of the easiest ways to leak memory in Rust. My understanding is that substructural typing systems are just not quite powerful enough to represent the semantics of double-linked lists, but I don't understand the math myself.

Obviously it won't leak memory if you do it correctly, but Rust will happily let you leak memory in an Rc<T>.

Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by ThomasWinwood on Friday December 24, 2021 @08:58PM (#29679609)
If you really do want to leak memory, Rc<T> is overcomplicated - just call `Box::leak` and throw away the output.
Re:Does the Bronze Garbage Collector Make Rust Easier to Use? by klodolph on Friday December 24, 2021 @09:34PM (#29679815)
Rc<T> is the preferred way to introduce unintentional memory leaks.