Hacker News new | ask | show | jobs
by v0y4g3r 802 days ago
I think no. In this particular case it involves how Rust handles lifetimes compared to other languages with garbage collector.

Essentially it's a comparison between reference counting and tracing garbage collection based on reacheability.

2 comments

Yes, and this is what I was curious about, because I haven't seen the performance cost of this discussed a lot.

Worded differently: does the strict concept of ownership/lifestimes in Rust bias a default (naive) implementation towards lower performance (eg due to required copying) when compared to a naive Golang (or even Java) implementation?

I have no doubts that after heavy optimization, Rust beats languages such as Go & Java.

Using clone or Arc with boxing everywhere to avoid using references with lifetimes at all will lead to code that's slower than Go/Java, yes, unless you're just cloning small objects that don't internally use heap allocations or your algorithm dictates to only need moves, not sharing, or perhaps except it will likely use less RAM which in some situations may still make it faster. But such "newbie" code will probably still be using some other existing code that is using references internally, which makes things faster for those parts. Also, the difficulty of the use of references varies depending on how long / indirect they are going to be used, in many places references are easy to deal with even for a beginner; so it becomes a question of how much the code relies on clone and reference counting.

When I learned Rust, I actually never went with the "use clone or Arc to make your life easier while learning" recommendation but always used references and learned how to use lifetime declarations and program design to go as far with them as reasonable. TBF I had experience with C and C++ already. But once reasonably experienced working in Rust (after a year?), your code should be faster most of the time the way you write it on the first try without needing optimization work.

With the caveat that there are various kinds of "languages with garbage collector", including some where manual resource management is an option.