<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kapil Rathore on Medium]]></title>
        <description><![CDATA[Stories by Kapil Rathore on Medium]]></description>
        <link>https://medium.com/@krkapilrathore?source=rss-3f8312ffe903------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*AsPBzxZXdP038NdD.</url>
            <title>Stories by Kapil Rathore on Medium</title>
            <link>https://medium.com/@krkapilrathore?source=rss-3f8312ffe903------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 12:49:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@krkapilrathore/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Optimizing Swift Compile Times]]></title>
            <link>https://medium.com/@krkapilrathore/optimizing-swift-compile-times-73f13f390f28?source=rss-3f8312ffe903------2</link>
            <guid isPermaLink="false">https://medium.com/p/73f13f390f28</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[compile-times]]></category>
            <dc:creator><![CDATA[Kapil Rathore]]></dc:creator>
            <pubDate>Wed, 23 Nov 2022 04:57:54 GMT</pubDate>
            <atom:updated>2022-11-23T04:57:54.169Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>I have lost a lot of precious time waiting for my code to compile (specially if it’s a clean build).</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*J-1MC3QGbIuwq4tb-yr-iA.png" /></figure><p>I believe every single iOS developer would agree with me on this. If not, it’s only a matter of time before you also sail on this boat (or not, if you are vigilant enough right from the very early stages of the project).</p><p>Although a lot of factors contribute to the project&#39;s high build time, we are only going to focus on Swift Compilation Time for now.</p><p>We will work on <a href="https://github.com/kapilrathore/ios-build-time">this project</a> to figure out the issues and find ways to optimize them.</p><h3><strong>If you can’t measure it, you can’t manage it.</strong></h3><p>So now the main question before we can proceed is how do we measure the compile times of our Swift Code?</p><p>The Swift compiler has various built-in <a href="https://github.com/apple/swift/blob/master/docs/CompilerPerformance.md#diagnostic-options">diagnostic options</a> you can use to profile the build. We are going to leverage two of these flags given below for our purpose.</p><ol><li><strong>-Xfrontend -debug-time-function-bodies</strong> — time spent typechecking every function in the program.</li><li><strong>-Xfrontend -debug-time-expression-type-checking</strong> — time spent typechecking every expression in the program.</li></ol><p>To set these flags, navigate to Project Build Settings, search for ‘Other Swift Flags’, and add these two flags (as shown below).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BBeZSoMnHSv8l2_XjzvG6g.png" /></figure><p>Alternatively, we can also do this entirely from the command line, without modifying the Xcode project at all using the <a href="https://developer.apple.com/library/archive/technotes/tn2339/_index.html"><strong>xcodebuild</strong></a><strong> </strong>cli. This can come in handy if we want to automate these checks.</p><pre>xcodebuild \<br>-project CompileTimeTests.xcodeproj \<br>-scheme CompileTimeTests \<br>-destination &#39;platform=iOS Simulator,OS=16.0,name=iPhone 14 \<br>clean \<br>build \<br>OTHER_SWIFT_FLAGS=&quot;-Xfrontend -debug-time-function-bodies -Xfrontend -debug-time-expression-type-checking&quot;</pre><h3>Identification &amp; Improvements</h3><p>Let’s go ahead and measure the compile times on <a href="https://github.com/kapilrathore/ios-build-time">our project</a>.</p><h3>Culprit #1 🧐</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hxZNUIXJ3J9_b2B1ItdGyQ.png" /></figure><pre>21.47ms — <a href="https://github.com/kapilrathore/ios-build-time/blob/main/CompileTimeTests/Tests.swift#L30">instance method typeInference1()</a></pre><p><strong>Problem </strong>— We have initialized a dictionary without specifying the type so the compiler goes through each of the key-value pairs in the dictionary and tries to infer the type at the end.</p><p><strong>Resolution</strong> — Create a new variable and specify the type of dictionary ([String: Any] in this case). The compiler saves some time in typechecking each key-value pair.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3qIFO-0EcCtKyKifAFtnbg.png" /></figure><pre>2.85ms — <a href="https://github.com/kapilrathore/ios-build-time/blob/main/CompileTimeTests/Tests.swift#L53">instance method typeInference2()</a></pre><p><strong>Improvement </strong>— 21.47ms to 2.85ms, we get <strong>~75 times faster</strong> compile time. (It varies exponentially with the number of key-value pairs in the dictionary)</p><h3>Culprit #2 😳</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*58UOs0vld9wGGrNJTujO9Q.png" /></figure><pre>530.45ms — <a href="https://github.com/kapilrathore/ios-build-time/blob/main/CompileTimeTests/TestingEuqtable.swift#L54">operator function ==(_:_:)</a></pre><p><strong>Problem </strong>— To make any type <strong>Equatable </strong>we have to check if all the properties of that type are equal or not. Comparing them using an &amp;&amp; operator makes the expression as long as the number of properties. So, the compile time grows exponentially with the number of properties.</p><p><strong>Resolution</strong> — Instead of clubbing the checks for all the properties into one big expression we can just check one property at a time using a guard statement and thus breaking it down into smaller expressions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZoHb7q5s7NZRiGiId1yM4g.png" /></figure><pre>98.32ms — <a href="https://github.com/kapilrathore/ios-build-time/blob/main/CompileTimeTests/TestingEuqtable.swift#L144">operator function ==(_:_:)</a></pre><p><strong>Improvement </strong>— 530.45 to 98.32,We get <strong>~5 times faster</strong> compile time. (It varies exponentially with the number of properties)</p><h3><strong>Culprit #3 🤯</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HhrwZgF1p6T6IJZO_NJ74Q.png" /></figure><pre>4476.01ms — getter <a href="https://github.com/kapilrathore/ios-build-time/blob/main/CompileTimeTests/Tests.swift#L83">testComputedProperty1</a></pre><p><strong>Problem</strong> —Multiple higher-order functions are chained one after the other making it a <strong>complex expression without specified types</strong>, thus, taking higher time to compile.</p><p><strong>Resolution </strong>— Breaking the higher order into simpler expressions and specifying the types explicitly for each one of them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UINoR8jFgUt2unXZ3EISNg.png" /></figure><pre>10.22ms — getter <a href="https://github.com/kapilrathore/ios-build-time/blob/main/CompileTimeTests/Tests.swift#L92">testComputedProperty2</a></pre><p><strong>Improvement</strong> — 4476.01ms to 10.22ms , We get <strong>~440 times faster</strong> compile time, which is a very significant improvement. 🤯</p><h3>Conclusions</h3><p>With just three improvements we were able to reduce the overall build time of the app to just half from ~10 sec to ~5 sec.</p><p>Now the question arises, is it even worth it to make these modifications to your code? Well, I leave that decision up to you, but this is surely worth auditing.</p><p>No one wants to touch the pieces of code that are already working for them, but if we have a proper test suit in place, we can easily make these changes since we see in some cases we got significant improvements with just simple modifications.</p><p>Summing up the <strong>key improvements to consider</strong> below —</p><ul><li>Specify types for complex expressions since type inference comes at a cost</li><li>Write smaller expressions since typechecking takes a heavy toll for larger and more complex expressions</li><li>Mark your properties and functions as private or <a href="https://developer.apple.com/swift/blog/?id=27">final</a> (classes) wherever possible (not used here)</li></ul><p>To catch these issues earlier during the development phase, make sure to set this flag in the Project Build Settings under Other Swift Flags</p><ul><li><strong>-Xfrontend -warn-long-expression-type-checking=200 </strong>— Raise a warning in the Xcode if any expression takes more than 200ms to compile.</li></ul><p>Thanks for reading through the end. Would appreciate your feedback. Let’s connect on <a href="https://twitter.com/krkapilrathore">Twitter</a> to discuss more. <br>Also, I am planning to cover other improvements around Build Time in future blog posts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73f13f390f28" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[WE]]></title>
            <link>https://medium.com/series/we-32ba4087562?source=rss-3f8312ffe903------2</link>
            <guid isPermaLink="false">https://medium.com/p/32ba4087562</guid>
            <dc:creator><![CDATA[Kapil Rathore]]></dc:creator>
            <pubDate>Fri, 04 May 2018 06:33:34 GMT</pubDate>
            <atom:updated>2018-05-04T06:35:19.409Z</atom:updated>
            <content:encoded><![CDATA[<h4>Write Everything Down</h4><p>Collect fun or creative ideas you have.</p><p>Even if a lot of the things you are writing down at the time don’t seem like good ideas, they may be a good inspiration or relevant later at a different time or context.</p><p>Here <strong>quantity</strong> can lead to <strong>quality</strong>.</p><h4>Leverage worry and fear</h4><p>Worry is interest paid on trouble before it’s due.</p><p>I’m a believer that harnessing fear by developing a strong backbone — not wishbone — is a key to success every day.</p><p>Stay mentally and spiritually strong in the face of adversity in difficult and painful circumstances by replacing worry and fear with determination.</p><h4><strong>Increase your energy levels</strong></h4><p>The primary factor of whether you can make it through is simply your level of energy.</p><p>Pursuing a big mission and navigating business challenges is largely about how you manage your energy</p><h4>Put first things first</h4><p>When you put first things first, you empower yourself to enjoy every day of this beautiful journey and achieve heights that can’t be reached by neglecting what is important in life.</p><p>Spend less time at work and more time with your family. Spend less time worried about getting one more hour in the office and focus on your fitness and your health.</p><p><strong>This isn’t a sprint, it is a marathon.</strong></p><h4>Take inventory of routine tasks that can be outsourced to others</h4><p>I’m constantly asking myself if what I’m doing is the highest and best use of my time and talent.</p><p>If not, look to delegate that task to a trusted member of your team. This allows me to use my time more effectively, but also saves my mental energy for the big problems we’re trying to solve.</p><h4><strong>Practice mindfulness</strong></h4><p>Mindfulness is the single most important habit to cultivate if you want to be truly successful</p><p>And the good news is: it’s fundamentally very simple… You just have to stay aware, stay present, and observe the various situations that come into your life. When you give people and circumstances your full attention, you can really hear, interpret, understand, and learn from them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32ba4087562" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>