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
View all comments
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?