{"id":4967,"date":"2016-10-03T16:00:46","date_gmt":"2016-10-03T16:00:46","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/facts\/"},"modified":"2023-06-26T12:40:36","modified_gmt":"2023-06-26T12:40:36","slug":"facts","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/facts\/","title":{"rendered":"Facts"},"content":{"rendered":"<p>After the <a href=\"https:\/\/www.modernescpp.com\/index.php\/myths\">myths<\/a>, the facts will follow. Therefore, we leave the area of half-truth and untruth statements about C++.<\/p>\n<p><!--more--><\/p>\n<p>My reasoning in this post is based on C++98. That is for two reasons. First, the MISRA C++ guidelines and the &#8220;Technical Report C++ on Performance&#8221; are written before the C++11 standard. Second, I show that classical C++ is even powerful enough to refute the myths.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4966\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/facts.png\" alt=\"facts\" width=\"643\" height=\"483\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/facts.png 643w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/facts-300x225.png 300w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<h3>MISRA C++<\/h3>\n<p>The Motor Industry Software Reliability Association published the current MISRA C++:2008 guidelines. They are based on the <a href=\"https:\/\/de.wikipedia.org\/wiki\/MISRA-C\">MISRA C<\/a> guidelines from the year 1998. Originally designed for the automotive industry, they became the de facto standard for implementing critical software in the aviation, military, and medical sectors. As MISRA C, MISRA C++ describes guidelines for a safe subset of C++.<\/p>\n<p>This subset consists of more than 200 rules being classified as a document, required, or advisory.<\/p>\n<ul>\n<li>Document:\n<ul>\n<li>Mandatory requirements for the developer<\/li>\n<li>Derivations are not permitted<\/li>\n<\/ul>\n<\/li>\n<li>Required:\n<ul>\n<li>Mandatory requirements for the developer<\/li>\n<li>Formal derivation must be raised<\/li>\n<\/ul>\n<\/li>\n<li>Advisory:\n<ul>\n<li>Should be followed as far as possible<\/li>\n<li>The formal derivation is not necessary but may be considered<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>The rules are about the C++ core language and the libraries. To clarify, I will present a few rules from <a href=\"http:\/\/www.misra-cpp.com\/MISRACHome\/tabid\/128\/Default.aspx\">MISRA C++.<\/a><\/p>\n<ul>\n<li>Unnecessary construct\n<ul>\n<li>The project shall not contain unreachable code. (required)<\/li>\n<li>The project shall not contain unused variables. (required)<\/li>\n<\/ul>\n<\/li>\n<li>Assembler\n<ul>\n<li>All usage of assemblers shall be documented. (document)<\/li>\n<\/ul>\n<\/li>\n<li>Arithmetic\n<ul>\n<li>The use of floating-point arithmetic shall be documented. (document)<\/li>\n<\/ul>\n<\/li>\n<li>Language\n<ul>\n<li>The code shall conform to the C++03 standard (Remark: Small addition to C++98). (required)<\/li>\n<\/ul>\n<\/li>\n<li>Comments\n<ul>\n<li>No C comments shall be used to &#8220;comment out&#8221; code. (required)<\/li>\n<li>No C++ comments shall be used to &#8220;comment out&#8221; code. (advisory)<\/li>\n<\/ul>\n<\/li>\n<li>Pointer conversions\n<ul>\n<li><span style=\"font-family: courier new,courier;\">NULL<\/span> shall not be used as an integer value. (required)<\/li>\n<\/ul>\n<\/li>\n<li>Multiple base classes\n<ul>\n<li>Classes should not be derived from virtual bases. (advisory)<\/li>\n<\/ul>\n<\/li>\n<li>Virtual functions\n<ul>\n<li>Each overriding virtual function shall be declared with the virtual keyword.<span style=\"font-family: courier new,courier;\"><\/span> (required)<\/li>\n<\/ul>\n<\/li>\n<li>Exception handling\n<ul>\n<li>Exceptions shall only be used for error handling. (document)<\/li>\n<\/ul>\n<\/li>\n<li>Templates\n<ul>\n<li>All partial and explicit specializations for a template shall be declared in the same file as the declarations of their primary template. (required)<\/li>\n<\/ul>\n<\/li>\n<li>Macro replacements\n<ul>\n<li>The # and ## operators should not be used. (advisory)<\/li>\n<\/ul>\n<\/li>\n<li>Library\n<ul>\n<li>The C library shall not be used. (required)<\/li>\n<li>All library code shall conform to&nbsp;MISRA C++.(document)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<\/li>\n<\/ul>\n<p>You can verify these and all the other MISRA C++ rules with static code analysis tools.<\/p>\n<p>Which conclusions can we draw from the MISRA C++ rules for using C++ in critical systems? <strong>MISRA C++ excludes neither one feature nor the whole language.<\/strong><\/p>\n<p>MISRA C++ even goes one step further and emphasizes why the importance of C++ in critical systems becomes more important. (1.1 The use of C++ in critical systems):<\/p>\n<ul>\n<li><em>C++ supports high-speed, low-level input\/output operations essential to many embedded systems.<\/em><\/li>\n<li><em>The increased complexity of applications makes using a high-level language more appropriate than assembly language.<\/em><\/li>\n<li><em>C++ compilers generate code similar to C&#8217;s size and RAM requirements.<\/em><\/li>\n<\/ul>\n<p>But one small downer remains. MISRA C++ is based on classical C++. Modern C++ has a lot more to offer for embedded systems. Sadly, <a href=\"http:\/\/www.misra.org.uk\/\">MISRA<\/a> C++ can not keep in lockstep with the C++ standardization. But from an online discussion, I know they want to fill the gap.<\/p>\n<\/p>\n<h2>Technical report on C++ performance<\/h2>\n<p>The Working Group WG 21 was published in the <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/TR18015.pdf\">ISO\/IEC TR 18015<\/a>. The title sounds not very interesting, but that document is the ultimate source if you want to get the performance numbers of the C++ features. The document expresses its concerns directly to the point.&nbsp;&nbsp;<\/p>\n<ul>\n<li><em>to give the reader a model of time and space overheads implied by the use of various C++ language and library features,<\/em><\/li>\n<li><em>to debunk widespread myths about performance problems,<\/em><\/li>\n<li><em>to present techniques for the use of C++ in applications where performance matters, and<\/em><\/li>\n<li><em>to present techniques for implementing C++ Standard language and library facilities to yield efficient code.<\/em><\/li>\n<\/ul>\n<p>The authors of the paper, with more than 200 pages are well-known C++ experts like&nbsp;Dave Abrahams, Howard Hinnand, Dietmar K\u00fchl, Dan Saks, Bill Seymour, Bjarne Stroustrup, and Detlef Vollmann.<\/p>\n<p>The scope of the document is the C++ features, their overhead and usage, the creation of efficient libraries in C++, the usage of C++ in embedded systems, and the interfaces in C++ to communicate with the hardware. In particular, the C++ features and their overhead and usage are the main topics of this post.<\/p>\n<h3>C++ features, overhead, and usage<\/h3>\n<p>The authors use for their analysis three computer architectures with five different compilers. They use compilers with different optimization options. I will give you only an idea of the results that are quite remarkable.<\/p>\n<ul>\n<li>Namespaces\n<ul>\n<li>Have no significant overhead in size and performance<\/li>\n<\/ul>\n<\/li>\n<li>Type converting operator\n<ul>\n<li>The C++ casts <span style=\"font-family: courier new,courier;\">const_cast, static_cast, <\/span>and <span style=\"font-family: courier new,courier;\">reinterpret_cast<\/span> differ neither in size nor performance from their C pedant.<\/li>\n<li>The runtime executed <span style=\"font-family: courier new,courier;\">dynamic_cast<\/span> has some overhead<em><\/em>. (Remark: The conversion has no C pendant.).<\/li>\n<\/ul>\n<\/li>\n<li>Inheritance\n<ul>\n<li>Class\n<ul>\n<li>A <span style=\"font-family: courier new,courier;\">class<\/span> without virtual functions is as big as a <span style=\"font-family: courier new,courier;\">struct<\/span>. <span style=\"font-family: courier new,courier;\"><\/span><span style=\"font-family: courier new,courier;\"><\/span><\/li>\n<li>A class with virtual functions has the overhead of a pointer and a virtual function table. These are about 2 to 4 bytes.&nbsp;<\/li>\n<\/ul>\n<\/li>\n<li>Function calls\n<ul>\n<li>The call of a non-virtual, non-static, and non-inline function is as expensive as a free function.<\/li>\n<li>The call of a virtual function is as expensive as a free function with the help of a pointer stored in a table.&nbsp;<\/li>\n<li>Virtual functions of a class template can cause overhead in size. (Remark: Functions that depend not on template parameters should be extracted in a base class. Therefore, the functionality &#8211; independent of template parameters &#8211; can be shared between all derived class templates.)<\/li>\n<li>The <em>inlining <\/em>of a function causes significant performance benefits and is close to the performance of a C macro.<\/li>\n<\/ul>\n<\/li>\n<li>Multiple inheritances\n<ul>\n<li>It can cause time and\/or space overhead.<\/li>\n<li>Virtual base classes have overhead compared to non-virtual base classes.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Run-time type&nbsp;information (RTTI)\n<ul>\n<li>There are about 40 additional bytes for each class necessary.<\/li>\n<li>The <span style=\"font-family: courier new,courier;\">typeid<\/span> call is relatively slow. That seems to be due to the quality of the implementation.<\/li>\n<li>The conversion during runtime with <span style=\"font-family: courier new,courier;\">dynamic_cast<\/span> is slow, according to the reports; that should also be due to the quality of the implementation.<\/li>\n<\/ul>\n<\/li>\n<li>Exception handling\n<ul>\n<li>There are two strategies for dealing with exceptions. These are the code and the table strategy. The coding strategy must move and manage additional data structures for exceptions. The table strategy has the execution context in a table.&nbsp;\n<ul>\n<li>The&nbsp;<em><\/em>coding strategy has a size overhead for the stack and the runtime. The runtime overhead is about 6%. This overhead exists even without the throwing of an exception.<\/li>\n<li><em><\/em>The table strategy has neither overhand in program size nor in runtime. (Remarks: That statements hold only if no exceptions were thrown.). The table strategy is more challenging to implement.&nbsp;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Templates\n<ul>\n<li>You get for each template instantiation a new class template or function template. Therefore, the naive use of temples can cause code bloat. Modern C++ compilers can massively reduce the number of template instantiations. The usage of partial or full specialization helps to reduce template instantiations.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>You can read the details, the exact number, and a few additional topics directly in the report: <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/TR18015.pdf\">TR18015.pdf.<\/a><\/p>\n<p>The &#8220;Technical Report on C++ Performance&#8221; also has a minor downer. The report is from 2006. In particular, C++11 has a lot of features for writing faster code.&nbsp;I asked Detlef Vollmann, an author of the paper on the <a href=\"http:\/\/meetingcpp.com\/\">Meeting C++<\/a>, if they plan to update the report to modern C++. His request to Bjarne Stroustrup gave the result that he has no time. That is understandable but a minor downer.&nbsp;<\/p>\n<p>I will write in the<a href=\"https:\/\/www.modernescpp.com\/index.php\/automatically-inititialized\"> next post<\/a> about the automatic type deduction with <span style=\"font-family: courier new,courier;\">auto<\/span>. What has <span style=\"font-family: courier new,courier;\">auto<\/span> in common with safety-critical systems? A lot!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After the myths, the facts will follow. Therefore, we leave the area of half-truth and untruth statements about C++.<\/p>\n","protected":false},"author":21,"featured_media":4966,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[467],"class_list":["post-4967","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-myths"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=4967"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4967\/revisions"}],"predecessor-version":[{"id":6940,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4967\/revisions\/6940"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4966"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}