18 ms·
C++23: Removing garbage collection support
- afavour 3y agoTL;DR: > Garbage collection and related features are being removed from C++23. If you are surprised to learn that the C++ standard had support for GC, you are not alone. It was unimplemented, confusing and pretty useless hence it’s removed.
- pipo234 3y agoGood riddance! So the proposal was sponsored by HP, Symantec and Intel. I was expecting something related to Microsoft and it's ill conceived idea of "Managed C++", but it all boils down to: Hans Boehm. As surprised as the next guy to find this in C++ standard. ...Again: good riddance.
- AdmiralAsshat 3y agoGarbage Collection is not a bad idea, but, my takeaway is that if you try to bolt it onto a language that didn't have it in its core design, you're gonna have a bad time.
- mikepurvis 3y agoInteresting that GC was originally part of rust and got un-bolted: http://pcwalton.github.io/_posts/2013-06-02-removing-garbage-collection-from-the-rust-language.html http://pcwalton.github.io/_posts/2013-06-02-removing-garbage...
- repelsteeltje 3y agoIt's rather that with RAII (C++, Rust) you don't need a GC. Of course, you can still create and operate a GC arena in C++, to manage a data structure or problem area where it makes sense. But why build in into the language, library, runtime if developers don't need it?
- simiones 3y ago(Well-designed, first-class) GC is still the only system proven to completely remove memory safety issues. Rust comes close, but it's not RAII but the compiler-enforced ownership tracking which allows for this. C++ is very much proven to be memory unsafe in practice despite the RAII and smart pointers being part of the standard for a decade or more now. Opt-in memory safety basically just means no memory safety. Of course, this doesn't mean that you can't create memory safe C++ programs. Probably one or two actually exist. But there also exist memory safe C programs and probably somewhere there is some memory safe program written in assembly as well.
- repelsteeltje 3y ago> (Well-designed, first-class) GC is still the only system proven to completely remove memory safety issues. To be pedantic, you need more than a GC for memory safety. Like a language (type system, bounds checks) that forces you to only look only inside the memory being managed. :-) Also, "for free" memory safety as offered by java's GC doesn't automatically guarantee resource safety. Without RAII, you still need to manage you file handles the old-fashioned manual way. Beyond that there are safety concerns that where the shared memory model managed by the GC bites you. For example: Rust solves threading issues with move semantics and pinning, guiding programmer away from shared mutable state. Also think of stop-the-world problems inherent with GCs. They can be life threatening in real-time systems. So: yes it's true that it's difficult to write safe code in C++, and many safety bugs are related to memory. But that doesn't mean that code in a GCed language is inherently safer.
- vips7L 3y ago> Also, "for free" memory safety as offered by java's GC doesn't automatically guarantee resource safety. Without RAII, you still need to manage you file handles the old-fashioned manual way Don’t you think a file handle is a completely different thing than what is normally talked about with memory safety? Can a un-closed handle let you ++ your way into remote code execution? Or reading uninitialized memory? Regardless, a GC is about trade offs. A GC alleviates the 99% of programming. Thinking about having to close a file resource occasionally, especially since javac will warn you when you don’t, is leagues easier than having to navigate the borrow checker all of the time. The same goes for concurrent programming. 99.9% of all code written in GC’d languages like Java never go over a thread boundary.
- xhdjcic 3y ago[dead]
- zozbot234 3y agoWhat's the formal difference between 'pointer safety' and 'pointer provenance' which is also a concern in recent C/C++ standardization work? Perhaps the former is getting removed because it turns out to be a less precise duplicate of the latter?
- orangepanda 3y agoDoesnt std::shared_ptr use garbage collection?
- zaphoyd 3y agostd::shared_ptr is reference counted. No GC involved.
- cpitman 3y agoNo, it uses reference counting, internal to the shared pointer implementation itself.
- conradludgate 3y agoIt uses reference counting and RAII. It does not use reachability analysis or tracing. Many people refer to a tracing routine or reachability analysis when they refer to garbage collectors
- repelsteeltje 3y agoNo, std::shared_ptr is plain ref counting
- eatonphil 3y agoIf you subscribe to the unified theory of garbage collection [0] then yes shared_ptr does use garbage collection. See also Herb Sutter [1] (describing a different project, but also mentioning shared_ptr): > Q: "Is this garbage collection?" > Of course, and remember that so is reference counting (e.g., shared_ptr). [0] https://www.cs.cornell.edu/courses/cs6120/2019fa/blog/unified-theory-gc/ https://www.cs.cornell.edu/courses/cs6120/2019fa/blog/unifie... [1] https://github.com/hsutter/gcpp#q-is-this-garbage-collection https://github.com/hsutter/gcpp#q-is-this-garbage-collection
- simiones 3y agoIt's a matter of definitions. Technically, reference counting is a form of garbage collection, and it is often discussed in the garbage collection literature. Most commonly though, "garbage collection" refers to global schemes which apply by default to all memory (perhaps with rare exceptions such as pinning). So, in typical usage, C++ smart pointers, being opt-in, are not considered garbage collection. Languages which do automatic reference counting globally, such as Python or Swift, are indeed considered garbage collection schemes.
- wg0 3y agoI have this impression that modern C++ is far more complex with a far more large surface area (from the perspective of learning the language) than Rust. Because I am not expert in both, what folks that came from C++ to Rust or have to work regularly work with both say about that?
- Filligree 3y agoDramatically more so. Rust is complex, but most (not all!) of the complexity is there to support a specific, modern, safe style of programming. C++ adds fifty years of cruft to that. A lot can be said about the surrounding social environment, but as far as the languages go, I don't think it's far wrong to say that Rust is "C++: The good bits".
- ReactiveJelly 3y agoYeah I guess I'd phrase it as 3 kinds of "complexity" Rust is complex because the compiler has many ways to say "No I won't compile that, it might be wrong". C++ is complex because it requires the programmer to understand every feature used in the codebase. (e.g. Will the compiler warn me if I use inheritance and dynamic dispatch wrong? I'm not sure. I find code with inheritance hard to read.) Go _code_ is complex because the language is too simple. (if err != nil is waterbed complexity)
- Sohcahtoa82 3y ago> Rust is complex because the compiler has many ways to say "No I won't compile that, it might be wrong". Doesn't Rust have an escape hatch in the form of the "unsafe" keyword for cases where you're reasonably sure the code is safe and correct? I haven't used Rust and most of my knowledge of it comes from HN, so I could easily be wrong.
- Filligree 3y agoIt does, which is where the social aspect comes in: You're expected to make every reasonable effort not to use it, even if that comes at slight costs in performance, because practically speaking people are terrible at making those judgements. Sometimes works well. Sometimes, unfortunately, it leads to bullying.
- layer8 3y ago> When you read above about std::pointer_safety, did you understand the difference between relaxed and preferred? If you did, please explain in the comments section. [Since I don’t want to sign up with Disqus, I’m commenting here.] According to https://cplusplus.github.io/LWG/issue1098 https://cplusplus.github.io/LWG/issue1098, “pointer_safety::preferred might be returned to indicate to the program that a leak detector is running so that the program can avoid spurious leak reports”. This means that a program which uses different program logic depending on whether a GC is running or not, can choose to opt for the non-GC logic when the GC is running with a leak detector.
- fluoridation 3y agoWho would write two separate but equivalent codebases, one which manages memory with RAII and another that assumes a GC, just to use that value for something? If you're already not assuming a GC is present then why rewrite every a second time assuming it's not?
- layer8 3y agoThis is probably aimed at libraries designed to be compatible with both kinds of environments. A library might implement some localized alternative logic to maximize efficiency in both cases. C++ is all about maximum runtime efficiency while allowing libraries to provide comprehensive compile-time abstractions.
- fluoridation 3y agoA library that uses RAII instead of relying on a GC should work with or without a GC.
- layer8 3y agoRAII requires allocation patterns to coincide with lexical scopes, or to use reference counting, which is usually more expensive than GC. If RAII were all you need, no one would be using GC in the first place.
- tempodox 3y ago> If you are surprised to learn that the C++ standard had support for GC, you are not alone. I am relieved to learn that I wasn't sleeping too deeply. > A bit of simplification to the standard never hurts. Absolutely right, but I'm afraid it might be too little too late. I doubt any one person can keep all of C++ inside their head.
- Night_Thastus 3y agoYou don't need to keep it all in your head. That wouldn't be practical or useful for most languages. Especially when libraries get involved. I think the better answer is to just remember the bits that are frequently useful to you, and pull up a tab on the rest if and when you need it.
- SonOfLilit 3y agoIn all other languages I worked in, I basically can keep the entire language (not stdlib) in my head, and it's extremely valuable to understand what's going on. In C++ I just can't.
- kllrnohj 3y agoWithout looking it up do you know what are the Java memory model guarantees? Or what's the difference between >> and >>> ? Even "simple" languages have things that most people simply don't remember without looking it up because it's just not useful to know in day to day work.
- SonOfLilit 3y agoThankfully I've never worked seriously in Java. Ask similar questions about Python, C, x86 assembly, old versions of C#... I just looked up >>>, it's something I'd definitely know had I delivered any Java projects.
- Night_Thastus 3y ago
- pjmlp 3y agoIt never suited the use cases for Unreal C++, C++/CLI, and COM/WinRT, with the three major C++ implementations of automatic memory management ignoring it, no wonder it was standard dead weight and should be removed. That is what happens when features are added without taking into consideration existing use cases.
- dgellow 3y agoDo you know the history of its inception? I feel that would be a good read. Like, what use cases did the original authors had in mind, how did they convince others to accept a GC in C++ spec, etc. I like these kind of development stories, it’s often a weird mix of social and technical challenges, from a specific time period.
- jcranmer 3y agoAs far as I can tell, the initial paper proposal is https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2310.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n23..., proposed for C++0x. The subsequent history appears to be this: * The "Kona compromise" of 2007, where a few features were stripped down to try to be able to get a new version of C++ shipped by the end of 2008 (which turned out to slip to 2011 anyways). This meant that GC went from a full-bore proposal to a barebones proposal. * https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2481.html https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n24... is the first proposal of barebones GC support. As you can see, it's stripped down a lot from the original GC proposal. What remains is largely "it's UB to have a pointer not be visible as a pointer address, except if you use new library functionality to declare your pointer shenanigans." * https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2670.htm https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n26... is the final variant that was voted in for the June 2008 meeting (see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2697.html https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n26...) with no opposition (although some abstention). * https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2186r0.html https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p21... proposes, in 2020, to remove the features added by N2670 on the basis of no real implementation or real-world support. The discussion in EWG indicates favor for removal (only one person objected), while LEWG indicates even stronger favor (nobody objects).
- KingLancelot 3y ago[dead]
- moralestapia 3y agoI was once made fun of (in a kind of humiliating way, now that I recall) by a bunch of HPC scientists in a room at KAUST for saying that C++ had support for GC. They were like "if you knew the least bit a about C++, you'd knew it doesn't have a GC". I was like, "yeah cool, but C++ does have some GC functionality", but they just shut me down. My background is Biology so there were a few remarks about how "a biologist just doesn't know these things, lol". This was around 2019, btw. And sure, I'm a biologist, but I also happen to have spent 8-10 hours/week for the past 20 years coding because I just really like it; so, there's my 10k hours, ¯\_(ツ)_/¯.
- ezoe 3y agoWell, "by support" it means a few functions that allow user to query some information about GC if implementation had it. No implementation has ever implemented GC.
- foota 3y agoThis is why I always carry around a hardcopy of the C++ spec so I can pull it out Phoenix Wright: Ace Attorney style /s
- MR4D 3y agoThat must be heavy! LOL!! For CHF 208, you can buy the reference directly from ISO...all 1,853 pages! [0] [0] - https://www.iso.org/standard/79358.html https://www.iso.org/standard/79358.html
- foota 3y agoIf anyone tells you C++ isn't a safe language, watch them try to stop a bullet with the C# language spec!
- rzzzt 3y agoECMA-334 (the C# language portions): https://www.ecma-international.org/publications-and-standards/standards/ecma-334/ https://www.ecma-international.org/publications-and-standard..., 639 pages ECMA-335 (the Common Language Infrastructure portions): https://www.ecma-international.org/publications-and-standards/standards/ecma-335/ https://www.ecma-international.org/publications-and-standard..., 574 pages
- zerr 3y agoAt least `export` keyword was implemented in Comeau C++ :)
- SillyUsername 3y agoSeems strange to me them doing this rather than implementing at least partially something like an optional flag for memory tracing safety features instead. A bit like we're C, we love pointers and overflows, we don't need no stinking GC or safety rails :D
- bfgeek 3y agoOne interesting thing which folks might not be aware of - most objects in Blink (Chromium rendering engine) are GC'd: https://v8.dev/blog/high-performance-cpp-gc https://v8.dev/blog/high-performance-cpp-gc This is in part due to lots of objects having strong references from JS objects, and the complexity in dealing with already having a GC with reference counting techniques. Additionally its much harder to introduce UAFs & Type confusion bugs, but also has interesting performance implications (not as simple as GC is slow - a lot of the time GC'ing objects can make things faster).
- clippy99 3y agoAt first glance I thought this was the headline of a satirical article. :D
- swingingFlyFish 3y agoThis language is a Night. Mare. Bjarne literally pushes every and anything down the committe's throats. C++ just needs to go away.
- Alifatisk 3y agoNever new C++ had GC!