<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Aug 9, 2016, at 7:00 PM, Félix Cloutier &lt;<a href="mailto:felixcca@yahoo.ca" class="">felixcca@yahoo.ca</a>&gt; wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">No, I fully understand this. My point is that this doesn't seem to accurately represent the cost of exceptions.<div class=""><br class=""></div><div class="">In a JSON parser, since the topic has been brought up, you don't have Y*P calls that succeed and N*(1-P) calls that fail. You have Y*P calls that succeed and *at most one* call that fails. That's because once you hit the (1-P), you stop parsing. This heavily biases calls in favor of succeeding, which is what I tried to illustrate with my anecdote.</div></div></div></blockquote><div><br class=""></div>This is true of JSON deserialization, where typically you wouldn't do something like prospectively deserialize a value one way and then try something else if that fails. &nbsp;But it's not true of, say, a parser for a more ambiguous language, or for any number of other applications in which non-terminal failures are more common.</div><div><br class=""></div><div>As Joe said, our intended ABI here is to get this down to a single branch-on-nonzero-register instruction immediately after the return, which is an overhead we're pretty comfortable with.</div><div><br class=""></div><div>John.</div><div><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">I haven't attempted statistics in a while, but that looks like a geometric distribution to me. That would give something like:</div><div class=""><br class=""></div><div class="">Y_1 * (1/P) + N_1 &lt; Y_2 * (1/P) + N_2</div><div class=""><br class=""></div><div class="">in which Y completely dominates N, especially as P goes smaller.</div><div class=""><div class=""><div class=""><div class="">
<br class="Apple-interchange-newline"><span style="font-family: 'Lucida Grande'; font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Félix</span>
</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">Le 9 août 2016 à 16:22:08, John McCall &lt;<a href="mailto:rjmccall@apple.com" class="">rjmccall@apple.com</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Aug 9, 2016, at 8:19 AM, Félix Cloutier via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><blockquote type="cite" class="">“Zero cost” EH is also *extremely* expensive in the case where an error is actually throw in normal use cases. &nbsp;This makes it completely inappropriate for use in APIs where errors are expected in edge cases (e.g. file not found errors).<br class=""></blockquote><br class="">Anecdote: I work with a web service that gets several million hits a day. Management loves to use the percentage of succeeding web requests as a measure of overall health. The problem with that metric is that when a web request fails, clients fall in an unhealthy state and stop issuing requests for a while. Therefore, one failing request prevents maybe twenty more that would all have failed if the client hadn't bailed out, but these don't show in statistics. This makes us look much better than we actually are.<br class=""><br class="">If I had any amount of experience with DTrace, I'd write a script that logs syscall errors to try and see how the programs that I use react to failures. I'm almost certain that when one thing stops working, most programs backs out of a much bigger process and don't retry right away. When a program fails to open a file, it's also failing to read/write to it, or whatever else people normally do after they open files. These things are also expensive, and they're rarely the type of things that you need to (or even just can) retry in a tight loop. My perception is that the immediate cost of failing, even with expensive throwing, is generally dwarfed by the immediate cost of succeeding, so we're not necessarily losing out on much.<br class=""><br class="">And if that isn't the case, there are alternatives to throwing that people are already embracing, to the point where error handling practices seem fractured.<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">I don't really know what to expect in terms of discussion, especially since it may boil down to "we're experts in this fields and you're just peasants”<br class=""></blockquote><br class="">I’m not sure why you think the Swift team would say something that derogatory. &nbsp;I hope there is no specific action that has led to this belief. If there is, then please let me know.<br class=""></blockquote><br class="">Of course not. All of you have been very nice and patient with us peasants, at least as far as "us" includes me. :) This was meant as a light-hearted reflection on discussing intimate parts of the language, where my best perspective is probably well-understood desktop/server development, whereas the core team has to see that but also needs a high focus on other things that don't even cross my mind (or at least, that's the heroic picture I have of you guys).<br class=""><br class="">For instance, my "expensive" stops at "takes a while". Your "expensive" might mean "takes a while and drains the very finite energy reserves that we have on this tiny device" or something still more expansive. These differences are not always immediately obvious.<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">However, as linked above, someone did for Microsoft platforms (for Microsoft-platform-style errors) and found that there is an impact.<span class="Apple-converted-space">&nbsp;</span><br class=""></blockquote><br class="">C++ and Swift are completely different languages in this respect, so the analysis doesn’t translate over.<br class=""></blockquote><br class="">The analysis was (probably?) done over C++ and HRESULTs but with the intention of applying it to another language (Midori), and it most likely validated the approach of other languages (essentially everything .NET-based). Several findings of the Midori team are being exported to Microsoft's new APIs, notably the async everywhere and exceptions everywhere paradigms, and these APIs are callable from both so-called managed programs (GCed) and unmanaged programs (ref-counted).<br class=""><br class="">Swift operations don't tend to throw very much, which is a net positive, but it seems to me that comparing the impact of Swift throws with another language's throws is relatively fair. C# isn't shy of FileNotFoundExceptions, for instance.</div></div></blockquote><div class=""><br class=""></div>I think you may be missing Chris's point here.</div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Exception ABIs trade off between two different cases: when the callee throws and when it doesn't. &nbsp;(There are multiple dimensions of trade-off here, but let's just talk about cycle-count performance.) &nbsp;Suppose that a compiler can implement a call to have cost C if it just "un-implements" exceptions, the way that a C++ compiler does when they're disabled. &nbsp;If we hand-wave a bit, we can pretend that all the costs are local and just say that any particular ABI will add cost N to calls that don't throw and cost Y to calls that do. &nbsp;Therefore, if calls throw with probability P, ABI 1 will be faster than ABI 2 if:</div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">&nbsp; &nbsp;Y_1 * P + &nbsp;N_1 * (1 - P) &lt; Y_2 * P + &nbsp;N_2 * (1 - P)</div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">So what is P? &nbsp;Well, there's a really important difference between programming languages.</div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">In C++ or C#, you have to compute P as a proportion of<span class="Apple-converted-space">&nbsp;</span><i class="">every call made by the program</i>. &nbsp;(Technically, C++ has a way to annotate that a function doesn't throw, and it's possible under very specific circumstances for a C++ or C# implementation to prove that even without an annotation; but for the most part, every call must be assumed to be able to throw.) &nbsp;Even if exceptions were idiomatically used in C++ for error reporting the way they are in Java and C#, the number of calls to such "failable" functions would still be completely negligible compared to the number of calls to functions that literally cannot throw unless (maybe!) the system runs out of memory. &nbsp;Therefore, P is tiny — maybe one in a trillion, or one in million in C# if the programmer hasn't yet discovered the non-throwing APIs for testing file existence. &nbsp;At that kind of ratio, it becomes imperative to do basically anything you can to move costs out of N.</div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">But in Swift, arbitrary functions can't throw. &nbsp;When computing P, the denominator only contains calls to functions that really can report some sort of ordinary semantic failure. &nbsp;(Unless you're in something like a rethrows function, but most of those are pretty easy to specialize for non-throwing argument functions.) &nbsp;So P is a lot higher just to begin with.</div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Furthermore, there are knock-on effects here. &nbsp;Error-handling is a really nice way to solve certain kinds of language problem. &nbsp;(Aside: I keep running into people writing things like JSON deserializers who for some reason insist on making their lives unnecessarily difficult by manually messing around with Optional/Either results or writing their own monad + combinator libraries or what not. &nbsp;Folks, there's an error monad built into the language, and it is designed exactly for this kind of error-propagation problem; please just use it.) &nbsp;But we know from experience that the expense (and other problems) of exception-handling in other languages drives people towards other, much more awkward mechanisms when they expect P to be higher, even if "higher" is still just 1 in 100 or so. &nbsp;That's awful; to us, that's a total language failure.</div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">So the shorter summary of the longer performance argument is that (1) we think that our language choices already make P high enough that the zero-cost trade-offs are questionable and (2) those trade-offs, while completely correct for other languages, are known to severely distort the ways that programmers use exceptions in those languages, leading to worse code and more bugs. &nbsp;So that's why we aren't using zero-cost exceptions in Swift.</div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">John.</div></div></blockquote></div><br class=""></div></div></div></div></div></blockquote></div><br class=""></body></html>