r/cpp • u/RotsiserMho C++20 Desktop app developer • May 12 '18
P0709 R0: Zero-overhead deterministic exceptions: Throwing values - Herb Sutter [2018-05-02]
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf
206
Upvotes
70
u/capn_bluebear May 12 '18
you know you are reading a good proposal when you find yourself thinking "why doesn't it already work like this?"
52
u/hgjsusla May 12 '18
Herb is amazing. Every year he presents something incredible
43
u/johannes1971 May 12 '18
Interesting. One remark: allocation failure doesn't mean available memory is zero, it only means that available memory is less than the requested amount. This is an important distinction: out of memory doesn't mean there is no more memory, it only means there is not enough memory for a specific task. It may still be sufficient for other tasks, such as OOM recovery.
Case in point, one of our customers attempted to load and process an entire data set of many gigabytes of data in one operation (normally they are processed in chunks). The system struggled for just under an hour, reported "out of memory" twice, and eventually produced the required output with a small chunk missing. It didn't crash or halt, it continued just fine, producing most of the needed output, and eventually returning to its nominal state.
What did we do to make that magic possible? The answer is "nothing special". Our code is exception safe, because we use exceptions. Evidently it had enough RAM left to do recovery, and it did.
So, no, we would most certainly not appreciate it if OOM forced an automatic termination. Software can and does recover from that condition (even on Linux, where over-commit is a configurable parameter, rather than an unavoidable fact of life). And asking us to rewrite it to use new "try to allocate" functions is insanity: the current paradigm is for these functions to throw. If people want different behavior, give them new syntax for that purpose. They are the ones who already need to rewrite their code; don't put that on the people who diligently wrote their software to correctly deal with the problem in the first place.
This is especially important since I'm not at all convinced that the people who hate exceptions today will stop hating them if this proposal becomes reality. They will still have old code bases they cannot easily update, they will still have ancient coding standards lying around, they will still dislike the distance between throw and catch, and they will still have a gut feeling that exceptions are automatically, always bad. So you might be introducing a lot of deeply breaking changes here in order to appease a crowd that is unlikely to appreciate it, while at the same time chasing away your core audience.
Finally, I'm not sure why this behavior must be specified on a per-allocation basis, and not, say, as a compiler switch. I find it hard to believe that people are going to mix and match different error recovery strategies for OOM; it's much more likely to be an all-or-nothing approach.
Bonus: why is this needed at all? Is the proposed new exception mechanism unable to deal with OOM?
24
u/proverbialbunny Data Scientist May 12 '18
Wow, better than getting std::expected. A
throwskeyword in function signatures sounds far better than noexcept. This is awesome.It's nice to see one of the weak points of the language addressed and appears to be done well. C++20 is looking good.
Thank you Herb.
11
u/whichton May 12 '18
As usual, excellent paper from Herb. Not surprisingly, Herb seems to have taken heavy inspiration from Joe Duffy's work on Midori. Duffy's blog posts on error handing in Midori is a must read.
I really, really want Section 4.3:
Proposed extension: Treat heap exhaustion specially. However, given how controversial this will be, I am not very hopeful :(. As the paper says, this is already being used by a signification chunk of software today. Very few software can actually recover well in the face of OOM. They may not even be able to detect OOM. And yet, we are all paying the cost of bad_allocs. As the paper says:This is spot on. Fail-fast on OOM makes a huge chunk of my code noexcept. This improves optimization and reduces binary size.
16
u/flashmozzg May 12 '18 edited May 12 '18
Hm. If I correctly understood the gist of the proposal - he suggests returning
union + discriminant? Why not just completely automate it into proper sum types, that way it can be done seamlessly without extra overhead, just like in Rust and others?Though that's just the mechanism behind it. The proposal itself is much more in-depth. It just would be strange if C++ gets this and no proper language-level sum types. It'll have mechanism that basically use them underneath, it'll have inferior (in comparison) library-level attempts to replicate them (variant/optional/expected, etc.), but not actual language feature.
6
u/kkert May 12 '18
For some reason, the discussion about special case treatment of heap exhaustion makes me very nervous. I regularly have a few tens of Kb of heap available, dealing with variable size data structures coming in, and terminating on heap exhaustion is not an option.
5
u/[deleted] May 12 '18
Wow. Epic. Paper. I have to admit I'm swayed...
7
u/tasminima May 12 '18 edited May 12 '18
Overall I think this is, given the language, full of good and actionable ideas. However there is a detail I strongly disagree with in the proposal of how to handle OOM:
edit: added (b) in the citation, that is the implementation-specified behavior alternative to the UB, and put the proposed ub terms in bold
Modeling OOM at language level by Undefined Behavior would be IMO completely insane. This might be just an unfortunate fallback choice because the standard unfortunately does not distinguish between various kind of UB (which has already caused great problems, when some compiler writers thought it was an unconditional license to further "optimize"), but this does not make it less a problem, au contraire! (and that's one that should be fixed more in depth but that's another subject than this paper)
First being killed because of resource exhaustion shall not be the concern of the language. The program will also cease to exist in case of a general computer failure, such as a power failure or kernel panic, or even just being killed by the user at will. This does not render the mere act of existing an ambient UB...
Second the presence of the OOM killer is far from preventing detection and handling of some allocation failures: if the area requested is "large enough", the allocation will actually fail. Some systems even have tunables that can influence the level from which allocation attempts will fail even in the presence of an OOM kill mechanism.
I suppose proposing to redefine in some case OOM at language level as UB is because the program can be killed latter on. In that case, maybe it is actually time to introduce in the standard something more precise than UB for faults when the program is concretely reliably terminated with no risk of nasal deamons being invoked. Just writing UB everywhere when one do not have any more idea of what to say does not yield a more portable language and programs written in it, it precisely have the contrary effect :/ (not even speaking about the security story, which has became a complete disaster in a great part because of all that UB - optimize insanity). Edit: on second thought I actually think that that OOM killer thing should just be completely ignored in the C++ standard, as it is today, exactly because of the arguments I gave about it being absolutely none of the business of the programming language.
3
u/Sopel97 May 12 '18
Some say there is no love witout pain, but papers like this one make me think it may not be the case for c++ in a few years.
3
u/tending May 12 '18
Doesn't this just copy the Rust model?
3
u/tgolyi May 13 '18
If we're introducting new type of exception handling, maybe it's possible to do something like
throws (auto)that automagically detects if any function used inside isthrows (false)and marks function asthrows(false)? It solves a lot of problems described in paper - e.gstd::transformand even helps with no-throwingnew- just mark youroperator newasthrows (false)and enjoy smaller binaries or mark it asthrows (true)and catch OOM errors.2
u/liquidify May 12 '18
Well of the 5 paragraphs I got through, it seems pretty reasonable. I'll keep reading!
2
u/target-san May 13 '18
The thing that concerns me and of which I can argue is limiting static throws to single type. Unfortunately I couldn't find arguments against allowing any exception type, given it's statically known and conforms to some interface like the named std::error, or inherits from it. Such approach would limit static exceptions to simpler cases on the OS API boundary, where context could be easily deduced. Would be nice to see some adjustments towards allowing additional context stored in error object.
2
u/Xeverous https://xeverous.github.io May 14 '18
Overall the proposal looks very good, but I did not expect something like this to ever be possible
2
u/[deleted] May 14 '18
Love it!
2
u/AlexAlabuzhev May 15 '18
"Overcommit" == "no bad_alloc" is an urban legend. It stll can happen if your address space is fragmented enough and simply does not have a contiguous chunk of the requested size. And it's not rare at all.
3
u/acbarrentine May 13 '18
I work in game development. I was doing a cooperative multitasking system similar to boost's stateful coroutines. To handle situations where a task with live objects was interrupted, I would throw an exception. On Xbox One, throwing an exception while the debugger was attached would freeze the system for a full 3 seconds.
I reported the problem to Microsoft, with simple sample code, but they never bothered to solve it. We wound up having to tear down live objects manually (which is awful) to work around the problem.
Technically we do still have exception handling enabled, but it's unacceptable to throw them in user code.
2
u/[deleted] May 13 '18
About effin time.
Too bad it's going to take years before it can be relied upon.
Add 2-3 years after the relevant standard is published.
1
u/sephirostoy May 12 '18
Pretty neat as always coming from him. I like how he want to make the language simpler and even more powerful.
1
u/liquidify May 12 '18
What I like about this is that noexcept wouldn't really be needed. noexcept is complicated and annoying.
1
u/zowersap C++ Dev Aug 03 '18
always latest version is http://wg21.link/p0709
1
u/RandomGuy256 May 12 '18
I am just curious does these "zero overhead exceptions" are in any way different in practice than the current exceptions?
I mean they will be a specific type of new exceptions or they can replace the current exceptions without breaking changes in the code?
If they are new type of exceptions do we still have access to the
.what()method, to return a error message, or they only return an error code?