24 ms·
Current hardware trends make C++ exceptions harder to justify
- dataangel 5y ago> The root cause is that the unwinder grabs a global mutex to protect the unwinding tables from concurrent changes from shared libraries. OK, so replace the mutex with a reader writer lock. Shared library loading is incredibly rare, and dear god I hope nobody seriously does it and expects it to work during unwinding.
- Thaxll 5y agoThat's why we disable exceptions in video games.
- interroboink 5y agoFor AAA / high-performance / console / close-to-the-metal video games, at least. There are tons of games where exceptions are perfectly fine. Though still more often used for "probably going to crash soon" situations than otherwise, I'd wager.
- CyberRabbi 5y agoSince exception handling in desktop runtime environments usually takes an unbounded amount of time, it’s not good practice to use them in your main loop since you have to guarantee a new frame at 60Hz. You can definitely do it but you’d be conceptually allowing for frame skips in your codebase. It could be difficult to audit and remove this assumption if down the road you wanted to tighten up your main loop code.
- interroboink 5y agoAgreed about main loop — that requires special care. But there's often lots of other stuff going on, such as IO in other threads. You wouldn't want that stuff in your main loop for the same reasons, so since it's segregated anyway, exceptions aren't so bad (usually).
- Negitivefrags 5y agoWe disable exceptions in video games due to lock contention when throwing exceptions on multiple threads in high core count situations? No. We disable exceptions in video games for dumb historical reasons that no longer apply.
- alfazaleng 5y ago[flagged]
- jmyeet 5y ago> Root cause > Traditional C++ exceptions have two main problems: > 1) the exceptions are allocated in dynamic memory because of inheritance and because of non-local constructs like std::current_exception. This prevents basic optimizations like transforming a throw into a goto, because other parts of the program should be able to see that dynamically allocated exception object. And it causes problems with throwing exceptions in out-of-memory situations. > 2) exception unwinding is effectively single-threaded, because the table driven unwinder logic used by modern C++ compilers grabs a global mutex to protect the tables from concurrent changes. This has disastrous consequences for high core counts and makes exceptions nearly unusable on such machines. That's really interesting. I've become very anti-exceptions in recent years far various much-discussed reasons (eg hard to follow, false economy, difficult if not impossible to write threadsafe C++ code in particular, use of exceptions as flow control is an anti-pattern). One of the porposals is a value-or-error type object, which is basically what Rust has. I really like Rust's enums and match expressions. It seems so difficult to make changes like this to C++ at this point, at what point do you just have to start again?
- nly 5y agostd::expected doesn't require language changes, it's a library type. If anything it shows how C++ is multi-paradigm
- ModernMech 5y agoThis is the great strength and weakness of C++. Increasingly the answer to C++'s rough edges is "We don't do things that way anymore. Everyone does X now", where X is the hot new thing. RAII is the best example I can think of, where some people insist that no one would ever use the "new" and "delete" keywords anymore. Except for all the C++ devs that do, and all the C++ code that exists that does and must be maintained. It leads to the current situation where you have C++ "the language" which is everything, and then C++ "the subset that everyone uses" where that subset constantly changes with time and development context.
- pjmlp 5y ago
- vlovich123 5y agoWouldn’t changing the global mutex into a read/write be a simple way to fix things? Shared libraries changing the exception table at the same time as exceptions being thrown seems rare. Might also be fixable in an API-preserving way… Edit: nope. This idea is discussed later in the paper (not fully ruled out but the answer may still require ABI changes for more subtle reasons)
- interroboink 5y agoI think this is addressed in the article in the section starting "A less radical change would be to change the global mutex into an rwlock, but unfortunately that is not easily possible either..."
- plorkyeran 5y ago> A less radical change would be to change the global mutex into an rwlock, but unfortunately that is not easily possible either. Unwinding is not a pure library function but a back and forth between the unwinder and application/compiler code, and existing code relies upon the fact that it is protected by a global lock. In libgcc the callback from dl_iterate_phdr manipulates shared state, and switching to an rwlock leads to data races. Of course it would make sense to change that, but that would be an ABI break, too.
- Findecanor 5y agoCould the mutex be wrapped in a rwlock? New code that don't share state would be able to unwind concurrently while old code would be locked out.
- vlovich123 5y agoThat’s an interesting idea. Might be an elegant way. I was thinking that the exclusive lock could be updated to be RW by hiding the read information in unused bits of the exclusive lock state that’s mutated atomically. I find the paper’s argument thin on why this could only be done in an ABI incompatible way.
- The_rationalist 5y ago
- tsimionescu 5y agoVery interesting that exceptions have much less overhead on the happy path than Rust/Haskell style Either types (std::expected): > For fib we see a slowdown of approx. 60% compared to traditional exceptions, which is still problematic.
- yakkityyak 5y agoI'm not convinced a recursive implementation of `fib` is a reasonable example to draw such a conclusion.
- fwsgonzo 5y agoIt really isn't. People need to stop making trivial benchmarks and draw ... silly ... conclusions. http://www.eecs.northwestern.edu/~robby/courses/322-2013-spring/mytkowicz-wrong-data.pdf http://www.eecs.northwestern.edu/~robby/courses/322-2013-spr... It is a fact that C++ exceptions are largely low overhead, and that you also don't have to use them. In fact, C++ can make everyone happy, because you can choose which parts you like. Personally, knowing how exceptions are implemented and having implemented small parts of the ABI, I can safely say that I will be using exceptions where appropriate in all my C++ projects. Parts where real-time behavior is needed we can use custom containers that don't randomly exit on failure. On low-memory hardware it is beneficial to have either tiny exception footprint or no exceptions at all. It is true that C++ exceptions as they are implemented can be improved upon, breaking ABI. There are also other contenders (Herbceptions) that show great promise, bringing another way of handling exceptions that is not source compatible. Either way, many who work with custom C++ code do not care much about ABI, as everything is source compiled, and such would benefit from a new ABI generation. Zero-overhead exceptions by Herb Sutter: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r1.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p070...
- _0w8t 5y agoIt is not trivial in practice to make code safe in presence of exceptions even if one follows the best C++ practices. The errors can be very subtle and hard to identify. It is another reason besides the performance and code bloat why Chromium disables them.
- forrestthewoods 5y ago> Nevertheless the overhead is so high that std::expected is not a good general purpose replacement for traditional exceptions. This is basically the path Rust has chosen. I’m curious if it’s _actually_ too slow for C++. I feel like the answer must be no.
- synergy20 5y agosame as golang, I doubt that causes serious performance issues but have no proof.
- ncmncm 5y agoIt is always easy to find places where performance doesn't matter, and even Python is fast enough, including startup code in programs where performance otherwise does matter. You cannot draw useful inferences from those cases.
- forrestthewoods 5y agoI would also strongly argue you can’t draw useful inferences from do_fib. To rephrase it another way. In how many contexts is std::expect insufficient? It’s easy to find places where nothing short of hand rolled SIMD is fast enough. You cannot draw useful inferences from those cases.
- shiado 5y agoThis somehow reminded me, wasn't there a competition years back to see who could generate the most compiler error output with the least amount of C++? A few too many templates and you could generate terabytes. Edit: don't think it was this but this is still a fun read https://codegolf.stackexchange.com/questions/1956/generate-the-longest-error-message-in-c https://codegolf.stackexchange.com/questions/1956/generate-t...
- gpderetta 5y agoI had ten of thousand lines C++ compilation errors that would crash emacs while trying to parse/colorize them. Still prefer them to the average python error message.
- CyberRabbi 5y agoI am glad more people are raising the issue with C++ exceptions. Unfortunately I don’t think the argument made in this article is compelling enough. Bjarne is against replacing the current model and has written an entire article responding to various criticisms of the current model. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1947r0.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p194... In particular he has already responded to the efficiency argument with the counter argument that current implementations can be optimized. Even if that optimization process breaks ABI compatibility, it’s still better than breaking source compatibility, which is usually what is being proposed. He is right about that so I don’t think efficiency arguments are going to sway him. I used to embrace C++ exceptions until I had to use C++ in non-conventional environments. For me C++ exceptions are wholly inappropriate for real-time programming since it’s difficult to statically quantify how much time an exception handling sequence may take. There’s also the issue of it requiring malloc() which has its own issues from an interface standpoint in the real-time context. To avoid unbounded malloc, you’d have to set aside a per-thread area for exception storage and require that you never throw an exception value past a certain size.
- _0w8t 5y agoIf the current model stays in place, then there would be no possibility to reconcile the de-facto fork of C++ into code disabling exceptions and code using it with the former can be running on more CPUs than the latter. In turn that leads to monstrosity like file system access API that try to have exception and exception-less versions of each function.
- chippiewill 5y agoProbably worth mentioning that while Bjarne is the creator of C++ and his opinion holds a lot of weight, he's also one of the few language creators who doesn't hold a BDFL hat (and hasn't held that kind of control for a very long time).
- rr808 5y agoI've never liked exceptions, its always awkward combination with returning null or throwing exceptions, or error codes. It always ends up breaking some abstraction making it leaky - eg read_doc throws a file exception, or web exception or higher level exception that has no useful detail. I'm really heartened in the last few years that FP popularity has caused more people to avoid exceptions and even golang doesn't support them.
- rossmohax 5y agoIf not exception, then how to fail constructor?
- CyberRabbi 5y agoThe “Herbceptions” proposal addressed this. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r1.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p070...
- synergy20 5y agoyou can still set a static flag or something in ctor then check it, but you can't do anything about dtor indeed if you need throw there.
- Koshkin 5y agoExcept you are not supposed to throw in a destructor.
- synergy20 5y agohttps://www.cs.technion.ac.il/users/yechiel/c++-faq/dtors-shouldnt-throw.html https://www.cs.technion.ac.il/users/yechiel/c++-faq/dtors-sh... but what if dtor fails(e.g. can't release a resource), is the by-default action terminate or ignore?
- Koshkin 5y agoWell, the "environment" may decide for you in some cases, but there is no default action as such. You log an error and either terminate or ignore - depending on which causes less harm.
- worik 5y agoIt has been many years since I used C++ But returning null is an obvious choice. Combined types which return the structure or an error are another. Has the advantage of returning error information, why failure. There are many ways
- _0w8t 5y agoAnother problematic area is RTTI and dynamic_cast. Chromium disables both that and exceptions. As a replacement in few places where dynamic_cast can be useful Chromium adds a virtual function to the base class to return the subclass or null. Chromium code replaces exceptions with boolean flags and logging or code to kill the current process for bad cases like out-of-bound access.
- erwincoumans 5y agoI always avoid C++ exceptions, and also try to avoid dynamic memory allocations, smart pointers and RTTI etc whenever possible. This is pretty common in latency and (pseudo) real-time performance critical work, such as robotics control and 3d gaming.
- worik 5y agoWhy do you use C++ in those domains? Curious as to the decision criteria.
- cyber_kinetist 5y agoIf you need any sort of numerical programming, C++ is basically unmatched. It's fast and efficient (have total control over memory and heap allocations), while able to create complex math abstractions thanks to its template system and operator overloading. For example, Eigen is the only library (not in C++, but in the entire programming space) that can optimize your math expressions at compile-time. You can also perform techniques like auto-differentiation by using templates. Meanwhile, Rust still doesn't have full const generics, which are needed to create a math library that's both efficient and easy to use (nalgebra still depends on typenum, which is much uglier than even the most esoteric C++ template stuff you can find!)
- mhh__ 5y agoThe D library "Mir GLAS" also does the eigen style optimizations, IIRC. It's not unique to C++ but yes Eigen is by far and away the most mature option in that space. D templates are significantly better than C++ ones in basically every way, but the work is already done in C++ so I can't be too boastful
- erwincoumans 5y agoAgreed. Eigen can be relatively slow: in our Tiny Differentiable Simulator, with C++ templatized code, we generate C code using CppAdCodegen (by tracing a sim step) which is easily 5 times faster than the original Eigen code. And together with openmp it runs very fast: Ant OpenAI gym simulation at 2 million steps per second on an AMD Ryzen 3900x at 12 cores/24 threads). See the (unreadable) generated code from Eigen+CppAd Codegen here: https://github.com/google-research/tiny-differentiable-simulator/blob/master/examples/environments/omp_model_ant_forward_zero.h https://github.com/google-research/tiny-differentiable-simul...
- RcouF1uZ4gsC 5y ago> It breaks the existing ABI, and all shared libraries would have to be compiled with the new model, as otherwise unwinding breaks. I am convinced that maintaining ABI is a huge technical debt to C++. And that is largely occasioned by shared libraries. Rust IMO made a fantastic decision to emphasize source distribution (and making it super easy via cargo and crates).
- synergy20 5y agoyou do pay the compilation time price though, sometimes it takes really a long time to rebuild.
- zozbot234 5y agoRust just punts to the C ABI for shared libraries. Which is sensible since the whole point of those is to share code system-wide, and the C ABI is the de-facto common interface for "foreign" code on systems where shared libraries are widely deployed.
- zabzonk 5y ago> As illustrational example consider this small code fragment: Consider this crap small code fragment. Any function that returns void is almost certainly wrong.
- gpderetta 5y agowhat should abort() return?
- turminal 5y agoEnlighten us, please
- zabzonk 5y agoAny function that does not return a value must have a side effect, else why call it. Calling functions that have side effects (and particularly don't indicate if the side effect was succesful), is not a great idea.
- torstenvl 5y agoAre there any software projects of substantial scale that are purely FP? Even things like I/O or dynamic memory allocation have side effects. So while FP has some great ideas implemented at scale (first class functions and MapReduce perhaps the most well-known), they don't seem very useful by themselves. EDIT: I'm not objecting to the idea "functions should return information," I'm objecting to the idea that side effects are "not a great idea."
- zabzonk 5y agoYou generally want to test if an "I/O or dynamic memory allocation" worked. BTW, I am basically a C++ programmer, not some functional maniac. But whenever I see a void return type, I think something is wrong.
- worik 5y agoNo it is not.
- john567 5y agoWhy do we need to have ambient control flow? This is what exception handling is, it's a hidden control flow. I don't use them. I just create an error type and pass that around. The only legitimate exception I will accept is when you access invalid memory. That's a special case and depending on the environment something extraordinary must happen. But exceptions and exception handling just creates annoying code. It doesn't add value, not really.
- jdlshore 5y agoThere are some kinds of errors that can’t be handled locally, but do need to be handled globally, or generically higher in the call chain. Continuing execution after the error occurs will make the problem worse. Exceptions allow you to cease execution without putting an if statement after every function call.
- jeffbee 5y agoThat's what you get from disabling exceptions: a call to std::abort instead of a throw.
- InfiniteRand 5y agoIn some ways that’s throwing an exception into the calling environment in the form of an error code
- WkndTriathlete 5y agoHaskell's IO monad says, "Hi! With me you don't need exceptions and you don't need to put an if statement after every function call."
- gpderetta 5y agoHow is the implicit control flow in do notation different from exceptions? If anything, the issue is between checked and unchecked exceptions.
- 5y ago
- ziml77 5y agoI find this analysis strange. Yes, C++ does need ergonomic ways to return an error without dynamic allocation (Rust's magic of ? combined with the From/Into traits is nice), but I don't know why you'd analyze the performance impact when you have many failures. If a failure is that common, then you aren't supposed to be using exceptions. It's not really an exceptional circumstance at that point.
- masklinn 5y ago> I don't know why you'd analyze the performance impact when you have many failures. If a failure is that common, then you aren't supposed to be using exceptions. The problem is in that case you get into a probabilistic estimation, which is a nice way to say you roll the dice on your performances: what’s the ratio at which exceptions are too costly or sufficiently cheap? And how does that impact your level of service if e.g. exceptions are extremely rare but they tend to all affect the same request or workload?
- HelloNurse 5y agoIf the program fails too often or too unfairly, why should you blame how such failures are handled? The only reasonable "probabilistic estimation" is that something that happens 10% of the time is normal and it shouldn't be treated as an "exception", even if actually thrown exceptions were fast.
- masklinn 5y ago> If the program fails too often or too unfairly, why should you blame how such failures are handled? Because the discussion is about methods of reporting and handling failure? > The only reasonable "probabilistic estimation" is that something that happens 10% of the time is normal and it shouldn't be treated as an "exception" That makes no sense whatsoever, and doesn't address the question. And even if a ctor fails at a rate of 0.9, it has to report errors via exceptions, because that's the only mechanism available to ctors.
- 5y ago
- synergy20 5y agoC++ STL is a strong selling point for c++, disabling exceptions mean you lose all of STL in the library, unless you're fine to use STL without any error reporting at all. In the gaming case(no rtti, no smart pointer, no exceptions(thus meaning ctor|dtor are "unsafe")), what else do you leave with c++ then? I use c++ but I'm always struggling with yes-or-no for exceptions. c++ is deeply rooted with exceptions, bad or good.
- Rarebox 5y agoThere's very little you need exceptions for with STL. Data structures, algorithms, etc. all work just fine without exceptions. C++ without exceptions is great. Google doesn't use exceptions and they still use STL. Gamedevs usually don't use exceptions and they're fine. Just use some other mechanism for errors, like error codes, absl::Status, std::expected, etc.
- _0w8t 5y agoNote Google explicitly configure runtime to instantly crash on memory allocation errors. With that STL works indeed nicely without exceptions.
- lanstin 5y agoI interviewed at Google once and during some whiteboard forgot to check malloc for NULL return, then mentioned, "oh yeah at <former company> malloc never returns NULL" and the interviewer commented "at Google, malloc spawns a new data center."
- synergy20 5y agoGoogle did not do exceptions due to legacy code base reason, in its announcement it says for new code it will do exception, it just had too many old code and can't do exceptions. Most STL operations and iteration etc will throw exception for errors, if you disable exception, any of those errors, be it recoverable or not, will just std::terminate, which might not be ideal.
- 5y ago
- worik 5y agoBegs the question: Why C++? Legacy projects (I am not being pejorative, they are important) which must be maintained aside, should not C++ be deprecated? We have many new languages, we always had C. What does C++ give us in 2022 that makes up for the enormous cognitive load of understanding and keeping up with it.
- usefulcat 5y agoThe ecosystem is frequently more important than the language.
- criddell 5y agoBelieve it or not, there are a lot of us who enjoy C++ and think the language is getting better all the time. IMHO, it's a pretty good time to learn C++. As for what do you get? Well, compared to C you can get higher programmer productivity and compared to any language other than C, you get great performance.
- addaon 5y agoI find that it's not rare to get better performance from C++ than C. As a trivial example, generic container code in C is likely to be run-time generic and sit on memcpy etc; the same functionality in idiomatic C++ is likely to be compile-time generic and be able to use fixed-size copy/move operations instead.
- freedomben 5y agoC++ is still a wonderfully performant language, and there are still domains where it is clearly a leader (networking, games/graphics, etc). Rust is slowly displacing it but there's still a lot of road left. The maturity, stability, prior art are also worth something. The pitfalls are really not that big and dangerous, although to someone who doesn't do C++ I can see why it has that perception. Additionally, if you aren't a functional programming lover (like I've become lately), C++ is one of the funnest languages to work in (as long as the codebase follows could principles). That said while I used to use C++ for nearly everything, these days I use Elixir for app dev whenever I can, and Ruby and bash for scripts. However if I were going to write a desktop app today I'd most likely go for C++ so I could use Qt. I do really need to try out new GTK though, sounds like it's gotten really great.
- _b 5y agoI think the dilemma for C++ exceptions is that if the programmer actually thinks something should never happen, it is almost always better to just crash. But if the programmer thinks it might happen sometimes, then it is risky to predict it is will be super rare, as often this code will be called in different contexts in the future, so it is safer to use normal returns and flow control. As a result, throwing an exception is basically never the best thing to do.
- Someone 5y agoFTA: The root cause is that the unwinder grabs a global mutex to protect the unwinding tables from concurrent changes from shared libraries Is it unavoidable that that hurts performance badly for the common case? I would think such concurrent changes are rare, so if there’s an asymmetric way to protect against that that’s faster in the happy path exists, that would be a big improvement. https://en.wikipedia.org/wiki/Readers–writer_lock https://en.wikipedia.org/wiki/Readers–writer_lock mentions Read-preferring RW locks, but (from cursory reading) doesn’t say how much that can help.
- chippiewill 5y agoThe article discusses this. RW lock is an option that helps but involves breaking the ABI of EVERY shared library.
- superjan 5y agoIt is avoidable by using locks like you mention, the article actually says so, but it causes an ABI change. They go on to say that this makes it undesirable, but compared to what alternative exactly?
- grandinj 5y agoCounterpoint: I do a lot of perf work on LibreOffice, which makes extensive use of exceptions, and I have never ever even seen the exception throwing show up on a profile, let alone become a problem. I think this paper started with a conclusion, and worked backwards to justify it.
- henrydark 5y agoAs per the piece, open 128 documents on a 128 core machine, and you'll see the difference.
- netr0ute 5y agoHow many people actually use a 128 core machine though? The absolute biggest AMD Threadripper you can get only has 64.
- tneumann 5y agoA dual socket EPYC costs about 15K. You can rent some online for 400$ a month. This gives you 128 cores for a relatively low price.
- henrydark 5y ago96 virtual cores is a popular choice on AWS, being cost effective in some industry scenarios. I'm certain the analysis of the piece applies
- mcguire 5y agoNo one needs more than 64 cores?
- nybble41 5y ago> The absolute biggest AMD Threadripper you can get only has 64. You can get a machine with dual 64-core AMD EPYC 7662 CPUs[0] for a total of 128 cores. It will cost you almost $20k, though—for the most basic configuration. [0] https://bizon-tech.com/bizon-x6000.html#959:8714;960:4858;961:4985;962:4879;963:4882;968:4911;973:4993;979:4995;1076:5882 https://bizon-tech.com/bizon-x6000.html#959:8714;960:4858;96...
- deleted 5y ago[deleted]
- GnarfGnarf 5y agoI manage 1.2MLOC C++ developed over 25 years. I am pleased to say we never had to use exceptions. Just check the return value.
- aeldidi 5y agoHow do you guys handle using other libraries? Everything I can see uses stuff like std::vector and the like at API boundaries, and I'm not aware of a way to construct an std::vector without throwing an exception.
- GnarfGnarf 5y agoWe use the STL. We have never observed an exception thrown.
- TheMonarchist 5y ago> I'm not aware of a way to construct an std::vector without throwing an exception. You use an allocator with enough pool for whatever you need to do and avoid out of bounds access.
- cryptonector 5y ago(Exceptions are bad, full stop. It should all have been monadic all along, with Maybe/Either/Result.) That said, exceptions should have been allocated on the stack, and catch handlers should have been closures that get called with the exception object. I guess this can't be implemented now. As for multi-threading unwinding, the issue there is that unwinding tables are part of the shared objects whence the associated object code comes, and it often has to be possible to unload loaded shared objects, so now what? The situation shouldn't be bleak though: a shared object should never be unloaded while there are threads executing its code, so it should be possible to arrange a slow unload-time operation to update the the process-wide unwinding tables -- think of user-land RCU if you like. The exception handling code should not have to synchronize around unwinding, except that when unwinding completes it might have to notice that there's a pending unload, so wake the thread that is waiting for unwinding. Or maybe not even, because maybe unloading could do something truly breathtaking like check that no thread's stack includes return addresses from the object to be unloaded, and then unwinders would never need to step into the unwinding tables from that object.
- gumby 5y agoI find this paper quite unconvincing. Exceptions are exceptional so in principle it doesn't matter (within reason) how long it takes to throw one as long as it costs nothing not to do so. So measuring the cost of repeated throws IMHO doesn't cast light on any useful case, and the approaches that add runtime cost for the path not taken, even Herb Sutter's, are not acceptable. His code transformation example is simply the compiler behaving properly, unless it can't make that transformation even when foo() is declared noexcept. The high core count is a real issue and a legitimate reason for an ABI break. Exceptions are the kind of below the surface plumbing that can't reasonably be implemented in regular code. And in that regard the paper does make a good suggestion, though it then dismisses it! The tree approach described in section 3.4 seems like the right kind of fix. Ultimately there's a spectrum of branching (`return` -> `break` -> `goto` -> `throw`) all of which need consideration in light of multicore deployment.
- cryptonector 5y agoHow exceptional exceptions really are depends on the code being run. The serialization of stack unwinding issue is a real serious problem.
- gumby 5y ago> How exceptional exceptions really are depends on the code being run. Well yes, but EH is basically trying to be the best way to do a highly non-local goto with a dynamic target. If that part is your bottleneck than indeed you may have a more specialized requirement. I do on occasion build a homemade version of a standard library datatype because I have a specialized need and only need implement the subset our code will call. That doesn't invalidate the more general implementations in the standard library (which tend to be quite good, even corner cases). Likewise sometimes I check for a null pointer being returned or other local error flag. That doesn't mean I don't think exceptions are a good idea. > The serialization of stack unwinding issue is a real serious problem. That is the important part of the paper and as I wrote in my comment, I am disappointed that a possible solution, written and tested by the paper's author, was dismissed.
- adrian17 5y agoThis paper confirmed what I suspected for some time: exceptions are still the only basically-zero-overhead solution available, which ironically completely justifies their existence in usage patterns where exceptions are actually exceptional. There are times when writing/profiling Rust when I wish I had access to exceptions instead of `Result` propagation. I do agree with the mentioned design issues though.
- cryptonector 5y agoThe problem is that if you have exception-happy code, then it becomes a scalability limiter for threading.
- adrian17 5y agoYes, if A: you have code that's expected to "fail" relatively many times, and B: this code is also multithreaded. A lot of code doesn't match one or both of these conditions while still being perf-sensitive. Further, as you can see in other comments, it's not an uncommon belief that exceptions simply shouldn't be used for A (and thus it's reasonable that they aren't optimized for something they aren't supposed to be used for).
- dureuill 5y ago> There are times when writing/profiling Rust when I wish I had access to exceptions instead of `Result` propagation. This introduces a whole bunch of design issues, but isn't `panic` (in unwinding mode) basically C++'s exceptions, implementation-wise? Couldn't we use `panic` + `catch_unwind` as a poor man's exception system, should the performance situation really require so? Also, could we maybe add an attribute `#[exceptional]` to enum variants in a match, that would result in a match implementation closer to exceptions, implementation wise?
- Dork1234 5y agoIt would be interesting to see a breakdown of exception overhead for C++/Rust/Haskell/Julia/Swift in multi-threaded workloads. Sometimes that happens rarely but blocks many threads is a huge pain to deal with.
- lanstin 5y agoJust write code with a bunch of threads that are all logging a few M / second and see if the CPUs are running equally hot or 1 is 100% and the rest are mostly idle.
- asveikau 5y ago> 2) exception unwinding is effectively single-threaded, because the table driven unwinder logic used by modern C++ compilers grabs a global mutex to protect the tables from concurrent changes. > The second problem could potentially be fixed by a sophisticated implementation, but that would definitively be an ABI break and it would require careful coordination of all components involved, including shared libraries. This seems like a perfect application for an rwlock. Threads that throw acquire for reading, so can occur in parallel. Threads that load or unload shared libraries (a pretty rare occurrence outside of process start) acquire for writing and therefore block reads from the table from throwing threads. The shared library loader shouldn't throw during this piece. Come to think of it, throwing should also be pretty rare. C++ exceptions are not really about common control flow, but truly exceptional circumstances. So the high cost serializing parallel throwers doesn't even sound like that huge of a deal. But as I've said, it seems pretty easy to improve upon it. [Edit: It would seem the article does say something about how an rwlock is not feasible with the current implementation ... It doesn't sound terribly convincing]
- zmmmmm 5y agoIt's unclear to me if the author is saying that the global mutex for unwinding the stack interferes with the non-exception-throwing code paths, or are they just saying that the exceptions themselves bottleneck and are so inefficient that this becomes a significant impact on overall throughput? It does seem like at least in theory it should be possible to create a non-locking / blocking exception unwinder, if there is no actual contention between the threads. If that can be done then it seems like the solution should be to do that rather than abandon a whole language feature. This is a bit like the Python GIL question. I would say if the language spec means you have to have a "GIL" in any context in a high performance language like C++ then it ought to be addressed at the spec level.
- beached_whale 5y agoOne can mix the error abstracts, we have to anyways(cmath methods and NaN). When the distance between the error and the caller that can act on the error is large, it makes much more sense to throw in a large number of cases. Otherwise the cost is code that would have been error neutral becoming error opinionated and that is a software engineering cost. But look at some of the costs we have to go to great lengths to get around. std::sqrt has to check for invalid inputs and that means a branch, often even when the code is checked prior or an ASSUME( f > 0.0 ) type thing is expressed. This inhibits auto-vectorization. On some compilers ASSUME( not std::isless( f, 0.0 ) ) can tell the compile that the number is real and >= 0 which elides the branch. But the math-error/errno issue is a big hinderance too. A lot of the costs is the branch itself and telling the compiler it is not needed can boost hot path perf more than the rare cold paths.