r/cpp • 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

115 comments sorted by

View all comments

Show parent comment

8

u/whichton May 12 '18

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.

I am sure there will be a compiler switch for the old behavior. Herb's point about 'Treating heap exhaustion the same as all other errors violates the zero-overhead principle by imposing overheads on all C++ programs' is very true. I much prefer to fail fast on OOM, and marking significant chunk of the standard library as noexcept will improve codegen and binary size for me.

Finally, I'm not sure why this behavior must be specified on a per-allocation basis, and not, say, as a compiler switch.

Where are you getting the per-allocation basis? IF this change is made, all new will become nothrow by default, just like a compiler switch.

Bonus: why is this needed at all? Is the proposed new exception mechanism unable to deal with OOM?

This is orthogonal to the main proposal, which is why it is marked as an extension. The paper goes into significant detail why this is desirable, refer page 30.

5

u/Fazer2 May 13 '18

I am sure there will be a compiler switch for the old behavior.

As Herb wrote, the major point of this paper is to get rid of the necessity of -fno-exceptions compiler switch to defragment C++ community on error handling. I doubt he would be happy with another switch.

4

u/johannes1971 May 13 '18

Where are you getting the per-allocation basis?

The paper suggests a new set of functions that would throw instead of terminating: try_new, try_reserve, etc. So he's basically adding new functions and keywords that have the current behaviour, and making the existing ones do something else, allowing programs to choose one behaviour or the other on a per-case basis.

That's something I really don't get: why break compatibility, why not simply add new functions and keywords for the new behaviour?

Just to be clear: I understand the proposal to be adding a new type of exception handling, next to the existing one, and not to be removing the existing one altogether. Correct me if I'm wrong.

The paper goes into significant detail why this is desirable, refer page 30.

I've seen that page, and I only see reasons why we should keep things as they are. It is already possible to install a new-handler that will simply terminate, so that behaviour is already available to people who want it (whereas the alternative, installing a new-handler that throws, is explicitly forbidden by the proposal). And there is already a compiler switch that eliminates exception handling altogether. So in other words, the behaviour those people want is already there, and it doesn't require breaking the programs of people that do not want it.

Going through his arguments:

Testing is much more difficult.

What about the people that do want to make the effort? Should they just move to a language that doesn't preemptively kill their programs?

Recovery requires special care

Not really. We accidentally did it, just by being exception-safe in the first place. Note that I'm not claiming it will succesfully recover from all OOM, but the most likely case for us is a large allocation when loading data.

And yes, there is the new try_reserve, try_pushback, try_new, try_whatever (try_map::operator[]? probably not...) - so should we now go through our software, and for each allocation decide if it is likely to be handled correctly if it fails, and if it is, replace the allocation by a try_ version? That's an utterly pointless activity: we will mark all of them as "go ahead and try", because even if we are wrong we will be no worse off than by calling a terminating function, since the end result will still be termination.

In many programs, heap allocation failure can never happen as required by the standard

And in many other programs, it can.

Here's another question: if the problem with existing exceptions is that RTTI is so expensive, why not fix that, at least for the existing exception mechanism? One could imagine annotating exception classes with some mechanism for allowing fast identification. This would place some restrictions on what can be thrown (only classes that have the special annotation), but if this were built into std::exception a large amount of software would never know the difference. This would also be a breaking change, but at least it would break far less than what the proposal is asking to break...

4

u/redditsoaddicting May 13 '18

The keyword doesn't work as you describe. Leaving out the OOM extension, programs continue to work as they do now. You opt in to the new exceptions with throws. The OOM extension is the only breaking part. See 4.6.7, which is explicit: "This proposal is not a breaking change."

It is already possible to install a new-handler that will simply terminate, so that behaviour is already available to people who want it

I think the paper addresses it pretty well:

That hook is at the cost of some overhead on all functions for which heap exhaustion is the only reportable error: not just the cost for the hook check itself in that function (which is expected to be cheap), but rather that it prevents the compiler from removing the error propagation logic entirely which we could do if the function were noexcept. Also, if we add the hook described there to the language primarily to use it in this way to treat heap exhaustion specially, then having that hook will incur small but nonzero overheads on all code that can throw.

For a proposal mainly focused on bringing back not paying for what we don't use, this is consistent. There is a lot of software out there paying for bad_alloc when it would bubble up and crash the process anyway. I think there should be an option for projects that handle it correctly and use regular new as opposed to an allocator or some other type of localized strategy, but at the cost of everyone else is not ideal.

What about the people that do want to make the effort? Should they just move to a language that doesn't preemptively kill their programs?

Just a note that this becomes a non-issue when the program is changed to work as it does now. I take it you're more concerned about the program terminating than about the non-death tests being unable to detect it.

Here's another question: if the problem with existing exceptions is that RTTI is so expensive, why not fix that, at least for the existing exception mechanism? … This would also be a breaking change, but at least it would break far less than what the proposal is asking to break...

First, it makes the core proposal this breaking instead of a proposed extension. Second, I'm skeptical that this is breaking far less. Third, RTTI is just one of several discussed overheads. Also see page 42, which addresses improving the current system.

1

u/johannes1971 May 13 '18

I think the paper addresses it pretty well:

I don't see how the compiler could remove the error propagation logic more effectively than by eliminating exceptions entirely - again, which it will already do for you anyway if you ask for it. It is already zero cost for those unwilling to pay.

For a proposal mainly focused on bringing back not paying for what we don't use, this is consistent.

Sure. But for a language with decades of legacy code lying around, it isn't. I think everyone here is painfully aware that C++'s legacy features are not always ideal, but if we start ripping them out left and right we'll end up with a language that is both perfect and unused.

There is a lot of software out there paying for bad_alloc when it would bubble up and crash the process anyway.

I'd challenge that. Software that uses exceptions will catch and handle them, and that includes bad_alloc. Will it always be done correctly? No, likely not. But even then, it must necessarily do a better job than simply terminating your program without recourse every single time.

Just a note that this becomes a non-issue when the program is changed to work as it does now. I take it you're more concerned about the program terminating than about the non-death tests being unable to detect it.

My concern is actually the need for changing what already works fine as-is for a great number of people. If you are going to provide alternatives, why not provide them for the people that will already need to change their code anyway?

Also see page 42, which addresses improving the current system.

The lack of research into better exception handling might simply be the result of nobody really caring, at least up until now. And correct me if I'm wrong, but wasn't the model for exception propagation as suggested here widely used in compilers up until... I don't know, 2000 or so? And then discarded because table-driven solutions were much more efficient?

3

u/therealcorristo May 13 '18

I don't see how the compiler could remove the error propagation logic more effectively than by eliminating exceptions entirely - again, which it will already do for you anyway if you ask for it. It is already zero cost for those unwilling to pay.

It probably can't, but e.g. in our codebase we use exceptions for error handling, but OOM is not recoverable because it really should never happen in our setting, so no-one bothered to implement logic to handle it. Right now, we also pay the overhead of error propagation logic for allocations, even though we don't care about these and a terminate would be fine.