{"id":106296,"date":"2022-02-28T07:00:00","date_gmt":"2022-02-28T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=106296"},"modified":"2022-02-27T17:20:20","modified_gmt":"2022-02-28T01:20:20","slug":"20220228-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20220228-00\/?p=106296","title":{"rendered":"Zero-cost exceptions aren&#8217;t actually zero cost"},"content":{"rendered":"<p>There are two common models for exception handling in C++. One is by updating some program state whenever there is a change to the list of things that need to be done when an exception occurs, say, because a new exception handler is in scope or has exited scope, or to add or remove a destructor from the list of things to execute during unwinding. Another model is to use metadata to describe what to do if an exception occurs. There is no explicit management of the state changes at runtime; instead, the exception machinery infers the state by looking at the program counter and consulting the metadata.<\/p>\n<p>Metadata-based exception handling is often <a href=\"https:\/\/mortoray.com\/2013\/09\/12\/the-true-cost-of-zero-cost-exceptions\/\"> misleadingly called <i>zero-cost exceptions<\/i><\/a>, which makes it sound like exceptions cost nothing. In fact, it&#8217;s the complete opposite: Metadata-based exception handling should really be called <i>super-expensive exceptions<\/i>.<\/p>\n<p>The point of metadata-based exception handling is that there is no code in the mainline (non-exceptional) code path for exception support. The hope is that exceptions are rare, so you end up with a net win:<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th>Mode<\/th>\n<th>Runtime-managed<\/th>\n<th>Metadata-based<\/th>\n<\/tr>\n<tr>\n<td>Mainline code<\/td>\n<td>Update state at runtime<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td>Exception occurs<\/td>\n<td valign=\"bottom\">Consult the state to<br \/>\nfind the correct handler<\/td>\n<td>Take the program counter,<br \/>\nfind the metadata that applies to it,<br \/>\nconsult the metadata to<br \/>\nfind the correct handler<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Notice that using metadata-based so-called &#8220;zero-cost&#8221; exceptions actually results in a significantly <i>higher<\/i> cost for throwing an exception, because the exception-throwing machinery has to go find the metadata so it can look up which handler to run. This metadata is typically stored in <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/making-cpp-exception-handling-smaller-x64\/\"> a format optimized for size, not speed<\/a>, so extra work has to happen at exception-throwing time to decode the data in order to find the correct handler.<\/p>\n<p>The name &#8220;zero-cost exceptions&#8221; refers to the empty box in the upper right corner. There is no code generated to maintain state just in case an exception occurs.<\/p>\n<p>But even though the box is empty, that doesn&#8217;t mean that things are still the same as if there were no exceptions.<\/p>\n<p>The presence of exceptions means that the code generation is subject to constraints that don&#8217;t show up explicitly in the code generation: Before performing any operation that could potentially throw an exception, the compiler must spill any object state back into memory if the object is observable from an exception handler. (Any object with a destructor is observable, since the exception handler may have to run the destructor.)<\/p>\n<p>Similarly, potentially-throwing operations limit the compiler&#8217;s ability to reorder or eliminate loads from or stores to observable objects because the exception removes the guarantee of mainline execution.<\/p>\n<p>These costs are not visible to the naked eye. They take the form of lost optimization opportunities.<\/p>\n<p>Zero-cost exceptions are great (despite the blatant misnomer), but be aware that the cost is not actually zero.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shifting the cost to the exceptional path.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-106296","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Shifting the cost to the exceptional path.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=106296"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106296\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=106296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=106296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=106296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}