<?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 Luis Gomez on Medium]]></title>
        <description><![CDATA[Stories by Luis Gomez on Medium]]></description>
        <link>https://medium.com/@luis15gomez?source=rss-2b4a50cf8f66------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*sDGzwRvZ6iYaVW4hrVVk1A.png</url>
            <title>Stories by Luis Gomez on Medium</title>
            <link>https://medium.com/@luis15gomez?source=rss-2b4a50cf8f66------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 13:56:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@luis15gomez/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[iOS app size reduction with Machine Outlining]]></title>
            <link>https://engineering.rappi.com/ios-app-size-reduction-with-machine-outlining-5ef2c6b53237?source=rss-2b4a50cf8f66------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ef2c6b53237</guid>
            <category><![CDATA[llvm]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[app-size]]></category>
            <dc:creator><![CDATA[Luis Gomez]]></dc:creator>
            <pubDate>Wed, 11 Aug 2021 20:49:54 GMT</pubDate>
            <atom:updated>2021-08-11T20:50:58.893Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7XAuao3t6oyvXgkSP1gWsg.png" /></figure><p>Many of us never consider the size of an iOS app until we have a massive one that sizes tens of MB or worse, hundreds of MB. That is exactly what happened to us at Rappi.</p><p>There are several ways to tackle this situation:</p><p>First, you must begin to measure the size of your application and how it grows to identify which modules and/or assets are the worst offenders. My colleague Moy has a fabulous post about it, if you haven’t read it, check it out <a href="https://engineering.rappi.com/how-rappi-deals-with-ios-app-size-36dffec805d7">here</a>.</p><p>Now that you know where the big problems are, you can start by applying the most common techniques to reduce the size of your app, such as removing duplicated code and assets, enabling whole-module optimization, and size optimization for swift and clang compilers.</p><p>Checkout this<a href="https://github.com/lu15gv/iOS-app-size-reduction-cheat-sheet"> iOS App Size Reduction Cheatsheet</a> to see how to do it.</p><p>But what happens when that is not enough? For example, when your app is so large that even after applying those techniques you still have a binary of hundreds of MB, such as Rappi.</p><p>Well, we need to find big solutions for big problems, and fortunately we discovered a huge <a href="https://eng.uber.com/how-uber-deals-with-large-ios-app-size/#:~:text=We%20%20established%20a%20%20correlation%20between,bookings%2C%20resulting%20in%20revenue%20loss.">article</a> / <a href="https://1fykyq3mdn5r21tpna3wkdyi-wpengine.netdna-ssl.com/wp-content/uploads/2021/02/cgo21main-p112-p-dbdebd6-49049-preprint.pdf">paper</a> from Uber where they faced the exact same problem, please check it out, it will help you understand this post.</p><p>Well, as mentioned in the above paper, the key of this implementation is the machine outlining throughout the entire app.</p><p>Wait… what?</p><h3>Machine Outlining</h3><p>All of us have done outlining in the past. Let’s look at the following code snippets that represent a person’s itinerary:</p><p>Baseline:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*kR7M7Nj20y7GVTa32HRuVQ.png" /></figure><pre>🛠 Lines of code:</pre><pre>🔹 func friday = 13</pre><pre>🔹 func saturday = 12</pre><pre>🔺 Total = 38</pre><p>Did you notice something? I’m sure you did. It seems that this person has some habits that are repeated throughout the days, so there are lines of code that can be extracted to reuse them. We can extract the following methods to avoid repeating code multiple times:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*VVwJuS0ALN94UZDnmFnd1A.png" /></figure><pre>🛠 Lines of code (without extension declaration):</pre><pre>🔹 func morning: 6</pre><pre>🔹 func afternoon: 5</pre><pre>🔹 func night: 5</pre><p>By doing this, we can rewrite our main methods like this:</p><p>Outlined 1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*kcESPGNgkDbDRF8sDIIZyA.png" /></figure><pre>🛠 Lines of code:</pre><pre>🔹 func monday = 6</pre><pre>🔹 func friday = 6</pre><pre>🔹 func saturday = 6</pre><pre>🔹 func morning = 6</pre><pre>🔹 func afternoon = 5</pre><pre>🔹 func night = 5</pre><pre>🔺 Total = 34</pre><p>Congratulations! That’s Outlining, you already knew it, you just learned how it’s called.</p><p>Applying the same concept, now you can infer that Machine Outlining is pretty much the same, but applied at low level code (assembly code).</p><p>Here’s an example of Machine Outlining explained with low level code:</p><p><a href="https://llvm.org/devmtg/2016-11/Slides/Paquette-Outliner.pdf">https://llvm.org/devmtg/2016-11/Slides/Paquette-Outliner.pdf</a></p><p>But that’s not all, we are not only going to do Outlining once, we will do it several times.</p><p>As you can see, after this first round, there is still a chance to do it again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*6NmDJ-AvD0ibJ5J_UfSSeQ.png" /></figure><p>So let’s do it</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/202/1*Wg4UIMkAYOpzJqd5kVV1Qg.png" /></figure><pre>🛠 Lines of code (without extension declaration):</pre><pre>🔹 func weekend = 4</pre><p>Outlined 2:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*QzqWxGb-edPJr-CDzQ63_g.png" /></figure><pre>🛠 Lines of code:</pre><pre>🔹 func monday = 6</pre><pre>🔹 func friday = 5</pre><pre>🔹 func saturday = 5</pre><pre>🔹 func morning = 6</pre><pre>🔹 func afternoon = 5</pre><pre>🔹 func night = 5</pre><pre>🔹 func weekend = 4</pre><pre>🔺 Total = 36</pre><p>There are no more combinations to extract, but there will be cases where we can do it many times.</p><p>And now you wonder why did we do all of this? What’s the purpose?</p><p>Well let’s count how many lines of code we had before and how many we have now (I will omit the extension declarations because they are not strictly necessary)</p><p><strong>Baseline: 38 lines</strong></p><p><strong>Outlined 1: 34 lines</strong></p><p><strong>Outlined 2: 36 lines</strong></p><p>In the case of “Outlined 1”, we have reduced only 4 lines of code compared to Baseline, but what if our source code (taking third party libraries into account) has millions of lines of code? We can probably find more patterns and reduce many more lines of code.</p><p>Here are the machine outliner stats of Rappi, it found more than 4 millions patterns and those were extracted in about 200 thousand functions.</p><p><strong>200,995 machine-outliner — Number of functions created</strong></p><p><strong>4,034,483 machine-outliner — Number of candidates outlined</strong></p><p>That’s a massive reduction, how much? Approximately 8.5% of the size of the app without changing a single line of code.</p><p>Did you notice that in the ‘Outlined 2’ the number of lines of code increased compared to the first round?</p><p>There are a number of rounds in which you can get the maximum reduction of size. For the example above it is only 1, for Rappi it is 5 times.</p><p>Doing it more times at best will only decrease the size a bit, but it’s probably not worth it considering the amount of time it takes, at worst it will increase the binary size like in this example.</p><h3>How do we do this?</h3><p>In the following diagram, you can see a normal Xcode archive that uses whole-module optimization, bitcode enabled, and static libraries:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*tPVl9snTzNgyBfxEwi8hLg.png" /></figure><p>But first, a glossary:</p><p>.bc are bitcode files (don’t confuse them with Bitcode with uppercase B). bitcode files are an intermediate representation (IR) used by LLVM. At first glance, bitcode files cannot be read, but can be transformed into human-readable LLVM assembly code (.ll) with the llvm-dis tool.</p><p>.o are object files (Mach-O), basically machine code for a specific architecture, so it is no longer possible to read it or transform it into assembly code.</p><p>.a are static libraries. It’s just a package for .o files</p><p>Now let’s look at a pipeline that makes machine outlining possible:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/1*5Grj0iauO0oK29bbnGbpLA.png" /></figure><p>Unlike Xcode, we are not creating static libraries, we are not even creating Mach-O files (just one at the end). We are linking all the IR files (.bc and .lto) into a single file (.ll) to optimize it and run the machine outlining throughout the app.</p><p>I highlighted the differences to the default Xcode Archive in green. Let’s review them:</p><h3>llvm-link</h3><p>Do you remember that I mentioned this before?</p><p><em>“The key of this implementation is the machine outlining throughout the entire app</em>.”</p><p>Well, you know what Machine Outlining is, but what about “<em> throughout the entire app</em>”? That’s where llvm-link takes place.</p><p>llvm-link is a tool from the LLVM project:</p><blockquote><strong><em>llvm-link</em></strong><em> takes several LLVM bitcode files and links them together into a single LLVM bitcode file</em></blockquote><p><a href="https://llvm.org/docs/CommandGuide/llvm-link.html">https://llvm.org/docs/CommandGuide/llvm-link.html</a></p><p>Eureka! We can use the llvm-link tool to link the entire app into a single file. As the diagram shows, we will first link the application by modules (RappiHome.ll, Alamofire.ll, etc.) and then we will link all the modules to obtain WholeApp.ll</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*terQi8NHRIt9byIbmL9smQ.png" /></figure><h3>-flto, -emit-bc</h3><p>According to the llvm-link definition, we need to have bitcode files (remember, with lowercase b). Originally in Xcode we produce .o files after compilation phase, in order to produce IR files (.bc, .lto), we need to pass -flto flag to clang compiler (to produce .lto), and -emit-bc to swift compiler (to produce .bc).</p><p>To add -flto flag to clang compiler, we need to enable LTO in Build Settings:</p><ul><li>Select your target &gt; Build Setting &gt; Apple Clang — Code generator &gt; Link-Time Optimization &gt; Release &gt; Monolithic</li></ul><p>To add -emit-bc to swift compiler, we need to pass it the flag:</p><ul><li>Select your target &gt; Build Setting &gt; Swift Compiler — Custom Flags &gt; Release &gt; -Xfrontend -emit-bc</li></ul><p>As you will see later, we only added the LTO flag, because the -emit-bc flag was added in a custom step outside of Xcode</p><h3>OPT</h3><p>OPT is a tool from the LLVM project:</p><blockquote><em>The </em><strong><em>opt</em></strong><em> command is the modular LLVM optimizer. It takes LLVM source files as input, runs the specified optimizations on it, and then outputs the optimized file.</em></blockquote><p><a href="https://llvm.org/docs/CommandGuide/opt.html">https://llvm.org/docs/CommandGuide/opt.html</a></p><p>It takes the IR (.ll) file generated by llvm-link to optimize it by cutting some KB and it will also execute the ARC contract, if we don’t do this, the pipeline will fail with:</p><ul><li>LLVM ERROR: Cannot select: intrinsic %llvm.objc.clang.arc.use</li></ul><p>I learned it the hard way</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*At8yuc43dI1P7zKLFPN5eA.png" /></figure><h3>llc</h3><p>The core of all this, this is the one in charge of doing the Machine Outlining</p><p>llc is a tool from the LLVM project:</p><blockquote><em>The </em><strong><em>llc</em></strong><em> command compiles LLVM source inputs into assembly language for a specified architecture. The assembly language output can then be passed through a native assembler and linker to generate a native executable.</em></blockquote><p><a href="https://llvm.org/docs/CommandGuide/opt.html">https://llvm.org/docs/CommandGuide/opt.html</a></p><p>It takes the IR file issued by the OPT, runs the Machine Outlining several times and finally produces a Mach-O (.o) file for a specific architecture (arm64 in this case).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p_3na4jY1q3FmK3w1HwKOQ.png" /></figure><h3>Pipeline</h3><h3>What you need to know</h3><p>The actual pipeline we build is slightly different than the one I showed before. Let’s look at it</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*aDhk3U7qgF4lang3_WqPmQ.png" /></figure><p>It’s different because we start with a default Xcode Archive and then use a lot of files from there, that’s why some extensions are .o but they are marked as .bc or .lto. This is because we reuse a file called &lt;TargetName&gt;.LinkFileList that contains all the object files to link (with extension .o). And it was easier to just overwrite those .o files than to modify each LinkFileList existing in the project</p><p>I made the pipeline in a shell script (pipeline.sh).</p><p><a href="https://github.com/lu15gv/sol/blob/main/Executables/pipeline.sh">https://github.com/lu15gv/sol/blob/main/Executables/pipeline.sh</a></p><p>Also I made a Xcode swift console project (SizeOptimizerLinker or sol)</p><p><a href="https://github.com/lu15gv/sol/tree/main/Sources/SizeOptimizerLinker">https://github.com/lu15gv/sol/tree/main/Sources/SizeOptimizerLinker</a></p><p>It parses xcodebuild.log for important data (derived data paths, clang linker arguments, certificate id), wraps the llvm-link tool (due to the limit of arguments that llvm-link can take), and also performs the final clang linking excluding the original static library parameters (since they were linked with llvm-link).</p><p>This is the GitHub project where you can get the full implementation and a sample project (of course we can’t share the actual Rappi project)</p><p><a href="https://github.com/lu15gv/sol">https://github.com/lu15gv/sol</a></p><p>You will also need the LLVM tools (llvm-link, opt, llc). Depending on your current version of Xcode, there is a specific version of the tools that you should use. You can get them <a href="https://github.com/lu15gv/sol/tree/main/LLVM_tools">here</a></p><h3>Xcode configuration</h3><p>We need to make sure to use LTO, optimization flags, and the custom clang compiler that contains LLVM PRs changes.</p><p>Go to Build Settings, find the following flags and replace their values ​​by following the table below (for release settings only!)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*sthmYZwS15xH1YmyeVyzXg.png" /></figure><p>Also, add two User-Defined Settings</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aHmRuTJAXdHOlVVs-tKetQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*Az72uuCQ3svd726YjkVjSA.png" /></figure><h3>Run the pipeline</h3><p>Clone the sol project:</p><p><a href="https://github.com/lu15gv/sol/tree/main/LLVM_tools">https://github.com/lu15gv/sol/tree/main/LLVM_tools</a></p><p>Open the runme.sh that it’s in the Example directory with a text editor.</p><p>Replace the section “Configure this”.</p><p>This is an example of the setup to optimize the Example project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3mBqk3KN854cmMdJeRh2yw.png" /></figure><p>There is an optional section where you can configure the following options:</p><p>TARGETS_WHITE_LIST: this allows you to exclude some Targets to be linked. This is useful when you use CocoaPods and want to exclude the target Pods-&lt;YourTargetName&gt;</p><p>JSON_SYMBOLS: this allows you to replace symbols. It’s useful when you have duplicated symbols and Xcode shows them as a warning. There’s a json example here</p><p><a href="https://github.com/lu15gv/sol/blob/main/Example/symbols_example.json">https://github.com/lu15gv/sol/blob/main/Example/symbols_example.json</a></p><p>Now, you just need to open a terminal window and run</p><p>sh &lt;path to sol project&gt;/Example/runme.sh</p><p>It will print information for each step and generate files in a few places:</p><p>1.- In &lt;path to sol project&gt;/Example/outputsyou will find the logs, some support files, a symbolic link pointing to the optimized archive, and debug files for Bitcode and DSYM (we’ll talk about these two later)</p><p>2.- In ~/Library/Developer/Xcode/DerivedData/&lt;your project&gt;/Build/Intermediates.noindex/ArchiveIntermediates/&lt;your project&gt;/IntermediateBuildFilesPath/optimized/arm64 you will find intermediate optimized files such as:</p><ul><li>WholeApp.LinkFileList</li><li>WholeApp.ll</li><li>WholeApp.opt.ll</li><li>WholeApp.o</li></ul><p>3.- The optimized archive can be opened in Xcode Organizer and distributed like any other normal archive (or not 👀, see problems)</p><h3>Results</h3><p>The following table shows the reductions we had in Rappi when applying this method. These results were taken directly from Testflight in AppStore Connect</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*vYXBipGyC8QcHA1h35xmLw.png" /></figure><p>As you can see, the reduction differs between models, but on average we could cut 7% of install size and 8.5 % in download size.</p><p>For the example project, I exported the archive (Ad-Hoc) and I got the next results (App Thinning Size Report.txt)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/1*6JMfx-AcfRyEo3IrADsqyw.png" /></figure><p>In this case, we got good results, yay!</p><p>Anyway, the example is just a guide, you should refer to the FAQ section because this method does not always work well with small projects.</p><h3>DSYMs:</h3><p>This used to be a problem, but we were able to solve it 🎉.</p><p><a href="https://bugs.swift.org/browse/SR-11539?focusedCommentId=62301&amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-62301">Here</a> you can see what the problem was.</p><h4>How to make them work?</h4><p>We need to disable “Generate Debug Symbols” in target build settings. This removes the -g flag during compilation time, and will allow us to generate the DSYMs later.</p><h4>Disclaimer:</h4><p>It’s possible to find crashes happening in methods called “OUTLINED_2342” (or any other number). This is a product of the machine outliner, but don’t worry, if you look at the stack trace, you will find the original method.</p><h3>Problems</h3><p>There is one error that we have not been able to solve:</p><h3>Bitcode:</h3><p>The Bitcode got corrupted after all this process, there is an XML embedded next to the bitcode section in the binary.</p><p><a href="https://jonasdevlieghere.com/embedded-bitcode/">https://jonasdevlieghere.com/embedded-bitcode/</a></p><p>That XML is the one that got corrupted and it is impossible to rebuild the app from the Bitcode. There is an open discussion on LLVM Discourse that explains the issue in detail:</p><p><a href="https://llvm.discourse.group/t/bitcode-xml-xar-corrupted-after-llvm-link/3295">https://llvm.discourse.group/t/bitcode-xml-xar-corrupted-after-llvm-link/3295</a></p><p>We ended up deactivating Rebuild from Bitcode when uploading it to the App Store</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/991/1*kVbVzijZLtya3Z6emKHkaQ.png" /></figure><h3>FAQ</h3><ul><li>Does it work in any project?</li></ul><p>It doesn’t, we only recommend it for large projects. On small projects with few targets, you may not find any size reduction when doing the Machine Outlining, you can even increase its size.</p><ul><li>Does it currently support dynamic frameworks?</li></ul><p>Short answer: not yet.</p><p>Long answer: look at the last diagram of the pipeline, the original static libraries are not present because we only need the original bitcode files (.bc) that are generated before the static libraries (.a). So in theory this can also support dynamic frameworks because the .bc files will exist too, but it will require some tweaking to the SizeOptimizerLinker project which I haven’t done.</p><ul><li>Does it support Bitcode?</li></ul><p>It doesn’t for now. You can check Problems section for more details</p><ul><li>What happend with simbolication and DSYMs?</li></ul><p>It works, see DSYMs section for more details.</p><ul><li>Is it in production?</li></ul><p>The app has passed the first quality control and is fully functional. We are about to release it soon.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ef2c6b53237" width="1" height="1" alt=""><hr><p><a href="https://engineering.rappi.com/ios-app-size-reduction-with-machine-outlining-5ef2c6b53237">iOS app size reduction with Machine Outlining</a> was originally published in <a href="https://engineering.rappi.com/">Rappi Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>