{"id":30589,"date":"2020-11-06T17:06:14","date_gmt":"2020-11-06T22:06:14","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=30589"},"modified":"2020-11-25T16:16:19","modified_gmt":"2020-11-25T21:16:19","slug":"slow-swift-compilation-of-boolean-comparisons","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2020\/11\/06\/slow-swift-compilation-of-boolean-comparisons\/","title":{"rendered":"Slow Swift Compilation of Boolean Comparisons"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/boolean-comparison-causes-extremely-slow-compilation\/41609\">DeskA<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/boolean-comparison-causes-extremely-slow-compilation\/41609\"><p>Since we moved parts of our codebase to swift, our compile times have effectively quadrupled. Trying to combat this, we&rsquo;ve used the function and expression debug time flags to figure out if there&rsquo;s something to be saved by simplifying expressions.<\/p><\/blockquote>\n\n<p>Via <a href=\"https:\/\/twitter.com\/nicklockwood\/status\/1324491275291295749\">Nick Lockwood<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/nicklockwood\/status\/1324491275291295749\">\n<p>TIL writing<\/p>\n<pre>if foo == false {}<\/pre>\n<p>Compiles dramatically more slowly in Swift than:<\/p>\n<pre>if !foo {}<\/pre>\n<\/blockquote>\n\n<p><a href=\"https:\/\/forums.swift.org\/t\/boolean-comparison-causes-extremely-slow-compilation\/41609\/11\">DeskA<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/boolean-comparison-causes-extremely-slow-compilation\/41609\/11\"><p>There are multiple instances of boolean comparisons being slow in our project. I can easily cut our overall compilation time by 10s by getting rid of explicit comparisons unfortunately<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/forums.swift.org\/t\/boolean-comparison-causes-extremely-slow-compilation\/41609\/12\">Xiaodi Wu<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/boolean-comparison-causes-extremely-slow-compilation\/41609\/12\"><p>In fact, it does not know that both sides are of type <code>Bool<\/code>, because <code>==<\/code> can be implemented to compare heterogeneous types and <code>false<\/code> can be any type that&rsquo;s expressible by a Boolean literal. Therefore, it has to figure out every possible combination of implementations of <code>==<\/code> and types conforming to <code>ExpressibleByBooleanLiteral<\/code> that is available for use here to see if it&rsquo;s a better match.<\/p>\n<p>[&#8230;]<\/p>\n<p>For numeric types specifically, there is a hardcoded compiler shortcut to make compile times tolerable until a general solution is discovered. No such optimization is hardcoded for Boolean values because it&rsquo;s <em>not<\/em> idiomatic to write <code>== true<\/code> and <code>== false<\/code>.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/10\/01\/buildwatch\/\">Buildwatch<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/12\/05\/guarding-against-long-swift-compiles\/\">Guarding Against Long Swift Compiles<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/07\/18\/exponential-time-complexity-in-the-swift-type-checker\/\">Exponential Time Complexity in the Swift Type Checker<\/a><\/li>\n<\/ul>\n\n<p id=\"slow-swift-compilation-of-boolean-comparisons-update-2020-11-25\">Update (2020-11-25): <a href=\"https:\/\/useyourloaf.com\/blog\/slow-swift-compiler-performance\/\">Keith Harrison<\/a>:<\/p>\n<blockquote cite=\"https:\/\/useyourloaf.com\/blog\/slow-swift-compiler-performance\/\">\n<p>Before you jump to conclusions and blame the Swift compiler for slow build times it&rsquo;s worth getting a summary of the whole build process. Xcode 10 added a build timing summary.<\/p>\n<p>[&#8230;]<\/p>\n<p>There are a couple of compiler flags you can enable to get some extra information when the compiler is slowing down[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>There&rsquo;s a good <a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2018\/408\/\">WWDC 2018 Session<\/a> on speeding up your Xcode builds.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>DeskA: Since we moved parts of our codebase to swift, our compile times have effectively quadrupled. Trying to combat this, we&rsquo;ve used the function and expression debug time flags to figure out if there&rsquo;s something to be saved by simplifying expressions. Via Nick Lockwood: TIL writing if foo == false {} Compiles dramatically more slowly [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2020-11-06T22:06:18Z","apple_news_api_id":"8f8fce55-102c-4ff5-9cb8-0142c76b70f7","apple_news_api_modified_at":"2020-11-25T21:16:23Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/Aj4_OVRAsT_WcuAFCx2tw9w","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[255,46,138,71,901],"class_list":["post-30589","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-compiler","tag-languagedesign","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/30589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=30589"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/30589\/revisions"}],"predecessor-version":[{"id":30808,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/30589\/revisions\/30808"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=30589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=30589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=30589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}