6 ms·
Some people need to use Rust in places where exceptions aren't allowed (because the unwind tables and cleanup code are too big). Those people include virtually
by pcwalton 12y ago
Some people need to use Rust in places where exceptions aren't allowed (because the unwind tables and cleanup code are too big). Those people include virtually all browser vendors and game developers.
Furthermore, exceptions have this nasty codegen tradeoff. Either you make them zero-cost (as C++, Obj-C, and Swift compilers typically do), in which case throwing an exception is very expensive at runtime, or you make them non-zero-cost (as Java HotSpot and Go 6g/8g do), in which case you eat a performance penalty for every single try block (in Go, defer) even if no exception is thrown. For a language with RAII, every single stack object with a destructor forms an implicit try block, so this is impractical in practice.
The performance overhead of zero-cost exceptions is not a theoretical issue. I remember stories of Eclipse taking 30 seconds to start up when compiled with GCJ (which used zero-cost exceptions) because it throws thousands of exceptions while starting.
The C approach to error handling has a great performance and code size story relative to exceptions when you consider both the error and success paths, which is why systems code overwhelmingly prefers it. It has poor ergonomics and safety, however, which Rust addresses with Result. Rust's approach forms a hybrid that's designed to achieve the performance of C error handling while eliminating its gotchas.
- coldtea 12y ago>The performance overhead of zero-cost exceptions is not a theoretical issue. I remember stories of Eclipse taking 30 seconds to start up when compiled with GCJ (which used zero-cost exceptions) because it throws thousands of exceptions while starting. Then maybe the solution is to not have it throw thousands of exceptions while starting? And have them be, well, exceptional?
- pcwalton 12y agoAPIs have to be designed to allow that in the first place. For instance, the easiest way to check whether a file exists and open if it so is to try to open it and handle the error, because "look before you leap" can be racy. But in exception-based languages, the only way to do that is typically to catch an exception, because the core file I/O APIs throw exceptions on error.
- coldtea 12y agoSure, but why would there be thousands of files missing for example? I can understand something like an unmounted drive or a network being down leading to a slow Eclipse launch due to thousands of exceptions. But those 2 are exceptional circumstances in themselves, and I think it's OK for exceptions to slow down a program in this case. But would those 0-cost exceptions be much of a burden in a normal run of a program? I don't see how checking for thousands of missing file references (or anything similar) is representative of what a program does in normal runs.
- pcwalton 12y agoConsider, for example, checking for robots.txt and favicon.ico. Any search engine or browser is going to generate tons of 404 requests for these resources. If your HTTP library throws exceptions on 404, you're eating an unnecessary cost of stack unwinding on almost every navigation. IIRC the Eclipse case was similar, checking for optional plugin metadata files or something like that.
- barrkel 12y agoAn integer parsing function should not throw an exception on failure to parse (see .NET's Int32.TryParse()); and an HTTP library should not throw an exception on 404. These are spurious examples typically inspired by Java's dreadful (especially early) API design. A REST API library might throw an exception for a 404. But even then it's debatable, because network errors are very common, so they're a poor fit for exceptions. Networks are laggy, so they're also a poor fit for synchronous APIs. Many procedural programming techniques are on shaky ground when they try to paper over the network.
- steveklabnik 12y agoYet, you still end up seeing stories like https://www.coffeepowered.net/2011/06/17/jruby-performance-exceptions-are-not-flow-control/ https://www.coffeepowered.net/2011/06/17/jruby-performance-e...
- 12y ago
- MichaelGG 12y agoIf exceptions were instead named traps or faults, or even just errors, what would the prescriptive advice be?
- cousin_it 12y ago> Some people need to use Rust in places where exceptions aren't allowed (because the unwind tables and cleanup code are too big). Can you describe that scenario in more detail? What happens in case of panic? > I remember stories of Eclipse taking 30 seconds to start up when compiled with GCJ (which used zero-cost exceptions) because it throws thousands of exceptions while starting. I guess many Java developers assume that it's cheap to throw exceptions, and GCJ can't easily change that. In a new language, you wouldn't have that problem. If you had zero-cost exceptions in Rust, and told people that they have the same performance characteristics as C++ exceptions, I think most people would understand.
- pcwalton 12y ago> What happens in case of panic? Process abort. > I guess many Java developers assume that it's cheap to throw exceptions, and GCJ can't easily change that. In a new language, you wouldn't have that problem. No, as I described below, you still do if the core library APIs throw exceptions on error. You have no choice but to catch exceptions if you want to do simple things that programs need to do.
- cousin_it 12y agoIf Rust had zero cost unchecked exceptions, I imagine their usage would be more like C++ than Java. You wouldn't throw on IO errors. You would throw on divide by zero, arithmetic overflow, array index out of bounds...
- pcwalton 12y agoIn fact that's exactly why Rust has panic, which is implemented via zero-cost unwinding under the hood. (Panics can be disabled and turned into an abort if you want to, but only in LTO right now. Improving the support for abort-on-panic is a wanted feature.)
- jallmann 12y ago> Furthermore, exceptions have this nasty codegen tradeoff In OCaml (a language with blazingly fast exceptions), the worst that can happen for each try-block is a register spill or memory store [1]. For a managed language, that's excellent. The only thing I don't like about OCaml exceptions is they are unchecked -- which is strangely out-of-place, given the ML emphasis on type safety. The resistance towards checked exceptions doesn't make sense, either: used properly, checked exceptions could be virtually indistinguishable from typechecked return values. They also enable some really elegant control-flow constructs, although that may take some discipline to use properly in a large project. [1] http://stackoverflow.com/questions/8564025/ocaml-internals-exceptions http://stackoverflow.com/questions/8564025/ocaml-internals-e...
- pcwalton 12y ago> In OCaml (a language with blazingly fast exceptions), the worst that can happen for each try-block is a register spill or memory store [1]. For a managed language, that's excellent. OK, but that's OCaml, a language with no value types (other than 31-bit ints) or RAII. So you don't need custom cleanup code in particular, since the runtime tagging tells you everything you need. That isn't an acceptable approach for Rust, which uses unboxed types with custom cleanup code everywhere. You would need an approach like the approach Go uses to implement defer, which involves pushing and popping custom handlers onto a linked list all the time. > The resistance towards checked exceptions doesn't make sense, either: used properly, checked exceptions could be virtually indistinguishable from typechecked return values. They also enable some really elegant control-flow constructs, although that may take some discipline to use properly in a large project. We could have used checked exceptions, I guess, as they're isomorphic to ADTs, but I don't think there's much of a benefit when checked exceptions still require you to annotate signatures or catch the exception. Also, without subtyping of exceptions, the annotation burden would likely be worse. And the automatic wrapping and unwrapping of values in exception types would probably make systems programmers unhappy that extra indirections and data structures are being conjured up in ways that aren't immediately obvious. Finally, checked exceptions have this annoying property whereby the easiest thing to do to handle them is to write "try { ... } catch (Exception e) {}", and programmers often pick the easiest thing to do. Compare to Rust's ".unwrap()", which is does the sensible thing if you don't want to bubble the exceptions up to your caller, and is much less typing.
- shmerl 12y ago> Some people need to use Rust in places where exceptions aren't allowed (because the unwind tables and cleanup code are too big). Those people include virtually all browser vendors and game developers. Kernel developers is another good example.
- quotemstr 12y agoThe NT kernel uses exceptions all over the place, in the form of SEH. Accessing an unmapped user pointer? Exception. NTFS log error? Exception. There's no fundamental incompatibility between ring 0 and exceptions.
- shmerl 12y agoI'm not sure it's a positive example.
- quotemstr 12y agoI actually prefer NT exceptions to __copy_from_user for obtaining values from userspace. With SEH exceptions, the kernel can just access user pointers directly and handle possible errors in a natural way. This pattern is especially nice because NT allows ring-0 code to call system call functions directly, and in that case, an input pointer might be a kernel pointer, meaning that the SEH block does no harm and everything Just Works. __copy_from_user feels very awkward by comparison.
- sanxiyn 12y agoIt sure is a positive example. NT kernel is very successful and well respected.
- comex 12y agoI don't think this argument makes much sense. If Rust decided to go all exception-happy, it could have an option to codegen all function calls as if they were wrapped in try! - or at least, all calls that can throw, a flag which can be either specified in declarations of stored in metadata, though not without some complexity in the latter case in order to handle separate compilation correctly. I disagree with the GP about whether Rust's error system is elegant, but syntax isn't that tightly bound to codegen. Indeed, I'd be interested to see an optional pass someday that does the opposite, converting Result returns to exceptions at the LLVM IR level.
- pcwalton 12y ago> If Rust decided to go all exception-happy, it could have an option to codegen all function calls as if they were wrapped in try! That's what I mean by overhead for even the non-throwing case. > - or at least, all calls that can throw, a flag which can be either specified in declarations of stored in metadata, though not without some complexity in the latter case in order to handle separate compilation correctly. That's checked exceptions, and I don't think they'd be a good fit for Rust for a few reasons that I've outlined in comments downthread, but briefly: (a) the annotation burden would be just as bad; (b) automatic unwrapping and wrapping of function result values could be surprising for systems programmers; (c) checked exceptions like Java don't allow for the nice .unwrap() syntax; (d) checked exceptions, as an out-of-band effect type, don't play as well with higher-order functions as just using the normal return value. But yes, it is possible.
- legulere 12y agoIt's still important to remember that zero-cost exceptions are a bit of a misnomer. They may not have runtime costs in case no exception is thrown, but can hinder optimisation by the compiler.
- quotemstr 12y ago> throwing an exception is very expensive at runtime They shouldn't be that expensive. It'd help if libgcc didn't take a mutex around the exception dispatch (because of the very unlikely possibility of a concurrent dlclose) so that different threads could do exception lookups in parallel. (I once tried replacing that mutex with an rwlock. It didn't help very much.)
- saurik 12y agoThe argument made by others--that you are working from a mental model of exceptions as made popular by Java as opposed to as a generic language feature for removing boilerplate (essentially a syntax sugar for a specific kind of error monad)--should be well examined (and I don't want to detract from it, as it is really important and seems to have colored your perception quite badly :( enough that it seems to have led you astray even in this comment... one of the big issues with Java exception performance is the required reification and storage of stacktraces, a language quirk that gcj must honor), but I am going to bring up a related one of error handling performance: C's error handling is expensive (in addition to being verbose and unsafe, Rust sadly only trying to work on the latter) because it requires functions that could possibly return an error but normally don't (which certainly should be "all functions that claim to return errors", though I will argue is actually the set of "all functions", but this is a longer argument) to have to go out of their way, moving around values in registers at minimum, to indicate "success". C++ zero-cost exceptions make successful code run fast--faster than the equivalent successful C code--and in the cases where the compiler infrastructure (or more likely in 2015, cultural baggage, as it really doesn't take much effort to bootstrap exceptions) or memory constraints prevents them from being used, the great thing about the C++ mindset is that they don't cost you anything to ignore: even in a program that has exceptions elsewhere, if your code doesn't need unwind semantics (and a lot of code doesn't), you don't take any overhead; and, as kernel and game developers also often build their own data structures--not to avoid exceptions, but for other important pragmatic reasons (including but not limited to virtual memory restrictions, control over non-determinism, and extreme case-specific optimization)--it is easy (and, I would argue, strongly preferable) to free most developers from frustrating error handling boilerplate while still seeing (as we do) the developers who somehow need to avoid these features having no issues doing so (as we do in the C++ community).
- burntsushi 12y ago> C's error handling is expensive (in addition to being verbose and unsafe, Rust sadly only trying to work on the latter) Did you read the OP? I realize the post is long, but the short story is that error handling in Rust can be concise and ergonomic. It's only verbose if you're doing explicit case analysis every where. I've found error handling in Rust to be the exact opposite of frustrating. It's pleasant and explicit.