{"id":3958,"date":"2026-04-01T18:13:46","date_gmt":"2026-04-01T18:13:46","guid":{"rendered":"https:\/\/writingagame.com\/?p=3958"},"modified":"2026-04-13T23:49:38","modified_gmt":"2026-04-13T23:49:38","slug":"chapter-15-code-optimization","status":"publish","type":"post","link":"https:\/\/writingagame.com\/index.php\/2026\/04\/01\/chapter-15-code-optimization\/","title":{"rendered":"Chapter 15. C++ code optimization"},"content":{"rendered":"\n<p><strong>Tags:<\/strong> <em>C++, Compiler Optimization, Debugging, Cross-platform, Windows &amp; Android, Project Management<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>I hasten to disappoint you: this chapter isn&#8217;t about &#8220;how to&#8221;, but quite opposite: &#8220;how to <strong>NOT<\/strong>&#8220;.<\/p>\n\n\n\n<p>Judging by the fact that you are reading this, I dare to assume you are a C++ developer, which means your code is ALREADY optimized <em>well enough<\/em> by your own design and Project vision. Compiler interference with the logic and consistency of your code can cause more harm than good.<\/p>\n\n\n\n<p>DISABLING &#8220;optimization&#8221; will force CPU to execute your code exactly the way you wrote it, not the way Compiler reshuffled, compressed and &#8220;optimized&#8221; it.<\/p>\n\n\n\n<p>After releasing my own Project to public, I was consistently plagued by mysterious &#8220;ghost&#8221; crashes on some users&#8217; devices that I couldn&#8217;t even reproduce on mine. While many of these were caused by my own logic and carelessness &#8211; and eventually fixed &#8211; many remained a complete mystery until <em>Gemini <\/em>brought a compiler optimization factor to my attention.<\/p>\n\n\n\n<p>A glance at Project&#8217;s properties surprised me a lot: every single &#8220;optimization&#8221; option was set to &#8220;on&#8221; or to default, which is also &#8220;on&#8221;, even though I never asked anyone to &#8220;optimize&#8221; me.<\/p>\n\n\n\n<p><strong>I don&#8217;t need &#8220;better,&#8221; I <strong>need <\/strong>exactly as I wrote.<\/strong><\/p>\n\n\n\n<p>Since my project is <strong>cross-platform<\/strong> (Android + Windows), will consider both:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Android:<\/h2>\n\n\n\n<p>(<em>Android Studio<\/em>)<\/p>\n\n\n\n<p>On Android it is handled by <em>CMakeList.txt<\/em>.<\/p>\n\n\n\n<p><strong>Step 1<\/strong>. Compiler: Right after <em>project(&#8220;whatever&#8221;)<\/em> insert:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#disable optimization\nset(CMAKE_CXX_FLAGS_RELEASE \"-O0 -g -fno-omit-frame-pointer\")\nset(CMAKE_C_FLAGS_RELEASE \"-O0 -g -fno-omit-frame-pointer\")<\/code><\/pre>\n\n\n\n<p><strong>Step 2<\/strong>. Linker: After <em>add_library(&#8230;)<\/em> add:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#disable optimization\ntarget_link_options(*** PRIVATE\n    \"-Wl,-z,max-page-size=16384\"\n    \"-Wl,--icf=none\"\n)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>IMPORTANT<\/strong>: Replace *** with your project name.<\/li>\n<\/ul>\n\n\n\n<p>That&#8217;s it. In my case, the APK file size increased by only 1 KB, which means there was practically nothing to &#8220;optimize.&#8221;<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Windows:<\/h2>\n\n\n\n<p>(<em>Visual Studio<\/em>)<\/p>\n\n\n\n<p>As you can guess, here it is handled by Project <em>Properties<\/em>. You have to fix it in 4 (FOUR, Carl !) places. The following instructions also include PDB handling (in order to get readable <em>crash stack trace<\/em> report). So, go to Properties, pick &#8220;<em>All configurations<\/em>&#8220;, then:<\/p>\n\n\n\n<p><strong>Step 1<\/strong>. <em>C++-&gt;Optimization<\/em>. Set to following, then &#8211; <em>Apply<\/em>:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b04\/c15\/01.jpg\"><\/p>\n\n\n\n<p><strong>Step 2<\/strong>. <em>Linker-&gt;Optimization<\/em>. Set to following, then &#8211; <em>Apply<\/em>:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b04\/c15\/02.jpg\"><\/p>\n\n\n\n<p><strong>Step 3<\/strong>. <em>Linker-&gt;Debugging<\/em>. Set to following, then &#8211; <em>Apply<\/em>:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b04\/c15\/03.jpg\"><\/p>\n\n\n\n<p><strong>Step 4<\/strong>. <em>C++-&gt;Code generation<\/em>. Set to following, then &#8211; <em>Apply<\/em>:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b04\/c15\/04.jpg\"><\/p>\n\n\n\n<p>Now &#8211; <em>Ok<\/em>.<\/p>\n\n\n\n<p>In my case EXE size TRIPLED, which suggests, that <em>Visual Studio<\/em> is more aggressive to your code than Android (or maybe just PDB-related extra load?). Now size is <strong>5MB<\/strong>, which is still not too bad for a 3D app, comparing to 100MB <em>optimized <\/em>&#8220;Hello World&#8221; in some commercial frameworks.<\/p>\n\n\n\n<p>So, congratulations! You&#8217;ve successfully gotten rid of the uninvited guest.<\/p>\n\n\n\n<p>While it\u2019s unlikely it will resolve <strong>ALL <\/strong>crashes, I can promise you\u2019ll get <strong>fewer<\/strong>, especially &#8220;ghost&#8221; ones.<\/p>\n\n\n\n<p>Additionally, on both platforms, execution has become noticeably smoother, I would even say more &#8220;<em>effortless<\/em>.&#8221;<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"mb-2\">Tags: C++, Compiler Optimization, Debugging, Cross-platform, Windows &amp; Android, Project Management I hasten to disappoint you: this chapter isn&#8217;t about &#8220;how to&#8221;, but quite opposite: &#8220;how to NOT&#8220;. Judging by the fact that you are reading this, I dare to assume you are a C++ developer, which means your code is ALREADY optimized well enough [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3971,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-3958","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cross-platform-3d"],"_links":{"self":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/3958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/comments?post=3958"}],"version-history":[{"count":37,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/3958\/revisions"}],"predecessor-version":[{"id":4312,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/3958\/revisions\/4312"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/media\/3971"}],"wp:attachment":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/media?parent=3958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/categories?post=3958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/tags?post=3958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}