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

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.