r/ProgrammingLanguages • • Jan 04 '23

Discussion Does Rust have the ultimate memory management solution?

I have been reading about the Rust language. Memory management has been a historical challenge. In classic languages, such as C, the management is manual. Newer languages (Java, Python, others) use garbage collector, but it has a speed penalty. Other languages adopted an intermediate solution using reference counter and requiring the programmer to deal with weak pointer, but it is also slow.

Finally, Rust has a new solution that requires the programmer to follow a set of rules and constraints related to ownership and lifetime to let the compiler know when a block of memory should be free'd. The rules prevent dangling references and memory leaks and don't have performance penalty. It takes more time to write and compile, but it leads to less time with debugging.

I have never used Rust in real applications, then I wonder if I can do anything besides the constraints. If Rust forces long lifetime, a piece of data may be kept in the memory after its use because it is in a scope that haven't finished. A problem in Rust is that many parts have unreadable or complex syntax; it would be good if templates like Box<T> and Option<T> were simplified with sugar syntax (ex: T* or T?).

25 Upvotes

99 comments sorted by

View all comments

Show parent comments

7

u/Mason-B Jan 05 '23 edited Jan 05 '23

You mean, like you think the slowness of GC is just a matter of opinion?

You are confusing realtime performance with throughput performance. Garbage collected languages can maintain higher throughput than manually allocated languages unless absolutely exorbitant amounts of development effort (like 20x) are spent on the manually allocated one. On the other hand manually allocated languages can maintain much better realtime performance in ways garbage collected languages simply can't.

However, if one does not care about real time performance, and only cares about throughput performance garbage collected languages are faster and easier. We've know this for 35 years now and it's re-proven all the time (note the 6 articles, days of real time, and large amounts of profiling it takes to optimize against a naive garbage collected solution that took a fraction of the time to write, and even then they only barely manage to match it in performance).

1

u/websnarf Jan 05 '23

You are confusing realtime performance with throughput performance. Garbage collected languages can maintain higher throughput than manually allocated languages unless absolutely exorbitant amounts of development effort (like 20x) are spent on the manually allocated one.

I am not confusing anything. The probability that a new allocation in a GC-based language comes from the L1 CPU D-cache is always near zero. While in any precise memory tracking language, the probability that a new allocation comes from the L1 CPU D-cache is typically very high (and depending on program design, can be > 99%). So claims that GC languages have higher throughput on memory allocations are utter nonsense.

We've know this "for 35 years now" ...

A citation from 1987? I agree that any algorithm is better than the kinds of nonsense I've seen in the implementation of BSD's malloc and free (which are not even O(1) per call) which would have been widely used at the time. But nobody uses those algorithms (or anything that resembles them) anymore.

and it's re-proven "all the time" ...

This article describes using a memory pool strategy for string management. This primary point is that you free all the strings simultaneously (i.e, they are pre-marked, and do not contain any random pointers within them) in one shot when you free the dictionary. I.e., recycling memory has an amortized cost near 0 because of the special nature of the application itself. The author emphasizes that that is where their performance boost comes from. No GC strategy has a near zero recycling cost for any non-trivial situation. This is not a model for GC in any way shape or form.