3 ms·
If you think that Rust doesn't care about addressing the needs of C++ users, I'm not sure what to tell you. It's not like C++ only focuses on features with zer
by pcwalton 7y ago
If you think that Rust doesn't care about addressing the needs of C++ users, I'm not sure what to tell you.
It's not like C++ only focuses on features with zero overhead. The zero-overhead principle means that you don't pay for what you don't use. It doesn't mean that compiler developers are forbidden from working on any language or library feature that consumes more than zero cycles at runtime.
- jnordwick 7y agoSo an opt-in GC would be considered zero-cost abstraction?
- pcwalton 7y agoSure. It's called Rc and Arc.
- hexane360 7y agohttps://en.wikipedia.org/wiki/Cycle_detection https://en.wikipedia.org/wiki/Cycle_detection
- pcwalton 7y agoReference counting is a form of garbage collection, despite the fact that it does not detect cycles without extra work. This has been the correct terminology in the memory management literature for decades.
- jnordwick 7y agoDon't be intentionally obtuse. You know I mean something more like mark and sweep
- kbenson 7y agoI think the issue is that the zero-cost abstraction terminology is used in two ways often. First, to mean a feature you can use and costs nothing because the compiler will change it into a more common form for you (such as functional programming approaches where it will take multiple filters and map passes and turn it into a single loop), and second, to mean that features added that do add overhead, only do so when you use them (as in, there's no runtime to manage M:N threading unless you include a crate that does so, because developers not using that feature shouldn't have to pay for it). I think the terminology here is looser than it should be, and these are not really the same thing, even though I've seen zero-cost abstractions used to describe them both. It causes confusion sometimes, as I believe it is doing here.
- nemothekid 7y agoIs shared_ptr a zero cost abstraction?
- kam 7y agoMostly, with the caveat that in single threaded usage, you're paying for the unnecessary overhead of atomic ops on the reference count. Rust has `Rc`, which is not atomic but `!Send` so the type system ensures that it stays within a thread, as well as the atomic `Arc`.
- quietbritishjim 7y agoSure, and there are a few hooks for one in the C++ standard library e.g. std::declare_reachable [1], although it doesn't include an actual implementation. Only mark and sweep is feasible, not mark and compact, for obvious reasons [2]. Here's a mention of garbage collection Stroustrop's old C++11 FAQ [3] [1] https://en.cppreference.com/w/cpp/memory/gc/declare_reachable https://en.cppreference.com/w/cpp/memory/gc/declare_reachabl... [2] https://herbsutter.com/2011/10/25/garbage-collection-synopsis-and-c/ https://herbsutter.com/2011/10/25/garbage-collection-synopsi... [3] https://web.archive.org/web/20120228143039/http://www2.research.att.com/~bs/C++0xFAQ.html#gc-abi https://web.archive.org/web/20120228143039/http://www2.resea...