<?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 Puneet on Medium]]></title>
        <description><![CDATA[Stories by Puneet on Medium]]></description>
        <link>https://medium.com/@puneet.oyo1?source=rss-19ae5dd722b7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*93ZSVFw_9m_7Lm_v</url>
            <title>Stories by Puneet on Medium</title>
            <link>https://medium.com/@puneet.oyo1?source=rss-19ae5dd722b7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 23:45:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@puneet.oyo1/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[How We Reduced Our iOS App Build Time by 90%  [Part 2]]]></title>
            <link>https://tech.oyorooms.com/how-we-reduced-our-ios-app-build-time-by-90-part-2-fb50074853ae?source=rss-19ae5dd722b7------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb50074853ae</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[compilers]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Puneet]]></dc:creator>
            <pubDate>Mon, 18 Sep 2023 09:36:04 GMT</pubDate>
            <atom:updated>2023-09-20T14:51:01.997Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HLHIX4oJTqWm4hk6aVpu2g.png" /></figure><p>This is the second part of our two-part series on how we reduced our iOS build time. In the first part, we discussed how we identified the major time-consuming processes. If you haven’t gone through the first part yet, you can check it out <a href="https://tech.oyorooms.com/how-we-reduced-our-ios-app-build-time-by-90-part-1-7203cc6fe632">here</a>.</p><p>As discussed in the first part, the compilation time of Swift files posed the most significant issue. We focused on identifying the major files responsible for this slowdown, which led us to some surprising results.</p><p>We discovered that Xcode utilizes batch processing, where a batch consists of multiple files (usually 25 by default). To investigate further, we examined the batches individually to understand their compilation times.</p><p><strong>How we analyzed these batches:</strong></p><p>1. We selected one batch at a time and listed all the files present in that batch. These batches and their files can be viewed in the build logs in the report navigator in Xcode.</p><p>2. Next, we picked each listed file one by one and made minor changes to observe their incremental build times. This allowed us to measure the build time for each file individually rather than for the entire batch.</p><p>By analyzing the compilation time file by file (instead of batch by batch), we made a significant finding. It turned out that in each batch, only one or two files were responsible for the slowdown.</p><p>We then chose 3–4 files that were causing the most significant delays and delved into their details. The common factor among them was that they were view handlers that utilized some very large structs for data models. Identifying this similarity allowed us to target the root cause of the compilation slowdown.</p><p><strong>Understanding iOS app’s code structure on OYO</strong></p><p>At OYO, every view controller follows a very specific code structure. Each view controller is composed of multiple views, and each view is managed by a view handler. The view handler stores and handles the data required for that view. As the codebase follows the VIPER architecture, the presenter handles all the view handlers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aBiqZz_SpZIaoXSx-mqW7Q.png" /><figcaption>Code structure</figcaption></figure><p>Upon investigation, we discovered that the major cause of the slowdown was the view handlers themselves. Specifically, the handlers with large and nested structs for data were leading to longer build times. The complexity and size of the struct directly correlated with the build time increase.</p><p>To tackle this issue, we decided to experiment with the data models. Our assumption proved correct, as removing the usage of these large data models from the files significantly reduced build times — from several minutes to just a few seconds. Additionally, we observed that removing certain properties from these files also contributed to faster build times.</p><p>In our search for similar experiences, we stumbled upon an <a href="https://medium.com/commencis/stop-using-structs-e1be9a86376f">article</a> by Gokhan Topcu, where he explained how large structs with value types, including strings, could cause performance issues. Encouraged by this insight, we attempted changing the data model from a struct to a class. Below are the results we obtained:</p><p><strong>Build time of one of the culprit batch when data model was struct</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*ljfdY9jqDLMiKFUJYkkO3Q.png" /><figcaption>Build time with struct</figcaption></figure><p><strong>Build time of same batch when data model was class</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*HgvqSpXlQfcoQZKl456Oyw.png" /><figcaption>Build time with class</figcaption></figure><p>That’s a whopping 95% reduction! 🎉</p><p>The app’s codebase follows a very generic structure for all data models, with all of them wrapped in one generic data model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*itW3rpAdbMAPCRpPE1_SPg.png" /></figure><p>Root data was originally created as a struct, but we changed it to a class.</p><p>The results were remarkable. We managed to reduce the build time by 80%, significantly cutting it down from 40 minutes to approximately 7 minutes. This optimization has greatly improved the overall development process and productivity.</p><p><strong><em>Our initial hypothesis on why struct is causing this slowdown</em></strong></p><p>As described <a href="https://medium.com/commencis/stop-using-structs-e1be9a86376f">here</a>, large structs with reference types (including strings and arrays, which are actually value types but are backed by reference types) can cause significant ARC (Automatic Reference Counting) retain/release overhead.</p><p>This issue is also explained in <a href="https://developer.apple.com/videos/play/wwdc2016/416/">Apple’s WWDC session</a>, where they demonstrate how structs can increase ARC overhead when they contain reference types.</p><p>These screenshots from Apple’s WWDC session illustrate the ARC overhead for class, struct with a single reference type, and struct with multiple reference types.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fnGz9RDzSJcwaSvON3sxtw.png" /><figcaption>Class performance</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HmRgvGDmibzgipa83Cm-1Q.png" /><figcaption>Struct containing a reference performance</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QCFk_dvKy7IBFtbt7d_k3w.png" /><figcaption>Struct containing many references performance</figcaption></figure><p>As depicted in the screenshots, the reference counting overhead increases drastically when structs contain multiple reference types (including strings and arrays, which are actually value types but are backed by reference types). This aligns precisely with our case, as we have multiple nested structs, and the overall struct contains hundreds (even thousands) of properties in some instances.</p><p>The next hypothesis is related to the copying of these data models, as view handlers keep references to data models. Since structs are value types, storing these data types in the view handler will lead to deep copying of each property, causing additional slowdowns.</p><p>Furthermore, the compiler automatically adds code (methods like `assignWithCopy`, `initializeWithCopy`) to the struct to enable copying, which involves numerous calls to retain and release, thus increasing the ARC (Automatic Reference Counting) overhead even further.</p><p>This can be verified <a href="https://godbolt.org/">here</a> by creating a struct and a class with reference types and comparing the counts of calls to `swift_bridgeObjectRetain` and `swift_bridgeObjectRelease`.</p><p><strong><em>Actual cause identified</em></strong></p><p>Upon delving deeper, we discovered that a significant amount of time was being consumed during the copying of the instance of the struct. The line `self.data = data` was identified as the main culprit, particularly when dealing with very large and nested structs.</p><p>To address this problem, instead of changing the struct to a class, we implemented a <a href="https://github.com/apple/swift/blob/master/docs/OptimizationTips.rst#advice-use-copy-on-write-semantics-for-large-values">copy-on-write (COW)</a> mechanism for the data model inside the root data. The following changes were made:</p><p><strong>Initially `RootData` was like this</strong></p><pre>struct RootData&lt;T&gt; {<br>  let data: T?<br>}</pre><p><strong>We modified the `RootData` structure as follows:</strong></p><pre>final class Ref&lt;T&gt; {<br>  var val: T<br>  init(_ v: T) { val = v }<br>}</pre><pre>struct RootData&lt;T&gt; {<br>  var data: T {<br>    get { return dataRef?.val }<br>    set {<br>      if !isKnownUniquelyReferenced(&amp;dataRef) {<br>        dataRef = Ref(newValue)<br>        return<br>      }<br>      dataRef?.val = newValue<br>    }<br>  }<br>  var dataRef: Ref&lt;T?&gt;?<br>}</pre><p>By making these adjustments, we were able to observe an improvement in build time, further confirming that the data copying was indeed the underlying issue.</p><p><strong>Bonus point :</strong></p><p>While checking the build settings, we discovered that -Os (optimization for speed) was enabled for the debug build, which ideally should be -Onone (No optimization) for debug builds.</p><p>By making this small tweak, we were able to further reduce our app’s build time by 30–35%, from around 7 minutes to less than 5 minutes.</p><p><strong>What did we learn?</strong></p><ul><li>What works for others may not work for you. It’s hard to generalize a problem. So, it’s essential to pay attention to the problem and understand it in detail. Collect enough data to prove your hypothesis. Conduct brainstorming sessions and only proceed with execution when you are convinced that something works and it’s backed by data.</li><li>Working on build time-related stuff is quite different from building an iOS app. There is little to no documentation, and StackOverflow will not be of much help. Therefore, you have to try out things, use trial and error methods — a mostly hit and miss approach, but it’s worth it.</li><li>After this exercise, we now have a much better understanding of the Xcode build system than we did before. We learned about the compilation process and how the build system works under the hood. Now, we are confident enough to debug build-related problems and can reason about build times.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb50074853ae" width="1" height="1" alt=""><hr><p><a href="https://tech.oyorooms.com/how-we-reduced-our-ios-app-build-time-by-90-part-2-fb50074853ae">How We Reduced Our iOS App Build Time by 90% 🔥 [Part 2]</a> was originally published in <a href="https://tech.oyorooms.com/">OYOTech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How We Reduced Our iOS App Build Time by 90%  [Part 1]]]></title>
            <link>https://tech.oyorooms.com/how-we-reduced-our-ios-app-build-time-by-90-part-1-7203cc6fe632?source=rss-19ae5dd722b7------2</link>
            <guid isPermaLink="false">https://medium.com/p/7203cc6fe632</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[compilers]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Puneet]]></dc:creator>
            <pubDate>Mon, 18 Sep 2023 09:34:35 GMT</pubDate>
            <atom:updated>2023-09-20T14:50:12.149Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rCEbN5oviRsZdZsy0B9Anw.png" /></figure><p>OYO’s iOS consumer app is a large repository set up with multiple modules and dependencies. This substantial arrangement presents its own set of challenges, and build time is one of the topmost concern. To keep up with the fast-paced development environment, improving build time was the need of the hour for us.</p><p>To give you an idea about the problem, our clean build time was around 40 minutes 📈, which is quite high. This issue was affecting developer’s productivity and creating a suboptimal developer experience.</p><p>While improving the build time, we were able to achieve improvements in other metrics, including app size as well.</p><p><strong>Following table shows the amazing results that we’ve been able to achieve</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gUC491rBqWN2km_KkWJiJA.png" /><figcaption>Final results</figcaption></figure><p><strong>How did we approach the problem?</strong></p><p><strong>Step 1: Remove redundant and unused code from the codebase.</strong></p><p>The first step involved removing unused code from the codebase to obtain a more accurate measurement of the current build time. There was no need to include data from files that were no longer in use.</p><p><strong>How we identified unused code:</strong></p><ul><li>Certain app features were running on an A/B experiment basis, wherein a distinct set of users had access to specific code (referred to as x), while another set of users had access to different code (referred to as y). We checked the experiments that had already concluded, focusing on the variant that was live for 100% of the users, and removed the other variant.</li><li>Used an open source tool named <a href="https://github.com/peripheryapp/periphery">Periphery</a> to identify unused classes, structs, functions and variables</li></ul><p><strong>Motive:</strong> This step was crucial in obtaining precise data for the build time analysis, as we wanted to avoid working with data from files that were no longer in use.</p><p><strong>Result: </strong>By implementing these changes, we successfully reduced the build time by 3 minutes.</p><p><strong>Step 2: Checked build logs 📄 , compiler build settings to get more data for analysis</strong></p><p>The second step involved a comprehensive analysis of the codebase, examining each file, function, and expression in detail. To gather more data, we introduced the following compiler flags:</p><p>1. `-driver-time-compilation`: This flag helped us to display the time taken by each driver process.</p><p><strong>Build Timing Summary</strong></p><pre>SwiftCompile (280 tasks) | 20210.813 seconds<br>CompileC (1450 tasks) | 669.827 seconds<br>CompileXIB (763 tasks) | 112.840 seconds<br>SwiftEmitModule (34 tasks) | 94.854 seconds<br>GenerateDSYMFile (1 task) | 15.608 seconds<br>Ld (57 tasks) | 14.850 seconds<br>ValidateEmbeddedBinary (7 tasks) | 12.791 seconds<br>SwiftDriver (34 tasks) | 8.352 seconds<br>SwiftGeneratePch (7 tasks) | 7.446 seconds<br>CompileAssetCatalog (2 tasks) | 7.063 seconds<br>PhaseScriptExecution (13 tasks) | 5.840 seconds<br>CompileStoryboard (8 tasks) | 4.264 seconds<br>CodeSign (57 tasks) | 3.396 seconds<br>ProcessPCH (1 task) | 1.166 seconds<br>IntentDefinitionCodegen (3 tasks) | 0.805 seconds<br>IntentDefinitionCompile (3 tasks) | 0.805 seconds<br>WriteAuxiliaryFile (693 tasks) | 0.700 seconds<br>CpHeader (601 tasks) | 0.577 seconds<br>Copy (198 tasks) | 0.574 seconds<br>CopySwiftLibs (1 task) | 0.539 seconds<br>RegisterExecutionPolicyException (55 tasks) | 0.393 seconds<br>CopyPNGFile (5 tasks) | 0.257 seconds<br>SwiftDriver Compilation Requirements (34 tasks) | 0.224 seconds<br>CopyStringsFile (120 tasks) | 0.202 seconds<br>Touch (55 tasks) | 0.174 seconds<br>ProcessInfoPlistFile (55 tasks) | 0.165 seconds<br>CpResource (58 tasks) | 0.137 seconds<br>SwiftDriver Compilation (34 tasks) | 0.131 seconds<br>LinkStoryboards (6 tasks) | 0.097 seconds<br>Libtool (4 tasks) | 0.087 seconds<br>ProcessProductPackagingDER (16 tasks) | 0.075 seconds<br>ExtractAppIntentsMetadata (25 tasks) | 0.072 seconds<br>SwiftMergeGeneratedHeaders (28 tasks) | 0.059 seconds<br>CreateUniversalBinary (7 tasks) | 0.025 seconds<br>Preprocess (1 task) | 0.014 seconds<br>ProcessProductPackaging (16 tasks) | 0.014 seconds<br>CopyPlistFile (5 tasks) | 0.010 seconds</pre><p>2. `-Xfrontend -warn-long-function-bodies=100`: We added this flag along with other compiler flags to generate warnings when a function took more than 100ms to compile.</p><p>3. `-Xfrontend -warn-long-expression-type-checking=100`: Similarly, we used this flag along with others to generate warnings when the time to check an expression’s type exceeded 100ms.</p><p>Motive: Our goal was to identify functions, classes, and expressions that were consuming significant build time.</p><p><strong>Findings:</strong></p><p>The results revealed that 90% of the build time was spent on compiling Swift resources exclusively. Additionally, we observed that some expressions and files were taking an unusually long time (close to 15 minutes in certain cases 🤯).</p><p><strong>Solutions for time-consuming expressions:</strong></p><p>To address these issues, we implemented the following solutions:</p><p>1. We updated the code to simplify time-consuming expressions.</p><p>2. Explicitly adding data types helped reduce the time spent on implicit data type checks by the compiler.</p><p>3. We stored frequently used expressions in variables, reducing redundant computations during compilation.</p><p><strong>Example:</strong></p><p><strong>Before</strong></p><pre>let velocity = sender.velocity(in: (self.navigationController?.view ?? self.view)) <br>let duration = TimeInterval(abs((self.navigationController?.view ?? self.view).transform.ty / velocity.y))</pre><p><strong>After</strong></p><pre>let velocity: CGPoint = sender.velocity(in: (self.navigationController?.view ?? self.view)) <br>let yTransform: Double = (self.navigationController?.view ?? self.view).transform.ty <br>let yVelocity: Double = velocity.y <br>let duration: TimeInterval = TimeInterval(abs(yTransform / yVelocity))</pre><p>By simplifying these expressions and providing explicit data types, we managed to achieve a reduction in build time by 10 seconds. While this may not seem like a significant difference, every improvement counts.</p><p>With the modifications made, all expressions now compiled in under 100ms, addressing some concerns. However, the compilation time of Swift files remained problematic, indicating that further optimizations were necessary in that area.</p><p><strong>Step 3 : Code modularisation</strong></p><p>Since Swift compilation time remained a significant issue, we attempted to address it by separating certain code from the main codebase and creating a separate module for it.</p><p><strong>Findings:</strong></p><p>After removing a substantial number of files, we observed that the overall build time remained relatively unchanged. This led us to conclude that the issue was more file-specific. We then focused our debugging efforts on the file level (as explained in the next step).</p><p><strong>Results:</strong></p><p>Surprisingly, even after removing a major component, the code’s build time remained nearly the same. This suggested that modularization alone did not significantly improve the build time in our case. Nevertheless, it’s worth noting that modularization can have a substantial impact on large codebases. When there is substantial coupling between elements, decoupling them and creating independent modules can prevent unnecessary recompilation of unlinked modules during each build, resulting in significant improvements in incremental build times.</p><p>That concludes the first part. In the <a href="https://tech.oyorooms.com/how-we-reduced-our-ios-app-build-time-by-90-part-2-fb50074853ae">next segment</a>, we will discuss how we addressed the issue of Swift file compilation time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7203cc6fe632" width="1" height="1" alt=""><hr><p><a href="https://tech.oyorooms.com/how-we-reduced-our-ios-app-build-time-by-90-part-1-7203cc6fe632">How We Reduced Our iOS App Build Time by 90% 🔥 [Part 1]</a> was originally published in <a href="https://tech.oyorooms.com/">OYOTech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>