<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Mostafa</title>
    <subtitle>Thoughts, learnings, and experiences from a computer engineer chasing the fundamentals.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://imostfa.github.io/Blog/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-07-12T00:00:00+00:00</updated>
    <id>https://imostfa.github.io/Blog/atom.xml</id>
    <entry xml:lang="en">
        <title>Profile-Guided Optimization, From First Principles</title>
        <published>2026-07-12T00:00:00+00:00</published>
        <updated>2026-07-12T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/ai-learnings/profile-guided-optimization/"/>
        <id>https://imostfa.github.io/Blog/ai-learnings/profile-guided-optimization/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/ai-learnings/profile-guided-optimization/">&lt;p&gt;This write-up was distilled from an LLVM talk about making Profile-Guided Optimization (PGO) useful
for mobile apps. The talk starts from a very practical problem: mobile startup is sensitive to page
faults, but traditional PGO often improves speed by increasing code size through inlining. That trade-off
is awkward on mobile, where smaller code can launch faster.&lt;&#x2F;p&gt;
&lt;p&gt;The interesting idea in the talk is temporal profiling: record when functions first execute, merge many
startup traces, then let the linker reorder functions so startup code sits together in the final binary.
The reported results were the kind worth remembering: up to 40% fewer startup page faults, almost 1-3%
compressed-size reduction, and in one Firefox iOS experiment, over 6% compressed-size reduction for the
client binary from function reordering alone.&lt;&#x2F;p&gt;
&lt;p&gt;Reddit’s iOS optimization write-up is the production-scale case study that made the idea click for me.
Using PGO, temporal profiling, and function reordering, Reddit reduced the main iOS binary from 198.6 MiB
to 157.1 MiB and the compressed IPA from 280.6 MiB to 239.6 MiB. The numbers are impressive, but the
core idea is more important: instead of asking the compiler and linker to guess what matters, you run the
app, record what actually happened, and feed that evidence back into the next build.&lt;&#x2F;p&gt;
&lt;p&gt;This document builds that idea from scratch. Prerequisites: you know an app eventually becomes a binary
and that a compiler turns source code into machine code. Nothing more.&lt;&#x2F;p&gt;
&lt;style&gt;
.xpgo { --pos:#1a7f37; --neg:#b3261e; --warn:#9a6700; --hot:#b3261e; --warm:#bf8700; --cold:#0969da; --ink:#57606a; }
[data-theme=&quot;dark&quot;] .xpgo { --pos:#7ee787; --neg:#ff7b72; --warn:#e3b341; --hot:#ff7b72; --warm:#e3b341; --cold:#79c0ff; --ink:#8b949e; }
@media (prefers-color-scheme: dark) { :root:not([data-theme=&quot;light&quot;]) .xpgo { --pos:#7ee787; --neg:#ff7b72; --warn:#e3b341; --hot:#ff7b72; --warm:#e3b341; --cold:#79c0ff; --ink:#8b949e; } }
.xpgo .toc-grid { display:grid; grid-template-columns:1fr 1fr; gap:.25rem 2rem; list-style:none; padding:0; margin:.5rem 0 0; }
.xpgo .toc-grid a { display:block; padding:.15rem 0; }
.xpgo .cards { display:grid; grid-template-columns:repeat(auto-fit,minmax(260px,1fr)); gap:.75rem; margin:1rem 0; }
.xpgo .card { background:var(--bg-light); border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1rem; }
.xpgo .card h4 { margin:.1rem 0 .5rem; font-size:1rem; }
.xpgo .card p, .xpgo .card li { font-size:.9rem; margin:.5rem 0; }
.xpgo .card ul { margin:.25rem 0; padding-left:1.2rem; }
.xpgo pre { font-size:.8rem; line-height:1.5; overflow-x:auto; }
.xpgo .dim { color:var(--text-light); }
.xpgo .ok { color:var(--pos); font-weight:700; }
.xpgo .err { color:var(--neg); font-weight:700; }
.xpgo .hot { color:var(--hot); font-weight:700; }
.xpgo .cold { color:var(--cold); font-weight:700; }
.xpgo .warm { color:var(--warm); font-weight:700; }
.xpgo .diagram { border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1.25rem; background:var(--bg-light); margin:1rem 0; }
.xpgo .dgrid { display:flex; flex-wrap:wrap; gap:.6rem; align-items:stretch; }
.xpgo .dbox { border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:.75rem .85rem; background:var(--bg); font-size:.82rem; line-height:1.5; flex:1; min-width:190px; }
.xpgo .dbox .dtitle { font-weight:700; font-size:.86rem; margin-bottom:.35rem; }
.xpgo .arrow { display:flex; align-items:center; justify-content:center; color:var(--text-light); font-weight:700; min-width:1.5rem; }
.xpgo .dcaption { font-size:.85rem; color:var(--text-light); margin-top:.8rem; line-height:1.6; }
.xpgo .callout { border:1px solid var(--accent); border-left:4px solid var(--accent); border-radius:var(--standard-border-radius); padding:1.1rem 1.25rem; margin:1rem 0; background:var(--bg-light); }
.xpgo .callout .rule { font-weight:700; font-size:1.03rem; line-height:1.55; margin:.2rem 0; }
.xpgo .callout .corollary { color:var(--text-light); font-size:.9rem; margin:.65rem 0 0; }
.xpgo table td, .xpgo table th { font-size:.88rem; vertical-align:top; }
.xpgo .pill { display:inline-block; border-radius:999px; padding:0 .55rem; font-size:.68rem; font-weight:700; border:1px solid currentColor; }
.xpgo .pill.hot { color:var(--hot); }
.xpgo .pill.cold { color:var(--cold); }
.xpgo .pill.warm { color:var(--warm); }
.xpgo .layout { display:grid; grid-template-columns:1fr; gap:.7rem; margin:1rem 0; }
.xpgo .row { display:grid; grid-template-columns:7rem 1fr; gap:.7rem; align-items:center; }
.xpgo .rlabel { font-size:.75rem; font-weight:700; color:var(--text-light); text-transform:uppercase; letter-spacing:.04em; }
.xpgo .pages { display:flex; flex-wrap:wrap; gap:.28rem; }
.xpgo .page { width:2rem; height:1.4rem; border:1px solid var(--border); border-radius:.25rem; background:var(--bg); }
.xpgo .page.hot { background:color-mix(in srgb,var(--hot) 34%,var(--bg)); border-color:var(--hot); }
.xpgo .page.warm { background:color-mix(in srgb,var(--warm) 30%,var(--bg)); border-color:var(--warm); }
.xpgo .page.cold { background:color-mix(in srgb,var(--cold) 24%,var(--bg)); border-color:var(--cold); }
.xpgo .metric-row { display:grid; grid-template-columns:repeat(auto-fit,minmax(180px,1fr)); gap:.75rem; margin:1rem 0; }
.xpgo .metric { border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1rem; background:var(--bg-light); border-top:3px solid var(--accent); }
.xpgo .metric .label { font-size:.72rem; font-weight:700; letter-spacing:.06em; text-transform:uppercase; color:var(--text-light); }
.xpgo .metric .value { font-size:1.6rem; font-weight:800; line-height:1.15; margin:.25rem 0; color:var(--accent); }
.xpgo .metric .sub { font-size:.82rem; color:var(--text-light); }
.xpgo .iviz { border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1rem; background:var(--bg-light); margin:1rem 0; }
.xpgo .iviz h4 { margin:.1rem 0 .65rem; font-size:1rem; }
.xpgo .seg { display:inline-flex; flex-wrap:wrap; gap:.25rem; border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:.25rem; background:var(--bg); margin:.15rem 0 .8rem; }
.xpgo .seg button { border:0; border-radius:.25rem; padding:.4rem .75rem; background:transparent; color:var(--text-light); cursor:pointer; font:inherit; font-size:.86rem; }
.xpgo .seg button.active { background:var(--accent); color:var(--accent-text); }
.xpgo .panel { display:none; }
.xpgo .panel.active { display:block; }
.xpgo .mini-flow { display:flex; flex-wrap:wrap; gap:.45rem; align-items:stretch; margin:.5rem 0; }
.xpgo .node { border:1px solid var(--border); border-radius:var(--standard-border-radius); background:var(--bg); padding:.65rem .75rem; min-width:8.5rem; flex:1; font-size:.84rem; line-height:1.45; }
.xpgo .node strong { display:block; margin-bottom:.2rem; }
.xpgo .node.hot { border-color:var(--hot); background:color-mix(in srgb,var(--hot) 12%,var(--bg)); }
.xpgo .node.cold { border-color:var(--cold); background:color-mix(in srgb,var(--cold) 12%,var(--bg)); }
.xpgo .node.warm { border-color:var(--warm); background:color-mix(in srgb,var(--warm) 12%,var(--bg)); }
.xpgo .bars { display:grid; gap:.45rem; margin:.6rem 0; }
.xpgo .barrow { display:grid; grid-template-columns:minmax(6rem,9rem) 1fr 3.5rem; gap:.6rem; align-items:center; font-size:.84rem; }
.xpgo .track { height:.85rem; border-radius:999px; background:var(--bg); border:1px solid var(--border); overflow:hidden; }
.xpgo .fill { display:block; height:100%; width:var(--w,50%); background:var(--accent); transition:width .15s ease; }
.xpgo .fill.hot { background:var(--hot); }
.xpgo .fill.cold { background:var(--cold); }
.xpgo .fill.warm { background:var(--warm); }
.xpgo .range-line { display:grid; grid-template-columns:7rem 1fr 4rem; gap:.7rem; align-items:center; margin:.5rem 0; font-size:.86rem; }
.xpgo input[type=&quot;range&quot;] { width:100%; accent-color:var(--accent); }
.xpgo .stamp-row { display:flex; flex-wrap:wrap; gap:.4rem; margin:.65rem 0; }
.xpgo .stamp { border:1px solid var(--border); border-radius:var(--standard-border-radius); background:var(--bg); padding:.45rem .6rem; min-width:4.8rem; text-align:center; font-family:var(--mono-font); font-size:.78rem; }
.xpgo .stamp.on { border-color:var(--hot); color:var(--hot); background:color-mix(in srgb,var(--hot) 10%,var(--bg)); }
.xpgo .stage { opacity:.55; }
.xpgo .stage.active { opacity:1; border-color:var(--accent); box-shadow:0 0 0 1px var(--accent) inset; }
.xpgo .choice-grid { display:grid; grid-template-columns:repeat(auto-fit,minmax(190px,1fr)); gap:.6rem; margin:.7rem 0; }
.xpgo .choice { border:1px solid var(--border); border-radius:var(--standard-border-radius); background:var(--bg); padding:.75rem; font-size:.86rem; }
.xpgo .choice strong { display:block; margin-bottom:.25rem; color:var(--accent); }
.xpgo .term-grid { display:grid; grid-template-columns:repeat(auto-fit,minmax(170px,1fr)); gap:.45rem; margin:.6rem 0; }
.xpgo .term-grid button { text-align:left; border:1px solid var(--border); border-radius:var(--standard-border-radius); background:var(--bg); color:var(--fg-color); padding:.55rem .65rem; cursor:pointer; font:inherit; font-size:.84rem; }
.xpgo .term-grid button.active { border-color:var(--accent); box-shadow:0 0 0 1px var(--accent) inset; }
.xpgo .term-card { border:1px solid var(--accent); border-radius:var(--standard-border-radius); background:var(--bg); padding:.85rem; font-size:.9rem; min-height:4rem; }
.xpgo details.faq { border:1px solid var(--border); border-radius:var(--standard-border-radius); margin-bottom:.55rem; padding:.2rem .6rem; }
.xpgo details.faq summary { cursor:pointer; font-weight:700; padding:.45rem 0; }
.xpgo details.faq p { font-size:.92rem; margin:.45rem 0; }
.xpgo .gloss dt { font-weight:700; color:var(--accent); margin-top:.9rem; }
.xpgo .gloss dd { margin:.15rem 0 0; font-size:.9rem; }
@media (max-width:650px) { .xpgo .toc-grid { grid-template-columns:1fr; } .xpgo .row { grid-template-columns:1fr; gap:.3rem; } .xpgo .arrow { min-width:100%; } }
&lt;&#x2F;style&gt;
&lt;div class=&quot;xpgo&quot;&gt;
&lt;nav aria-label=&quot;Contents&quot;&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;&#x2F;h2&gt;
&lt;ol class=&quot;toc-grid&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#part1&quot;&gt;What the compiler normally guesses&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part2&quot;&gt;Why mobile cares about hot code and size&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part3&quot;&gt;The PGO feedback loop&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part4&quot;&gt;Coverage profiling&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part5&quot;&gt;Temporal profiling&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part6&quot;&gt;Function layout and page faults&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part7&quot;&gt;Balanced partitioning&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part8&quot;&gt;Reddit&#x27;s iOS pipeline&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part9&quot;&gt;Case study: reading a real profile&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part10&quot;&gt;Measurement and traps&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part11&quot;&gt;Diagnostic reference&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part12&quot;&gt;FAQ&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part13&quot;&gt;Glossary&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;nav&gt;
&lt;h2 id=&quot;part1&quot;&gt;Part 1 — What the compiler normally guesses&lt;&#x2F;h2&gt;
&lt;p&gt;A compiler has to make performance decisions before your app has ever run. Should this function be inlined? Which branch is likely? Which loop deserves more aggressive optimization? Which code should be kept close together in the final binary?&lt;&#x2F;p&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Static guess vs. profile-guided build&lt;&#x2F;h4&gt;
&lt;div class=&quot;mini-flow&quot;&gt;
&lt;div class=&quot;node warm&quot;&gt;&lt;strong&gt;Static build&lt;&#x2F;strong&gt;Source code shape&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node warm&quot;&gt;&lt;strong&gt;Heuristics&lt;&#x2F;strong&gt;Compiler guesses hot paths&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node cold&quot;&gt;&lt;strong&gt;Risk&lt;&#x2F;strong&gt;Budget goes to the wrong code&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;mini-flow&quot;&gt;
&lt;div class=&quot;node hot&quot;&gt;&lt;strong&gt;PGO build&lt;&#x2F;strong&gt;Instrumented app run&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node hot&quot;&gt;&lt;strong&gt;Profile data&lt;&#x2F;strong&gt;Real hot paths recorded&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node&quot;&gt;&lt;strong&gt;Optimized build&lt;&#x2F;strong&gt;Budget follows evidence&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;The difference is not that PGO has a smarter compiler. It gives the compiler a measured workload to believe.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Without profiles, the compiler uses static heuristics: educated guesses based on the shape of the code. Those guesses are often reasonable, but they are still guesses. A function that looks small may be irrelevant. A branch that looks unlikely may be the branch every user hits during startup. A generic utility may be cold in most of the app but hot on the first screen.&lt;&#x2F;p&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Static optimization&lt;&#x2F;h4&gt;
&lt;p&gt;The compiler looks only at source&#x2F;IR. It optimizes based on general rules: small functions are inline candidates, loops get transformed, branches get predicted from code shape.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Profile-guided optimization&lt;&#x2F;h4&gt;
&lt;p&gt;You first build an instrumented app, run representative flows, merge the generated profile files, then rebuild using those profiles. The compiler now has evidence.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Switch the compiler&#x27;s view&lt;&#x2F;h4&gt;
&lt;div class=&quot;seg&quot; role=&quot;tablist&quot; aria-label=&quot;Compiler view&quot;&gt;
&lt;button class=&quot;active&quot; type=&quot;button&quot; onclick=&quot;xpgoShow(&#x27;guess-view&#x27;,&#x27;guess&#x27;)&quot;&gt;Static guess&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoShow(&#x27;guess-view&#x27;,&#x27;profile&#x27;)&quot;&gt;Profile evidence&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div data-xpgo-group=&quot;guess-view&quot; data-xpgo-panel=&quot;guess&quot; class=&quot;panel active&quot;&gt;
&lt;div class=&quot;mini-flow&quot;&gt;
&lt;div class=&quot;node warm&quot;&gt;&lt;strong&gt;Source shape&lt;&#x2F;strong&gt;Small function, branch, loop&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node&quot;&gt;&lt;strong&gt;Heuristic&lt;&#x2F;strong&gt;&quot;This probably matters&quot;&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node cold&quot;&gt;&lt;strong&gt;Risk&lt;&#x2F;strong&gt;Wrong path gets budget&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;Without profiles, the compiler only sees the code&#x27;s shape. It cannot know which path users actually hit.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div data-xpgo-group=&quot;guess-view&quot; data-xpgo-panel=&quot;profile&quot; class=&quot;panel&quot;&gt;
&lt;div class=&quot;mini-flow&quot;&gt;
&lt;div class=&quot;node hot&quot;&gt;&lt;strong&gt;Runtime data&lt;&#x2F;strong&gt;startup called this 1,000x&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node&quot;&gt;&lt;strong&gt;Optimizer&lt;&#x2F;strong&gt;Spend budget here&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node cold&quot;&gt;&lt;strong&gt;Cold path&lt;&#x2F;strong&gt;Keep small, move later&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;PGO replaces &quot;probably&quot; with measured behavior from the workload you chose.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;PGO is a feedback loop: build with instrumentation → run realistic behavior → collect profiles → rebuild using those profiles.&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;The profile does not change what your program means. It changes how aggressively and where the toolchain spends optimization budget.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;The rule to remember: coverage PGO tells the compiler what code is hot. Temporal PGO tells the linker what code runs together. On mobile, both matter because code size and code layout affect startup.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part2&quot;&gt;Part 2 — Why mobile cares about hot code and size&lt;&#x2F;h2&gt;
&lt;p&gt;Mobile startup is not only about how fast one function executes after it is already in memory. Before that function can run, its code has to be present in memory. If it is not, the kernel brings in the page that contains it, and that page fault costs time. A smaller and better-laid-out &lt;code&gt;__TEXT&lt;&#x2F;code&gt; section usually means fewer pages touched during startup.&lt;&#x2F;p&gt;
&lt;p&gt;This is why the hot&#x2F;cold distinction matters. PGO is deciding where the toolchain should spend optimization budget. Some code deserves aggressive treatment because it runs during startup or a critical flow. Some code should mostly stay out of the way because making it bigger or placing it early can hurt the path users actually feel.&lt;&#x2F;p&gt;
&lt;p&gt;PGO starts with a simple split: some code is &lt;span class=&quot;hot&quot;&gt;hot&lt;&#x2F;span&gt;, some code is &lt;span class=&quot;cold&quot;&gt;cold&lt;&#x2F;span&gt;. Hot does not mean &quot;important to the product&quot; and cold does not mean &quot;dead&quot;. Hot means &quot;executed often, or executed during an important measured window&quot;. Cold means &quot;rarely executed in the profile you collected&quot;.&lt;&#x2F;p&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Category&lt;&#x2F;th&gt;&lt;th&gt;Meaning&lt;&#x2F;th&gt;&lt;th&gt;Optimization implication&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;span class=&quot;pill hot&quot;&gt;Hot&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;td&gt;Runs frequently or during a critical flow such as cold startup.&lt;&#x2F;td&gt;&lt;td&gt;Worth inlining, branch tuning, layout priority, and sometimes more code size.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;span class=&quot;pill cold&quot;&gt;Cold&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;td&gt;Runs rarely or never in the collected workload.&lt;&#x2F;td&gt;&lt;td&gt;Avoid wasting binary size. Keep it out of the hot path and often optimize for size.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;span class=&quot;pill warm&quot;&gt;Warm&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;td&gt;Runs sometimes, but not in the first-order path.&lt;&#x2F;td&gt;&lt;td&gt;Optimize normally; do not let it disrupt startup layout.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;
&lt;&#x2F;table&gt;
&lt;p&gt;The phrase &quot;hot path&quot; is only meaningful relative to the workload you profiled. If your profile comes from login and home-feed startup, then PGO learns about login and home-feed startup. It cannot magically optimize a checkout flow, editor flow, or obscure settings screen unless that behavior appears in the profile.&lt;&#x2F;p&gt;
&lt;h3&gt;Why &lt;code&gt;-Oz&lt;&#x2F;code&gt; can beat &quot;faster&quot; optimization&lt;&#x2F;h3&gt;
&lt;p&gt;On desktop&#x2F;server code, &quot;optimize for speed&quot; often sounds like the obvious default. On mobile, size is part of speed. That is why mobile apps are often built with &lt;code&gt;-Oz&lt;&#x2F;code&gt; or other size-aware settings instead of blindly choosing speed-oriented optimization. More inlining can make a hot CPU loop faster, but it can also inflate code size enough to hurt cold startup.&lt;&#x2F;p&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Move the inlining dial&lt;&#x2F;h4&gt;
&lt;div class=&quot;range-line&quot;&gt;
&lt;label for=&quot;xpgo-inline-range&quot;&gt;Inlining&lt;&#x2F;label&gt;
&lt;input id=&quot;xpgo-inline-range&quot; type=&quot;range&quot; min=&quot;0&quot; max=&quot;100&quot; value=&quot;35&quot; oninput=&quot;xpgoInline(this.value)&quot;&gt;
&lt;strong id=&quot;xpgo-inline-label&quot;&gt;35%&lt;&#x2F;strong&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;bars&quot;&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;CPU call overhead&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span id=&quot;xpgo-overhead&quot; class=&quot;fill cold&quot; style=&quot;--w:65%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span id=&quot;xpgo-overhead-label&quot;&gt;65%&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;Code size&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span id=&quot;xpgo-codesize&quot; class=&quot;fill hot&quot; style=&quot;--w:35%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span id=&quot;xpgo-codesize-label&quot;&gt;35%&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;Startup page risk&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span id=&quot;xpgo-pagerisk&quot; class=&quot;fill warm&quot; style=&quot;--w:42%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span id=&quot;xpgo-pagerisk-label&quot;&gt;42%&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;Inlining can reduce call overhead, but it also grows code. On mobile, too much growth can make startup worse even if individual calls are faster.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part3&quot;&gt;Part 3 — The PGO feedback loop&lt;&#x2F;h2&gt;
&lt;p&gt;At a high level, PGO has two builds and one run in the middle.&lt;&#x2F;p&gt;
&lt;div class=&quot;diagram&quot;&gt;
&lt;div class=&quot;dgrid&quot;&gt;
&lt;div class=&quot;dbox&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;1. Instrumented build&lt;&#x2F;div&gt;Compile with flags such as &lt;code&gt;-fprofile-generate&lt;&#x2F;code&gt;. The produced app includes counters or probes that write execution data.&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;2. Representative run&lt;&#x2F;div&gt;Exercise the app through realistic flows: startup, logged-out, logged-in, notifications, critical screens, experiments.&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;3. Merge profiles&lt;&#x2F;div&gt;Combine raw outputs with &lt;code&gt;llvm-profdata merge&lt;&#x2F;code&gt; into a stable &lt;code&gt;.profdata&lt;&#x2F;code&gt; file.&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;4. Optimized build&lt;&#x2F;div&gt;Rebuild with &lt;code&gt;-fprofile-use=&#x2F;path&#x2F;profile.profdata&lt;&#x2F;code&gt;. The compiler and linker now optimize with evidence.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;The profiles are build inputs. Treat them like generated artifacts with a freshness policy, not like magic constants checked in forever.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Step through the feedback loop&lt;&#x2F;h4&gt;
&lt;div class=&quot;seg&quot; role=&quot;tablist&quot; aria-label=&quot;PGO loop steps&quot;&gt;
&lt;button class=&quot;active&quot; type=&quot;button&quot; onclick=&quot;xpgoStage(&#x27;loop&#x27;,1)&quot;&gt;1 Build&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoStage(&#x27;loop&#x27;,2)&quot;&gt;2 Run&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoStage(&#x27;loop&#x27;,3)&quot;&gt;3 Merge&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoStage(&#x27;loop&#x27;,4)&quot;&gt;4 Rebuild&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;mini-flow&quot; data-xpgo-stage-group=&quot;loop&quot;&gt;
&lt;div class=&quot;node stage active&quot; data-xpgo-stage=&quot;1&quot;&gt;&lt;strong&gt;Instrument&lt;&#x2F;strong&gt;Add counters&#x2F;timestamps&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node stage&quot; data-xpgo-stage=&quot;2&quot;&gt;&lt;strong&gt;Exercise&lt;&#x2F;strong&gt;Run representative flows&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node stage&quot; data-xpgo-stage=&quot;3&quot;&gt;&lt;strong&gt;Merge&lt;&#x2F;strong&gt;Turn raw runs into profile input&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node stage&quot; data-xpgo-stage=&quot;4&quot;&gt;&lt;strong&gt;Optimize&lt;&#x2F;strong&gt;Compiler&#x2F;linker use the profile&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p id=&quot;xpgo-loop-caption&quot; class=&quot;dcaption&quot;&gt;The first build is deliberately instrumented; it is not the final app you ship.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;pre&gt;clang -fprofile-generate ...       &lt;span class=&quot;dim&quot;&gt;# build an instrumented binary&lt;&#x2F;span&gt;
.&#x2F;run-representative-workload      &lt;span class=&quot;dim&quot;&gt;# emits one or more .profraw files&lt;&#x2F;span&gt;
llvm-profdata merge *.profraw -o app.profdata
clang -fprofile-use=app.profdata ...&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;part4&quot;&gt;Part 4 — Coverage profiling&lt;&#x2F;h2&gt;
&lt;p&gt;Coverage profiling answers: &lt;strong&gt;what ran, and how much?&lt;&#x2F;strong&gt; It records function counts, branch behavior, and other execution-frequency signals. When you feed that profile into a later compile, the compiler can make better local decisions.&lt;&#x2F;p&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Inlining&lt;&#x2F;h4&gt;
&lt;p&gt;Inlining removes a function call by copying the callee into the caller. It can expose further optimization, but it can also grow the binary. PGO helps spend inlining budget on hot calls instead of cold ones.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Branch layout&lt;&#x2F;h4&gt;
&lt;p&gt;If one branch is usually taken, the compiler can lay out the common path so the CPU does less jumping and prediction is easier.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Loop and codegen choices&lt;&#x2F;h4&gt;
&lt;p&gt;Loops that actually run many iterations can justify heavier optimization. Cold loops should not receive the same attention by default.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Choose a coverage profile&lt;&#x2F;h4&gt;
&lt;div class=&quot;seg&quot; role=&quot;tablist&quot; aria-label=&quot;Coverage workload&quot;&gt;
&lt;button class=&quot;active&quot; type=&quot;button&quot; onclick=&quot;xpgoShow(&#x27;coverage-view&#x27;,&#x27;startup&#x27;)&quot;&gt;Startup profile&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoShow(&#x27;coverage-view&#x27;,&#x27;settings&#x27;)&quot;&gt;Settings profile&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div data-xpgo-group=&quot;coverage-view&quot; data-xpgo-panel=&quot;startup&quot; class=&quot;panel active&quot;&gt;
&lt;div class=&quot;bars&quot;&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;bootstrap()&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill hot&quot; style=&quot;--w:95%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;950&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;decodeFeed()&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill hot&quot; style=&quot;--w:78%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;780&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;openSettings()&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill cold&quot; style=&quot;--w:4%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;40&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;The compiler sees startup and feed code as hot, so inlining and branch decisions bias toward that path.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div data-xpgo-group=&quot;coverage-view&quot; data-xpgo-panel=&quot;settings&quot; class=&quot;panel&quot;&gt;
&lt;div class=&quot;bars&quot;&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;bootstrap()&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill warm&quot; style=&quot;--w:35%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;350&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;decodeFeed()&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill cold&quot; style=&quot;--w:12%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;120&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;openSettings()&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill hot&quot; style=&quot;--w:92%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;920&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;A different workload teaches a different truth. This is why representative profiling matters.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;Coverage PGO is mostly about compiler decisions inside and between functions.&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;It tells the optimizer where the execution weight is, so it can avoid treating every path as equally important. The mobile caveat: default IRPGO is often tuned toward inlining and speed, so without size-aware settings it can grow the binary.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;That caveat matters. If PGO says &quot;this call is hot&quot;, the compiler may inline it. Sometimes that is exactly right. Sometimes the extra code size creates more instruction-cache pressure and more pages to fault in. LLVM has size-oriented knobs such as profile-guided size optimization and inlining controls for this reason. PGO is evidence; policy still matters.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;part5&quot;&gt;Part 5 — Temporal profiling&lt;&#x2F;h2&gt;
&lt;p&gt;Coverage answers &quot;how often?&quot;. Temporal profiling answers &lt;strong&gt;&quot;when, and in what order?&quot;&lt;&#x2F;strong&gt; That distinction matters for startup. During a cold launch, the device does not load your entire binary into memory at once. It faults in pages as code is touched. If startup functions are scattered across the file, the system touches more pages earlier.&lt;&#x2F;p&gt;
&lt;p&gt;Temporal profiling records an execution trace: function A happened, then B, then C. For mobile startup, that sequence is gold. It lets the linker place functions that execute close together physically close together in the binary.&lt;&#x2F;p&gt;
&lt;div class=&quot;diagram&quot;&gt;
&lt;div class=&quot;dgrid&quot;&gt;
&lt;div class=&quot;dbox&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;Coverage profile&lt;&#x2F;div&gt;&lt;code&gt;FeedViewController.viewDidLoad&lt;&#x2F;code&gt;: 1&lt;br&gt;&lt;code&gt;JSONDecoder.decode&lt;&#x2F;code&gt;: 340&lt;br&gt;&lt;code&gt;SettingsScreen.open&lt;&#x2F;code&gt;: 0&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;Compiler learns&lt;&#x2F;div&gt;Decode path is hot. Settings path is cold. Inline and branch decisions should reflect that.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;dgrid&quot; style=&quot;margin-top:.7rem&quot;&gt;
&lt;div class=&quot;dbox&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;Temporal profile&lt;&#x2F;div&gt;&lt;code&gt;main → AppDelegate → bootstrap → feed → first cell&lt;&#x2F;code&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;Linker learns&lt;&#x2F;div&gt;These functions belong near each other because startup touches them in this order.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;Both profiles describe the same app, but they answer different optimization questions.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Record first-call timestamps&lt;&#x2F;h4&gt;
&lt;div class=&quot;seg&quot; role=&quot;tablist&quot; aria-label=&quot;Temporal trace&quot;&gt;
&lt;button class=&quot;active&quot; type=&quot;button&quot; onclick=&quot;xpgoTrace(0)&quot;&gt;Reset&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTrace(1)&quot;&gt;Call 1&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTrace(2)&quot;&gt;Call 2&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTrace(3)&quot;&gt;Call 3&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTrace(4)&quot;&gt;Call 4&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;stamp-row&quot;&gt;
&lt;span id=&quot;xpgo-stamp-main&quot; class=&quot;stamp&quot;&gt;main&lt;br&gt;0&lt;&#x2F;span&gt;
&lt;span id=&quot;xpgo-stamp-app&quot; class=&quot;stamp&quot;&gt;App&lt;br&gt;0&lt;&#x2F;span&gt;
&lt;span id=&quot;xpgo-stamp-feed&quot; class=&quot;stamp&quot;&gt;Feed&lt;br&gt;0&lt;&#x2F;span&gt;
&lt;span id=&quot;xpgo-stamp-cell&quot; class=&quot;stamp&quot;&gt;Cell&lt;br&gt;0&lt;&#x2F;span&gt;
&lt;span id=&quot;xpgo-stamp-settings&quot; class=&quot;stamp&quot;&gt;Settings&lt;br&gt;0&lt;&#x2F;span&gt;
&lt;&#x2F;div&gt;
&lt;p id=&quot;xpgo-trace-caption&quot; class=&quot;dcaption&quot;&gt;Before execution, every timestamp is zero. Zero means &quot;not observed in this run.&quot;&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;In Reddit&#x27;s write-up, temporal profiling used LLVM&#x27;s &lt;code&gt;-pgo-temporal-instrumentation&lt;&#x2F;code&gt;. The LLVM talk explains the mechanism: every instrumented function gets a timestamp slot initialized to zero; on the first call, the runtime writes the current global timestamp and increments it. When the raw profile is dumped, functions can be sorted by timestamp to recover a trace. A zero timestamp means the function did not run in that profile.&lt;&#x2F;p&gt;
&lt;p&gt;A key detail: temporal instrumentation has much lower binary-size overhead than traditional IR PGO instrumentation, which helps the profiled app behave more like the real release app. If the profiled build is wildly different from release, its function order is less trustworthy.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;part6&quot;&gt;Part 6 — Function layout and page faults&lt;&#x2F;h2&gt;
&lt;p&gt;A binary is not just a bag of functions. It is an ordered file. That order affects cold startup because the kernel brings code into memory in pages. Reddit&#x27;s article calls out 16 KiB pages on iOS. The exact page size matters less than the invariant: touching code from many far-apart regions causes more page faults than touching code from nearby regions.&lt;&#x2F;p&gt;
&lt;div class=&quot;layout&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;rlabel&quot;&gt;Before layout&lt;&#x2F;div&gt;
&lt;div class=&quot;pages&quot; aria-label=&quot;Scattered hot and cold pages before layout&quot;&gt;
&lt;span class=&quot;page hot&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page cold&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page hot&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page warm&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page cold&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page hot&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page cold&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page warm&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page hot&quot;&gt;&lt;&#x2F;span&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;rlabel&quot;&gt;After layout&lt;&#x2F;div&gt;
&lt;div class=&quot;pages&quot; aria-label=&quot;Clustered hot pages after layout&quot;&gt;
&lt;span class=&quot;page hot&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page hot&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page hot&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page hot&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page warm&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page warm&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page cold&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page cold&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page cold&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;page&quot;&gt;&lt;&#x2F;span&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Compare page touches&lt;&#x2F;h4&gt;
&lt;div class=&quot;seg&quot; role=&quot;tablist&quot; aria-label=&quot;Binary layout&quot;&gt;
&lt;button class=&quot;active&quot; type=&quot;button&quot; onclick=&quot;xpgoShow(&#x27;layout-view&#x27;,&#x27;scattered&#x27;)&quot;&gt;Scattered&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoShow(&#x27;layout-view&#x27;,&#x27;clustered&#x27;)&quot;&gt;Clustered&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div data-xpgo-group=&quot;layout-view&quot; data-xpgo-panel=&quot;scattered&quot; class=&quot;panel active&quot;&gt;
&lt;div class=&quot;bars&quot;&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;Pages touched&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill hot&quot; style=&quot;--w:92%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;11&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;Startup wait&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill warm&quot; style=&quot;--w:80%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;high&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;Startup functions are spread apart, so the launch path jumps across many pages.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div data-xpgo-group=&quot;layout-view&quot; data-xpgo-panel=&quot;clustered&quot; class=&quot;panel&quot;&gt;
&lt;div class=&quot;bars&quot;&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;Pages touched&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill hot&quot; style=&quot;--w:38%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;5&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;Startup wait&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill warm&quot; style=&quot;--w:32%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;lower&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;Startup functions are adjacent, so the launch path reuses nearby pages.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Function reordering does not make a function faster in isolation. It makes the startup walk through the binary cheaper: fewer scattered pages, fewer cold misses, less waiting before the app becomes interactive.&lt;&#x2F;p&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;For cold startup, where code lives can matter almost as much as what the code does.&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;That is the part of PGO I want to remember: profiles can guide not only compiler transforms, but also physical binary layout.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Also keep two goals separate: the compressed IPA should be small so install&#x2F;update is fast, while the installed binary should be laid out so launch is fast. The app is not decompressing the IPA during startup; compression helps distribution, page-fault reduction helps launch.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;part7&quot;&gt;Part 7 — Balanced partitioning&lt;&#x2F;h2&gt;
&lt;p&gt;The hard part is that layout has two goals that can pull in different directions.&lt;&#x2F;p&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Startup locality&lt;&#x2F;h4&gt;
&lt;p&gt;Functions that run near each other in time should sit near each other in the file. This reduces page faults during cold startup and critical flows.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Compression locality&lt;&#x2F;h4&gt;
&lt;p&gt;Functions with similar instruction patterns often compress better when placed near each other, because LZ-style compression benefits from repeated nearby patterns.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Pick the layout objective&lt;&#x2F;h4&gt;
&lt;div class=&quot;seg&quot; role=&quot;tablist&quot; aria-label=&quot;Balanced partitioning objective&quot;&gt;
&lt;button class=&quot;active&quot; type=&quot;button&quot; onclick=&quot;xpgoShow(&#x27;bp-view&#x27;,&#x27;startup&#x27;)&quot;&gt;Startup&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoShow(&#x27;bp-view&#x27;,&#x27;compression&#x27;)&quot;&gt;Compression&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoShow(&#x27;bp-view&#x27;,&#x27;both&#x27;)&quot;&gt;Both&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div data-xpgo-group=&quot;bp-view&quot; data-xpgo-panel=&quot;startup&quot; class=&quot;panel active&quot;&gt;
&lt;div class=&quot;mini-flow&quot;&gt;
&lt;div class=&quot;node hot&quot;&gt;&lt;strong&gt;A&lt;&#x2F;strong&gt;main&lt;&#x2F;div&gt;&lt;div class=&quot;node hot&quot;&gt;&lt;strong&gt;B&lt;&#x2F;strong&gt;bootstrap&lt;&#x2F;div&gt;&lt;div class=&quot;node hot&quot;&gt;&lt;strong&gt;C&lt;&#x2F;strong&gt;feed&lt;&#x2F;div&gt;&lt;div class=&quot;node cold&quot;&gt;&lt;strong&gt;X&lt;&#x2F;strong&gt;settings&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;Prioritize functions that execute close together in the startup trace.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div data-xpgo-group=&quot;bp-view&quot; data-xpgo-panel=&quot;compression&quot; class=&quot;panel&quot;&gt;
&lt;div class=&quot;mini-flow&quot;&gt;
&lt;div class=&quot;node cold&quot;&gt;&lt;strong&gt;X1&lt;&#x2F;strong&gt;similar bytes&lt;&#x2F;div&gt;&lt;div class=&quot;node cold&quot;&gt;&lt;strong&gt;X2&lt;&#x2F;strong&gt;similar bytes&lt;&#x2F;div&gt;&lt;div class=&quot;node warm&quot;&gt;&lt;strong&gt;Y1&lt;&#x2F;strong&gt;similar bytes&lt;&#x2F;div&gt;&lt;div class=&quot;node warm&quot;&gt;&lt;strong&gt;Y2&lt;&#x2F;strong&gt;similar bytes&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;Prioritize functions with similar instruction patterns so compression sees repeated nearby sequences.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div data-xpgo-group=&quot;bp-view&quot; data-xpgo-panel=&quot;both&quot; class=&quot;panel&quot;&gt;
&lt;div class=&quot;mini-flow&quot;&gt;
&lt;div class=&quot;node hot&quot;&gt;&lt;strong&gt;Hot trace&lt;&#x2F;strong&gt;A · B · C&lt;&#x2F;div&gt;&lt;div class=&quot;node warm&quot;&gt;&lt;strong&gt;Warm similar&lt;&#x2F;strong&gt;Y1 · Y2&lt;&#x2F;div&gt;&lt;div class=&quot;node cold&quot;&gt;&lt;strong&gt;Cold similar&lt;&#x2F;strong&gt;X1 · X2&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;Balanced partitioning searches for a useful compromise: startup locality first where it matters, compression locality elsewhere.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Balanced partitioning treats layout like a graph problem. Functions are nodes. Relationships between functions are utilities: &quot;these two run near each other&quot; or &quot;these two look similar for compression&quot;. The algorithm recursively partitions the graph so strongly related functions tend to stay together. With an option such as &lt;code&gt;--compression-sort=both&lt;&#x2F;code&gt;, the linker can balance temporal locality and compression locality instead of choosing only one.&lt;&#x2F;p&gt;
&lt;div class=&quot;diagram&quot;&gt;
&lt;div class=&quot;dgrid&quot;&gt;
&lt;div class=&quot;dbox&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;Temporal utility&lt;&#x2F;div&gt;&lt;code&gt;A → B → C&lt;&#x2F;code&gt; appeared in the startup trace, so A, B, and C should be close.&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;Compression utility&lt;&#x2F;div&gt;&lt;code&gt;X&lt;&#x2F;code&gt; and &lt;code&gt;Y&lt;&#x2F;code&gt; have similar machine-code patterns, so placing them close may shrink the compressed IPA.&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;Partitioning decision&lt;&#x2F;div&gt;Keep high-utility relationships inside the same partition when possible; split weaker relationships first.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;This is why the same optimization can improve startup and reduce size. The layout is optimized using real execution traces plus code-similarity signals.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;The LLVM talk gives a concrete compression model: LZ-style compression benefits from repeated instruction sequences appearing within a sliding window. Function contents can be represented by stable hashes of instructions; functions sharing many utility vertices are good candidates to place near each other. For startup, the utility vertices come from temporal traces instead.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;part8&quot;&gt;Part 8 — Reddit&#x27;s iOS pipeline&lt;&#x2F;h2&gt;
&lt;p&gt;Reddit&#x27;s production story becomes much easier to read once the pieces are separated.&lt;&#x2F;p&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;1. Representative workloads&lt;&#x2F;h4&gt;
&lt;p&gt;They reused UI performance tests around important P0 use cases: fresh install, signed-out, logged-in, account switching, opening posts, and feed paths. HTTP stubbing reduced noise so the profiles reflected app behavior instead of network timing.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;2. Two profile families&lt;&#x2F;h4&gt;
&lt;p&gt;Coverage profiles fed compiler optimization through &lt;code&gt;-profile-use&lt;&#x2F;code&gt;. Temporal profiles fed linker function ordering through LLD.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;3. Final optimized release build&lt;&#x2F;h4&gt;
&lt;p&gt;The final App Store binary was built with profile inputs, a custom LLVM linker, and balanced partitioning for startup locality and compression.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;pre&gt;instrumented app + UI tests
  ├─ coverage .profraw files  → llvm-profdata merge → coverage.profdata
  └─ temporal .profraw files  → llvm-profdata merge → temporal.profdata

release build
  ├─ swiftc &#x2F; clang use coverage.profdata for compiler PGO
  └─ LLD uses temporal.profdata for function ordering&lt;&#x2F;pre&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Follow one profile through the release pipeline&lt;&#x2F;h4&gt;
&lt;div class=&quot;seg&quot; role=&quot;tablist&quot; aria-label=&quot;Reddit pipeline&quot;&gt;
&lt;button class=&quot;active&quot; type=&quot;button&quot; onclick=&quot;xpgoStage(&#x27;reddit&#x27;,1)&quot;&gt;UI tests&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoStage(&#x27;reddit&#x27;,2)&quot;&gt;Merge&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoStage(&#x27;reddit&#x27;,3)&quot;&gt;Compile&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoStage(&#x27;reddit&#x27;,4)&quot;&gt;Link&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;mini-flow&quot; data-xpgo-stage-group=&quot;reddit&quot;&gt;
&lt;div class=&quot;node stage active&quot; data-xpgo-stage=&quot;1&quot;&gt;&lt;strong&gt;Tests&lt;&#x2F;strong&gt;realistic app paths&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node stage&quot; data-xpgo-stage=&quot;2&quot;&gt;&lt;strong&gt;Profiles&lt;&#x2F;strong&gt;coverage + temporal&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node stage&quot; data-xpgo-stage=&quot;3&quot;&gt;&lt;strong&gt;swiftc&#x2F;clang&lt;&#x2F;strong&gt;use coverage data&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow&quot;&gt;→&lt;&#x2F;div&gt;
&lt;div class=&quot;node stage&quot; data-xpgo-stage=&quot;4&quot;&gt;&lt;strong&gt;LLD&lt;&#x2F;strong&gt;uses temporal layout&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p id=&quot;xpgo-reddit-caption&quot; class=&quot;dcaption&quot;&gt;The profile starts as UI-test behavior, not as a hand-written order file.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;They also tuned the inlining threshold upward. That is an important reminder: PGO is not just one flag. It is a build pipeline. Profiles make bolder optimization choices safer, but they do not remove trade-offs. More inlining can speed hot paths and expose optimization, but it can increase binary size. In Reddit&#x27;s case, the layout&#x2F;compression savings created room to be more aggressive.&lt;&#x2F;p&gt;
&lt;h3&gt;Results Worth Remembering&lt;&#x2F;h3&gt;
&lt;div class=&quot;metric-row&quot;&gt;
&lt;div class=&quot;metric&quot;&gt;&lt;div class=&quot;label&quot;&gt;Compressed IPA&lt;&#x2F;div&gt;&lt;div class=&quot;value&quot;&gt;280.6 → 239.6 MiB&lt;&#x2F;div&gt;&lt;div class=&quot;sub&quot;&gt;14.6% reduction&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;metric&quot;&gt;&lt;div class=&quot;label&quot;&gt;Uncompressed payload&lt;&#x2F;div&gt;&lt;div class=&quot;value&quot;&gt;359.8 → 313.1 MiB&lt;&#x2F;div&gt;&lt;div class=&quot;sub&quot;&gt;15.3% reduction&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;metric&quot;&gt;&lt;div class=&quot;label&quot;&gt;Main binary&lt;&#x2F;div&gt;&lt;div class=&quot;value&quot;&gt;198.6 → 157.1 MiB&lt;&#x2F;div&gt;&lt;div class=&quot;sub&quot;&gt;20.8% reduction&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Those are size wins. The startup win was subtler to measure because iOS performs background optimizations after installation. Reddit focused on first-day behavior after install&#x2F;update, where cold-start layout matters most, and used a dual-release strategy: one standard release, then an identical-code optimized release.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;part9&quot;&gt;Part 9 — Case study: reading a real profile&lt;&#x2F;h2&gt;
&lt;p&gt;Everything above was theory plus other people&#x27;s numbers. So I collected an IR PGO profile from the app I work on — a large marketplace iOS app, ~100 build modules — using a UI test that performs 6 cold launches and a feed browse pass. Then I actually read the profile. This section is what the theory looks like against production-scale code, and it changed how I think about two things: how extreme the hot&#x2F;cold split really is, and whose code the hot set is made of.&lt;&#x2F;p&gt;
&lt;div class=&quot;metric-row&quot;&gt;
&lt;div class=&quot;metric&quot;&gt;&lt;div class=&quot;label&quot;&gt;Functions instrumented&lt;&#x2F;div&gt;&lt;div class=&quot;value&quot;&gt;383,676&lt;&#x2F;div&gt;&lt;div class=&quot;sub&quot;&gt;the whole app + every dependency&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;metric&quot;&gt;&lt;div class=&quot;label&quot;&gt;Executions recorded&lt;&#x2F;div&gt;&lt;div class=&quot;value&quot;&gt;36.65M&lt;&#x2F;div&gt;&lt;div class=&quot;sub&quot;&gt;sum of every branch-point counter&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;metric&quot;&gt;&lt;div class=&quot;label&quot;&gt;Never called&lt;&#x2F;div&gt;&lt;div class=&quot;value&quot;&gt;96.32%&lt;&#x2F;div&gt;&lt;div class=&quot;sub&quot;&gt;369,574 functions had entry count 0&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;h3&gt;How skewed is &quot;skewed&quot;?&lt;&#x2F;h3&gt;
&lt;p&gt;Before looking, I assumed hot&#x2F;cold meant something like 20&#x2F;80. The real distribution is not in that universe. &lt;code&gt;llvm-profdata show --detailed-summary&lt;&#x2F;code&gt; prints a cumulative table: how many counter blocks (branch points) does it take to cover each share of all recorded executions? Verbatim output:&lt;&#x2F;p&gt;
&lt;pre&gt;$ xcrun llvm-profdata show --detailed-summary app.profdata
Total functions: 383676
Total number of blocks: 1073586
Total count: 36654739
Detailed summary:
26 blocks (0.00%) with count &gt;= 197216 account for 50% of the total counts.
485 blocks (0.05%) with count &gt;= 6384 account for 90% of the total counts.
1084 blocks (0.10%) with count &gt;= 1505 account for 95% of the total counts.
4666 blocks (0.43%) with count &gt;= 133 account for 99% of the total counts.&lt;&#x2F;pre&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Blocks needed to cover each share of execution&lt;&#x2F;h4&gt;
&lt;div class=&quot;bars&quot;&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;50% of counts&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill hot&quot; style=&quot;--w:50%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;26&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;80%&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill hot&quot; style=&quot;--w:80%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;181&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;90%&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill warm&quot; style=&quot;--w:90%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;485&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;95%&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill warm&quot; style=&quot;--w:95%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;1,084&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;99%&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill cold&quot; style=&quot;--w:99%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;4,666&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;100%&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill cold&quot; style=&quot;--w:100%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;24,071&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;Bar length = share of execution covered; the number on the right = blocks needed, out of 1,073,586. Only 24,071 blocks (2.2%) executed at all.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;If execution were uniform, covering 50% of the counts would take ~537,000 blocks. It took 26. That is a concentration factor of roughly 20,000×.&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;This is why PGO works at all: the compiler can lavish attention on a few hundred functions and treat literally everything else as cold, and that policy is correct for 97.8% of the binary.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h3&gt;Whose code is hot? Not mine.&lt;&#x2F;h3&gt;
&lt;p&gt;My second wrong assumption: that the hot set would be my app&#x27;s code. Bucketing all 21M function &lt;em&gt;entries&lt;&#x2F;em&gt; by what kind of code was entered:&lt;&#x2F;p&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Share of function entries by code kind&lt;&#x2F;h4&gt;
&lt;div class=&quot;bars&quot;&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;Swift runtime helpers&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill hot&quot; style=&quot;--w:43.5%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;43.5%&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;Compiler glue&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill warm&quot; style=&quot;--w:37.8%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;37.8%&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;Third-party SDKs&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill cold&quot; style=&quot;--w:16%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;16.0%&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;barrow&quot;&gt;&lt;span&gt;My app&#x27;s code&lt;&#x2F;span&gt;&lt;span class=&quot;track&quot;&gt;&lt;span class=&quot;fill&quot; style=&quot;--w:1.5%&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span&gt;1.5%&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;&quot;Compiler glue&quot; = protocol&#x2F;value witnesses, outlined copy&#x2F;destroy helpers, thunks, metadata accessors — machinery the compiler generates around everyone&#x27;s code.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;The single most-called function in the entire app was &lt;code&gt;__swift_memcpy1_1&lt;&#x2F;code&gt; — 2,920,324 calls. That is 2.9 million &lt;em&gt;one-byte copies&lt;&#x2F;em&gt;, almost all driven by one third-party experimentation SDK that round-trips its JSON payloads through type-erased &lt;code&gt;Any&lt;&#x2F;code&gt; boxes during launch. The hottest single branch point in the binary was the read loop of nanopb — the C protobuf parser inside the analytics SDK — at 3.31M executions, because analytics state is parsed &lt;em&gt;and re-encoded&lt;&#x2F;em&gt; on every cold start.&lt;&#x2F;p&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Hottest spots in the binary&lt;&#x2F;th&gt;&lt;th&gt;What it is&lt;&#x2F;th&gt;&lt;th&gt;Max count&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;buf_read&lt;&#x2F;code&gt; (nanopb)&lt;&#x2F;td&gt;&lt;td&gt;protobuf decode loop, analytics SDK&lt;&#x2F;td&gt;&lt;td&gt;3,312,715&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;__swift_memcpy1_1&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;runtime helper: copy one byte&lt;&#x2F;td&gt;&lt;td&gt;2,920,324&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;__swift_noop_void_return&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;runtime helper: do nothing&lt;&#x2F;td&gt;&lt;td&gt;2,629,953&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;AnyEncodable&lt;&#x2F;code&gt; destroy&#x2F;copy witnesses&lt;&#x2F;td&gt;&lt;td&gt;Codable type-erasure glue, experiments SDK&lt;&#x2F;td&gt;&lt;td&gt;535,920 &#x2F; 446,600&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;
&lt;&#x2F;table&gt;
&lt;p&gt;Three more things the profile revealed that no amount of code review would have surfaced:&lt;&#x2F;p&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;The profile is a free product audit&lt;&#x2F;h4&gt;
&lt;p&gt;17% of all function entries belonged to one experimentation SDK&#x27;s Codable glue. The localization layer burned 250K entries in just 56 functions, copying a large enum payload on every string lookup. Neither showed up in any Instruments session we had run — they were spread across too many tiny calls.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Dead weight is measurable&lt;&#x2F;h4&gt;
&lt;p&gt;A generated analytics-events package contributed 6.8% of all functions in the binary but ~0.05% of execution. That is exactly the code the cold-marking half of PGO shrinks — and the strongest argument for the layout techniques in Parts 5–7.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;The hot set is glue, so PGO&#x27;s wins are glue wins&lt;&#x2F;h4&gt;
&lt;p&gt;Inlining a 3-line value witness into its hot caller eliminates call overhead on millions of one-byte copies. The classic C wins apply too: precise branch weights in nanopb&#x27;s decode loops, the hottest code in the binary.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;To do this on any app: build with &lt;code&gt;-fprofile-generate&lt;&#x2F;code&gt; (plus swiftc&#x27;s IR-profile flags on iOS), run a launch workload, merge, then read the result before applying it:&lt;&#x2F;p&gt;
&lt;pre&gt;xcrun llvm-profdata show --detailed-summary app.profdata   &lt;span class=&quot;dim&quot;&gt;# the skew table&lt;&#x2F;span&gt;
xcrun llvm-profdata show --topn=50 app.profdata | xcrun swift-demangle   &lt;span class=&quot;dim&quot;&gt;# hottest functions&lt;&#x2F;span&gt;
xcrun llvm-profdata show --all-functions --counts app.profdata | less    &lt;span class=&quot;dim&quot;&gt;# everything&lt;&#x2F;span&gt;&lt;&#x2F;pre&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;Read the profile before you feed it to the compiler. The same file that guides the optimizer is the most honest launch-behavior audit you will ever get for free.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part10&quot;&gt;Part 10 — Measurement and traps&lt;&#x2F;h2&gt;
&lt;p&gt;PGO sounds mechanical, but the quality of the result depends on the quality of the profile and the measurement plan.&lt;&#x2F;p&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Trap&lt;&#x2F;th&gt;&lt;th&gt;Why it matters&lt;&#x2F;th&gt;&lt;th&gt;Mitigation&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Unrepresentative profile&lt;&#x2F;td&gt;&lt;td&gt;The optimizer improves the behavior you showed it, not the behavior you hoped it would infer.&lt;&#x2F;td&gt;&lt;td&gt;Profile critical user flows, feature-flag combinations, and startup states.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Profile staleness&lt;&#x2F;td&gt;&lt;td&gt;As code changes, old profiles mention removed functions and miss new hot paths.&lt;&#x2F;td&gt;&lt;td&gt;Regenerate profiles automatically in CI, especially before release.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Instrumentation distortion&lt;&#x2F;td&gt;&lt;td&gt;If the instrumented app is much larger or slower, temporal order may differ from release.&lt;&#x2F;td&gt;&lt;td&gt;Prefer low-overhead temporal instrumentation for layout profiles where available.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Speed-vs-size policy&lt;&#x2F;td&gt;&lt;td&gt;Inlining-heavy PGO can improve CPU-bound paths while making mobile startup worse through code growth.&lt;&#x2F;td&gt;&lt;td&gt;Use size-aware optimization settings and measure both binary size and startup&#x2F;page-fault behavior.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Toolchain mismatch&lt;&#x2F;td&gt;&lt;td&gt;Profiles and compilers&#x2F;linkers need compatible formats and versions.&lt;&#x2F;td&gt;&lt;td&gt;Pin toolchain versions and treat profile corruption&#x2F;version errors as infrastructure failures.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Confounded rollout&lt;&#x2F;td&gt;&lt;td&gt;If code changes and optimization changes ship together, impact is hard to isolate.&lt;&#x2F;td&gt;&lt;td&gt;Use a controlled release plan when measuring pure optimization impact.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;
&lt;&#x2F;table&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Tap a trap&lt;&#x2F;h4&gt;
&lt;div class=&quot;choice-grid&quot;&gt;
&lt;button class=&quot;choice&quot; type=&quot;button&quot; onclick=&quot;xpgoTrap(&#x27;profile&#x27;)&quot;&gt;&lt;strong&gt;Bad profile&lt;&#x2F;strong&gt;Tests do not match real behavior.&lt;&#x2F;button&gt;
&lt;button class=&quot;choice&quot; type=&quot;button&quot; onclick=&quot;xpgoTrap(&#x27;stale&#x27;)&quot;&gt;&lt;strong&gt;Stale profile&lt;&#x2F;strong&gt;Code changed after collection.&lt;&#x2F;button&gt;
&lt;button class=&quot;choice&quot; type=&quot;button&quot; onclick=&quot;xpgoTrap(&#x27;rollout&#x27;)&quot;&gt;&lt;strong&gt;Confounded rollout&lt;&#x2F;strong&gt;Code and optimization ship together.&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div id=&quot;xpgo-trap-card&quot; class=&quot;term-card&quot;&gt;Pick a trap to see the failure mode.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;The hard engineering is not &quot;turn on PGO&quot;. The hard engineering is collecting profiles you trust and proving the optimized binary helped.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part11&quot;&gt;Part 11 — Diagnostic reference&lt;&#x2F;h2&gt;
&lt;p&gt;Useful commands and flags to recognize when reading a PGO pipeline:&lt;&#x2F;p&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Thing&lt;&#x2F;th&gt;&lt;th&gt;Role&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-fprofile-generate&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Build an instrumented binary that emits raw profile data.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;LLVM_PROFILE_FILE&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Common runtime environment variable used to control where profile files are written.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;llvm-profdata merge&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Merge one or more &lt;code&gt;.profraw&lt;&#x2F;code&gt; files into a &lt;code&gt;.profdata&lt;&#x2F;code&gt; file consumed by the compiler&#x2F;linker.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-fprofile-use=path&lt;&#x2F;code&gt; &#x2F; &lt;code&gt;-profile-use=path&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Use a merged profile during the optimized build.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-pgo-temporal-instrumentation&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;LLVM temporal profiling instrumentation for ordering-sensitive profile data.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;--compression-sort=both&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;LLD layout mode that balances startup-oriented ordering and compression-oriented ordering.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-Oz&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Optimize for size. Important on mobile because code size can affect page faults and startup.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;PGSO &#x2F; inlining controls&lt;&#x2F;td&gt;&lt;td&gt;Size-aware policy knobs that can mitigate binary growth from PGO-guided inlining.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;pgo-warn-missing-function=false&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Suppress warnings&#x2F;errors when profiles mention functions not present in the final binary. Useful when profiled and release builds are close but not identical.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;
&lt;&#x2F;table&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Command map&lt;&#x2F;h4&gt;
&lt;div class=&quot;term-grid&quot;&gt;
&lt;button class=&quot;active&quot; type=&quot;button&quot; onclick=&quot;xpgoTerm(&#x27;cmd&#x27;,&#x27;generate&#x27;)&quot;&gt;-fprofile-generate&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTerm(&#x27;cmd&#x27;,&#x27;merge&#x27;)&quot;&gt;llvm-profdata merge&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTerm(&#x27;cmd&#x27;,&#x27;use&#x27;)&quot;&gt;-fprofile-use&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTerm(&#x27;cmd&#x27;,&#x27;temporal&#x27;)&quot;&gt;-pgo-temporal-instrumentation&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTerm(&#x27;cmd&#x27;,&#x27;sort&#x27;)&quot;&gt;--compression-sort=both&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div id=&quot;xpgo-cmd-card&quot; class=&quot;term-card&quot;&gt;Build an instrumented binary that emits raw profile data. This is the learning build, not the final release build.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part12&quot;&gt;Part 12 — FAQ&lt;&#x2F;h2&gt;
&lt;details class=&quot;faq&quot;&gt;
&lt;summary&gt;Is PGO the same as test coverage?&lt;&#x2F;summary&gt;
&lt;p&gt;No. Both may use instrumentation, but the goal differs. Test coverage asks &quot;did tests execute this code?&quot; PGO asks &quot;how should the optimizer spend effort based on execution behavior?&quot; A profile can be useful for PGO even if it is not a complete coverage report.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;faq&quot;&gt;
&lt;summary&gt;Does PGO require production user data?&lt;&#x2F;summary&gt;
&lt;p&gt;No. Production profiles can be more representative, but they are operationally harder. Reddit chose UI tests with stable stubs and many important use cases. That is a practical trade-off: less real-world diversity, more repeatability and lower complexity.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;faq&quot;&gt;
&lt;summary&gt;Can PGO make code slower?&lt;&#x2F;summary&gt;
&lt;p&gt;Yes. A stale or biased profile can over-optimize the wrong paths, increase code size, or damage instruction-cache locality. That is why measurement and rollback matter.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;faq&quot;&gt;
&lt;summary&gt;Why does binary layout affect compressed size?&lt;&#x2F;summary&gt;
&lt;p&gt;Compression algorithms such as LZ variants exploit repeated patterns within a window. If similar machine-code sequences are placed near each other, the compressed artifact can shrink.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;faq&quot;&gt;
&lt;summary&gt;Is the app decompressing its IPA during startup?&lt;&#x2F;summary&gt;
&lt;p&gt;No. The compressed IPA matters for download and installation. Startup page faults come from touching installed binary pages that are not resident in memory yet. These are related optimization goals, but not the same mechanism.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;faq&quot;&gt;
&lt;summary&gt;Why not always use speed optimization with PGO?&lt;&#x2F;summary&gt;
&lt;p&gt;Because mobile startup is sensitive to code size. A speed-oriented profile can encourage more inlining, which can increase the amount of code the app has to page in. For mobile, the best policy often balances speed, size, and layout.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;faq&quot;&gt;
&lt;summary&gt;Why is startup impact strongest after install or update?&lt;&#x2F;summary&gt;
&lt;p&gt;Because the first launches touch cold filesystem and memory state. Over time, OS-level caching and background optimizations can reduce the visible difference, so first-day metrics are often the cleanest window.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Quick check&lt;&#x2F;h4&gt;
&lt;div class=&quot;seg&quot; role=&quot;tablist&quot; aria-label=&quot;PGO quiz&quot;&gt;
&lt;button class=&quot;active&quot; type=&quot;button&quot; onclick=&quot;xpgoShow(&#x27;quiz-view&#x27;,&#x27;q&#x27;)&quot;&gt;Question&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoShow(&#x27;quiz-view&#x27;,&#x27;a&#x27;)&quot;&gt;Answer&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div data-xpgo-group=&quot;quiz-view&quot; data-xpgo-panel=&quot;q&quot; class=&quot;panel active&quot;&gt;
&lt;p&gt;If the optimized app has a smaller compressed IPA but the same startup page faults, which half of the optimization probably helped?&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div data-xpgo-group=&quot;quiz-view&quot; data-xpgo-panel=&quot;a&quot; class=&quot;panel&quot;&gt;
&lt;p&gt;&lt;strong&gt;Compression locality.&lt;&#x2F;strong&gt; Startup locality is measured through page faults and time-to-interactive behavior; compressed IPA size is a distribution&#x2F;install win.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part13&quot;&gt;Part 13 — Glossary&lt;&#x2F;h2&gt;
&lt;div class=&quot;iviz&quot;&gt;
&lt;h4&gt;Pick a term&lt;&#x2F;h4&gt;
&lt;div class=&quot;term-grid&quot;&gt;
&lt;button class=&quot;active&quot; type=&quot;button&quot; onclick=&quot;xpgoTerm(&#x27;gloss&#x27;,&#x27;pgo&#x27;)&quot;&gt;PGO&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTerm(&#x27;gloss&#x27;,&#x27;coverage&#x27;)&quot;&gt;Coverage profile&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTerm(&#x27;gloss&#x27;,&#x27;temporal&#x27;)&quot;&gt;Temporal profile&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTerm(&#x27;gloss&#x27;,&#x27;reorder&#x27;)&quot;&gt;Function reordering&lt;&#x2F;button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;xpgoTerm(&#x27;gloss&#x27;,&#x27;fault&#x27;)&quot;&gt;Page fault&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div id=&quot;xpgo-gloss-card&quot; class=&quot;term-card&quot;&gt;A feedback loop: collect execution behavior from an instrumented run, then use it to optimize a later build.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;dl class=&quot;gloss&quot;&gt;
&lt;dt&gt;PGO&lt;&#x2F;dt&gt;
&lt;dd&gt;Profile-Guided Optimization: optimizing a later build using profiles collected from an earlier instrumented run.&lt;&#x2F;dd&gt;
&lt;dt&gt;IRPGO&lt;&#x2F;dt&gt;
&lt;dd&gt;Instrumentation-based PGO at the LLVM IR level. The compiler inserts instrumentation into intermediate representation to collect execution data.&lt;&#x2F;dd&gt;
&lt;dt&gt;Coverage profile&lt;&#x2F;dt&gt;
&lt;dd&gt;A profile describing which functions, branches, and paths ran and how often.&lt;&#x2F;dd&gt;
&lt;dt&gt;Temporal profile&lt;&#x2F;dt&gt;
&lt;dd&gt;A profile describing execution order over time, useful for function layout.&lt;&#x2F;dd&gt;
&lt;dt&gt;Function reordering&lt;&#x2F;dt&gt;
&lt;dd&gt;Changing the order of functions in the final binary so related code is physically close.&lt;&#x2F;dd&gt;
&lt;dt&gt;Page fault&lt;&#x2F;dt&gt;
&lt;dd&gt;An event where the system must bring a memory page into the process because the app touched code or data not currently resident.&lt;&#x2F;dd&gt;
&lt;dt&gt;LLD&lt;&#x2F;dt&gt;
&lt;dd&gt;LLVM&#x27;s linker. In this context, it consumes temporal profiles and performs function layout optimizations.&lt;&#x2F;dd&gt;
&lt;dt&gt;Profile staleness&lt;&#x2F;dt&gt;
&lt;dd&gt;The loss of profile usefulness as source code and behavior drift away from the workload that produced the profile.&lt;&#x2F;dd&gt;
&lt;&#x2F;dl&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;Reddit Engineering: &lt;a href=&quot;https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;RedditEng&#x2F;comments&#x2F;1hgbldp&#x2F;reddits_ios_app_binary_optimization&#x2F;&quot;&gt;Reddit&#x27;s iOS App Binary Optimization&lt;&#x2F;a&gt;, written by Karim Alweheshy.&lt;&#x2F;li&gt;
&lt;li&gt;YouTube &#x2F; LLVM talk: &lt;a href=&quot;https:&#x2F;&#x2F;youtu.be&#x2F;yd4pbSTjwuA&quot;&gt;temporal profiling and balanced partitioning for mobile PGO&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;LLVM Clang Users Manual: &lt;a href=&quot;https:&#x2F;&#x2F;clang.llvm.org&#x2F;docs&#x2F;UsersManual.html#profile-guided-optimization&quot;&gt;Profile Guided Optimization&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;LLVM discourse RFC: &lt;a href=&quot;https:&#x2F;&#x2F;discourse.llvm.org&#x2F;t&#x2F;rfc-temporal-profiling-extension-for-irpgo&#x2F;68068&quot;&gt;Temporal profiling extension for IRPGO&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;script&gt;
function xpgoShow(group, panel) {
  document.querySelectorAll(&#x27;[data-xpgo-group=&quot;&#x27; + group + &#x27;&quot;]&#x27;).forEach(function (el) {
    el.classList.toggle(&#x27;active&#x27;, el.getAttribute(&#x27;data-xpgo-panel&#x27;) === panel);
  });
  document.querySelectorAll(&#x27;[onclick*=&quot;&#x27; + group + &#x27;&quot;]&#x27;).forEach(function (btn) {
    btn.classList.toggle(&#x27;active&#x27;, btn.getAttribute(&#x27;onclick&#x27;).indexOf(&quot;&#x27;&quot; + panel + &quot;&#x27;&quot;) !== -1);
  });
}

function xpgoInline(value) {
  var inline = Number(value);
  var overhead = 100 - inline;
  var pageRisk = Math.min(100, Math.round(inline * 1.2));
  document.getElementById(&#x27;xpgo-inline-label&#x27;).textContent = inline + &#x27;%&#x27;;
  document.getElementById(&#x27;xpgo-overhead&#x27;).style.setProperty(&#x27;--w&#x27;, overhead + &#x27;%&#x27;);
  document.getElementById(&#x27;xpgo-overhead-label&#x27;).textContent = overhead + &#x27;%&#x27;;
  document.getElementById(&#x27;xpgo-codesize&#x27;).style.setProperty(&#x27;--w&#x27;, inline + &#x27;%&#x27;);
  document.getElementById(&#x27;xpgo-codesize-label&#x27;).textContent = inline + &#x27;%&#x27;;
  document.getElementById(&#x27;xpgo-pagerisk&#x27;).style.setProperty(&#x27;--w&#x27;, pageRisk + &#x27;%&#x27;);
  document.getElementById(&#x27;xpgo-pagerisk-label&#x27;).textContent = pageRisk + &#x27;%&#x27;;
}

function xpgoStage(group, step) {
  document.querySelectorAll(&#x27;[data-xpgo-stage-group=&quot;&#x27; + group + &#x27;&quot;] .stage&#x27;).forEach(function (el) {
    el.classList.toggle(&#x27;active&#x27;, Number(el.getAttribute(&#x27;data-xpgo-stage&#x27;)) === step);
  });
  document.querySelectorAll(&quot;[onclick*=\&quot;xpgoStage(&#x27;&quot; + group + &quot;&#x27;\&quot;]&quot;).forEach(function (btn) {
    btn.classList.toggle(&#x27;active&#x27;, btn.getAttribute(&#x27;onclick&#x27;).indexOf(&#x27;,&#x27; + step + &#x27;)&#x27;) !== -1);
  });
  var captions = {
    loop: {
      1: &#x27;The first build is deliberately instrumented; it is not the final app you ship.&#x27;,
      2: &#x27;The run must look like real behavior, because the optimizer will believe it.&#x27;,
      3: &#x27;Raw profile files become a stable .profdata input for the next build.&#x27;,
      4: &#x27;The final build uses evidence to guide compiler and linker choices.&#x27;
    },
    reddit: {
      1: &#x27;The profile starts as UI-test behavior, not as a hand-written order file.&#x27;,
      2: &#x27;Many raw runs are merged so one path does not dominate the release.&#x27;,
      3: &#x27;Coverage data guides compiler choices such as inlining and branch layout.&#x27;,
      4: &#x27;Temporal data guides linker layout so startup functions sit together.&#x27;
    }
  };
  var caption = document.getElementById(&#x27;xpgo-&#x27; + group + &#x27;-caption&#x27;);
  if (caption) caption.textContent = captions[group][step];
}

function xpgoTrace(step) {
  var order = [
    [&#x27;xpgo-stamp-main&#x27;, &#x27;main&#x27;, 1],
    [&#x27;xpgo-stamp-app&#x27;, &#x27;App&#x27;, 2],
    [&#x27;xpgo-stamp-feed&#x27;, &#x27;Feed&#x27;, 3],
    [&#x27;xpgo-stamp-cell&#x27;, &#x27;Cell&#x27;, 4]
  ];
  [&#x27;xpgo-stamp-main&#x27;, &#x27;xpgo-stamp-app&#x27;, &#x27;xpgo-stamp-feed&#x27;, &#x27;xpgo-stamp-cell&#x27;, &#x27;xpgo-stamp-settings&#x27;].forEach(function (id) {
    var el = document.getElementById(id);
    var name = el.textContent.split(&#x2F;\s+&#x2F;)[0];
    el.innerHTML = name + &#x27;&lt;br&gt;0&#x27;;
    el.classList.remove(&#x27;on&#x27;);
  });
  order.slice(0, step).forEach(function (item) {
    var el = document.getElementById(item[0]);
    el.innerHTML = item[1] + &#x27;&lt;br&gt;&#x27; + item[2];
    el.classList.add(&#x27;on&#x27;);
  });
  var captions = [
    &#x27;Before execution, every timestamp is zero. Zero means &quot;not observed in this run.&quot;&#x27;,
    &#x27;main is first, so it receives timestamp 1.&#x27;,
    &#x27;App startup code follows, so it receives timestamp 2.&#x27;,
    &#x27;The feed path appears next in this workload.&#x27;,
    &#x27;The first visible cell is part of the startup trace; Settings remains zero.&#x27;
  ];
  document.getElementById(&#x27;xpgo-trace-caption&#x27;).textContent = captions[step];
}

function xpgoTrap(kind) {
  var text = {
    profile: &#x27;&lt;strong&gt;Failure mode:&lt;&#x2F;strong&gt; the optimizer improves the test fantasy, not the real app. Fix it by profiling critical flows and feature-flag combinations.&#x27;,
    stale: &#x27;&lt;strong&gt;Failure mode:&lt;&#x2F;strong&gt; old profiles miss new hot code and mention deleted functions. Fix it by regenerating profiles in CI before release.&#x27;,
    rollout: &#x27;&lt;strong&gt;Failure mode:&lt;&#x2F;strong&gt; you cannot tell whether PGO helped because code changed too. Fix it with an identical-code optimized release when measuring impact.&#x27;
  };
  document.getElementById(&#x27;xpgo-trap-card&#x27;).innerHTML = text[kind];
}

function xpgoTerm(group, key) {
  var cards = {
    cmd: {
      generate: &#x27;Build an instrumented binary that emits raw profile data. This is the learning build, not the final release build.&#x27;,
      merge: &#x27;Combine multiple .profraw files into one .profdata file so the next build can consume it.&#x27;,
      use: &#x27;Feed the merged profile into the optimized build so compiler decisions use runtime evidence.&#x27;,
      temporal: &#x27;Record first-call timing so the linker can reconstruct function order for startup layout.&#x27;,
      sort: &#x27;Ask the linker to balance startup locality and compression locality when ordering functions.&#x27;
    },
    gloss: {
      pgo: &#x27;A feedback loop: collect execution behavior from an instrumented run, then use it to optimize a later build.&#x27;,
      coverage: &#x27;The &quot;what ran and how often&quot; profile. It mostly guides compiler decisions.&#x27;,
      temporal: &#x27;The &quot;when did it run&quot; profile. It mostly guides linker layout.&#x27;,
      reorder: &#x27;Changing physical function order in the final binary so related code sits close together.&#x27;,
      fault: &#x27;The cost paid when the app touches a code page that is not resident in memory yet.&#x27;
    }
  };
  var card = document.getElementById(&#x27;xpgo-&#x27; + group + &#x27;-card&#x27;);
  if (card) card.textContent = cards[group][key];
  var onclickPrefix = &quot;xpgoTerm(&#x27;&quot; + group + &quot;&#x27;&quot;;
  document.querySelectorAll(&#x27;[onclick*=&quot;&#x27; + onclickPrefix + &#x27;&quot;]&#x27;).forEach(function (btn) {
    btn.classList.toggle(&#x27;active&#x27;, btn.getAttribute(&#x27;onclick&#x27;).indexOf(&quot;&#x27;&quot; + key + &quot;&#x27;&quot;) !== -1);
  });
}
&lt;&#x2F;script&gt;
&lt;&#x2F;div&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>iOS Code Signing, From First Principles</title>
        <published>2026-06-27T00:00:00+00:00</published>
        <updated>2026-06-27T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/ai-learnings/ios-code-signing/"/>
        <id>https://imostfa.github.io/Blog/ai-learnings/ios-code-signing/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/ai-learnings/ios-code-signing/">&lt;p&gt;This write-up was distilled from a real session: wiring a CI pipeline to ship a &lt;strong&gt;ShowCase&lt;&#x2F;strong&gt;
app (a design-system demo app) to &lt;strong&gt;TestFlight&lt;&#x2F;strong&gt;, debugged end-to-end with an AI agent. The release
build failed with &lt;code&gt;no provisioning profile was found named &#x27;match AppStore com.example.designkit.ShowCase&#x27;&lt;&#x2F;code&gt;.
A profile was downloaded to “fix” it — and turned out to be the &lt;em&gt;wrong type&lt;&#x2F;em&gt;. Untangling that one error
required understanding the entire chain: certificates, App IDs, devices, profiles, the four distribution
channels, fastlane match, and where a profile has to physically sit for a build to find it. This document
builds every concept from scratch and ends with a reference you can reuse. Prerequisites: you’ve built an
iOS app once and know what an &lt;code&gt;.ipa&lt;&#x2F;code&gt; roughly is. Nothing more.&lt;&#x2F;p&gt;
&lt;style&gt;
.csign { --pos:#1a7f37; --neg:#b3261e; --warn:#9a6700; }
[data-theme=&quot;dark&quot;] .csign { --pos:#7ee787; --neg:#ff7b72; --warn:#e3b341; }
@media (prefers-color-scheme: dark) { :root:not([data-theme=&quot;light&quot;]) .csign { --pos:#7ee787; --neg:#ff7b72; --warn:#e3b341; } }
.csign .toc-grid { display:grid; grid-template-columns:1fr 1fr; gap:.25rem 2rem; list-style:none; padding:0; margin:.5rem 0 0; }
.csign .toc-grid a { display:block; padding:.15rem 0; }
.csign .cards { display:grid; grid-template-columns:repeat(auto-fit,minmax(260px,1fr)); gap:.75rem; margin:1rem 0; }
.csign .card { background:var(--bg-light); border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1rem; }
.csign .card h4 { margin:.1rem 0 .5rem; font-size:1rem; }
.csign .card p, .csign .card li { font-size:.9rem; margin:.5rem 0; }
.csign .card ul { margin:.25rem 0; padding-left:1.2rem; }
.csign pre { font-size:.8rem; line-height:1.5; overflow-x:auto; }
.csign .err { color:var(--neg); font-weight:700; }
.csign .ok { color:var(--pos); font-weight:700; }
.csign .dim { color:var(--text-light); }
.csign .diagram { border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1.25rem; background:var(--bg-light); margin:1rem 0; }
.csign .dgrid { display:flex; flex-wrap:wrap; gap:.6rem; }
.csign .dbox { border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:.7rem .8rem; background:var(--bg); font-size:.8rem; line-height:1.5; flex:1; min-width:200px; }
.csign .dbox .dtitle { font-weight:700; font-size:.85rem; margin-bottom:.4rem; }
.csign .dbox.big { flex-basis:100%; }
.csign .arrow-down { text-align:center; color:var(--text-light); width:100%; margin:.2rem 0; }
.csign .dcaption { font-size:.85rem; color:var(--text-light); margin-top:.8rem; line-height:1.6; }
.csign .callout { border:1px solid var(--accent); border-left:4px solid var(--accent); border-radius:var(--standard-border-radius); padding:1.1rem 1.25rem; margin:1rem 0; background:var(--bg-light); }
.csign .callout .rule { font-weight:700; font-size:1.02rem; line-height:1.5; margin:.2rem 0; }
.csign .callout .corollary { color:var(--text-light); font-size:.9rem; margin:.6rem 0 0; }
.csign table td, .csign table th { font-size:.86rem; vertical-align:top; }
.csign .pill { display:inline-block; border-radius:999px; padding:0 .55rem; font-size:.7rem; font-weight:700; }
.csign .pill.red { color:var(--neg); border:1px solid var(--neg); }
.csign .pill.green { color:var(--pos); border:1px solid var(--pos); }
.csign .pill.blue { color:var(--link); border:1px solid var(--link); }
.csign .gloss dt { font-weight:700; color:var(--accent); margin-top:.9rem; }
.csign .gloss dd { margin:.15rem 0 0; font-size:.9rem; }
.csign details.faq { border:1px solid var(--border); border-radius:var(--standard-border-radius); margin-bottom:.5rem; padding:.2rem .6rem; }
.csign details.faq summary { cursor:pointer; font-weight:700; padding:.4rem 0; }
.csign details.faq p { font-size:.92rem; margin:.4rem 0; }
@media (max-width:600px) { .csign .toc-grid { grid-template-columns:1fr; } }
&lt;&#x2F;style&gt;
&lt;div class=&quot;csign&quot;&gt;
&lt;nav aria-label=&quot;Contents&quot;&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;&#x2F;h2&gt;
&lt;ol class=&quot;toc-grid&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#part1&quot;&gt;The cast of five&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part2&quot;&gt;Certificates: who you are&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part3&quot;&gt;App IDs &amp;amp; entitlements: what the app is&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part4&quot;&gt;The provisioning profile: the knot&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part5&quot;&gt;Four profile types, two flags&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part6&quot;&gt;Channels: how a build reaches a human&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part7&quot;&gt;fastlane match: signing for teams&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part8&quot;&gt;Where the profile must physically be&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part9&quot;&gt;Case study: the ShowCase failure&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part10&quot;&gt;Diagnostic reference&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part11&quot;&gt;FAQ&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part12&quot;&gt;Glossary&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;nav&gt;
&lt;h2 id=&quot;part1&quot;&gt;Part 1 — The cast of five&lt;&#x2F;h2&gt;
&lt;p&gt;Apple code signing answers one question at install&#x2F;launch time: &lt;em&gt;&quot;Is this exact app allowed to run on this device, and who vouches for it?&quot;&lt;&#x2F;em&gt; Everything else is bookkeeping that supports that check. There are exactly five primitives. Learn what each one &lt;strong&gt;binds&lt;&#x2F;strong&gt; and the rest follows.&lt;&#x2F;p&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;1. Team &#x2F; Account&lt;&#x2F;h4&gt;
&lt;p&gt;Your Apple Developer Program membership, identified by a &lt;strong&gt;Team ID&lt;&#x2F;strong&gt; (e.g. &lt;code&gt;ABCDE12345&lt;&#x2F;code&gt;). Everything below is scoped to one team. Two apps in the same team share certificates; two apps in different accounts share nothing.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;2. Certificate&lt;&#x2F;h4&gt;
&lt;p&gt;Proves &lt;em&gt;who built the app&lt;&#x2F;em&gt;. A public&#x2F;private key pair where Apple signs your public key. &lt;span class=&quot;dim&quot;&gt;Team-scoped, not app-scoped.&lt;&#x2F;span&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;3. App ID&lt;&#x2F;h4&gt;
&lt;p&gt;The app&#x27;s identity — its &lt;strong&gt;bundle id&lt;&#x2F;strong&gt; (&lt;code&gt;com.example.designkit.ShowCase&lt;&#x2F;code&gt;) plus the capabilities it&#x27;s allowed to use.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;4. Device&lt;&#x2F;h4&gt;
&lt;p&gt;A specific iPhone&#x2F;iPad registered by &lt;strong&gt;UDID&lt;&#x2F;strong&gt;. Only matters for two of the four profile types. Capped at 100 per device class per year.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;5. Provisioning profile&lt;&#x2F;h4&gt;
&lt;p&gt;The document that &lt;strong&gt;ties the other four together&lt;&#x2F;strong&gt; and is signed by Apple. This is the piece everyone finds confusing, because it&#x27;s the knot — so we build up to it.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;The mental model: a certificate says &lt;em&gt;who&lt;&#x2F;em&gt;, an App ID says &lt;em&gt;what&lt;&#x2F;em&gt;, devices say &lt;em&gt;where&lt;&#x2F;em&gt;, and a provisioning profile is Apple&#x27;s signed statement that this &lt;em&gt;who + what + where&lt;&#x2F;em&gt; combination is permitted.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part2&quot;&gt;Part 2 — Certificates: who you are&lt;&#x2F;h2&gt;
&lt;p&gt;A signing certificate is a &lt;strong&gt;public&#x2F;private key pair&lt;&#x2F;strong&gt;. You generate the pair locally; the private key never leaves your Keychain. You send a Certificate Signing Request (the public half) to Apple, and Apple hands back a certificate — essentially your public key with Apple&#x27;s signature on it, saying &quot;this key belongs to this team.&quot;&lt;&#x2F;p&gt;
&lt;p&gt;When you &lt;em&gt;sign&lt;&#x2F;em&gt; an app, &lt;code&gt;codesign&lt;&#x2F;code&gt; uses the &lt;strong&gt;private key&lt;&#x2F;strong&gt; to produce a signature over the app bundle. When iOS verifies the app, it checks that signature against the &lt;strong&gt;certificate&lt;&#x2F;strong&gt; embedded (by reference) in the provisioning profile. No private key → you cannot sign. No matching certificate in the profile → the device rejects the signature.&lt;&#x2F;p&gt;
&lt;p&gt;Two kinds matter:&lt;&#x2F;p&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Certificate&lt;&#x2F;th&gt;&lt;th&gt;Purpose&lt;&#x2F;th&gt;&lt;th&gt;Scope&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Apple Development&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Sign debug builds you run from Xcode on your own registered devices.&lt;&#x2F;td&gt;&lt;td&gt;Per-developer (each dev usually has their own).&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Apple Distribution&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Sign everything you ship: Ad-Hoc, TestFlight, App Store.&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;One per team, shared.&lt;&#x2F;strong&gt; The same distribution cert signs every app in the team.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;
&lt;&#x2F;table&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;Certificates are team-scoped, never app-scoped.&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;This is why, in our session, the existing Apple Distribution certificate could sign a brand-new app (&lt;code&gt;com.example.designkit.ShowCase&lt;&#x2F;code&gt;) without anything new — as long as the app lived in the same team. A new bundle id does &lt;em&gt;not&lt;&#x2F;em&gt; require a new certificate. (It &lt;em&gt;does&lt;&#x2F;em&gt; require a new profile — see Part 4.)&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Practical gotcha: Apple limits the number of distribution certificates per account (historically ~2–3). If every engineer and every CI machine generated their own, you&#x27;d exhaust the limit and break each other&#x27;s signing. That problem is exactly what &lt;a href=&quot;#part7&quot;&gt;fastlane match&lt;&#x2F;a&gt; exists to solve.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;part3&quot;&gt;Part 3 — App IDs &amp;amp; entitlements: what the app is&lt;&#x2F;h2&gt;
&lt;p&gt;An &lt;strong&gt;App ID&lt;&#x2F;strong&gt; registers a bundle id with Apple and records which &lt;strong&gt;capabilities&lt;&#x2F;strong&gt; it may use (Push Notifications, Associated Domains, Keychain Sharing, App Groups, …). Two forms:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Explicit&lt;&#x2F;strong&gt;: &lt;code&gt;com.example.designkit.ShowCase&lt;&#x2F;code&gt; — matches exactly one app. Required for capabilities like Push.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Wildcard&lt;&#x2F;strong&gt;: &lt;code&gt;com.example.*&lt;&#x2F;code&gt; — matches a family of apps, but can&#x27;t carry most capabilities.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;Entitlements&lt;&#x2F;strong&gt; are the runtime permissions your app actually requests (in an &lt;code&gt;.entitlements&lt;&#x2F;code&gt; file). The rule the OS enforces: the entitlements baked into your signed app must be a &lt;em&gt;subset&lt;&#x2F;em&gt; of what the App ID enables and what the provisioning profile grants. Ask for an entitlement the profile doesn&#x27;t carry → install fails. This is a common source of &quot;it ran in debug but the release build won&#x27;t install.&quot;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;part4&quot;&gt;Part 4 — The provisioning profile: the knot&lt;&#x2F;h2&gt;
&lt;p&gt;A provisioning profile is a single file (&lt;code&gt;.mobileprovision&lt;&#x2F;code&gt;) that is a &lt;strong&gt;CMS-signed property list&lt;&#x2F;strong&gt;. &quot;Signed&quot; means Apple has cryptographically endorsed its contents — you can&#x27;t edit it. Inside, it bundles:&lt;&#x2F;p&gt;
&lt;div class=&quot;diagram&quot;&gt;
&lt;div class=&quot;dgrid&quot;&gt;
&lt;div class=&quot;dbox big&quot;&gt;&lt;div class=&quot;dtitle&quot;&gt;embedded.mobileprovision — what&#x27;s inside&lt;&#x2F;div&gt;
One or more &lt;strong&gt;certificates&lt;&#x2F;strong&gt; (the allowed signers) · exactly one &lt;strong&gt;App ID&lt;&#x2F;strong&gt; (bundle id + entitlements) · a &lt;strong&gt;device list&lt;&#x2F;strong&gt; (for some types) · the &lt;strong&gt;Team ID&lt;&#x2F;strong&gt; · an &lt;strong&gt;expiry date&lt;&#x2F;strong&gt; · a &lt;strong&gt;UUID&lt;&#x2F;strong&gt; and a human &lt;strong&gt;Name&lt;&#x2F;strong&gt;.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow-down&quot;&gt;↓ at build time&lt;&#x2F;div&gt;
&lt;div class=&quot;dgrid&quot;&gt;
&lt;div class=&quot;dbox&quot;&gt;codesign signs the &lt;code&gt;.app&lt;&#x2F;code&gt; with your &lt;strong&gt;private key&lt;&#x2F;strong&gt;, and copies the profile into the bundle as &lt;code&gt;embedded.mobileprovision&lt;&#x2F;code&gt;.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow-down&quot;&gt;↓ at install &#x2F; launch&lt;&#x2F;div&gt;
&lt;div class=&quot;dgrid&quot;&gt;
&lt;div class=&quot;dbox&quot;&gt;iOS checks: signature matches a cert in the profile? bundle id matches the App ID? device in the list (or channel allows all)? entitlements ⊆ profile? profile not expired? &lt;strong&gt;All must pass.&lt;&#x2F;strong&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;The profile is the contract the device evaluates. It is the only one of the five primitives that travels &lt;em&gt;inside&lt;&#x2F;em&gt; the shipped app.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Two consequences that trip everyone up:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Profiles are bundle-id-scoped.&lt;&#x2F;strong&gt; A profile for &lt;code&gt;com.example.OtherApp&lt;&#x2F;code&gt; can never sign &lt;code&gt;com.example.designkit.ShowCase&lt;&#x2F;code&gt;, even in the same team with the same certificate. A new app always needs a new profile.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Profiles expire&lt;&#x2F;strong&gt; (typically one year). The cert can be valid while the profile is stale, or vice-versa. &quot;It built last quarter&quot; is not evidence it builds today.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;part5&quot;&gt;Part 5 — Four profile types, two flags&lt;&#x2F;h2&gt;
&lt;p&gt;There are four profile types, and you can tell them apart by just &lt;strong&gt;two fields&lt;&#x2F;strong&gt; inside the decoded plist: &lt;code&gt;get-task-allow&lt;&#x2F;code&gt; (is the app debuggable?) and &lt;code&gt;ProvisionedDevices&lt;&#x2F;code&gt; (is there a UDID allowlist?).&lt;&#x2F;p&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Type&lt;&#x2F;th&gt;&lt;th&gt;&lt;code&gt;get-task-allow&lt;&#x2F;code&gt;&lt;&#x2F;th&gt;&lt;th&gt;&lt;code&gt;ProvisionedDevices&lt;&#x2F;code&gt;&lt;&#x2F;th&gt;&lt;th&gt;Signed with&lt;&#x2F;th&gt;&lt;th&gt;Used for&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Development&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td class=&quot;ok&quot;&gt;true&lt;&#x2F;td&gt;&lt;td&gt;yes (your devices)&lt;&#x2F;td&gt;&lt;td&gt;Apple Development&lt;&#x2F;td&gt;&lt;td&gt;Xcode run&#x2F;debug&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Ad-Hoc&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;false&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;yes&lt;&#x2F;strong&gt; (allowlist)&lt;&#x2F;td&gt;&lt;td&gt;Apple Distribution&lt;&#x2F;td&gt;&lt;td&gt;OTA install to listed devices&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;App Store&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;false&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;none&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Apple Distribution&lt;&#x2F;td&gt;&lt;td&gt;TestFlight &amp;amp; App Store&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Enterprise&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;false&lt;&#x2F;td&gt;&lt;td&gt;none, but &lt;code&gt;ProvisionsAllDevices=true&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;In-House Distribution&lt;&#x2F;td&gt;&lt;td&gt;Internal distribution (Apple Enterprise Program only)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;
&lt;&#x2F;table&gt;
&lt;p&gt;The single most useful distinction in practice:&lt;&#x2F;p&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;Ad-Hoc and App Store profiles are &lt;em&gt;both&lt;&#x2F;em&gt; signed with the same distribution certificate and &lt;em&gt;both&lt;&#x2F;em&gt; have &lt;code&gt;get-task-allow=false&lt;&#x2F;code&gt;. They differ in one thing: &lt;strong&gt;Ad-Hoc has a device allowlist, App Store does not.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;That one difference decides everything downstream. A device list means &quot;only these phones.&quot; No device list means &quot;this goes through Apple&#x27;s store infrastructure (TestFlight&#x2F;App Store), which controls distribution instead.&quot; You cannot mix them: an Ad-Hoc profile cannot be uploaded to TestFlight, and an App Store profile cannot sideload onto a random device.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part6&quot;&gt;Part 6 — Channels: how a build reaches a human&lt;&#x2F;h2&gt;
&lt;p&gt;&quot;How do I get the app onto someone&#x27;s phone?&quot; has four answers, and each is locked to a profile type. Choosing the channel &lt;em&gt;is&lt;&#x2F;em&gt; choosing the profile.&lt;&#x2F;p&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;&lt;span class=&quot;pill blue&quot;&gt;Development&lt;&#x2F;span&gt;&lt;&#x2F;h4&gt;
&lt;p&gt;Xcode → your own registered device, with the debugger attached. Devices must be in the profile. For engineers, not testers.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;&lt;span class=&quot;pill red&quot;&gt;Ad-Hoc&lt;&#x2F;span&gt;&lt;&#x2F;h4&gt;
&lt;p&gt;Build an &lt;code&gt;.ipa&lt;&#x2F;code&gt;, host it on an OTA install page (Bitrise public install page, Firebase App Distribution, Diawi…). &lt;strong&gt;Every tester&#x27;s UDID must be registered&lt;&#x2F;strong&gt; and the profile regenerated. Hard cap ~100 devices&#x2F;year&#x2F;class.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;&lt;span class=&quot;pill green&quot;&gt;TestFlight&lt;&#x2F;span&gt;&lt;&#x2F;h4&gt;
&lt;p&gt;Upload an App-Store-signed build to App Store Connect. Testers join by &lt;strong&gt;email &#x2F; group — no UDID&lt;&#x2F;strong&gt;. Up to 100 internal + 10,000 external testers (external needs a light review).&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;&lt;span class=&quot;pill green&quot;&gt;App Store&lt;&#x2F;span&gt;&lt;&#x2F;h4&gt;
&lt;p&gt;Same App Store profile, but submitted for full App Review and released publicly.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;If the destination is TestFlight, the profile must be App Store. Full stop.&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;This is the crux of the whole session. The goal was &quot;designers install the ShowCase app via TestFlight.&quot; TestFlight only accepts App-Store-signed builds, so Ad-Hoc was never an option — even though Ad-Hoc &lt;em&gt;feels&lt;&#x2F;em&gt; simpler. The payoff for App Store signing is huge for this use case: &lt;strong&gt;zero UDID management&lt;&#x2F;strong&gt;. Designers come and go; nobody wants to register and re-register phones.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part7&quot;&gt;Part 7 — fastlane match: signing for teams&lt;&#x2F;h2&gt;
&lt;p&gt;Everything above works for one developer on one Mac. It falls apart for a team + CI, for two reasons: (1) Apple limits distribution certificates, so you can&#x27;t let everyone generate their own; (2) certificates depend on a &lt;em&gt;private key&lt;&#x2F;em&gt; that lives in one Keychain — a CI machine that didn&#x27;t generate the key can&#x27;t sign at all.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.fastlane.tools&#x2F;actions&#x2F;match&#x2F;&quot;&gt;fastlane &lt;strong&gt;match&lt;&#x2F;strong&gt;&lt;&#x2F;a&gt; solves this with one idea: &lt;strong&gt;store the shared distribution certificate (including its private key) and all the provisioning profiles in a private, encrypted git repository.&lt;&#x2F;strong&gt; Every machine — laptops and CI — runs &lt;code&gt;match&lt;&#x2F;code&gt; to &lt;em&gt;download and install&lt;&#x2F;em&gt; the same identity instead of creating new ones.&lt;&#x2F;p&gt;
&lt;div class=&quot;diagram&quot;&gt;
&lt;div class=&quot;dgrid&quot;&gt;
&lt;div class=&quot;dbox&quot;&gt;Private repo &lt;code&gt;ios-match&lt;&#x2F;code&gt;&lt;br&gt;&lt;span class=&quot;dim&quot;&gt;encrypted .p12 (cert+key) + .mobileprovision files&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox&quot;&gt;→ &lt;code&gt;fastlane match&lt;&#x2F;code&gt; on any machine →&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox&quot;&gt;Keychain gets the cert+key&lt;br&gt;profiles land in &lt;code&gt;~&#x2F;Library&#x2F;MobileDevice&#x2F;Provisioning&amp;nbsp;Profiles&#x2F;&lt;&#x2F;code&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;One source of truth, encrypted with a passphrase. New CI box? Run match, you&#x27;re signing in seconds.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;match also imposes a &lt;strong&gt;naming convention&lt;&#x2F;strong&gt; on the profiles it creates: &lt;code&gt;match {Type} {bundleid}&lt;&#x2F;code&gt; — e.g. &lt;code&gt;match AppStore com.example.designkit.ShowCase&lt;&#x2F;code&gt;. That exact string becomes the profile&#x27;s &lt;em&gt;Name&lt;&#x2F;em&gt;, which matters in the next part. A subtle trap we hit: the &lt;code&gt;app_identifier&lt;&#x2F;code&gt; list in the &lt;code&gt;Matchfile&lt;&#x2F;code&gt; is only a &lt;em&gt;default&lt;&#x2F;em&gt;; if your Fastfile lanes call &lt;code&gt;match(app_identifier: [...])&lt;&#x2F;code&gt; with an explicit list, that list wins and the Matchfile is ignored. Adding a new bundle id means editing the lane, not just the Matchfile.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;part8&quot;&gt;Part 8 — Where the profile must physically be&lt;&#x2F;h2&gt;
&lt;p&gt;Creating a profile in the portal (or in the match repo) is not enough. The signing tool has to &lt;em&gt;find&lt;&#x2F;em&gt; it on the machine doing the build. There is one canonical location:&lt;&#x2F;p&gt;
&lt;pre&gt;~&#x2F;Library&#x2F;MobileDevice&#x2F;Provisioning Profiles&#x2F;*.mobileprovision&lt;&#x2F;pre&gt;
&lt;p&gt;How it gets there, and how a build refers to it, depends on the toolchain:&lt;&#x2F;p&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Toolchain&lt;&#x2F;th&gt;&lt;th&gt;How it finds the profile&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Xcode (automatic)&lt;&#x2F;td&gt;&lt;td&gt;Manages and installs profiles for you, matching by bundle id + capabilities.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;fastlane match&lt;&#x2F;td&gt;&lt;td&gt;Downloads from the encrypted repo into the folder above.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Bitrise CI&lt;&#x2F;td&gt;&lt;td&gt;The &lt;code&gt;certificate-and-profile-installer&lt;&#x2F;code&gt; step installs whatever profiles&#x2F;certs are uploaded to the Bitrise app&#x27;s &quot;Code Signing &amp;amp; Files&quot; store into that folder.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Bazel (rules_apple &lt;code&gt;local_provisioning_profile&lt;&#x2F;code&gt;)&lt;&#x2F;td&gt;&lt;td&gt;Searches the folder above &lt;strong&gt;by the profile&#x27;s Name field&lt;&#x2F;strong&gt; — not by bundle id, not by file path.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;
&lt;&#x2F;table&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;Bazel resolves the profile by &lt;em&gt;name&lt;&#x2F;em&gt;, so the name in your BUILD config must equal the &lt;em&gt;Name&lt;&#x2F;em&gt; field inside the actual installed profile.&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;Our build config asked for &lt;code&gt;match AppStore com.example.designkit.ShowCase&lt;&#x2F;code&gt; (the match convention). A hand-made portal profile named &lt;code&gt;showcase-adhoc-testflight&lt;&#x2F;code&gt; would not be found even if it were the right type — the names don&#x27;t match. Either name the portal profile to match, or point the build config at the real name.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part9&quot;&gt;Part 9 — Case study: the ShowCase failure&lt;&#x2F;h2&gt;
&lt;p&gt;Now the whole session reads cleanly. The pipeline ran a release (App Store) build of &lt;code&gt;&#x2F;&#x2F;ShowCase:ShowCaseApp&lt;&#x2F;code&gt; and died:&lt;&#x2F;p&gt;
&lt;pre&gt;&lt;span class=&quot;err&quot;&gt;error: no provisioning profile was found named &#x27;match AppStore com.example.designkit.ShowCase&#x27;&lt;&#x2F;span&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Correct and expected: the certificate (team-scoped) was reusable, but the &lt;em&gt;profile&lt;&#x2F;em&gt; (bundle-id-scoped) for this brand-new app didn&#x27;t exist anywhere yet — not in the portal, not in the match repo, not installed on the runner.&lt;&#x2F;p&gt;
&lt;p&gt;A profile was then downloaded to unblock it. The right move before trusting any &lt;code&gt;.mobileprovision&lt;&#x2F;code&gt; is to &lt;strong&gt;decode and inspect it&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
&lt;pre&gt;security cms -D -i showcase-testflight.mobileprovision &amp;gt; &#x2F;tmp&#x2F;p.plist
plutil -extract Name        raw -o - &#x2F;tmp&#x2F;p.plist   &lt;span class=&quot;dim&quot;&gt;# showcase-adhoc-testflight&lt;&#x2F;span&gt;
plutil -extract TeamIdentifier.0 raw -o - &#x2F;tmp&#x2F;p.plist &lt;span class=&quot;dim&quot;&gt;# ABCDE12345  ✅&lt;&#x2F;span&gt;
plutil -extract Entitlements.get-task-allow raw -o - &#x2F;tmp&#x2F;p.plist &lt;span class=&quot;dim&quot;&gt;# false&lt;&#x2F;span&gt;
plutil -extract ProvisionedDevices raw -o - &#x2F;tmp&#x2F;p.plist &lt;span class=&quot;dim&quot;&gt;# 69  ← devices!&lt;&#x2F;span&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Team ✅, bundle id ✅ — but &lt;code&gt;get-task-allow=false&lt;&#x2F;code&gt; &lt;strong&gt;and&lt;&#x2F;strong&gt; a 69-device &lt;code&gt;ProvisionedDevices&lt;&#x2F;code&gt; list. Cross-reference the table in Part 5: that is an &lt;strong&gt;Ad-Hoc&lt;&#x2F;strong&gt; profile, despite the word &quot;testflight&quot; in its filename. App Store Connect would reject it. The fix wasn&#x27;t to install it — it was to go back to the portal and create the &lt;em&gt;App Store&lt;&#x2F;em&gt; distribution type (Distribution → App Store Connect, no device selection) for the same App ID.&lt;&#x2F;p&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;A profile&#x27;s filename is a human label and lies freely. Its &lt;em&gt;type&lt;&#x2F;em&gt; lives in two fields you must decode to see.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part10&quot;&gt;Part 10 — Diagnostic reference&lt;&#x2F;h2&gt;
&lt;p&gt;Keep these. They turn &quot;signing is magic&quot; into &quot;signing is a plist I can read.&quot;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Decode any profile to readable XML:&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre&gt;security cms -D -i Some.mobileprovision        &lt;span class=&quot;dim&quot;&gt;# prints the plist&lt;&#x2F;span&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Identify a profile&#x27;s type&lt;&#x2F;strong&gt; (run after piping the decode to a file, e.g. &lt;code&gt;&#x2F;tmp&#x2F;p.plist&lt;&#x2F;code&gt;):&lt;&#x2F;p&gt;
&lt;pre&gt;plutil -extract Name              raw -o - &#x2F;tmp&#x2F;p.plist
plutil -extract AppIDName         raw -o - &#x2F;tmp&#x2F;p.plist
plutil -extract TeamIdentifier.0  raw -o - &#x2F;tmp&#x2F;p.plist
plutil -extract Entitlements.application-identifier raw -o - &#x2F;tmp&#x2F;p.plist
plutil -extract Entitlements.get-task-allow         raw -o - &#x2F;tmp&#x2F;p.plist
plutil -extract ExpirationDate    raw -o - &#x2F;tmp&#x2F;p.plist
plutil -extract ProvisionedDevices raw -o - &#x2F;tmp&#x2F;p.plist  &lt;span class=&quot;dim&quot;&gt;# errors&#x2F;absent ⇒ App Store; a number ⇒ Ad-Hoc&#x2F;Dev&lt;&#x2F;span&gt;
plutil -extract ProvisionsAllDevices raw -o - &#x2F;tmp&#x2F;p.plist &lt;span class=&quot;dim&quot;&gt;# true ⇒ Enterprise&lt;&#x2F;span&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;List what&#x27;s installed on this machine:&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre&gt;ls -1 ~&#x2F;Library&#x2F;MobileDevice&#x2F;Provisioning\ Profiles&#x2F;
&lt;span class=&quot;dim&quot;&gt;# then decode each to read its Name&lt;&#x2F;span&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Inspect signing certificates in your Keychain:&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre&gt;security find-identity -v -p codesigning   &lt;span class=&quot;dim&quot;&gt;# lists usable signing identities&lt;&#x2F;span&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;See how an already-built app was signed:&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre&gt;codesign -dvvv --entitlements :- &#x2F;path&#x2F;to&#x2F;Your.app&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;part11&quot;&gt;FAQ&lt;&#x2F;h2&gt;
&lt;details class=&quot;faq&quot;&gt;&lt;summary&gt;I added a new app to the same team. Do I need a new certificate?&lt;&#x2F;summary&gt;
&lt;p&gt;&lt;strong&gt;No.&lt;&#x2F;strong&gt; Certificates are team-scoped. The existing Apple Distribution certificate signs any app in the team. You &lt;em&gt;do&lt;&#x2F;em&gt; need a new App ID and a new provisioning profile, because both are bundle-id-scoped.&lt;&#x2F;p&gt;&lt;&#x2F;details&gt;
&lt;details class=&quot;faq&quot;&gt;&lt;summary&gt;Why can&#x27;t I just use my Ad-Hoc profile for TestFlight? It&#x27;s distribution-signed.&lt;&#x2F;summary&gt;
&lt;p&gt;TestFlight distribution is handled by Apple&#x27;s store infrastructure, which requires an App Store profile (no device list). An Ad-Hoc profile carries a UDID allowlist and is meant for direct OTA installs; App Store Connect rejects it on upload.&lt;&#x2F;p&gt;&lt;&#x2F;details&gt;
&lt;details class=&quot;faq&quot;&gt;&lt;summary&gt;The build says it can&#x27;t find a profile, but I can see it in the Apple portal.&lt;&#x2F;summary&gt;
&lt;p&gt;Existing in the portal ≠ installed on the build machine. It must be downloaded into &lt;code&gt;~&#x2F;Library&#x2F;MobileDevice&#x2F;Provisioning Profiles&#x2F;&lt;&#x2F;code&gt; (via Xcode, fastlane match, or a CI installer step). And if you build with Bazel, the profile&#x27;s &lt;em&gt;Name&lt;&#x2F;em&gt; must match what your BUILD config asks for.&lt;&#x2F;p&gt;&lt;&#x2F;details&gt;
&lt;details class=&quot;faq&quot;&gt;&lt;summary&gt;Development vs Ad-Hoc — both have my devices. What&#x27;s the difference?&lt;&#x2F;summary&gt;
&lt;p&gt;&lt;code&gt;get-task-allow&lt;&#x2F;code&gt;. Development profiles set it &lt;code&gt;true&lt;&#x2F;code&gt; (the app is debuggable and signed with a &lt;em&gt;development&lt;&#x2F;em&gt; cert); Ad-Hoc sets it &lt;code&gt;false&lt;&#x2F;code&gt; (release behaviour, signed with the &lt;em&gt;distribution&lt;&#x2F;em&gt; cert). Same device list, different runtime posture and certificate.&lt;&#x2F;p&gt;&lt;&#x2F;details&gt;
&lt;details class=&quot;faq&quot;&gt;&lt;summary&gt;How long do these last?&lt;&#x2F;summary&gt;
&lt;p&gt;Provisioning profiles typically expire after one year; certificates similarly. They expire independently, so always check &lt;code&gt;ExpirationDate&lt;&#x2F;code&gt; rather than assuming &quot;it worked before.&quot;&lt;&#x2F;p&gt;&lt;&#x2F;details&gt;
&lt;h2 id=&quot;part12&quot;&gt;Glossary&lt;&#x2F;h2&gt;
&lt;dl class=&quot;gloss&quot;&gt;
&lt;dt&gt;Team ID&lt;&#x2F;dt&gt;&lt;dd&gt;Identifier for your Apple Developer account (e.g. &lt;code&gt;ABCDE12345&lt;&#x2F;code&gt;). The scope for certificates.&lt;&#x2F;dd&gt;
&lt;dt&gt;Certificate&lt;&#x2F;dt&gt;&lt;dd&gt;A public&#x2F;private key pair endorsed by Apple, proving who signed an app. Development (per-dev) or Distribution (team-shared).&lt;&#x2F;dd&gt;
&lt;dt&gt;Private key&lt;&#x2F;dt&gt;&lt;dd&gt;The secret half of a certificate, kept in the Keychain. Without it you cannot sign — which is why CI needs it shared (see match).&lt;&#x2F;dd&gt;
&lt;dt&gt;App ID&lt;&#x2F;dt&gt;&lt;dd&gt;A registered bundle id plus its allowed capabilities. Explicit or wildcard.&lt;&#x2F;dd&gt;
&lt;dt&gt;Bundle id&lt;&#x2F;dt&gt;&lt;dd&gt;The app&#x27;s unique reverse-DNS identifier, e.g. &lt;code&gt;com.example.designkit.ShowCase&lt;&#x2F;code&gt;.&lt;&#x2F;dd&gt;
&lt;dt&gt;Entitlements&lt;&#x2F;dt&gt;&lt;dd&gt;Runtime permissions the app requests; must be a subset of what the App ID and profile grant.&lt;&#x2F;dd&gt;
&lt;dt&gt;UDID&lt;&#x2F;dt&gt;&lt;dd&gt;Unique identifier of a physical device. Listed in Development and Ad-Hoc profiles.&lt;&#x2F;dd&gt;
&lt;dt&gt;Provisioning profile&lt;&#x2F;dt&gt;&lt;dd&gt;Apple-signed file binding certificate(s) + App ID + (maybe) devices + entitlements + team. Embedded in the shipped app as &lt;code&gt;embedded.mobileprovision&lt;&#x2F;code&gt;.&lt;&#x2F;dd&gt;
&lt;dt&gt;get-task-allow&lt;&#x2F;dt&gt;&lt;dd&gt;Entitlement flag; &lt;code&gt;true&lt;&#x2F;code&gt; = debuggable (Development), &lt;code&gt;false&lt;&#x2F;code&gt; = release (Ad-Hoc &#x2F; App Store).&lt;&#x2F;dd&gt;
&lt;dt&gt;ProvisionedDevices&lt;&#x2F;dt&gt;&lt;dd&gt;The UDID allowlist inside a profile. Present ⇒ Development or Ad-Hoc. Absent ⇒ App Store.&lt;&#x2F;dd&gt;
&lt;dt&gt;Ad-Hoc&lt;&#x2F;dt&gt;&lt;dd&gt;Distribution profile with a device allowlist; for direct OTA installs, not TestFlight.&lt;&#x2F;dd&gt;
&lt;dt&gt;App Store profile&lt;&#x2F;dt&gt;&lt;dd&gt;Distribution profile with no device list; required for TestFlight and the App Store.&lt;&#x2F;dd&gt;
&lt;dt&gt;TestFlight&lt;&#x2F;dt&gt;&lt;dd&gt;Apple&#x27;s beta distribution service. Accepts App-Store-signed builds; testers join by email, no UDID.&lt;&#x2F;dd&gt;
&lt;dt&gt;fastlane match&lt;&#x2F;dt&gt;&lt;dd&gt;Stores the shared certificate and profiles in an encrypted git repo so all machines install the same signing identity. Names profiles &lt;code&gt;match {Type} {bundleid}&lt;&#x2F;code&gt;.&lt;&#x2F;dd&gt;
&lt;dt&gt;codesign&lt;&#x2F;dt&gt;&lt;dd&gt;The macOS tool that signs an app bundle with your private key and embeds the profile.&lt;&#x2F;dd&gt;
&lt;dt&gt;local_provisioning_profile&lt;&#x2F;dt&gt;&lt;dd&gt;The rules_apple Bazel rule that finds an installed profile by its Name field.&lt;&#x2F;dd&gt;
&lt;&#x2F;dl&gt;
&lt;&#x2F;div&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Mean, Median, and Percentiles, From First Principles</title>
        <published>2026-06-14T00:00:00+00:00</published>
        <updated>2026-06-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/ai-learnings/mean-median-percentiles/"/>
        <id>https://imostfa.github.io/Blog/ai-learnings/mean-median-percentiles/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/ai-learnings/mean-median-percentiles/">&lt;p&gt;This write-up was distilled from a real session reading a build dashboard: three numbers —
mean &lt;strong&gt;11.2 min&lt;&#x2F;strong&gt;, P50 &lt;strong&gt;8.35 min&lt;&#x2F;strong&gt;, P90 &lt;strong&gt;19.8 min&lt;&#x2F;strong&gt; — and one question, &lt;em&gt;do these even make sense?&lt;&#x2F;em&gt;
The document below builds every concept needed to answer it from the ground up: what a mean and a
median actually measure, why latency is always uneven, what percentiles add, and how to sanity-check
a set of summary numbers in seconds. Prerequisites: you can add and divide. Nothing more is assumed.&lt;&#x2F;p&gt;
&lt;style&gt;
.xstat { --xpos:#1a7f37; --xneg:#b3261e; --xwarn:#9a6700; --xmean:#9a6700; --xp50:#0969da; --xp90:#b3261e; }
[data-theme=&quot;dark&quot;] .xstat { --xpos:#7ee787; --xneg:#ff7b72; --xwarn:#e3b341; --xmean:#e3b341; --xp50:#79c0ff; --xp90:#ff7b72; }
@media (prefers-color-scheme: dark) { :root:not([data-theme=&quot;light&quot;]) .xstat { --xpos:#7ee787; --xneg:#ff7b72; --xwarn:#e3b341; --xmean:#e3b341; --xp50:#79c0ff; --xp90:#ff7b72; } }
.xstat .toc-grid { display:grid; grid-template-columns:1fr 1fr; gap:.25rem 2rem; list-style:none; padding:0; margin:.5rem 0 0; }
.xstat .toc-grid a { display:block; padding:.15rem 0; }
.xstat .cards { display:grid; grid-template-columns:repeat(auto-fit,minmax(270px,1fr)); gap:.75rem; margin:1rem 0; }
.xstat .card { background:var(--bg-light); border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1rem; }
.xstat .card h4 { margin:.1rem 0 .5rem; font-size:1rem; }
.xstat .card p, .xstat .card li { font-size:.9rem; margin:.5rem 0; }
.xstat .card ul { margin:.25rem 0; padding-left:1.25rem; }
.xstat pre { font-size:.8rem; line-height:1.5; overflow-x:auto; }
.xstat .err { color:var(--xneg); font-weight:700; }
.xstat .ok { color:var(--xpos); }
.xstat .dim { color:var(--text-light); }
.xstat .mono { font-family:var(--mono-font); }
.xstat .stat-row { display:grid; grid-template-columns:repeat(auto-fit,minmax(180px,1fr)); gap:.75rem; margin:1rem 0; }
.xstat .stat-box { border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1rem; background:var(--bg-light); border-top:3px solid var(--accent); }
.xstat .stat-box.b-p50 { border-top-color:var(--xp50); }
.xstat .stat-box.b-mean { border-top-color:var(--xmean); }
.xstat .stat-box.b-p90 { border-top-color:var(--xp90); }
.xstat .stat-box .label { font-size:.72rem; font-weight:700; letter-spacing:.06em; text-transform:uppercase; color:var(--text-light); }
.xstat .stat-box .value { font-size:1.9rem; font-weight:800; line-height:1.1; margin:.2rem 0; }
.xstat .stat-box .value .u { font-size:.9rem; font-weight:500; color:var(--text-light); }
.xstat .stat-box.b-p50 .value { color:var(--xp50); }
.xstat .stat-box.b-mean .value { color:var(--xmean); }
.xstat .stat-box.b-p90 .value { color:var(--xp90); }
.xstat .stat-box .sub { font-size:.82rem; color:var(--text-light); }
.xstat .mode-toggle { display:inline-flex; gap:.25rem; border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:.25rem; margin:1rem 0 .75rem; background:var(--bg-light); }
.xstat .mode-toggle button { font-family:var(--mono-font); font-size:.9rem; border:none; border-radius:.25rem; padding:.4rem 1rem; cursor:pointer; background:transparent; color:var(--text-light); }
.xstat .mode-toggle button.active { background:var(--accent); color:var(--accent-text); }
.xstat .diagram { border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1.25rem; background:var(--bg-light); }
.xstat .chart-wrap { position:relative; height:340px; margin:.5rem 0 1rem; }
.xstat .dcaption { font-size:.85rem; color:var(--text-light); margin-top:.4rem; line-height:1.6; }
.xstat .callout { border:1px solid var(--accent); border-left:4px solid var(--accent); border-radius:var(--standard-border-radius); padding:1.25rem; margin:1rem 0; background:var(--bg-light); }
.xstat .callout .rule { font-weight:700; font-size:1.05rem; line-height:1.55; margin:.25rem 0; }
.xstat .callout .corollary { color:var(--text-light); font-size:.92rem; margin:.75rem 0 0; }
.xstat .check { border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1rem 1.1rem; margin:.6rem 0; }
.xstat .check.pass { border-left:4px solid var(--xpos); }
.xstat .check.watch { border-left:4px solid var(--xwarn); }
.xstat .check h4 { margin:.1rem 0 .35rem; }
.xstat .check h4 .badge { font-size:.7rem; font-weight:700; letter-spacing:.05em; text-transform:uppercase; padding:.05rem .5rem; border-radius:999px; margin-left:.5rem; }
.xstat .check.pass .badge { color:var(--xpos); border:1px solid var(--xpos); }
.xstat .check.watch .badge { color:var(--xwarn); border:1px solid var(--xwarn); }
.xstat .check p { font-size:.92rem; margin:.3rem 0; }
.xstat .pill-formula { display:inline-block; font-family:var(--mono-font); font-size:.82rem; background:var(--bg); border:1px solid var(--border); border-radius:.3rem; padding:.05rem .45rem; }
.xstat .lesson { border-left:3px solid var(--xwarn); padding:.5rem .8rem; margin-top:1rem; font-size:.88rem; background:var(--bg); border-radius:0 .25rem .25rem 0; }
.xstat .lesson strong { color:var(--xwarn); }
.xstat .q { font-size:.75rem; font-weight:700; letter-spacing:.06em; text-transform:uppercase; color:var(--accent); margin:1rem 0 .25rem; }
.xstat .gloss dt { font-weight:700; color:var(--accent); margin-top:.9rem; }
.xstat .gloss dd { margin:0.15rem 0 0 0; font-size:.9rem; }
.xstat table td, .xstat table th { font-size:.88rem; vertical-align:top; }
.xstat details { margin-bottom:.6rem; }
.xstat details summary { cursor:pointer; font-weight:700; }
.xstat details p { font-size:.92rem; }
&#x2F;* ===== static HTML&#x2F;CSS math (no KaTeX, no JS, themes via currentColor) ===== *&#x2F;
.xstat .m { font-family:Georgia,&#x27;Times New Roman&#x27;,serif; font-style:italic; white-space:nowrap; }
.xstat .m sub, .xstat .m sup { font-style:normal; }
.xstat .rm { font-style:normal; font-family:Georgia,&#x27;Times New Roman&#x27;,serif; }
.xstat .eq { margin:1.1rem 0; text-align:center; font-family:Georgia,&#x27;Times New Roman&#x27;,serif; font-size:1.15rem; overflow-x:auto; overflow-y:hidden; }
.xstat .frac { display:inline-block; vertical-align:-0.55em; text-align:center; margin:0 .15em; }
.xstat .frac &gt; .n { display:block; padding:0 .35em; }
.xstat .frac &gt; .d { display:block; padding:0 .35em; border-top:1px solid currentColor; }
.xstat .cases { display:inline-flex; align-items:center; vertical-align:middle; }
.xstat .cases .brace { font-size:2.6em; line-height:.9; font-weight:300; }
.xstat .cases .rows { display:inline-flex; flex-direction:column; gap:.3em; margin-left:.25em; text-align:left; font-size:.92rem; }
.xstat .cases .row { white-space:nowrap; }
.xstat .under { display:inline-flex; flex-direction:column; align-items:center; margin:0 .35em; vertical-align:middle; }
.xstat .under .ulbl { font-style:normal; font-family:var(--mono-font); font-size:.68rem; letter-spacing:.04em; text-transform:uppercase; color:var(--text-light); margin-top:.25em; }
.xstat figure { margin:0; }
.xstat .xsfig svg, .xstat .skewfig svg { width:100%; height:auto; display:block; }
.xstat [hidden] { display:none !important; }
.xstat .svg-cap { font-size:.82rem; color:var(--text-light); text-align:center; margin-top:.4rem; }
@media (max-width:600px) { .xstat .toc-grid { grid-template-columns:1fr; } .xstat .eq { font-size:1rem; } }
&lt;&#x2F;style&gt;
&lt;div class=&quot;xstat&quot;&gt;
&lt;nav aria-label=&quot;Contents&quot;&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;&#x2F;h2&gt;
&lt;ol class=&quot;toc-grid&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#part1&quot;&gt;Two ways to summarize a list&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part2&quot;&gt;Why latency is uneven&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part3&quot;&gt;Percentiles: P50, P90, P99&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part4&quot;&gt;The shape, drawn&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part5&quot;&gt;Sanity-checking a set of numbers&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part6&quot;&gt;Which number to use when&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part7&quot;&gt;Things people get wrong&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part8&quot;&gt;Glossary&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;nav&gt;
&lt;h2 id=&quot;part1&quot;&gt;Part 1 — Two ways to summarize a list of numbers&lt;&#x2F;h2&gt;
&lt;p&gt;You have many measurements — build durations, request latencies, page loads — and you want one number that says &quot;this is what&#x27;s typical&quot;. There are two common choices, and they answer subtly different questions.&lt;&#x2F;p&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Mean (the average)&lt;&#x2F;h4&gt;
&lt;p&gt;The arithmetic mean of &lt;span class=&quot;m&quot;&gt;n&lt;&#x2F;span&gt; values is&lt;&#x2F;p&gt;
&lt;div class=&quot;eq&quot;&gt;&lt;span class=&quot;m&quot;&gt;x&amp;#772; = &lt;span class=&quot;frac&quot;&gt;&lt;span class=&quot;n&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;d&quot;&gt;n&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; Σ&lt;sub&gt;i=1&lt;&#x2F;sub&gt;&lt;sup&gt;n&lt;&#x2F;sup&gt; x&lt;sub&gt;i&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;p&gt;Every point enters the sum, which is its weakness: a single large &lt;span class=&quot;m&quot;&gt;x&lt;sub&gt;i&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; shifts &lt;span class=&quot;m&quot;&gt;x&amp;#772;&lt;&#x2F;span&gt; by &lt;span class=&quot;m&quot;&gt;Δx&lt;sub&gt;i&lt;&#x2F;sub&gt; &#x2F; n&lt;&#x2F;span&gt;. The mean is the distribution&#x27;s &lt;strong&gt;center of mass&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Median (the middle, a.k.a. P50)&lt;&#x2F;h4&gt;
&lt;p&gt;Sort the values &lt;span class=&quot;m&quot;&gt;x&lt;sub&gt;(1)&lt;&#x2F;sub&gt; ≤ x&lt;sub&gt;(2)&lt;&#x2F;sub&gt; ≤ … ≤ x&lt;sub&gt;(n)&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;; the median is the middle order statistic:&lt;&#x2F;p&gt;
&lt;div class=&quot;eq&quot;&gt;&lt;span class=&quot;m&quot;&gt;x&amp;#771; =&lt;&#x2F;span&gt;&lt;span class=&quot;cases&quot;&gt;&lt;span class=&quot;brace&quot;&gt;{&lt;&#x2F;span&gt;&lt;span class=&quot;rows&quot;&gt;&lt;span class=&quot;row&quot;&gt;&lt;span class=&quot;m&quot;&gt;x&lt;sub&gt;((n+1)&#x2F;2)&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;rm&quot;&gt;,&amp;nbsp; n odd&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;row&quot;&gt;&lt;span class=&quot;m&quot;&gt;½(x&lt;sub&gt;(n&#x2F;2)&lt;&#x2F;sub&gt; + x&lt;sub&gt;(n&#x2F;2+1)&lt;&#x2F;sub&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;rm&quot;&gt;,&amp;nbsp; n even&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;p&gt;It depends only on &lt;em&gt;position&lt;&#x2F;em&gt;, not magnitude — so a single huge value barely moves it.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;The example that shows the difference&lt;&#x2F;h4&gt;
&lt;p&gt;Five builds finish in &lt;span class=&quot;m&quot;&gt;x = (6, 7, 8, 9, 40)&lt;&#x2F;span&gt; minutes (one was a cold-cache straggler):&lt;&#x2F;p&gt;
&lt;div class=&quot;eq&quot;&gt;&lt;span class=&quot;m&quot;&gt;x&amp;#772; = &lt;span class=&quot;frac&quot;&gt;&lt;span class=&quot;n&quot;&gt;6+7+8+9+40&lt;&#x2F;span&gt;&lt;span class=&quot;d&quot;&gt;5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; = &lt;span class=&quot;frac&quot;&gt;&lt;span class=&quot;n&quot;&gt;70&lt;&#x2F;span&gt;&lt;span class=&quot;d&quot;&gt;5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; = &lt;strong style=&quot;color:var(--xneg)&quot;&gt;14&lt;&#x2F;strong&gt;,&amp;nbsp;&amp;nbsp;&amp;nbsp; x&amp;#771; = x&lt;sub&gt;(3)&lt;&#x2F;sub&gt; = &lt;strong style=&quot;color:var(--xpos)&quot;&gt;8&lt;&#x2F;strong&gt;&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;p&gt;Four of five builds finished under 10 minutes, yet &lt;span class=&quot;m&quot;&gt;x&amp;#772; = 14&lt;&#x2F;span&gt; — the lone 40 dragged it up, while &lt;span class=&quot;m&quot;&gt;x&amp;#771; = 8&lt;&#x2F;span&gt; stayed put. &lt;strong&gt;The mean is sensitive to outliers; the median is robust to them.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot; style=&quot;text-align:center;font-family:Georgia,serif;&quot;&gt;&lt;span class=&quot;m&quot;&gt;x&amp;#772; &amp;gt; x&amp;#771;&lt;&#x2F;span&gt; &amp;nbsp;⟺&amp;nbsp; &lt;span class=&quot;rm&quot;&gt;long tail on the high side (right skew)&lt;&#x2F;span&gt;&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;The gap measures the pull. Our build numbers give &lt;span class=&quot;m&quot;&gt;x&amp;#772; − x&amp;#771; = 11.2 − 8.35 = 2.85&lt;&#x2F;span&gt; min, a ratio of &lt;span class=&quot;m&quot;&gt;x&amp;#772; &#x2F; x&amp;#771; = 11.2 &#x2F; 8.35 ≈ 1.34&lt;&#x2F;span&gt; — a slow minority pulling the mean ~34% above the median. The normal signature of timing data (Part 2).&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part2&quot;&gt;Part 2 — Why latency is (almost) always uneven&lt;&#x2F;h2&gt;
&lt;p&gt;Build times, request times, any &quot;how long did it take&quot; metric share a structural property: they are &lt;strong&gt;right-skewed&lt;&#x2F;strong&gt; — a tall cluster of fast results with a long tail trailing off to the right.&lt;&#x2F;p&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;There is a floor, but no ceiling&lt;&#x2F;h4&gt;
&lt;p&gt;A build can&#x27;t take less than zero time, and in practice not less than some minimum (compile the world, link, etc.). But it &lt;em&gt;can&lt;&#x2F;em&gt; take arbitrarily long: a cold cache, a busy machine, a flaky network download, a retry. The fast side is bounded; the slow side is open-ended. That asymmetry &lt;em&gt;is&lt;&#x2F;em&gt; the skew.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Consequence: &lt;span class=&quot;m&quot;&gt;x&amp;#772; ≥ x&amp;#771;&lt;&#x2F;span&gt;, by default&lt;&#x2F;h4&gt;
&lt;p&gt;The high-side tail pulls the center-of-mass while the middle position holds, so for healthy latency you should &lt;em&gt;expect&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;div class=&quot;eq&quot;&gt;&lt;span class=&quot;m&quot;&gt;x&amp;#772; &amp;ge; x&amp;#771;&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;p&gt;It is not a defect to fix — it is the shape of the phenomenon.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;figure class=&quot;skewfig&quot;&gt;
&lt;svg viewBox=&quot;0 0 640 270&quot; role=&quot;img&quot; aria-label=&quot;A right-skewed density curve. A red wall on the left marks a hard floor — builds cannot be faster than a minimum. To the right the curve trails off into a long tail labelled &#x27;no ceiling: cold cache, contention, retries make it arbitrarily slow&#x27;. The median marker sits near the peak and the mean marker sits to its right, with the gap between them labelled as the skew.&quot;&gt;
  &lt;defs&gt;
    &lt;marker id=&quot;xsArrow&quot; markerWidth=&quot;9&quot; markerHeight=&quot;9&quot; refX=&quot;7&quot; refY=&quot;3&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L7,3 L0,6 Z&quot; fill=&quot;var(--text-light)&quot;&#x2F;&gt;&lt;&#x2F;marker&gt;
  &lt;&#x2F;defs&gt;
  &lt;path d=&quot;M70,215 C95,215 150,60 185,58 C235,55 270,150 350,180 C440,206 520,212 610,213 L610,215 L70,215 Z&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.16&quot;&#x2F;&gt;
  &lt;path d=&quot;M70,215 C95,215 150,60 185,58 C235,55 270,150 350,180 C440,206 520,212 610,213&quot; fill=&quot;none&quot; stroke=&quot;var(--accent)&quot; stroke-width=&quot;2.5&quot;&#x2F;&gt;
  &lt;line x1=&quot;70&quot; y1=&quot;215&quot; x2=&quot;620&quot; y2=&quot;215&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1&quot;&#x2F;&gt;
  &lt;line x1=&quot;70&quot; y1=&quot;215&quot; x2=&quot;70&quot; y2=&quot;40&quot; stroke=&quot;var(--xneg)&quot; stroke-width=&quot;2.5&quot;&#x2F;&gt;
  &lt;line x1=&quot;58&quot; y1=&quot;62&quot; x2=&quot;70&quot; y2=&quot;74&quot; stroke=&quot;var(--xneg)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;line x1=&quot;58&quot; y1=&quot;92&quot; x2=&quot;70&quot; y2=&quot;104&quot; stroke=&quot;var(--xneg)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;line x1=&quot;58&quot; y1=&quot;122&quot; x2=&quot;70&quot; y2=&quot;134&quot; stroke=&quot;var(--xneg)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;line x1=&quot;58&quot; y1=&quot;152&quot; x2=&quot;70&quot; y2=&quot;164&quot; stroke=&quot;var(--xneg)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;line x1=&quot;58&quot; y1=&quot;182&quot; x2=&quot;70&quot; y2=&quot;194&quot; stroke=&quot;var(--xneg)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;78&quot; y=&quot;52&quot; font-size=&quot;12&quot; font-weight=&quot;700&quot; fill=&quot;var(--xneg)&quot;&gt;hard floor&lt;&#x2F;text&gt;
  &lt;text x=&quot;78&quot; y=&quot;67&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;can&#x27;t be faster&lt;&#x2F;text&gt;
  &lt;text x=&quot;78&quot; y=&quot;80&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;than the minimum&lt;&#x2F;text&gt;
  &lt;text x=&quot;185&quot; y=&quot;40&quot; font-size=&quot;12&quot; font-weight=&quot;700&quot; fill=&quot;var(--accent)&quot; text-anchor=&quot;middle&quot;&gt;most builds cluster here&lt;&#x2F;text&gt;
  &lt;text x=&quot;500&quot; y=&quot;150&quot; font-size=&quot;12&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;long tail&lt;&#x2F;text&gt;
  &lt;line x1=&quot;360&quot; y1=&quot;240&quot; x2=&quot;625&quot; y2=&quot;240&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.5&quot; marker-end=&quot;url(#xsArrow)&quot;&#x2F;&gt;
  &lt;text x=&quot;358&quot; y=&quot;256&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot;&gt;no ceiling: cold cache, contention, retries &amp;#8594; arbitrarily slow&lt;&#x2F;text&gt;
  &lt;line x1=&quot;212&quot; y1=&quot;215&quot; x2=&quot;212&quot; y2=&quot;95&quot; stroke=&quot;var(--xp50)&quot; stroke-width=&quot;2&quot; stroke-dasharray=&quot;4 3&quot;&#x2F;&gt;
  &lt;text x=&quot;212&quot; y=&quot;88&quot; font-size=&quot;11&quot; font-weight=&quot;700&quot; fill=&quot;var(--xp50)&quot; text-anchor=&quot;middle&quot;&gt;median&lt;&#x2F;text&gt;
  &lt;line x1=&quot;268&quot; y1=&quot;215&quot; x2=&quot;268&quot; y2=&quot;95&quot; stroke=&quot;var(--xmean)&quot; stroke-width=&quot;2&quot; stroke-dasharray=&quot;4 3&quot;&#x2F;&gt;
  &lt;text x=&quot;268&quot; y=&quot;88&quot; font-size=&quot;11&quot; font-weight=&quot;700&quot; fill=&quot;var(--xmean)&quot; text-anchor=&quot;middle&quot;&gt;mean&lt;&#x2F;text&gt;
  &lt;line x1=&quot;212&quot; y1=&quot;201&quot; x2=&quot;268&quot; y2=&quot;201&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.2&quot;&#x2F;&gt;
  &lt;line x1=&quot;212&quot; y1=&quot;197&quot; x2=&quot;212&quot; y2=&quot;205&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.2&quot;&#x2F;&gt;
  &lt;line x1=&quot;268&quot; y1=&quot;197&quot; x2=&quot;268&quot; y2=&quot;205&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.2&quot;&#x2F;&gt;
  &lt;text x=&quot;276&quot; y=&quot;205&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;&amp;#8592; gap = skew&lt;&#x2F;text&gt;
&lt;&#x2F;svg&gt;
&lt;&#x2F;figure&gt;
&lt;p class=&quot;svg-cap&quot;&gt;A hard floor on the left, an open-ended tail on the right: that asymmetry &lt;em&gt;is&lt;&#x2F;em&gt; the right-skew, and it is what pushes the mean to the right of the median.&lt;&#x2F;p&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;Reframe:&lt;&#x2F;strong&gt; the surprising case is &lt;span class=&quot;m&quot;&gt;x&amp;#772; &amp;lt; x&amp;#771;&lt;&#x2F;span&gt;. For a duration metric that almost never happens naturally — if you see it, suspect the metric is computed wrong, units are mixed, or negative&#x2F;clamped values leaked in.&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part3&quot;&gt;Part 3 — Percentiles: P50, P90, P99&lt;&#x2F;h2&gt;
&lt;p&gt;A percentile answers one plain question: &lt;strong&gt;compared to everything else, how far up the list is this?&lt;&#x2F;strong&gt; Sort your builds from fastest to slowest — &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;90&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; is the point that 90% of builds come in &lt;em&gt;under&lt;&#x2F;em&gt;. Only the slowest 10% take longer.&lt;&#x2F;p&gt;
&lt;p&gt;Make it concrete. Here are five build times, in the order they finished: &lt;span class=&quot;mono&quot;&gt;8, 5, 19, 6, 9&lt;&#x2F;span&gt; minutes. Line them up fastest&amp;nbsp;&amp;#8594;&amp;nbsp;slowest:&lt;&#x2F;p&gt;
&lt;div class=&quot;eq&quot;&gt;&lt;span class=&quot;mono&quot;&gt;5&amp;nbsp;&amp;nbsp;&amp;nbsp;6&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;strong style=&quot;color:var(--xp50)&quot;&gt;8&lt;&#x2F;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;9&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;strong style=&quot;color:var(--xp90)&quot;&gt;19&lt;&#x2F;strong&gt;&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong style=&quot;color:var(--xp50)&quot;&gt;middle&lt;&#x2F;strong&gt; value is &lt;strong&gt;8&lt;&#x2F;strong&gt; — that is the &lt;strong&gt;median&lt;&#x2F;strong&gt;, also called &lt;strong&gt;P50&lt;&#x2F;strong&gt;. Half the builds are faster, half are slower. The &quot;typical&quot; build.&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;strong&gt;average&lt;&#x2F;strong&gt; is (5+6+8+9+19) ÷ 5 = &lt;strong&gt;9.4&lt;&#x2F;strong&gt; — bigger than the median, because the one slow &lt;strong style=&quot;color:var(--xp90)&quot;&gt;19&lt;&#x2F;strong&gt; drags it up. That gap between 9.4 and 8 is exactly the problem with averages from Part&amp;nbsp;1.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;P90&lt;&#x2F;strong&gt; is the same move, but at 90% instead of 50%: walk up the sorted list until you have passed 90% of the builds. With lots of data it is &quot;the line that 9 out of 10 builds beat&quot; — it lives out in the slow tail.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Now imagine doing this for hundreds of builds. Walk left&amp;nbsp;&amp;#8594;&amp;nbsp;right through every build time, and each time you pass one build, step up a little. The height at any point is just &lt;em&gt;&quot;what fraction of builds have I passed so far?&quot;&lt;&#x2F;em&gt; — that rising staircase is the picture below. To read any percentile, find its height on the left, slide across to the steps, and drop down to the time.&lt;&#x2F;p&gt;
&lt;figure class=&quot;skewfig&quot;&gt;
&lt;svg viewBox=&quot;0 0 640 290&quot; role=&quot;img&quot; aria-label=&quot;An empirical cumulative distribution function drawn as a rising staircase. The horizontal axis is build time in minutes; the vertical axis is the fraction of samples at or below that time, climbing from 0 to 1 in ten equal steps of one tenth. A dashed blue line at height 0.50 runs across to the staircase and drops down to P50 equals 8 minutes, the median. A dashed red line at height 0.90 runs across and drops to P90 equals 19 minutes, further out in the tail.&quot;&gt;
  &lt;defs&gt;
    &lt;marker id=&quot;xsArrow2&quot; markerWidth=&quot;9&quot; markerHeight=&quot;9&quot; refX=&quot;7&quot; refY=&quot;3&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L7,3 L0,6 Z&quot; fill=&quot;var(--text-light)&quot;&#x2F;&gt;&lt;&#x2F;marker&gt;
  &lt;&#x2F;defs&gt;
  &lt;line x1=&quot;70&quot; y1=&quot;250&quot; x2=&quot;620&quot; y2=&quot;250&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.5&quot; marker-end=&quot;url(#xsArrow2)&quot;&#x2F;&gt;
  &lt;line x1=&quot;70&quot; y1=&quot;250&quot; x2=&quot;70&quot; y2=&quot;32&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.5&quot; marker-end=&quot;url(#xsArrow2)&quot;&#x2F;&gt;
  &lt;text x=&quot;78&quot; y=&quot;30&quot; font-size=&quot;11&quot; font-weight=&quot;700&quot; fill=&quot;var(--text-light)&quot;&gt;F(x) = fraction of builds &amp;#8804; x&lt;&#x2F;text&gt;
  &lt;text x=&quot;64&quot; y=&quot;44&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;end&quot;&gt;1.0&lt;&#x2F;text&gt;
  &lt;text x=&quot;64&quot; y=&quot;65&quot; font-size=&quot;10.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--xp90)&quot; text-anchor=&quot;end&quot;&gt;0.9&lt;&#x2F;text&gt;
  &lt;text x=&quot;64&quot; y=&quot;149&quot; font-size=&quot;10.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--xp50)&quot; text-anchor=&quot;end&quot;&gt;0.5&lt;&#x2F;text&gt;
  &lt;text x=&quot;64&quot; y=&quot;253&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;end&quot;&gt;0&lt;&#x2F;text&gt;
  &lt;text x=&quot;616&quot; y=&quot;268&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;end&quot;&gt;build time (min) &amp;#8594;&lt;&#x2F;text&gt;
  &lt;line x1=&quot;70&quot; y1=&quot;61&quot; x2=&quot;437&quot; y2=&quot;61&quot; stroke=&quot;var(--xp90)&quot; stroke-width=&quot;1.6&quot; stroke-dasharray=&quot;4 3&quot;&#x2F;&gt;
  &lt;line x1=&quot;437&quot; y1=&quot;61&quot; x2=&quot;437&quot; y2=&quot;250&quot; stroke=&quot;var(--xp90)&quot; stroke-width=&quot;1.6&quot; stroke-dasharray=&quot;4 3&quot;&#x2F;&gt;
  &lt;line x1=&quot;70&quot; y1=&quot;145&quot; x2=&quot;200&quot; y2=&quot;145&quot; stroke=&quot;var(--xp50)&quot; stroke-width=&quot;1.6&quot; stroke-dasharray=&quot;4 3&quot;&#x2F;&gt;
  &lt;line x1=&quot;200&quot; y1=&quot;145&quot; x2=&quot;200&quot; y2=&quot;250&quot; stroke=&quot;var(--xp50)&quot; stroke-width=&quot;1.6&quot; stroke-dasharray=&quot;4 3&quot;&#x2F;&gt;
  &lt;path d=&quot;M70,250 L113,250 L113,229 L135,229 L135,208 L156,208 L156,187 L178,187 L178,166 L200,166 L200,145 L221,145 L221,124 L264,124 L264,103 L329,103 L329,82 L437,82 L437,61 L567,61 L567,40 L610,40&quot; fill=&quot;none&quot; stroke=&quot;var(--accent)&quot; stroke-width=&quot;2.5&quot; stroke-linejoin=&quot;round&quot;&#x2F;&gt;
  &lt;circle cx=&quot;113&quot; cy=&quot;229&quot; r=&quot;3.4&quot; fill=&quot;var(--accent)&quot;&#x2F;&gt;
  &lt;circle cx=&quot;135&quot; cy=&quot;208&quot; r=&quot;3.4&quot; fill=&quot;var(--accent)&quot;&#x2F;&gt;
  &lt;circle cx=&quot;156&quot; cy=&quot;187&quot; r=&quot;3.4&quot; fill=&quot;var(--accent)&quot;&#x2F;&gt;
  &lt;circle cx=&quot;178&quot; cy=&quot;166&quot; r=&quot;3.4&quot; fill=&quot;var(--accent)&quot;&#x2F;&gt;
  &lt;circle cx=&quot;200&quot; cy=&quot;145&quot; r=&quot;4.4&quot; fill=&quot;var(--xp50)&quot;&#x2F;&gt;
  &lt;circle cx=&quot;221&quot; cy=&quot;124&quot; r=&quot;3.4&quot; fill=&quot;var(--accent)&quot;&#x2F;&gt;
  &lt;circle cx=&quot;264&quot; cy=&quot;103&quot; r=&quot;3.4&quot; fill=&quot;var(--accent)&quot;&#x2F;&gt;
  &lt;circle cx=&quot;329&quot; cy=&quot;82&quot; r=&quot;3.4&quot; fill=&quot;var(--accent)&quot;&#x2F;&gt;
  &lt;circle cx=&quot;437&quot; cy=&quot;61&quot; r=&quot;4.4&quot; fill=&quot;var(--xp90)&quot;&#x2F;&gt;
  &lt;circle cx=&quot;567&quot; cy=&quot;40&quot; r=&quot;3.4&quot; fill=&quot;var(--accent)&quot;&#x2F;&gt;
  &lt;text x=&quot;200&quot; y=&quot;267&quot; font-size=&quot;11&quot; font-weight=&quot;700&quot; fill=&quot;var(--xp50)&quot; text-anchor=&quot;middle&quot;&gt;P50 = 8&lt;&#x2F;text&gt;
  &lt;text x=&quot;200&quot; y=&quot;280&quot; font-size=&quot;9.5&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;(median)&lt;&#x2F;text&gt;
  &lt;text x=&quot;437&quot; y=&quot;267&quot; font-size=&quot;11&quot; font-weight=&quot;700&quot; fill=&quot;var(--xp90)&quot; text-anchor=&quot;middle&quot;&gt;P90 = 19&lt;&#x2F;text&gt;
&lt;&#x2F;svg&gt;
&lt;&#x2F;figure&gt;
&lt;p class=&quot;svg-cap&quot;&gt;Each build nudges the line up one step. To find &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;90&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;: start at 90% on the left, slide across to the staircase, then drop to the time below it. &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;50&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; is the median; &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;90&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; and &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;99&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; sit further out in the slow tail.&lt;&#x2F;p&gt;
&lt;details&gt;
&lt;summary&gt;The same idea, written as a formula&lt;&#x2F;summary&gt;
&lt;p&gt;Let &lt;span class=&quot;m&quot;&gt;F(x)&lt;&#x2F;span&gt; be the fraction of samples at or below &lt;span class=&quot;m&quot;&gt;x&lt;&#x2F;span&gt; — exactly the height of the staircase above. Then the &lt;span class=&quot;m&quot;&gt;k&lt;&#x2F;span&gt;-th percentile is the smallest &lt;span class=&quot;m&quot;&gt;x&lt;&#x2F;span&gt; whose height has reached &lt;span class=&quot;m&quot;&gt;k&#x2F;100&lt;&#x2F;span&gt;:&lt;&#x2F;p&gt;
&lt;div class=&quot;eq&quot;&gt;&lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;k&lt;&#x2F;sub&gt; = inf { x : F(x) ≥ &lt;span class=&quot;frac&quot;&gt;&lt;span class=&quot;n&quot;&gt;k&lt;&#x2F;span&gt;&lt;span class=&quot;d&quot;&gt;100&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; }&lt;&#x2F;span&gt;,&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class=&quot;m&quot;&gt;x&amp;#771; = P&lt;sub&gt;50&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;p&gt;&quot;&lt;span class=&quot;m&quot;&gt;inf&lt;&#x2F;span&gt;&quot; just means &quot;the smallest value that qualifies&quot; — it only matters for the flat parts of the staircase, where many &lt;span class=&quot;m&quot;&gt;x&lt;&#x2F;span&gt; share the same height.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;P50 — the typical case&lt;&#x2F;h4&gt;
&lt;p&gt;Half of builds are faster than this. The everyday experience. Same thing as the median.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;P90 — the &quot;bad day&quot;&lt;&#x2F;h4&gt;
&lt;p&gt;9 of every 10 builds beat this; the slowest 10% are worse. This is the number that captures &quot;sometimes it just takes forever&quot; without being thrown off by the single worst outlier.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;P99 — the worst-case-ish&lt;&#x2F;h4&gt;
&lt;p&gt;Only 1 in 100 is slower. Matters when rare slowness is expensive (CI queues, user-facing requests at scale). Not in our three numbers, but worth requesting.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;Key property:&lt;&#x2F;strong&gt; percentiles are &lt;em&gt;positions&lt;&#x2F;em&gt; in the sorted data, so — like the median — they are robust to outliers. A single catastrophic build moves P99 a little and P50 not at all. That robustness is exactly why SLOs are written on percentiles, not on the mean.&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part4&quot;&gt;Part 4 — The shape, drawn&lt;&#x2F;h2&gt;
&lt;p&gt;Here is the distribution of build durations. Toggle between a hypothetical &lt;em&gt;symmetric&lt;&#x2F;em&gt; dataset and the &lt;em&gt;skewed&lt;&#x2F;em&gt; one our real numbers describe, and watch the three markers move relative to each other.&lt;&#x2F;p&gt;
&lt;div class=&quot;mode-toggle&quot; role=&quot;tablist&quot; aria-label=&quot;Distribution toggle&quot;&gt;
&lt;button id=&quot;xsBtnSkew&quot; class=&quot;active&quot; onclick=&quot;xstatShow(&#x27;skew&#x27;)&quot; role=&quot;tab&quot; aria-selected=&quot;true&quot;&gt;Skewed (our build data)&lt;&#x2F;button&gt;
&lt;button id=&quot;xsBtnSym&quot; onclick=&quot;xstatShow(&#x27;sym&#x27;)&quot; role=&quot;tab&quot; aria-selected=&quot;false&quot;&gt;Symmetric (for contrast)&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;diagram&quot;&gt;
&lt;figure id=&quot;xsFigSkew&quot; class=&quot;xsfig&quot;&gt;
&lt;svg viewBox=&quot;0 0 640 300&quot; role=&quot;img&quot; aria-label=&quot;Histogram of build durations, right-skewed: most builds fall between 4 and 12 minutes, with a long tail past 25 minutes. The median marker (P50, 8.35) sits left of the mean marker (11.2), and the P90 marker (19.8) sits far out in the tail.&quot;&gt;
&lt;line x1=&quot;44&quot; y1=&quot;248&quot; x2=&quot;612&quot; y2=&quot;248&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1&quot;&#x2F;&gt;
&lt;rect x=&quot;47.9&quot; y=&quot;228.0&quot; width=&quot;35.8&quot; height=&quot;20.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;91.6&quot; y=&quot;188.0&quot; width=&quot;35.8&quot; height=&quot;60.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;135.3&quot; y=&quot;108.0&quot; width=&quot;35.8&quot; height=&quot;140.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;179.0&quot; y=&quot;28.0&quot; width=&quot;35.8&quot; height=&quot;220.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;222.7&quot; y=&quot;68.0&quot; width=&quot;35.8&quot; height=&quot;180.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;266.4&quot; y=&quot;128.0&quot; width=&quot;35.8&quot; height=&quot;120.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;310.1&quot; y=&quot;168.0&quot; width=&quot;35.8&quot; height=&quot;80.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;353.8&quot; y=&quot;198.0&quot; width=&quot;35.8&quot; height=&quot;50.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;397.5&quot; y=&quot;218.0&quot; width=&quot;35.8&quot; height=&quot;30.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;441.2&quot; y=&quot;208.0&quot; width=&quot;35.8&quot; height=&quot;40.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;484.9&quot; y=&quot;218.0&quot; width=&quot;35.8&quot; height=&quot;30.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;528.5&quot; y=&quot;228.0&quot; width=&quot;35.8&quot; height=&quot;20.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;572.2&quot; y=&quot;238.0&quot; width=&quot;35.8&quot; height=&quot;10.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;text x=&quot;44.0&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;0&lt;&#x2F;text&gt;
&lt;text x=&quot;131.4&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;4&lt;&#x2F;text&gt;
&lt;text x=&quot;218.8&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;8&lt;&#x2F;text&gt;
&lt;text x=&quot;306.2&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;12&lt;&#x2F;text&gt;
&lt;text x=&quot;393.5&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;16&lt;&#x2F;text&gt;
&lt;text x=&quot;480.9&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;20&lt;&#x2F;text&gt;
&lt;text x=&quot;524.6&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;25&lt;&#x2F;text&gt;
&lt;text x=&quot;568.3&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;30&lt;&#x2F;text&gt;
&lt;text x=&quot;328&quot; y=&quot;296&quot; font-size=&quot;12&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;build duration (minutes)&lt;&#x2F;text&gt;
&lt;line x1=&quot;226.4&quot; y1=&quot;28&quot; x2=&quot;226.4&quot; y2=&quot;248&quot; stroke=&quot;var(--xp50)&quot; stroke-width=&quot;2&quot; stroke-dasharray=&quot;5 4&quot;&#x2F;&gt;
&lt;rect x=&quot;196.4&quot; y=&quot;21&quot; width=&quot;60&quot; height=&quot;15&quot; rx=&quot;3&quot; fill=&quot;var(--bg-light)&quot;&#x2F;&gt;
&lt;text x=&quot;226.4&quot; y=&quot;32&quot; font-size=&quot;11&quot; font-weight=&quot;700&quot; fill=&quot;var(--xp50)&quot; text-anchor=&quot;middle&quot;&gt;P50 8.35&lt;&#x2F;text&gt;
&lt;line x1=&quot;288.7&quot; y1=&quot;28&quot; x2=&quot;288.7&quot; y2=&quot;248&quot; stroke=&quot;var(--xmean)&quot; stroke-width=&quot;2&quot; stroke-dasharray=&quot;5 4&quot;&#x2F;&gt;
&lt;rect x=&quot;258.7&quot; y=&quot;51&quot; width=&quot;60&quot; height=&quot;15&quot; rx=&quot;3&quot; fill=&quot;var(--bg-light)&quot;&#x2F;&gt;
&lt;text x=&quot;288.7&quot; y=&quot;62&quot; font-size=&quot;11&quot; font-weight=&quot;700&quot; fill=&quot;var(--xmean)&quot; text-anchor=&quot;middle&quot;&gt;Mean 11.2&lt;&#x2F;text&gt;
&lt;line x1=&quot;476.6&quot; y1=&quot;28&quot; x2=&quot;476.6&quot; y2=&quot;248&quot; stroke=&quot;var(--xp90)&quot; stroke-width=&quot;2&quot; stroke-dasharray=&quot;5 4&quot;&#x2F;&gt;
&lt;rect x=&quot;446.6&quot; y=&quot;81&quot; width=&quot;60&quot; height=&quot;15&quot; rx=&quot;3&quot; fill=&quot;var(--bg-light)&quot;&#x2F;&gt;
&lt;text x=&quot;476.6&quot; y=&quot;92&quot; font-size=&quot;11&quot; font-weight=&quot;700&quot; fill=&quot;var(--xp90)&quot; text-anchor=&quot;middle&quot;&gt;P90 19.8&lt;&#x2F;text&gt;
&lt;&#x2F;svg&gt;
&lt;&#x2F;figure&gt;
&lt;figure id=&quot;xsFigSym&quot; class=&quot;xsfig&quot; hidden&gt;
&lt;svg viewBox=&quot;0 0 640 300&quot; role=&quot;img&quot; aria-label=&quot;Histogram of a symmetric distribution: a bell-shaped cluster with no tail. The median marker (P50, 10.0) and the mean marker (10.0) land on the same line, illustrating that for symmetric data the mean and median coincide.&quot;&gt;
&lt;line x1=&quot;44&quot; y1=&quot;248&quot; x2=&quot;612&quot; y2=&quot;248&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1&quot;&#x2F;&gt;
&lt;rect x=&quot;47.9&quot; y=&quot;238.0&quot; width=&quot;35.8&quot; height=&quot;10.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;91.6&quot; y=&quot;218.0&quot; width=&quot;35.8&quot; height=&quot;30.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;135.3&quot; y=&quot;168.0&quot; width=&quot;35.8&quot; height=&quot;80.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;179.0&quot; y=&quot;88.0&quot; width=&quot;35.8&quot; height=&quot;160.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;222.7&quot; y=&quot;28.0&quot; width=&quot;35.8&quot; height=&quot;220.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;266.4&quot; y=&quot;28.0&quot; width=&quot;35.8&quot; height=&quot;220.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;310.1&quot; y=&quot;88.0&quot; width=&quot;35.8&quot; height=&quot;160.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;353.8&quot; y=&quot;168.0&quot; width=&quot;35.8&quot; height=&quot;80.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;397.5&quot; y=&quot;218.0&quot; width=&quot;35.8&quot; height=&quot;30.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;rect x=&quot;441.2&quot; y=&quot;238.0&quot; width=&quot;35.8&quot; height=&quot;10.0&quot; rx=&quot;2&quot; fill=&quot;var(--accent)&quot; opacity=&quot;0.38&quot;&#x2F;&gt;
&lt;text x=&quot;44.0&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;0&lt;&#x2F;text&gt;
&lt;text x=&quot;131.4&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;4&lt;&#x2F;text&gt;
&lt;text x=&quot;218.8&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;8&lt;&#x2F;text&gt;
&lt;text x=&quot;306.2&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;12&lt;&#x2F;text&gt;
&lt;text x=&quot;393.5&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;16&lt;&#x2F;text&gt;
&lt;text x=&quot;480.9&quot; y=&quot;264&quot; font-size=&quot;11&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;20&lt;&#x2F;text&gt;
&lt;text x=&quot;328&quot; y=&quot;296&quot; font-size=&quot;12&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;build duration (minutes)&lt;&#x2F;text&gt;
&lt;line x1=&quot;262.5&quot; y1=&quot;28&quot; x2=&quot;262.5&quot; y2=&quot;248&quot; stroke=&quot;var(--xp50)&quot; stroke-width=&quot;2&quot; stroke-dasharray=&quot;5 4&quot;&#x2F;&gt;
&lt;rect x=&quot;232.5&quot; y=&quot;21&quot; width=&quot;61&quot; height=&quot;15&quot; rx=&quot;3&quot; fill=&quot;var(--bg-light)&quot;&#x2F;&gt;
&lt;text x=&quot;263&quot; y=&quot;32&quot; font-size=&quot;11&quot; font-weight=&quot;700&quot; fill=&quot;var(--xp50)&quot; text-anchor=&quot;middle&quot;&gt;P50 = Mean = 10&lt;&#x2F;text&gt;
&lt;line x1=&quot;380.4&quot; y1=&quot;28&quot; x2=&quot;380.4&quot; y2=&quot;248&quot; stroke=&quot;var(--xp90)&quot; stroke-width=&quot;2&quot; stroke-dasharray=&quot;5 4&quot;&#x2F;&gt;
&lt;rect x=&quot;350.4&quot; y=&quot;81&quot; width=&quot;60&quot; height=&quot;15&quot; rx=&quot;3&quot; fill=&quot;var(--bg-light)&quot;&#x2F;&gt;
&lt;text x=&quot;380.4&quot; y=&quot;92&quot; font-size=&quot;11&quot; font-weight=&quot;700&quot; fill=&quot;var(--xp90)&quot; text-anchor=&quot;middle&quot;&gt;P90 15.4&lt;&#x2F;text&gt;
&lt;&#x2F;svg&gt;
&lt;&#x2F;figure&gt;
&lt;p class=&quot;dcaption&quot; id=&quot;xstatCaption&quot;&gt;Right-skewed: a tall cluster of fast builds and a long tail. The mean (yellow) is dragged right of the median (blue) by that tail; P90 (red) sits far out in it.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;stat-row&quot;&gt;
&lt;div class=&quot;stat-box b-p50&quot;&gt;&lt;div class=&quot;label&quot;&gt;P50 · Median&lt;&#x2F;div&gt;&lt;div class=&quot;value&quot;&gt;8.35&lt;span class=&quot;u&quot;&gt; min&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;&lt;div class=&quot;sub&quot;&gt;The typical build. Half finish faster.&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;stat-box b-mean&quot;&gt;&lt;div class=&quot;label&quot;&gt;Mean · Average&lt;&#x2F;div&gt;&lt;div class=&quot;value&quot;&gt;11.2&lt;span class=&quot;u&quot;&gt; min&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;&lt;div class=&quot;sub&quot;&gt;Pulled up by the slow tail.&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;
&lt;div class=&quot;stat-box b-p90&quot;&gt;&lt;div class=&quot;label&quot;&gt;P90 · Tail&lt;&#x2F;div&gt;&lt;div class=&quot;value&quot;&gt;19.8&lt;span class=&quot;u&quot;&gt; min&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;&lt;div class=&quot;sub&quot;&gt;9 of 10 beat this; slowest 10% are worse.&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part5&quot;&gt;Part 5 — Sanity-checking a set of numbers&lt;&#x2F;h2&gt;
&lt;p&gt;Back to the original question: &lt;em&gt;do mean 11.2, P50 8.35, P90 19.8 make sense?&lt;&#x2F;em&gt; Two quick checks settle it.&lt;&#x2F;p&gt;
&lt;div class=&quot;check pass&quot;&gt;
&lt;h4&gt;Ordering check &lt;span class=&quot;badge&quot;&gt;passes&lt;&#x2F;span&gt;&lt;&#x2F;h4&gt;
&lt;p&gt;For right-skewed latency the only sensible order is &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;50&lt;&#x2F;sub&gt; &amp;lt; x&amp;#772; &amp;lt; P&lt;sub&gt;90&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;. Ours:&lt;&#x2F;p&gt;
&lt;div class=&quot;eq&quot;&gt;&lt;span class=&quot;m&quot;&gt;8.35 &amp;lt; 11.2 &amp;lt; 19.8&lt;&#x2F;span&gt; &amp;nbsp;&lt;span class=&quot;ok&quot; style=&quot;font-style:normal&quot;&gt;✓&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;p&gt;&lt;span class=&quot;m&quot;&gt;x&amp;#772;&lt;&#x2F;span&gt; above the median is the expected fingerprint of skew (Part 2). Had &lt;span class=&quot;m&quot;&gt;x&amp;#772; &amp;lt; 8.35&lt;&#x2F;span&gt;, that would be the red flag worth chasing.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;check watch&quot;&gt;
&lt;h4&gt;Tail check &lt;span class=&quot;badge&quot;&gt;worth a look&lt;&#x2F;span&gt;&lt;&#x2F;h4&gt;
&lt;p&gt;Two dimensionless ratios gauge how uneven the data is:&lt;&#x2F;p&gt;
&lt;div class=&quot;eq&quot;&gt;&lt;span class=&quot;under&quot;&gt;&lt;span class=&quot;m&quot;&gt;&lt;span class=&quot;frac&quot;&gt;&lt;span class=&quot;n&quot;&gt;P&lt;sub&gt;90&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;d&quot;&gt;P&lt;sub&gt;50&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; = &lt;span class=&quot;frac&quot;&gt;&lt;span class=&quot;n&quot;&gt;19.8&lt;&#x2F;span&gt;&lt;span class=&quot;d&quot;&gt;8.35&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; ≈ 2.37&lt;&#x2F;span&gt;&lt;span class=&quot;ulbl&quot;&gt;spread&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class=&quot;under&quot;&gt;&lt;span class=&quot;m&quot;&gt;&lt;span class=&quot;frac&quot;&gt;&lt;span class=&quot;n&quot;&gt;x&amp;#772;&lt;&#x2F;span&gt;&lt;span class=&quot;d&quot;&gt;P&lt;sub&gt;50&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; = &lt;span class=&quot;frac&quot;&gt;&lt;span class=&quot;n&quot;&gt;11.2&lt;&#x2F;span&gt;&lt;span class=&quot;d&quot;&gt;8.35&lt;&#x2F;span&gt;&lt;&#x2F;span&gt; ≈ 1.34&lt;&#x2F;span&gt;&lt;span class=&quot;ulbl&quot;&gt;skew&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;p&gt;The slowest 10% take well over double a typical build. Both ratios sit in the normal range (most real systems land 2–4×), but the tail is where time is actually lost.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;Verdict: the numbers are internally consistent and realistic. They describe an ordinary, healthy distribution — most builds near 8 minutes, an average nudged to ~11 by a slow minority, a &quot;bad day&quot; near 20.&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;The one thing to act on is that 8 → 20 minute tail, not the headline average.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part6&quot;&gt;Part 6 — Which number to use when&lt;&#x2F;h2&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Number&lt;&#x2F;th&gt;&lt;th&gt;Use it for&lt;&#x2F;th&gt;&lt;th&gt;Why&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;P50&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Headline &quot;how fast are builds&quot; &#x2F; daily developer experience&lt;&#x2F;td&gt;&lt;td&gt;Reflects the common case, immune to rare outliers.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;P90 &#x2F; P95&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;SLOs, alerting thresholds, &quot;worst-case&quot; budgeting&lt;&#x2F;td&gt;&lt;td&gt;Catches tail regressions before they become &quot;builds randomly hang&quot; complaints.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;P99 &#x2F; max&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Rare-but-expensive slowness at scale&lt;&#x2F;td&gt;&lt;td&gt;The true worst case lives past P90; request it when the tail is costly.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td class=&quot;dim&quot;&gt;&lt;strong&gt;Mean&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td class=&quot;dim&quot;&gt;Almost nothing, on its own&lt;&#x2F;td&gt;&lt;td class=&quot;dim&quot;&gt;One 60-min build moves it without telling you whether the typical case or the tail changed. Useful only paired with the median as a skew signal.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;
&lt;&#x2F;table&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;General rule:&lt;&#x2F;strong&gt; report a percentile for decisions, keep the mean only as a companion to the median. A dashboard showing the mean alone hides whether your typical build or your tail got worse.&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part7&quot;&gt;Part 7 — Things people get wrong&lt;&#x2F;h2&gt;
&lt;details open&gt;
&lt;summary&gt;&quot;I&#x27;ll just average the percentiles to combine two services.&quot;&lt;&#x2F;summary&gt;
&lt;p&gt;First, what &quot;combine&quot; means here: you have build times measured &lt;em&gt;separately&lt;&#x2F;em&gt; for two things — say a Linux runner pool and a macOS pool, or the &lt;span class=&quot;mono&quot;&gt;app&lt;&#x2F;span&gt; and &lt;span class=&quot;mono&quot;&gt;core&lt;&#x2F;span&gt; targets in a monorepo — and you want one P90 across both. The tempting shortcut is to average the two P90s your dashboard already shows. It doesn&#x27;t work, because a percentile is a &lt;em&gt;position&lt;&#x2F;em&gt; in the sorted data, not a quantity you can add or average.&lt;&#x2F;p&gt;
&lt;p&gt;Counterexample: pool A is ten builds all at &lt;strong&gt;2&amp;nbsp;min&lt;&#x2F;strong&gt; (so &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;90&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; = 2), pool B is ten builds at &lt;strong&gt;10&amp;nbsp;min&lt;&#x2F;strong&gt; (&lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;90&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; = 10). Averaging the two gives 6&amp;nbsp;min — but dump all 20 builds into one pile and the real combined &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;90&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; is &lt;strong style=&quot;color:var(--xp90)&quot;&gt;10&amp;nbsp;min&lt;&#x2F;strong&gt;. Build volume tips it further: if &lt;span class=&quot;mono&quot;&gt;core&lt;&#x2F;span&gt; builds 2,000&amp;times;&#x2F;day and &lt;span class=&quot;mono&quot;&gt;app&lt;&#x2F;span&gt; builds 10&amp;times;&#x2F;day, the combined P90 is essentially &lt;span class=&quot;mono&quot;&gt;core&lt;&#x2F;span&gt;&#x27;s — one pool dominates the pile, so averaging the two numbers as equals is meaningless.&lt;&#x2F;p&gt;
&lt;div class=&quot;eq&quot;&gt;&lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;90&lt;&#x2F;sub&gt;(A ∪ B) ≠ ½(P&lt;sub&gt;90&lt;&#x2F;sub&gt;(A) + P&lt;sub&gt;90&lt;&#x2F;sub&gt;(B))&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;p&gt;You also can&#x27;t recover the mean &lt;span class=&quot;m&quot;&gt;x&amp;#772;&lt;&#x2F;span&gt; from &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;50&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; and &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;90&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;: those say nothing about the top 10%, where the mean&#x27;s weight sits. Two pools with the &lt;em&gt;same&lt;&#x2F;em&gt; P50 and P90 can have wildly different averages — one&#x27;s slowest builds might top out at 25&amp;nbsp;min, another&#x27;s drag out to hours.&lt;&#x2F;p&gt;
&lt;p&gt;Each percentile is its own independent cut of the data. To combine pools or projects, go back to the &lt;strong&gt;raw build durations&lt;&#x2F;strong&gt; (pool them and recompute), or keep a mergeable summary per pool like a &lt;strong&gt;histogram&lt;&#x2F;strong&gt; or &lt;strong&gt;t-digest&lt;&#x2F;strong&gt; and merge those &lt;em&gt;before&lt;&#x2F;em&gt; reading the percentile — never do arithmetic on the finished P90 numbers.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details&gt;
&lt;summary&gt;&quot;Mean and median are basically the same thing.&quot;&lt;&#x2F;summary&gt;
&lt;p&gt;Only for symmetric data (toggle the chart in Part 4 to see them coincide). For anything with a tail — which is all latency — they diverge, and the gap is information: it tells you how skewed the data is.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details&gt;
&lt;summary&gt;&quot;A 2.37× P90&#x2F;P50 ratio looks bad.&quot;&lt;&#x2F;summary&gt;
&lt;p&gt;It&#x27;s typical, not bad. Build and request systems commonly sit between 2× and 4×. A ratio drifting toward 5×+ signals an unstable tail worth investigating; a ratio near 1× would mean unusually consistent timing. 2.37× is normal variability with clear room to tighten.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details&gt;
&lt;summary&gt;&quot;These three numbers are enough.&quot;&lt;&#x2F;summary&gt;
&lt;p&gt;Three things are missing: the &lt;strong&gt;sample count and time window&lt;&#x2F;strong&gt; (11.2 min over 50 builds is far less trustworthy than over 50,000), the &lt;strong&gt;P99 &#x2F; max&lt;&#x2F;strong&gt; (the real worst case lives past P90), and a &lt;strong&gt;trend over time&lt;&#x2F;strong&gt; (a single snapshot can&#x27;t say whether builds are getting faster or slower).&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;h2 id=&quot;part8&quot;&gt;Glossary&lt;&#x2F;h2&gt;
&lt;dl class=&quot;gloss&quot;&gt;
&lt;dt&gt;mean (average)&lt;&#x2F;dt&gt;&lt;dd&gt;&lt;span class=&quot;m&quot;&gt;x&amp;#772; = (1&#x2F;n) Σ&lt;sub&gt;i&lt;&#x2F;sub&gt; x&lt;sub&gt;i&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;. Uses every point; sensitive to outliers. The distribution&#x27;s center of mass.&lt;&#x2F;dd&gt;
&lt;dt&gt;median (P50)&lt;&#x2F;dt&gt;&lt;dd&gt;&lt;span class=&quot;m&quot;&gt;x&amp;#771; = P&lt;sub&gt;50&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;, the middle order statistic of the sorted data — half below, half above. Robust to outliers.&lt;&#x2F;dd&gt;
&lt;dt&gt;percentile (P&lt;em&gt;k&lt;&#x2F;em&gt;)&lt;&#x2F;dt&gt;&lt;dd&gt;&lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;k&lt;&#x2F;sub&gt; = inf{ x : F(x) ≥ k&#x2F;100 }&lt;&#x2F;span&gt;, the value below which &lt;span class=&quot;m&quot;&gt;k&lt;&#x2F;span&gt;% of measurements fall. &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;50&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; is the median.&lt;&#x2F;dd&gt;
&lt;dt&gt;right-skew&lt;&#x2F;dt&gt;&lt;dd&gt;A distribution with a long high-side tail and a low-side bound. Produces &lt;span class=&quot;m&quot;&gt;x&amp;#772; &amp;gt; x&amp;#771;&lt;&#x2F;span&gt;. The natural shape of latency data.&lt;&#x2F;dd&gt;
&lt;dt&gt;tail&lt;&#x2F;dt&gt;&lt;dd&gt;The slow minority of measurements (e.g. the slowest 10%, captured by &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;90&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt;). Where most &quot;it&#x27;s sometimes slow&quot; pain lives.&lt;&#x2F;dd&gt;
&lt;dt&gt;SLO&lt;&#x2F;dt&gt;&lt;dd&gt;Service Level Objective — a target almost always written on a percentile (e.g. &lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;95&lt;&#x2F;sub&gt; &amp;lt; 15&lt;&#x2F;span&gt; min) because percentiles are robust to single outliers.&lt;&#x2F;dd&gt;
&lt;dt&gt;spread &#x2F; skew ratio&lt;&#x2F;dt&gt;&lt;dd&gt;&lt;span class=&quot;m&quot;&gt;P&lt;sub&gt;90&lt;&#x2F;sub&gt;&#x2F;P&lt;sub&gt;50&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; measures tail spread; &lt;span class=&quot;m&quot;&gt;x&amp;#772;&#x2F;P&lt;sub&gt;50&lt;&#x2F;sub&gt;&lt;&#x2F;span&gt; measures skew. Quick gauges of how uneven a distribution is.&lt;&#x2F;dd&gt;
&lt;&#x2F;dl&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;The mean is a balance point and moves with every outlier; the median and other percentiles are positions and ignore magnitude. Latency data has a floor but no ceiling, so it is right-skewed, so its mean sits above its median by default. To sanity-check a set of summary numbers, confirm the ordering (P50 &amp;lt; Mean &amp;lt; P90 for skewed data) and read the gaps as skew. Make decisions on percentiles; keep the mean only as a companion that reveals skew.&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;Build numbers verified consistent: P50 8.35 · Mean 11.2 · P90 19.8 min · June 2026&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;script&gt;
&#x2F;&#x2F; Pure DOM toggle between the two static SVG figures — no canvas, no resize observers.
var XSTAT_CAP = {
  skew: &#x27;Right-skewed: a tall cluster of fast builds and a long tail. The mean (yellow) is dragged right of the median (blue) by that tail; P90 (red) sits far out in it.&#x27;,
  sym: &#x27;Symmetric (hypothetical): with no tail, the mean and median coincide — they land on the same line. This is the only case where &quot;average&quot; and &quot;typical&quot; mean the same thing. Real latency rarely looks like this.&#x27;
};
function xstatShow(mode) {
  var skew = mode === &#x27;skew&#x27;;
  var fSkew = document.getElementById(&#x27;xsFigSkew&#x27;), fSym = document.getElementById(&#x27;xsFigSym&#x27;);
  var bSkew = document.getElementById(&#x27;xsBtnSkew&#x27;), bSym = document.getElementById(&#x27;xsBtnSym&#x27;);
  if (fSkew) fSkew.hidden = !skew;
  if (fSym) fSym.hidden = skew;
  if (bSkew) { bSkew.classList.toggle(&#x27;active&#x27;, skew); bSkew.setAttribute(&#x27;aria-selected&#x27;, String(skew)); }
  if (bSym) { bSym.classList.toggle(&#x27;active&#x27;, !skew); bSym.setAttribute(&#x27;aria-selected&#x27;, String(!skew)); }
  var cap = document.getElementById(&#x27;xstatCaption&#x27;);
  if (cap) cap.textContent = XSTAT_CAP[mode];
}
&lt;&#x2F;script&gt;
&lt;&#x2F;div&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Clang Modules and Bazel, From First Principles</title>
        <published>2026-06-12T00:00:00+00:00</published>
        <updated>2026-06-12T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/ai-learnings/clang-modules-bazel/"/>
        <id>https://imostfa.github.io/Blog/ai-learnings/clang-modules-bazel/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/ai-learnings/clang-modules-bazel/">&lt;p&gt;This write-up was distilled from a real migration session: enabling explicit Clang modules
(&lt;code&gt;swift.use_c_modules&lt;&#x2F;code&gt;) in a large iOS monorepo, debugged end-to-end with an AI agent. One build flag
flipped; nine builds failed; none of the failing code had changed. The document below builds every
concept needed to understand why — header resolution, module maps, PCMs, Bazel’s action model, aspects —
and then walks through each failure as a case study. Prerequisites: you can read Swift&#x2F;Objective-C and
know Bazel has “rules”. Nothing more is assumed.&lt;&#x2F;p&gt;
&lt;style&gt;
.xmod { --xpos:#1a7f37; --xneg:#b3261e; --xwarn:#9a6700; }
[data-theme=&quot;dark&quot;] .xmod { --xpos:#7ee787; --xneg:#ff7b72; --xwarn:#e3b341; }
@media (prefers-color-scheme: dark) { :root:not([data-theme=&quot;light&quot;]) .xmod { --xpos:#7ee787; --xneg:#ff7b72; --xwarn:#e3b341; } }
.xmod .toc-grid { display:grid; grid-template-columns:1fr 1fr; gap:.25rem 2rem; list-style:none; padding:0; margin:.5rem 0 0; }
.xmod .toc-grid a { display:block; padding:.15rem 0; }
.xmod .cards { display:grid; grid-template-columns:repeat(auto-fit,minmax(270px,1fr)); gap:.75rem; margin:1rem 0; }
.xmod .card { background:var(--bg-light); border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1rem; }
.xmod .card h4 { margin:.1rem 0 .5rem; font-size:1rem; }
.xmod .card p, .xmod .card li { font-size:.9rem; margin:.5rem 0; }
.xmod .card ul { margin:.25rem 0; padding-left:1.25rem; }
.xmod pre { font-size:.8rem; line-height:1.5; overflow-x:auto; }
.xmod .err { color:var(--xneg); font-weight:700; }
.xmod .ok { color:var(--xpos); }
.xmod .dim { color:var(--text-light); }
.xmod .mode-toggle { display:inline-flex; gap:.25rem; border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:.25rem; margin:1rem 0 .75rem; background:var(--bg-light); }
.xmod .mode-toggle button { font-family:var(--mono-font); font-size:.9rem; border:none; border-radius:.25rem; padding:.4rem 1rem; cursor:pointer; background:transparent; color:var(--text-light); }
.xmod .mode-toggle button.active { background:var(--accent); color:var(--accent-text); }
.xmod .mode-panel { display:none; }
.xmod .mode-panel.active { display:block; }
.xmod .diagram { border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:1.25rem; background:var(--bg-light); }
.xmod .dgrid { display:flex; flex-wrap:wrap; gap:.6rem; }
.xmod .dbox { border:1px solid var(--border); border-radius:var(--standard-border-radius); padding:.7rem .8rem; background:var(--bg); font-size:.78rem; line-height:1.5; flex:1; min-width:200px; }
.xmod .dbox .dtitle { font-weight:700; font-size:.84rem; margin-bottom:.4rem; display:flex; align-items:center; gap:.5rem; flex-wrap:wrap; }
.xmod .dbox.big { flex-basis:100%; }
.xmod .dbox.fail { border-color:var(--xneg); }
.xmod .dbox.pass { border-color:var(--xpos); }
.xmod .pill { display:inline-block; border-radius:999px; padding:0 .55rem; font-size:.68rem; font-weight:700; }
.xmod .pill.red { color:var(--xneg); border:1px solid var(--xneg); }
.xmod .pill.green { color:var(--xpos); border:1px solid var(--xpos); }
.xmod .pill.blue { color:var(--link); border:1px solid var(--link); }
.xmod .arrow-down { text-align:center; color:var(--text-light); width:100%; margin:.2rem 0; }
.xmod .dcaption { font-size:.85rem; color:var(--text-light); margin-top:.8rem; line-height:1.6; }
.xmod .callout { border:1px solid var(--accent); border-left:4px solid var(--accent); border-radius:var(--standard-border-radius); padding:1.25rem; margin:1rem 0; background:var(--bg-light); }
.xmod .callout .rule { font-weight:700; font-size:1.05rem; line-height:1.55; margin:.25rem 0; }
.xmod .callout .corollary { color:var(--text-light); font-size:.92rem; margin:.75rem 0 0; }
.xmod details.issue { border:1px solid var(--border); border-radius:var(--standard-border-radius); margin-bottom:.6rem; }
.xmod details.issue summary { display:flex; align-items:baseline; gap:.7rem; cursor:pointer; }
.xmod details.issue .num { color:var(--accent); font-weight:700; flex-shrink:0; }
.xmod details.issue .t { font-weight:700; }
.xmod details.issue .s { display:block; font-size:.8rem; color:var(--text-light); font-weight:400; }
.xmod .ibody { padding:.25rem .5rem .5rem; }
.xmod .q { font-size:.75rem; font-weight:700; letter-spacing:.06em; text-transform:uppercase; color:var(--accent); margin:1rem 0 .25rem; }
.xmod .ibody p { font-size:.92rem; margin:.25rem 0; }
.xmod .lesson { border-left:3px solid var(--xwarn); padding:.5rem .8rem; margin-top:1rem; font-size:.88rem; background:var(--bg); border-radius:0 .25rem .25rem 0; }
.xmod .lesson strong { color:var(--xwarn); }
.xmod .chart-wrap { position:relative; height:320px; margin:1rem 0; }
.xmod .legend { list-style:none; padding:0; font-size:.88rem; }
.xmod .legend li { display:flex; gap:.6rem; padding:.4rem 0; border-bottom:1px solid var(--border); align-items:baseline; }
.xmod .legend li:last-child { border-bottom:none; }
.xmod .legend .ldot { width:.7rem; height:.7rem; border-radius:.2rem; flex-shrink:0; transform:translateY(.05rem); }
.xmod .legend .ld { color:var(--text-light); display:block; font-size:.82rem; }
.xmod .gloss dt { font-weight:700; color:var(--accent); margin-top:.9rem; }
.xmod .gloss dd { margin:0.15rem 0 0 0; font-size:.9rem; }
.xmod table td, .xmod table th { font-size:.88rem; vertical-align:top; }
.xmod figure { margin:1rem 0; }
.xmod .fig svg { width:100%; height:auto; display:block; }
.xmod .svg-cap { font-size:.82rem; color:var(--text-light); text-align:center; margin-top:.5rem; line-height:1.55; }
.xmod .donut-wrap { display:flex; flex-wrap:wrap; gap:1.25rem 2rem; align-items:center; }
.xmod .donut-wrap svg { width:200px; height:200px; flex-shrink:0; margin:0 auto; }
.xmod .donut-wrap .legend { flex:1; min-width:250px; }
.xmod .legend .ldot { display:inline-block; }
@media (max-width:600px) { .xmod .toc-grid { grid-template-columns:1fr; } }
&lt;&#x2F;style&gt;
&lt;div class=&quot;xmod&quot;&gt;
&lt;nav aria-label=&quot;Contents&quot;&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;&#x2F;h2&gt;
&lt;ol class=&quot;toc-grid&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#part1&quot;&gt;Header imports and search paths&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part2&quot;&gt;Module maps and PCMs&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part3&quot;&gt;Bazel: the minimum working model&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part4&quot;&gt;Implicit vs. explicit compilation&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part5&quot;&gt;The governing invariant&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part6&quot;&gt;Case studies: nine failures&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part7&quot;&gt;Root-cause classification&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part8&quot;&gt;Diagnostic reference&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part9&quot;&gt;FAQ&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;#part10&quot;&gt;Glossary&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;nav&gt;
&lt;h2 id=&quot;part1&quot;&gt;Part 1 — How a header import is resolved&lt;&#x2F;h2&gt;
&lt;p&gt;Everything in this document reduces to one question: given an &lt;code&gt;#import&lt;&#x2F;code&gt; line, where does the compiler look for the file? There are two import forms and three search mechanisms.&lt;&#x2F;p&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Textual inclusion&lt;&#x2F;h4&gt;
&lt;p&gt;&lt;code&gt;#import &quot;X.h&quot;&lt;&#x2F;code&gt; and &lt;code&gt;#import &amp;lt;Y&#x2F;Y.h&amp;gt;&lt;&#x2F;code&gt; are preprocessor directives. By default the preprocessor replaces the line with the full text of the file, recursively. A translation unit (one &lt;code&gt;.m&lt;&#x2F;code&gt; file) expands to itself plus every header it transitively includes — frequently 100k+ lines — re-parsed for every translation unit.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Quote include: &lt;code&gt;#import &quot;X.h&quot;&lt;&#x2F;code&gt;&lt;&#x2F;h4&gt;
&lt;p&gt;Resolution order:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The directory of the &lt;em&gt;including file&lt;&#x2F;em&gt; (&quot;includer-relative&quot;). &lt;code&gt;&quot;..&#x2F;Shared&#x2F;X.h&quot;&lt;&#x2F;code&gt; resolves here without any flags.&lt;&#x2F;li&gt;
&lt;li&gt;Then each directory passed with &lt;code&gt;-I&lt;&#x2F;code&gt;, in order.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;A quote include naming a file in a &lt;em&gt;different&lt;&#x2F;em&gt; directory only works if some &lt;code&gt;-I&lt;&#x2F;code&gt; flag points there. This is the root cause of case 6.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Angle include: &lt;code&gt;#import &amp;lt;Y&#x2F;Z.h&amp;gt;&lt;&#x2F;code&gt;&lt;&#x2F;h4&gt;
&lt;p&gt;Resolution order:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Each &lt;code&gt;-I&lt;&#x2F;code&gt; directory: looks for &lt;code&gt;&amp;lt;dir&amp;gt;&#x2F;Y&#x2F;Z.h&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;Each &lt;code&gt;-F&lt;&#x2F;code&gt; directory (framework search path): looks for &lt;code&gt;&amp;lt;dir&amp;gt;&#x2F;Y.framework&#x2F;Headers&#x2F;Z.h&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;System&#x2F;SDK paths.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;code&gt;#import &amp;lt;FBSDKCoreKit_Basics&#x2F;FBSDKCoreKit_Basics.h&amp;gt;&lt;&#x2F;code&gt; requires a &lt;code&gt;-F&lt;&#x2F;code&gt; flag pointing at the directory containing &lt;code&gt;FBSDKCoreKit_Basics.framework&lt;&#x2F;code&gt;. Root cause of case 1.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Framework layout&lt;&#x2F;h4&gt;
&lt;p&gt;A built framework is a directory with fixed structure:&lt;&#x2F;p&gt;
&lt;pre&gt;FBSDKCoreKit.framework&#x2F;
├── FBSDKCoreKit          (binary)
├── Headers&#x2F;              (public headers)
│   └── FBSDKCoreKit.h
└── Modules&#x2F;
    └── module.modulemap  (Part 2)&lt;&#x2F;pre&gt;
&lt;p&gt;An &lt;strong&gt;xcframework&lt;&#x2F;strong&gt; is a wrapper directory holding one such framework per platform slice (device, simulator, …).&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;figure class=&quot;fig&quot;&gt;
&lt;svg viewBox=&quot;0 0 640 372&quot; role=&quot;img&quot; aria-label=&quot;Two side-by-side search-order flowcharts for resolving an import. The quote include &amp;quot;X.h&amp;quot; tries the including file&#x27;s own directory first, then each -I directory, otherwise file not found. The angle include &amp;lt;Y&#x2F;Z.h&amp;gt; tries each -I directory, then each -F framework directory, then system and SDK paths, otherwise file not found. The first step of the quote path and the framework step of the angle path are highlighted as the steps that explain cases 6 and 1 respectively.&quot;&gt;
  &lt;defs&gt;
    &lt;marker id=&quot;xmDown&quot; markerWidth=&quot;9&quot; markerHeight=&quot;9&quot; refX=&quot;7&quot; refY=&quot;3&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L7,3 L0,6 Z&quot; fill=&quot;var(--text-light)&quot;&#x2F;&gt;&lt;&#x2F;marker&gt;
  &lt;&#x2F;defs&gt;
  &lt;!-- QUOTE lane --&gt;
  &lt;rect x=&quot;35&quot; y=&quot;14&quot; width=&quot;255&quot; height=&quot;42&quot; rx=&quot;6&quot; fill=&quot;var(--bg-light)&quot; stroke=&quot;var(--accent)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;162&quot; y=&quot;33&quot; font-size=&quot;13&quot; font-weight=&quot;700&quot; fill=&quot;var(--accent)&quot; text-anchor=&quot;middle&quot; font-family=&quot;var(--mono-font)&quot;&gt;#import &quot;X.h&quot;&lt;&#x2F;text&gt;
  &lt;text x=&quot;162&quot; y=&quot;48&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;quote include&lt;&#x2F;text&gt;
  &lt;line x1=&quot;162&quot; y1=&quot;58&quot; x2=&quot;162&quot; y2=&quot;75&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.4&quot; marker-end=&quot;url(#xmDown)&quot;&#x2F;&gt;
  &lt;rect x=&quot;35&quot; y=&quot;78&quot; width=&quot;255&quot; height=&quot;52&quot; rx=&quot;6&quot; fill=&quot;var(--bg)&quot; stroke=&quot;var(--xpos)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;48&quot; y=&quot;100&quot; font-size=&quot;12&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot;&gt;1 · Includer&#x27;s own directory&lt;&#x2F;text&gt;
  &lt;text x=&quot;48&quot; y=&quot;118&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;&quot;..&#x2F;Shared&#x2F;X.h&quot; resolves here — no flags&lt;&#x2F;text&gt;
  &lt;line x1=&quot;162&quot; y1=&quot;132&quot; x2=&quot;162&quot; y2=&quot;151&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.4&quot; marker-end=&quot;url(#xmDown)&quot;&#x2F;&gt;
  &lt;text x=&quot;172&quot; y=&quot;145&quot; font-size=&quot;10&quot; fill=&quot;var(--text-light)&quot;&gt;not found ↓&lt;&#x2F;text&gt;
  &lt;rect x=&quot;35&quot; y=&quot;154&quot; width=&quot;255&quot; height=&quot;52&quot; rx=&quot;6&quot; fill=&quot;var(--bg)&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;48&quot; y=&quot;176&quot; font-size=&quot;12&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot;&gt;2 · each -I dir, in order&lt;&#x2F;text&gt;
  &lt;text x=&quot;48&quot; y=&quot;194&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;supplied by flags only&lt;&#x2F;text&gt;
  &lt;line x1=&quot;162&quot; y1=&quot;208&quot; x2=&quot;162&quot; y2=&quot;227&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.4&quot; marker-end=&quot;url(#xmDown)&quot;&#x2F;&gt;
  &lt;text x=&quot;172&quot; y=&quot;221&quot; font-size=&quot;10&quot; fill=&quot;var(--text-light)&quot;&gt;not found ↓&lt;&#x2F;text&gt;
  &lt;rect x=&quot;35&quot; y=&quot;230&quot; width=&quot;255&quot; height=&quot;42&quot; rx=&quot;6&quot; fill=&quot;var(--bg)&quot; stroke=&quot;var(--xneg)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;162&quot; y=&quot;256&quot; font-size=&quot;12&quot; font-weight=&quot;700&quot; fill=&quot;var(--xneg)&quot; text-anchor=&quot;middle&quot;&gt;else → file not found&lt;&#x2F;text&gt;
  &lt;!-- ANGLE lane --&gt;
  &lt;rect x=&quot;350&quot; y=&quot;14&quot; width=&quot;255&quot; height=&quot;42&quot; rx=&quot;6&quot; fill=&quot;var(--bg-light)&quot; stroke=&quot;var(--accent)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;477&quot; y=&quot;33&quot; font-size=&quot;13&quot; font-weight=&quot;700&quot; fill=&quot;var(--accent)&quot; text-anchor=&quot;middle&quot; font-family=&quot;var(--mono-font)&quot;&gt;#import &amp;lt;Y&#x2F;Z.h&amp;gt;&lt;&#x2F;text&gt;
  &lt;text x=&quot;477&quot; y=&quot;48&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;angle include&lt;&#x2F;text&gt;
  &lt;line x1=&quot;477&quot; y1=&quot;58&quot; x2=&quot;477&quot; y2=&quot;75&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.4&quot; marker-end=&quot;url(#xmDown)&quot;&#x2F;&gt;
  &lt;rect x=&quot;350&quot; y=&quot;78&quot; width=&quot;255&quot; height=&quot;52&quot; rx=&quot;6&quot; fill=&quot;var(--bg)&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;363&quot; y=&quot;100&quot; font-size=&quot;12&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot;&gt;1 · each -I dir&lt;&#x2F;text&gt;
  &lt;text x=&quot;363&quot; y=&quot;118&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;looks for &amp;lt;dir&amp;gt;&#x2F;Y&#x2F;Z.h&lt;&#x2F;text&gt;
  &lt;line x1=&quot;477&quot; y1=&quot;132&quot; x2=&quot;477&quot; y2=&quot;151&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.4&quot; marker-end=&quot;url(#xmDown)&quot;&#x2F;&gt;
  &lt;text x=&quot;487&quot; y=&quot;145&quot; font-size=&quot;10&quot; fill=&quot;var(--text-light)&quot;&gt;not found ↓&lt;&#x2F;text&gt;
  &lt;rect x=&quot;350&quot; y=&quot;154&quot; width=&quot;255&quot; height=&quot;52&quot; rx=&quot;6&quot; fill=&quot;var(--bg)&quot; stroke=&quot;var(--xpos)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;363&quot; y=&quot;176&quot; font-size=&quot;12&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot;&gt;2 · each -F framework dir&lt;&#x2F;text&gt;
  &lt;text x=&quot;363&quot; y=&quot;194&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;&amp;lt;dir&amp;gt;&#x2F;Y.framework&#x2F;Headers&#x2F;Z.h — case 1&lt;&#x2F;text&gt;
  &lt;line x1=&quot;477&quot; y1=&quot;208&quot; x2=&quot;477&quot; y2=&quot;227&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.4&quot; marker-end=&quot;url(#xmDown)&quot;&#x2F;&gt;
  &lt;text x=&quot;487&quot; y=&quot;221&quot; font-size=&quot;10&quot; fill=&quot;var(--text-light)&quot;&gt;not found ↓&lt;&#x2F;text&gt;
  &lt;rect x=&quot;350&quot; y=&quot;230&quot; width=&quot;255&quot; height=&quot;52&quot; rx=&quot;6&quot; fill=&quot;var(--bg)&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;363&quot; y=&quot;252&quot; font-size=&quot;12&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot;&gt;3 · System &#x2F; SDK paths&lt;&#x2F;text&gt;
  &lt;text x=&quot;363&quot; y=&quot;270&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;toolchain defaults&lt;&#x2F;text&gt;
  &lt;line x1=&quot;477&quot; y1=&quot;284&quot; x2=&quot;477&quot; y2=&quot;303&quot; stroke=&quot;var(--text-light)&quot; stroke-width=&quot;1.4&quot; marker-end=&quot;url(#xmDown)&quot;&#x2F;&gt;
  &lt;rect x=&quot;350&quot; y=&quot;306&quot; width=&quot;255&quot; height=&quot;42&quot; rx=&quot;6&quot; fill=&quot;var(--bg)&quot; stroke=&quot;var(--xneg)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;477&quot; y=&quot;332&quot; font-size=&quot;12&quot; font-weight=&quot;700&quot; fill=&quot;var(--xneg)&quot; text-anchor=&quot;middle&quot;&gt;else → file not found&lt;&#x2F;text&gt;
&lt;&#x2F;svg&gt;
&lt;&#x2F;figure&gt;
&lt;p class=&quot;svg-cap&quot;&gt;Resolution is just this walk down a list of locations. The &lt;span style=&quot;color:var(--xpos);font-weight:700&quot;&gt;green&lt;&#x2F;span&gt; steps need no target-private flags; every other step depends on a &lt;code&gt;-I&lt;&#x2F;code&gt; or &lt;code&gt;-F&lt;&#x2F;code&gt; that must be present &lt;em&gt;in the running compilation&lt;&#x2F;em&gt; — which is exactly what changes when a module is compiled in isolation (Parts 4–5).&lt;&#x2F;p&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;The consequence that matters&lt;&#x2F;h4&gt;
&lt;p&gt;Header resolution is governed entirely by &lt;strong&gt;which flags are on the command line of the compilation currently running&lt;&#x2F;strong&gt;. The same header can resolve in one compilation and fail in another with no change to any file — only the flag set differs. The entire migration is about &lt;em&gt;which compilation&lt;&#x2F;em&gt; parses each header, and therefore which flags are in effect.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part2&quot;&gt;Part 2 — Module maps and PCMs&lt;&#x2F;h2&gt;
&lt;p&gt;Textual inclusion has two structural problems: cost (every consumer re-parses every header) and fragility (a header&#x27;s meaning depends on whatever macros and flags the consumer happens to have). Clang modules address both.&lt;&#x2F;p&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;The module map&lt;&#x2F;h4&gt;
&lt;p&gt;A &lt;code&gt;module.modulemap&lt;&#x2F;code&gt; file declares that a set of headers forms a named unit:&lt;&#x2F;p&gt;
&lt;pre&gt;framework module FBSDKCoreKit {
  umbrella header &quot;FBSDKCoreKit.h&quot;   &lt;span class=&quot;dim&quot;&gt;&#x2F;&#x2F; this header (and its includes) define the module&#x27;s contents&lt;&#x2F;span&gt;
  export *                            &lt;span class=&quot;dim&quot;&gt;&#x2F;&#x2F; importers also see what this module&#x27;s headers import&lt;&#x2F;span&gt;
  module * { export * }              &lt;span class=&quot;dim&quot;&gt;&#x2F;&#x2F; one submodule per header under the umbrella&lt;&#x2F;span&gt;
}&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;umbrella header&lt;&#x2F;strong&gt; — a header that includes all other public headers; the module&#x27;s contents are whatever it declares.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;framework module&lt;&#x2F;strong&gt; — header paths resolve inside the framework&#x27;s &lt;code&gt;Headers&#x2F;&lt;&#x2F;code&gt; directory.&lt;&#x2F;li&gt;
&lt;li&gt;Non-framework module maps list headers explicitly: &lt;code&gt;module LookinServer { header &quot;A.h&quot; header &quot;B.h&quot; }&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;The PCM (precompiled module)&lt;&#x2F;h4&gt;
&lt;p&gt;To &quot;build a module,&quot; the compiler parses the headers named by the module map — in a compilation of its own, with its own flag set — and serializes the resulting AST to a binary &lt;code&gt;.pcm&lt;&#x2F;code&gt; file. Importers deserialize the PCM instead of re-parsing headers. Analogous to &lt;code&gt;.c → .o&lt;&#x2F;code&gt;: parse once, reuse many times.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;How imports change&lt;&#x2F;h4&gt;
&lt;p&gt;&lt;code&gt;@import FBSDKCoreKit;&lt;&#x2F;code&gt; (ObjC) and &lt;code&gt;import FBSDKCoreKit&lt;&#x2F;code&gt; (Swift) load the module by name. With modules enabled, even a textual-looking &lt;code&gt;#import &amp;lt;FBSDKCoreKit&#x2F;FBSDKCoreKit.h&amp;gt;&lt;&#x2F;code&gt; is remapped to a module import when a module map claims that header. Swift has no headers at all — &lt;strong&gt;every&lt;&#x2F;strong&gt; ObjC dependency reaches Swift through a module, which is why module correctness is non-negotiable in a mixed codebase.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;Module-level dependencies&lt;&#x2F;h4&gt;
&lt;p&gt;Building module A&#x27;s PCM may itself encounter &lt;code&gt;#import &amp;lt;B&#x2F;B.h&amp;gt;&lt;&#x2F;code&gt;. If B is covered by a module map, A&#x27;s PCM records a dependency on module B, and B&#x27;s PCM must be available. Modules therefore form a dependency graph of their own, parallel to the build system&#x27;s target graph. The migration&#x27;s central problem: these two graphs &lt;strong&gt;must agree&lt;&#x2F;strong&gt;, and under the old mode nothing checked that they did.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;The two open questions&lt;&#x2F;h4&gt;
&lt;p&gt;The module map says nothing about &lt;em&gt;when&lt;&#x2F;em&gt; the PCM is built or &lt;em&gt;which flags&lt;&#x2F;em&gt; that build sees. Two strategies exist:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Implicit&lt;&#x2F;strong&gt; — the compiler builds PCMs on demand, mid-compilation (Part 4, left panel).&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Explicit&lt;&#x2F;strong&gt; — the build system builds every PCM as a scheduled step (Part 4, right panel).&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The migration is the switch from the first to the second.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;figure class=&quot;fig&quot;&gt;
&lt;svg viewBox=&quot;0 0 640 274&quot; role=&quot;img&quot; aria-label=&quot;Two graphs side by side over the same two nodes, FBSDKCoreKit and FBSDKCoreKit_Basics. On the left, the module graph, an arrow runs from FBSDKCoreKit down to FBSDKCoreKit_Basics, labelled by the umbrella header&#x27;s angle import. On the right, the Bazel target graph, the same two nodes have no connecting edge because the SPM binary target was generated with deps equal to an empty list; the missing edge is drawn as a red dashed arrow with a cross. The caption states the two graphs must agree under explicit compilation.&quot;&gt;
  &lt;defs&gt;
    &lt;marker id=&quot;xmMA&quot; markerWidth=&quot;9&quot; markerHeight=&quot;9&quot; refX=&quot;7&quot; refY=&quot;3&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L7,3 L0,6 Z&quot; fill=&quot;var(--accent)&quot;&#x2F;&gt;&lt;&#x2F;marker&gt;
    &lt;marker id=&quot;xmMR&quot; markerWidth=&quot;9&quot; markerHeight=&quot;9&quot; refX=&quot;7&quot; refY=&quot;3&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L7,3 L0,6 Z&quot; fill=&quot;var(--xneg)&quot;&#x2F;&gt;&lt;&#x2F;marker&gt;
  &lt;&#x2F;defs&gt;
  &lt;text x=&quot;170&quot; y=&quot;26&quot; font-size=&quot;12.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--accent)&quot; text-anchor=&quot;middle&quot;&gt;Module graph — header imports&lt;&#x2F;text&gt;
  &lt;text x=&quot;475&quot; y=&quot;26&quot; font-size=&quot;12.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--accent)&quot; text-anchor=&quot;middle&quot;&gt;Bazel target graph — deps&lt;&#x2F;text&gt;
  &lt;line x1=&quot;320&quot; y1=&quot;42&quot; x2=&quot;320&quot; y2=&quot;258&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1&quot; stroke-dasharray=&quot;3 4&quot;&#x2F;&gt;
  &lt;!-- LEFT: module graph --&gt;
  &lt;rect x=&quot;80&quot; y=&quot;50&quot; width=&quot;180&quot; height=&quot;46&quot; rx=&quot;6&quot; fill=&quot;var(--bg-light)&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;170&quot; y=&quot;78&quot; font-size=&quot;12.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot; text-anchor=&quot;middle&quot; font-family=&quot;var(--mono-font)&quot;&gt;FBSDKCoreKit&lt;&#x2F;text&gt;
  &lt;line x1=&quot;170&quot; y1=&quot;96&quot; x2=&quot;170&quot; y2=&quot;182&quot; stroke=&quot;var(--accent)&quot; stroke-width=&quot;2&quot; marker-end=&quot;url(#xmMA)&quot;&#x2F;&gt;
  &lt;text x=&quot;180&quot; y=&quot;134&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;#import&lt;&#x2F;text&gt;
  &lt;text x=&quot;180&quot; y=&quot;148&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;&amp;lt;…_Basics&#x2F;…&amp;gt;&lt;&#x2F;text&gt;
  &lt;rect x=&quot;80&quot; y=&quot;186&quot; width=&quot;180&quot; height=&quot;46&quot; rx=&quot;6&quot; fill=&quot;var(--bg-light)&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;170&quot; y=&quot;214&quot; font-size=&quot;11.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot; text-anchor=&quot;middle&quot; font-family=&quot;var(--mono-font)&quot;&gt;FBSDKCoreKit_Basics&lt;&#x2F;text&gt;
  &lt;!-- RIGHT: target graph --&gt;
  &lt;rect x=&quot;385&quot; y=&quot;50&quot; width=&quot;180&quot; height=&quot;46&quot; rx=&quot;6&quot; fill=&quot;var(--bg-light)&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;475&quot; y=&quot;73&quot; font-size=&quot;12.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot; text-anchor=&quot;middle&quot; font-family=&quot;var(--mono-font)&quot;&gt;FBSDKCoreKit&lt;&#x2F;text&gt;
  &lt;text x=&quot;475&quot; y=&quot;88&quot; font-size=&quot;10&quot; fill=&quot;var(--xneg)&quot; text-anchor=&quot;middle&quot; font-family=&quot;var(--mono-font)&quot;&gt;deps = [ ]&lt;&#x2F;text&gt;
  &lt;line x1=&quot;475&quot; y1=&quot;96&quot; x2=&quot;475&quot; y2=&quot;182&quot; stroke=&quot;var(--xneg)&quot; stroke-width=&quot;2&quot; stroke-dasharray=&quot;5 4&quot; marker-end=&quot;url(#xmMR)&quot;&#x2F;&gt;
  &lt;circle cx=&quot;475&quot; cy=&quot;139&quot; r=&quot;11&quot; fill=&quot;var(--bg)&quot; stroke=&quot;var(--xneg)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;475&quot; y=&quot;143&quot; font-size=&quot;13&quot; font-weight=&quot;700&quot; fill=&quot;var(--xneg)&quot; text-anchor=&quot;middle&quot;&gt;✗&lt;&#x2F;text&gt;
  &lt;text x=&quot;492&quot; y=&quot;135&quot; font-size=&quot;10.5&quot; fill=&quot;var(--xneg)&quot;&gt;edge&lt;&#x2F;text&gt;
  &lt;text x=&quot;492&quot; y=&quot;149&quot; font-size=&quot;10.5&quot; fill=&quot;var(--xneg)&quot;&gt;absent&lt;&#x2F;text&gt;
  &lt;rect x=&quot;385&quot; y=&quot;186&quot; width=&quot;180&quot; height=&quot;46&quot; rx=&quot;6&quot; fill=&quot;var(--bg-light)&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;475&quot; y=&quot;214&quot; font-size=&quot;11.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot; text-anchor=&quot;middle&quot; font-family=&quot;var(--mono-font)&quot;&gt;FBSDKCoreKit_Basics&lt;&#x2F;text&gt;
&lt;&#x2F;svg&gt;
&lt;&#x2F;figure&gt;
&lt;p class=&quot;svg-cap&quot;&gt;The umbrella header creates a &lt;span style=&quot;color:var(--accent);font-weight:700&quot;&gt;module-graph edge&lt;&#x2F;span&gt;, but RSPM generated the binary target with &lt;code&gt;deps = []&lt;&#x2F;code&gt;, so the matching &lt;span style=&quot;color:var(--xneg);font-weight:700&quot;&gt;target-graph edge is missing&lt;&#x2F;span&gt;. Implicit mode never compared the two; explicit mode compiles each module in isolation and the gap becomes case 1&#x27;s &lt;code&gt;file not found&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;part3&quot;&gt;Part 3 — Bazel: the minimum working model&lt;&#x2F;h2&gt;
&lt;p&gt;Six concepts are sufficient to read everything that follows.&lt;&#x2F;p&gt;
&lt;div class=&quot;cards&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;1 · Targets and labels&lt;&#x2F;h4&gt;
&lt;p&gt;A &lt;strong&gt;target&lt;&#x2F;strong&gt; is a named buildable unit declared in a &lt;code&gt;BUILD&lt;&#x2F;code&gt; file, addressed by a &lt;strong&gt;label&lt;&#x2F;strong&gt;: &lt;code&gt;&#x2F;&#x2F;Modules&#x2F;Networking:Networking&lt;&#x2F;code&gt; means &quot;target &lt;code&gt;Networking&lt;&#x2F;code&gt; in package &lt;code&gt;Modules&#x2F;Networking&lt;&#x2F;code&gt;&quot;. Labels starting with &lt;code&gt;@&lt;&#x2F;code&gt; address targets in &lt;strong&gt;external repositories&lt;&#x2F;strong&gt; — downloaded dependencies, e.g. &lt;code&gt;@swiftpkg_facebook_ios_sdk&#x2F;&#x2F;:FacebookCore&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;2 · Rules and attributes&lt;&#x2F;h4&gt;
&lt;p&gt;Each target is an instance of a &lt;strong&gt;rule&lt;&#x2F;strong&gt; (&lt;code&gt;swift_library&lt;&#x2F;code&gt;, &lt;code&gt;objc_library&lt;&#x2F;code&gt;, &lt;code&gt;apple_dynamic_xcframework_import&lt;&#x2F;code&gt;) with &lt;strong&gt;attributes&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;srcs&lt;&#x2F;code&gt;, &lt;code&gt;hdrs&lt;&#x2F;code&gt; — source and header files.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;deps&lt;&#x2F;code&gt; — labels of targets this target depends on. &lt;strong&gt;This is the build graph.&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;copts&lt;&#x2F;code&gt; — extra compiler flags, private to this target&#x27;s own compilations.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;includes&lt;&#x2F;code&gt; — header search dirs that &lt;em&gt;propagate&lt;&#x2F;em&gt;: dependents get them automatically.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;features&lt;&#x2F;code&gt; — named toolchain behaviors; &lt;code&gt;&quot;x&quot;&lt;&#x2F;code&gt; enables, &lt;code&gt;&quot;-x&quot;&lt;&#x2F;code&gt; disables, per target or globally.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;3 · Actions and sandboxing&lt;&#x2F;h4&gt;
&lt;p&gt;Building a target produces &lt;strong&gt;actions&lt;&#x2F;strong&gt;: concrete command lines (one compiler invocation each) with &lt;strong&gt;declared inputs and outputs&lt;&#x2F;strong&gt;. Actions run sandboxed — the command can only read files declared as inputs. An undeclared file is absent, not merely discouraged. Actions are cacheable (locally and remotely) and reproducible precisely because their inputs are closed.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;4 · Providers&lt;&#x2F;h4&gt;
&lt;p&gt;Targets pass structured data to dependents through &lt;strong&gt;providers&lt;&#x2F;strong&gt;. Two matter here:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CcInfo&lt;&#x2F;code&gt; — C-family compilation context: headers, defines, &lt;code&gt;-I&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;-F&lt;&#x2F;code&gt; search paths. A target&#x27;s &lt;code&gt;copts&lt;&#x2F;code&gt; are &lt;em&gt;not&lt;&#x2F;em&gt; in CcInfo; &lt;code&gt;includes&lt;&#x2F;code&gt; are.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;SwiftInfo&lt;&#x2F;code&gt; — Swift modules and, under explicit compilation, the Clang modules (module map + PCM) a target exposes.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&quot;X propagates to dependents&quot; always means &quot;X travels in a provider along &lt;code&gt;deps&lt;&#x2F;code&gt; edges.&quot;&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;5 · Aspects&lt;&#x2F;h4&gt;
&lt;p&gt;An &lt;strong&gt;aspect&lt;&#x2F;strong&gt; walks the dependency graph and attaches &lt;em&gt;additional&lt;&#x2F;em&gt; actions and providers to existing targets without editing their BUILD files. The relevant one is rules_swift&#x27;s &lt;code&gt;swift_clang_module_aspect&lt;&#x2F;code&gt;: it visits every target reachable through &lt;code&gt;deps&lt;&#x2F;code&gt;, and for each target that owns a module map, registers a &lt;code&gt;SwiftPrecompileCModule&lt;&#x2F;code&gt; action that builds the PCM. Error messages name it explicitly: &lt;em&gt;&quot;from aspects [swift_clang_module_aspect] on objc_library rule target …&quot;&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h4&gt;6 · Generated external repos (RSPM)&lt;&#x2F;h4&gt;
&lt;p&gt;Swift Package Manager dependencies are integrated by &lt;strong&gt;rules_swift_package_manager (RSPM)&lt;&#x2F;strong&gt;: it downloads each package, reads its manifest, and &lt;em&gt;generates&lt;&#x2F;em&gt; a BUILD file — one target per SPM target. A binary SPM target (&lt;code&gt;binaryTarget&lt;&#x2F;code&gt;, a prebuilt xcframework) becomes an &lt;code&gt;apple_*_xcframework_import&lt;&#x2F;code&gt; target. Critical limitation: SPM&#x27;s manifest format gives binary targets &lt;strong&gt;no dependency list&lt;&#x2F;strong&gt;, so RSPM generates them with &lt;code&gt;deps = []&lt;&#x2F;code&gt;. RSPM ≥ 1.17 adds &lt;code&gt;configure_package(target_deps = {...})&lt;&#x2F;code&gt; to inject edges into the generated BUILD by hand.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part4&quot;&gt;Part 4 — Implicit vs. explicit compilation&lt;&#x2F;h2&gt;
&lt;p&gt;Same sources, same module maps. The difference is who schedules PCM compilation and which flags it sees. The migration enables two features in &lt;code&gt;.bazelrc&lt;&#x2F;code&gt;: &lt;code&gt;swift.emit_c_module&lt;&#x2F;code&gt; and &lt;code&gt;swift.use_c_modules&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;div class=&quot;mode-toggle&quot; role=&quot;tablist&quot; aria-label=&quot;Module mode toggle&quot;&gt;
&lt;button id=&quot;btnImplicit&quot; class=&quot;active&quot; onclick=&quot;xmodShowMode(&#x27;implicit&#x27;)&quot; role=&quot;tab&quot; aria-selected=&quot;true&quot;&gt;Implicit (before)&lt;&#x2F;button&gt;
&lt;button id=&quot;btnExplicit&quot; onclick=&quot;xmodShowMode(&#x27;explicit&#x27;)&quot; role=&quot;tab&quot; aria-selected=&quot;false&quot;&gt;Explicit (after)&lt;&#x2F;button&gt;
&lt;&#x2F;div&gt;
&lt;div id=&quot;panelImplicit&quot; class=&quot;mode-panel active diagram&quot; role=&quot;tabpanel&quot;&gt;
&lt;div class=&quot;dgrid&quot;&gt;
&lt;div class=&quot;dbox big pass&quot;&gt;
&lt;div class=&quot;dtitle&quot;&gt;Step 1 — Bazel schedules ONE action: the consumer&#x27;s Swift compile &lt;span class=&quot;pill green&quot;&gt;all search paths merged&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;span class=&quot;dim&quot;&gt;swiftc compiles&lt;&#x2F;span&gt; RudderManager.swift&lt;span class=&quot;dim&quot;&gt;. Its target&#x27;s deps are many, so the merged CcInfo yields:&lt;&#x2F;span&gt;&lt;br&gt;
-F facebook&amp;nbsp; -F braze&amp;nbsp; -F zendesk&amp;nbsp; -F sentry&amp;nbsp; -F firebase&amp;nbsp; -I lookin&#x2F;Shared&amp;nbsp; &lt;span class=&quot;dim&quot;&gt;… one command line, everything visible&lt;&#x2F;span&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow-down&quot;&gt;↓ during that compile, swiftc encounters &lt;code&gt;import FBSDKCoreKit&lt;&#x2F;code&gt; and no PCM exists yet&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox big&quot;&gt;
&lt;div class=&quot;dtitle&quot;&gt;Step 2 — the compiler builds the PCM itself, mid-compilation&lt;&#x2F;div&gt;
&lt;span class=&quot;dim&quot;&gt;It finds the module map via the -F paths, then parses FBSDKCoreKit.h. That parse hits&lt;&#x2F;span&gt; #import &amp;lt;FBSDKCoreKit_Basics&#x2F;…&amp;gt;&lt;br&gt;
&lt;span class=&quot;ok&quot;&gt;resolves&lt;&#x2F;span&gt; &lt;span class=&quot;dim&quot;&gt;— this parse runs INSIDE the consumer&#x27;s compilation and inherits its full flag set (Part 1: resolution = current flags).&lt;&#x2F;span&gt;&lt;br&gt;
&lt;span class=&quot;dim&quot;&gt;The PCM lands in a compiler-private cache, outside Bazel: ~&#x2F;…&#x2F;ModuleCache&#x2F;&amp;lt;hash&amp;gt;&#x2F;FBSDKCoreKit-&amp;lt;hash&amp;gt;.pcm&lt;&#x2F;span&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;&lt;strong&gt;Properties of implicit compilation.&lt;&#x2F;strong&gt; (1) PCM builds are not Bazel actions: invisible to the action graph, excluded from remote caching, re-done per machine and per flag-hash. (2) The flag set used to build a PCM is whatever the &lt;em&gt;first importer&lt;&#x2F;em&gt; had — module content can vary by import order and configuration. (3) Header resolution inside a module build borrows the consumer&#x27;s search paths, so a module&#x27;s undeclared dependency on a sibling resolves successfully whenever any consumer&#x27;s flags cover it. No mechanism verifies that the module graph matches the target graph.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div id=&quot;panelExplicit&quot; class=&quot;mode-panel diagram&quot; role=&quot;tabpanel&quot;&gt;
&lt;div class=&quot;dgrid&quot;&gt;
&lt;div class=&quot;dbox big&quot;&gt;
&lt;div class=&quot;dtitle&quot;&gt;Step 0 — swift_clang_module_aspect walks the deps graph and schedules one PCM action per module map&lt;&#x2F;div&gt;
&lt;span class=&quot;dim&quot;&gt;Each SwiftPrecompileCModule action&#x27;s inputs = the owning target&#x27;s files + its declared deps&#x27; CcInfo&#x2F;SwiftInfo. Nothing else exists inside the sandbox.&lt;&#x2F;span&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox pass&quot;&gt;
&lt;div class=&quot;dtitle&quot;&gt;PCM: FBSDKCoreKit_Basics &lt;span class=&quot;pill green&quot;&gt;OK&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;span class=&quot;dim&quot;&gt;Needs no other module → compiles from its own framework files alone.&lt;&#x2F;span&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox fail&quot;&gt;
&lt;div class=&quot;dtitle&quot;&gt;PCM: FBSDKCoreKit &lt;span class=&quot;pill red&quot;&gt;FAILS&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;span class=&quot;dim&quot;&gt;deps = &lt;&#x2F;span&gt;&lt;span class=&quot;err&quot;&gt;[]&lt;&#x2F;span&gt; &lt;span class=&quot;dim&quot;&gt;(SPM binary target, Part 3 §6)&lt;&#x2F;span&gt;&lt;br&gt;
&lt;span class=&quot;dim&quot;&gt;→ no -F path to the sibling in this sandbox&lt;&#x2F;span&gt;&lt;br&gt;
#import &amp;lt;FBSDKCoreKit_Basics&#x2F;…&amp;gt; → &lt;span class=&quot;err&quot;&gt;file not found&lt;&#x2F;span&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox&quot;&gt;
&lt;div class=&quot;dtitle&quot;&gt;PCM: BrazeKit &lt;span class=&quot;pill blue&quot;&gt;independent&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;span class=&quot;dim&quot;&gt;Own action, own sandbox, runs in parallel.&lt;&#x2F;span&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;arrow-down&quot;&gt;↓ Swift compiles are scheduled only after the PCMs they need exist&lt;&#x2F;div&gt;
&lt;div class=&quot;dbox big&quot;&gt;
&lt;div class=&quot;dtitle&quot;&gt;Consumer compile receives finished artifacts — it never parses dependency headers&lt;&#x2F;div&gt;
-fmodule-map-file=…&#x2F;module.modulemap&amp;nbsp;&amp;nbsp;-fmodule-file=FBSDKCoreKit=bazel-out&#x2F;…&#x2F;FBSDKCoreKit.pcm&lt;br&gt;
&lt;span class=&quot;dim&quot;&gt;Each PCM is an ordinary Bazel action: sandboxed, deterministic per configuration, remote-cacheable, shared across machines.&lt;&#x2F;span&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p class=&quot;dcaption&quot;&gt;&lt;strong&gt;Properties of explicit compilation.&lt;&#x2F;strong&gt; (1) Every module map reachable through &lt;code&gt;deps&lt;&#x2F;code&gt; is precompiled — &lt;em&gt;whether or not any source imports it&lt;&#x2F;em&gt; (relevant in case 2). (2) Each PCM build sees only declared inputs; an undeclared dependency is a hard error rather than a silent borrow. (3) PCM actions are cached and shared org-wide, and run in parallel ahead of Swift compiles. The cost: every assumption the implicit mode silently absorbed must now hold explicitly. The nine case studies are exactly those assumptions.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part5&quot;&gt;Part 5 — The governing invariant&lt;&#x2F;h2&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;Under explicit compilation, each module map is compiled in an isolated, sandboxed action whose visible inputs are the owning target&#x27;s files plus its declared dependencies&#x27; providers. Consumer search paths do not exist inside that sandbox.&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;&lt;strong&gt;Corollary (root cause of cases 6 and 8):&lt;&#x2F;strong&gt; when the aspect constructs a PCM action for a target, it takes the compilation context from &lt;code&gt;CcInfo&lt;&#x2F;code&gt; — and forwards only &lt;code&gt;-D&lt;&#x2F;code&gt; defines from the target&#x27;s &lt;code&gt;copts&lt;&#x2F;code&gt;. Since &lt;code&gt;copts&lt;&#x2F;code&gt; never enter CcInfo (Part 3 §4), any &lt;code&gt;-I&lt;&#x2F;code&gt; search path supplied via copts is &lt;strong&gt;absent&lt;&#x2F;strong&gt; from the PCM compile. Search paths reach a PCM action only through the &lt;code&gt;includes&lt;&#x2F;code&gt; attribute or a dependency&#x27;s CcInfo.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;figure class=&quot;fig&quot;&gt;
&lt;svg viewBox=&quot;0 0 640 296&quot; role=&quot;img&quot; aria-label=&quot;Two boxes contrasting the same module import in two compilations. The left box, the consumer Swift compile, lists framework search paths merged from all dependencies — dash F facebook, braze, zendesk, sentry, and dash I lookin slash Shared — and the import of the sibling header resolves with a green check because the PCM is built inside this compilation and inherits its full flag set. The right box, the isolated PCM action sandbox, contains only FBSDKCoreKit&#x27;s own framework files because deps is empty, so the same sibling import fails with a red file-not-found, since the consumer search paths do not exist inside the sandbox.&quot;&gt;
  &lt;!-- LEFT: consumer compile --&gt;
  &lt;rect x=&quot;22&quot; y=&quot;40&quot; width=&quot;292&quot; height=&quot;240&quot; rx=&quot;8&quot; fill=&quot;var(--bg)&quot; stroke=&quot;var(--xpos)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;168&quot; y=&quot;26&quot; font-size=&quot;12.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot; text-anchor=&quot;middle&quot;&gt;Consumer Swift compile&lt;&#x2F;text&gt;
  &lt;text x=&quot;40&quot; y=&quot;68&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;search paths merged from all deps&#x27; CcInfo:&lt;&#x2F;text&gt;
  &lt;text x=&quot;40&quot; y=&quot;92&quot; font-size=&quot;11.5&quot; fill=&quot;var(--fg-color)&quot; font-family=&quot;var(--mono-font)&quot;&gt;-F facebook&amp;#160;&amp;#160;-F braze&lt;&#x2F;text&gt;
  &lt;text x=&quot;40&quot; y=&quot;112&quot; font-size=&quot;11.5&quot; fill=&quot;var(--fg-color)&quot; font-family=&quot;var(--mono-font)&quot;&gt;-F zendesk&amp;#160;&amp;#160;&amp;#160;-F sentry&lt;&#x2F;text&gt;
  &lt;text x=&quot;40&quot; y=&quot;132&quot; font-size=&quot;11.5&quot; fill=&quot;var(--fg-color)&quot; font-family=&quot;var(--mono-font)&quot;&gt;-I lookin&#x2F;Shared&amp;#160;&amp;#160;…&lt;&#x2F;text&gt;
  &lt;line x1=&quot;40&quot; y1=&quot;150&quot; x2=&quot;296&quot; y2=&quot;150&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1&quot;&#x2F;&gt;
  &lt;text x=&quot;40&quot; y=&quot;178&quot; font-size=&quot;11.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot;&gt;PCM built inside this compile&lt;&#x2F;text&gt;
  &lt;text x=&quot;40&quot; y=&quot;206&quot; font-size=&quot;11.5&quot; fill=&quot;var(--xpos)&quot; font-family=&quot;var(--mono-font)&quot;&gt;#import &amp;lt;…_Basics&#x2F;…&amp;gt;&lt;&#x2F;text&gt;
  &lt;text x=&quot;40&quot; y=&quot;226&quot; font-size=&quot;12.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--xpos)&quot;&gt;✓ resolves&lt;&#x2F;text&gt;
  &lt;text x=&quot;40&quot; y=&quot;252&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;inherits the full flag set&lt;&#x2F;text&gt;
  &lt;!-- RIGHT: isolated PCM sandbox --&gt;
  &lt;rect x=&quot;326&quot; y=&quot;40&quot; width=&quot;292&quot; height=&quot;240&quot; rx=&quot;8&quot; fill=&quot;var(--bg)&quot; stroke=&quot;var(--xneg)&quot; stroke-width=&quot;1.5&quot;&#x2F;&gt;
  &lt;text x=&quot;472&quot; y=&quot;26&quot; font-size=&quot;12.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot; text-anchor=&quot;middle&quot;&gt;Isolated PCM action — sandbox&lt;&#x2F;text&gt;
  &lt;text x=&quot;344&quot; y=&quot;68&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;visible inputs (deps = [ ]):&lt;&#x2F;text&gt;
  &lt;text x=&quot;344&quot; y=&quot;92&quot; font-size=&quot;11.5&quot; fill=&quot;var(--fg-color)&quot; font-family=&quot;var(--mono-font)&quot;&gt;FBSDKCoreKit&#x2F; own files&lt;&#x2F;text&gt;
  &lt;text x=&quot;344&quot; y=&quot;116&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;— nothing else exists here —&lt;&#x2F;text&gt;
  &lt;line x1=&quot;344&quot; y1=&quot;150&quot; x2=&quot;600&quot; y2=&quot;150&quot; stroke=&quot;var(--border)&quot; stroke-width=&quot;1&quot;&#x2F;&gt;
  &lt;text x=&quot;344&quot; y=&quot;178&quot; font-size=&quot;11.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--fg-color)&quot;&gt;PCM built as its own action&lt;&#x2F;text&gt;
  &lt;text x=&quot;344&quot; y=&quot;206&quot; font-size=&quot;11.5&quot; fill=&quot;var(--xneg)&quot; font-family=&quot;var(--mono-font)&quot;&gt;#import &amp;lt;…_Basics&#x2F;…&amp;gt;&lt;&#x2F;text&gt;
  &lt;text x=&quot;344&quot; y=&quot;226&quot; font-size=&quot;12.5&quot; font-weight=&quot;700&quot; fill=&quot;var(--xneg)&quot;&gt;✗ file not found&lt;&#x2F;text&gt;
  &lt;text x=&quot;344&quot; y=&quot;252&quot; font-size=&quot;10.5&quot; fill=&quot;var(--text-light)&quot;&gt;consumer -F paths absent here&lt;&#x2F;text&gt;
&lt;&#x2F;svg&gt;
&lt;&#x2F;figure&gt;
&lt;p class=&quot;svg-cap&quot;&gt;Same import, two worlds. The consumer compile sees every dependency&#x27;s search paths merged together, so a sibling resolves incidentally; the isolated PCM action sees only declared inputs, so the identical line fails. That difference is the entire migration.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;part6&quot;&gt;Part 6 — Case studies: nine failures, in build order&lt;&#x2F;h2&gt;
&lt;p&gt;Each entry names the failing action, explains why the same code compiled under implicit mode, derives the root cause from Parts 1–5, and documents the fix.&lt;&#x2F;p&gt;
&lt;details class=&quot;issue&quot; open&gt;
&lt;summary&gt;&lt;span class=&quot;num&quot;&gt;01&lt;&#x2F;span&gt;&lt;span&gt;&lt;span class=&quot;t&quot;&gt;Facebook SDK — &lt;code&gt;&#x27;FBSDKCoreKit_Basics.h&#x27; file not found&lt;&#x2F;code&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;s&quot;&gt;Undeclared edges between sibling binary frameworks · fixed via RSPM target_deps&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;summary&gt;
&lt;div class=&quot;ibody&quot;&gt;
&lt;p class=&quot;q&quot;&gt;Failing action&lt;&#x2F;p&gt;
&lt;p&gt;PCM precompile of &lt;code&gt;FBSDKCoreKit&lt;&#x2F;code&gt;. Its umbrella header contains an angle include of a sibling framework, &lt;code&gt;#import &amp;lt;FBSDKCoreKit_Basics&#x2F;FBSDKCoreKit_Basics.h&amp;gt;&lt;&#x2F;code&gt;, which requires a &lt;code&gt;-F&lt;&#x2F;code&gt; path to the directory containing &lt;code&gt;FBSDKCoreKit_Basics.framework&lt;&#x2F;code&gt; (Part 1).&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Why it compiled under implicit mode&lt;&#x2F;p&gt;
&lt;p&gt;The PCM was built inside a consumer&#x27;s compilation, which had every Facebook framework&#x27;s &lt;code&gt;-F&lt;&#x2F;code&gt; path merged from its many deps (Part 4, implicit panel, step 2).&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Root cause&lt;&#x2F;p&gt;
&lt;p&gt;SPM binary targets carry no dependency list, so RSPM generated &lt;code&gt;deps = []&lt;&#x2F;code&gt; (Part 3 §6). The isolated PCM action&#x27;s sandbox contains only FBSDKCoreKit&#x27;s own files — the sibling framework is not an input, so its headers do not exist there (Part 3 §3).&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Fix&lt;&#x2F;p&gt;
&lt;p&gt;Upgraded RSPM 1.13.0 → 1.19.0 (the &lt;code&gt;target_deps&lt;&#x2F;code&gt; API landed in 1.17.0) and declared the SDK&#x27;s internal layering in &lt;code&gt;MODULE.bazel&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre&gt;swift_deps.configure_package(
    name = &quot;facebook-ios-sdk&quot;,
    target_deps = {
        &quot;FBSDKCoreKit&quot;:           [&quot;FBAEMKit&quot;, &quot;FBSDKCoreKit_Basics&quot;],
        &quot;FBAEMKit&quot;:               [&quot;FBSDKCoreKit_Basics&quot;],
        &quot;FBSDKLoginKit&quot;:          [&quot;FBSDKCoreKit&quot;],
        &quot;FBSDKShareKit&quot;:          [&quot;FBSDKCoreKit&quot;],
        &quot;FBSDKGamingServicesKit&quot;: [&quot;FBSDKCoreKit&quot;, &quot;FBSDKShareKit&quot;],
    },
)&lt;&#x2F;pre&gt;
&lt;p&gt;With the edges declared, the sibling&#x27;s framework directory becomes a sandbox input and its CcInfo supplies the &lt;code&gt;-F&lt;&#x2F;code&gt; path; the dependency PCM is also built first, in topological order.&lt;&#x2F;p&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;General rule:&lt;&#x2F;strong&gt; the module graph (who imports whom at the header level) must be mirrored by the target graph (&lt;code&gt;deps&lt;&#x2F;code&gt;). Implicit mode never checked this; explicit mode enforces it.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;issue&quot;&gt;
&lt;summary&gt;&lt;span class=&quot;num&quot;&gt;02&lt;&#x2F;span&gt;&lt;span&gt;&lt;span class=&quot;t&quot;&gt;GoogleAppMeasurement — &lt;code&gt;umbrella header not found&lt;&#x2F;code&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;s&quot;&gt;Module map present, headers absent · fixed via rules_apple patch&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;summary&gt;
&lt;div class=&quot;ibody&quot;&gt;
&lt;p class=&quot;q&quot;&gt;Failing action&lt;&#x2F;p&gt;
&lt;p&gt;PCM precompile of GoogleAppMeasurement. The framework ships a module map whose &lt;code&gt;umbrella header&lt;&#x2F;code&gt; directive names &lt;code&gt;GoogleAppMeasurement-umbrella.h&lt;&#x2F;code&gt; — but the framework contains no &lt;code&gt;Headers&#x2F;&lt;&#x2F;code&gt; directory at all. The referenced file does not exist in the vendor&#x27;s artifact.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Why it compiled under implicit mode&lt;&#x2F;p&gt;
&lt;p&gt;It never compiled — and never needed to. No source imports this module; the framework is link-only. Implicit PCM builds are triggered by imports (Part 4), so an unimported module map is never parsed. The defect was inert.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Root cause&lt;&#x2F;p&gt;
&lt;p&gt;Explicit-mode property (1): the aspect precompiles every module map reachable in the graph, imported or not. Invalid metadata that implicit mode never evaluated now fails a scheduled action.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Fix&lt;&#x2F;p&gt;
&lt;p&gt;A patch to rules_apple&#x27;s xcframework import rules: when the imported framework ships no headers, do not create the Swift interop provider, so the aspect schedules no PCM action for it. Correct because a headerless framework has no importable surface; linking information is unaffected.&lt;&#x2F;p&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;General rule:&lt;&#x2F;strong&gt; eager precompilation validates all module metadata in the dependency graph, including metadata for modules that no source imports.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;issue&quot;&gt;
&lt;summary&gt;&lt;span class=&quot;num&quot;&gt;03&lt;&#x2F;span&gt;&lt;span&gt;&lt;span class=&quot;t&quot;&gt;Zendesk &#x2F; LiveRamp &#x2F; Braze — &lt;code&gt;module &#x27;SDKConfigurations&#x27; not found&lt;&#x2F;code&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;s&quot;&gt;Cross-package edges; imports hidden in -Swift.h and .swiftinterface · fixed via target_deps with public labels&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;summary&gt;
&lt;div class=&quot;ibody&quot;&gt;
&lt;p class=&quot;q&quot;&gt;Failing actions&lt;&#x2F;p&gt;
&lt;p&gt;Two kinds. (a) PCM precompile of MessagingSDK: binary frameworks containing Swift code ship a generated ObjC header (&lt;code&gt;MessagingSDK-Swift.h&lt;&#x2F;code&gt;) that re-exposes their Swift API to ObjC; it is part of the umbrella, and it contains &lt;code&gt;@import SDKConfigurations;&lt;&#x2F;code&gt; — a module import of a module from a &lt;em&gt;different SPM package&lt;&#x2F;em&gt;. (b) Compilation of MessagingAPI&#x27;s &lt;code&gt;.swiftinterface&lt;&#x2F;code&gt;: frameworks with Swift API ship a textual interface file that rules_apple compiles into a binary swiftmodule, and that compilation must resolve every &lt;code&gt;import&lt;&#x2F;code&gt; statement in the file.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Root cause&lt;&#x2F;p&gt;
&lt;p&gt;Identical to case 1 — binary targets, no declared edges — extended across package boundaries. The required modules live in other generated external repositories (Part 3 §6).&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Implementation notes&lt;&#x2F;p&gt;
&lt;p&gt;① A binary framework&#x27;s true dependency set spans three file types: ObjC headers, the generated &lt;code&gt;-Swift.h&lt;&#x2F;code&gt;, and &lt;code&gt;.swiftinterface&lt;&#x2F;code&gt; files. The initial header-only audit missed the latter two; the complete audit found MessagingSDK → SDKConfigurations + CommonUISDK + MessagingAPI, SupportSDK → five Zendesk modules, BrazeLocation → BrazeKit, LRAtsSDKMediationAdapter → LRAtsSDK + GoogleMobileAds. ② Generated &lt;code&gt;.rspm&lt;&#x2F;code&gt; targets have package-private visibility; cross-package &lt;code&gt;target_deps&lt;&#x2F;code&gt; values must use each repo&#x27;s &lt;em&gt;public product&lt;&#x2F;em&gt; labels, e.g. &lt;code&gt;@swiftpkg_core_sdk_ios&#x2F;&#x2F;:ZendeskCoreSDK&lt;&#x2F;code&gt; — referencing the &lt;code&gt;.rspm&lt;&#x2F;code&gt; targets directly fails Bazel&#x27;s visibility check.&lt;&#x2F;p&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;General rule:&lt;&#x2F;strong&gt; audit ObjC headers, &lt;code&gt;-Swift.h&lt;&#x2F;code&gt;, and &lt;code&gt;.swiftinterface&lt;&#x2F;code&gt; imports to determine a binary framework&#x27;s dependency set. Any one alone is incomplete.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;issue&quot;&gt;
&lt;summary&gt;&lt;span class=&quot;num&quot;&gt;04&lt;&#x2F;span&gt;&lt;span&gt;&lt;span class=&quot;t&quot;&gt;Firebase umbrella — &lt;code&gt;&#x27;FirebaseCore&#x2F;FirebaseCore.h&#x27; file not found&lt;&#x2F;code&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;s&quot;&gt;The required edge would create a cycle · fixed via __has_include source patch&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;summary&gt;
&lt;div class=&quot;ibody&quot;&gt;
&lt;p class=&quot;q&quot;&gt;Failing action&lt;&#x2F;p&gt;
&lt;p&gt;PCM precompile of the &lt;code&gt;Firebase&lt;&#x2F;code&gt; umbrella module — a source target whose only purpose is letting app code write &lt;code&gt;import Firebase&lt;&#x2F;code&gt;. Its header unconditionally angle-includes FirebaseCore&#x27;s umbrella.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Why target_deps does not apply&lt;&#x2F;p&gt;
&lt;p&gt;The build graph already contains the reverse edge: &lt;code&gt;FirebaseCore&lt;&#x2F;code&gt; depends on &lt;code&gt;Firebase&lt;&#x2F;code&gt; (it consumes the &lt;code&gt;Firebase_VERSION&lt;&#x2F;code&gt; define from it). Bazel&#x27;s target graph must be acyclic — adding Firebase → FirebaseCore is rejected at analysis time. This is the one failure where &quot;declare the edge&quot; is structurally impossible.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Fix&lt;&#x2F;p&gt;
&lt;p&gt;A source patch (RSPM &lt;code&gt;configure_package(patches)&lt;&#x2F;code&gt; applies patches to package sources after download, before BUILD generation) making the include conditional:&lt;&#x2F;p&gt;
&lt;pre&gt;&lt;span class=&quot;err&quot;&gt;-#import &amp;lt;FirebaseCore&#x2F;FirebaseCore.h&amp;gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;ok&quot;&gt;+#if __has_include(&amp;lt;FirebaseCore&#x2F;FirebaseCore.h&amp;gt;)&lt;&#x2F;span&gt;
&lt;span class=&quot;ok&quot;&gt;+  #import &amp;lt;FirebaseCore&#x2F;FirebaseCore.h&amp;gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;ok&quot;&gt;+#endif&lt;&#x2F;span&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;__has_include&lt;&#x2F;code&gt; evaluates against the current compilation&#x27;s search paths (Part 1): true in every consumer compile (unchanged behavior), false in the isolated PCM action (import skipped, module compiles near-empty). Acceptable because no source in the repository imports &lt;code&gt;Firebase&lt;&#x2F;code&gt;; the rest of that header already uses the same guard pattern.&lt;&#x2F;p&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;General rule:&lt;&#x2F;strong&gt; when a required module edge would create a target-graph cycle, make the header conditional with &lt;code&gt;__has_include&lt;&#x2F;code&gt; instead of modifying the graph.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;issue&quot;&gt;
&lt;summary&gt;&lt;span class=&quot;num&quot;&gt;05&lt;&#x2F;span&gt;&lt;span&gt;&lt;span class=&quot;t&quot;&gt;Sentry — &lt;code&gt;type argument &#x27;SentrySpan&#x27; must be a pointer&lt;&#x2F;code&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;s&quot;&gt;A never-compiled fallback branch is invalid ObjC · fixed via target_deps on the clang child target&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;summary&gt;
&lt;div class=&quot;ibody&quot;&gt;
&lt;p class=&quot;q&quot;&gt;Failing action&lt;&#x2F;p&gt;
&lt;p&gt;PCM precompile of &lt;code&gt;SentryInternal&lt;&#x2F;code&gt;, a source target in sentry-cocoa. The header:&lt;&#x2F;p&gt;
&lt;pre&gt;#if __has_include(&amp;lt;Sentry&#x2F;Sentry.h&amp;gt;)
#  import &amp;lt;Sentry&#x2F;Sentry.h&amp;gt;        &lt;span class=&quot;dim&quot;&gt;&#x2F;&#x2F; declares: @protocol SentrySpan&lt;&#x2F;span&gt;
#endif
&lt;span class=&quot;dim&quot;&gt;…&lt;&#x2F;span&gt;
@class SentrySpan;                    &lt;span class=&quot;dim&quot;&gt;&#x2F;&#x2F; fallback: forward-declares a CLASS of the same name&lt;&#x2F;span&gt;
@interface SentryTracer : NSObject &amp;lt;SentrySpan&amp;gt;&lt;&#x2F;pre&gt;
&lt;p&gt;ObjC keeps class and protocol names in separate namespaces. When the protocol exists, &lt;code&gt;&amp;lt;SentrySpan&amp;gt;&lt;&#x2F;code&gt; in that position resolves to it — valid. When only the forward-declared &lt;em&gt;class&lt;&#x2F;em&gt; exists, Clang parses &lt;code&gt;NSObject&amp;lt;SentrySpan&amp;gt;&lt;&#x2F;code&gt; as a generic specialization with a non-pointer type argument — the reported diagnostics. The fallback branch is invalid code that no compilation had ever selected.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Root cause&lt;&#x2F;p&gt;
&lt;p&gt;The isolated PCM action has no path to &lt;code&gt;&amp;lt;Sentry&#x2F;Sentry.h&amp;gt;&lt;&#x2F;code&gt; (no declared edge to the Sentry framework), so &lt;code&gt;__has_include&lt;&#x2F;code&gt; evaluates false for the first time in this codebase&#x27;s history, selecting the broken branch.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Fix&lt;&#x2F;p&gt;
&lt;p&gt;Declare the edge so the primary branch is selected. One subtlety: RSPM splits a mixed target into a parent plus per-language child targets; the failing action belongs to the generated clang child &lt;code&gt;SentryInternal.rspm_objc&lt;&#x2F;code&gt;. &lt;code&gt;target_deps&lt;&#x2F;code&gt; keys containing &lt;code&gt;.rspm&lt;&#x2F;code&gt; are matched against generated target names verbatim, allowing the edge to be attached exactly where the action runs: &lt;code&gt;&quot;SentryInternal.rspm_objc&quot;: [&quot;Sentry&quot;]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;General rule:&lt;&#x2F;strong&gt; &lt;code&gt;__has_include&lt;&#x2F;code&gt; fallback branches are unexercised code paths. The isolated PCM compile is often the first compilation ever to execute them; expect latent invalidity.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;issue&quot;&gt;
&lt;summary&gt;&lt;span class=&quot;num&quot;&gt;06&lt;&#x2F;span&gt;&lt;span&gt;&lt;span class=&quot;t&quot;&gt;LookinServer — &lt;code&gt;&#x27;LookinDefines.h&#x27; file not found&lt;&#x2F;code&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;s&quot;&gt;Headers depend on copts -I, which never reaches the PCM action · fixed via include-path rewrite&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;summary&gt;
&lt;div class=&quot;ibody&quot;&gt;
&lt;p class=&quot;q&quot;&gt;Failing action&lt;&#x2F;p&gt;
&lt;p&gt;PCM precompile of LookinServer (a debug-tooling package; the module is imported from Swift, so it cannot be suppressed). Its public headers use quote includes such as &lt;code&gt;#import &quot;LookinDefines.h&quot;&lt;&#x2F;code&gt; for files located in &lt;em&gt;other directories&lt;&#x2F;em&gt; of the package (&lt;code&gt;Src&#x2F;Main&#x2F;Shared&#x2F;&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Why it compiled under implicit mode&lt;&#x2F;p&gt;
&lt;p&gt;RSPM emits the package&#x27;s internal header search dirs as &lt;code&gt;-I&lt;&#x2F;code&gt; flags in &lt;code&gt;copts&lt;&#x2F;code&gt;. Object-file compilations receive copts, and implicit PCM builds ran inside compilations that had them. Quote includes that fail includer-relative lookup (Part 1) fell through to those &lt;code&gt;-I&lt;&#x2F;code&gt; dirs and resolved.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Root cause&lt;&#x2F;p&gt;
&lt;p&gt;The corollary from Part 5, verbatim: the PCM action takes its search paths from CcInfo, and copts &lt;code&gt;-I&lt;&#x2F;code&gt; flags are not in CcInfo. The PCM compile therefore has includer-relative lookup only, and &lt;code&gt;&quot;LookinDefines.h&quot;&lt;&#x2F;code&gt; is not in the includer&#x27;s directory.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Fix&lt;&#x2F;p&gt;
&lt;p&gt;A package patch rewriting the 24 cross-directory quote includes (15 headers) to includer-relative paths, e.g. &lt;code&gt;#import &quot;..&#x2F;..&#x2F;Shared&#x2F;LookinDefines.h&quot;&lt;&#x2F;code&gt;. Includer-relative resolution needs no flags at all (Part 1), so both the PCM action and ordinary object compilations resolve identically.&lt;&#x2F;p&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;General rule:&lt;&#x2F;strong&gt; module headers must be self-contained — resolvable without target-private flags. A header needing copts &lt;code&gt;-I&lt;&#x2F;code&gt; to find its own module&#x27;s files will fail explicit compilation.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;issue&quot;&gt;
&lt;summary&gt;&lt;span class=&quot;num&quot;&gt;07&lt;&#x2F;span&gt;&lt;span&gt;&lt;span class=&quot;t&quot;&gt;RudderFirebase — &lt;code&gt;no module named &#x27;RudderFirebase&#x27; declared in module map&lt;&#x2F;code&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;s&quot;&gt;Declared module name diverges from framework name · fixed by renaming the module&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;summary&gt;
&lt;div class=&quot;ibody&quot;&gt;
&lt;p class=&quot;q&quot;&gt;Failing action&lt;&#x2F;p&gt;
&lt;p&gt;PCM precompile of a first-party vendored xcframework. Under explicit mode the build system must name the module it is building: rules_apple derives that name from the xcframework&#x27;s library name (&lt;code&gt;RudderFirebase&lt;&#x2F;code&gt;) and instructs Clang to build &lt;em&gt;that&lt;&#x2F;em&gt; module from the map. The map declares &lt;code&gt;module Rudder_Firebase&lt;&#x2F;code&gt; — the requested module is not defined in it.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Why it compiled under implicit mode&lt;&#x2F;p&gt;
&lt;p&gt;Implicit lookup works in the opposite direction: the compiler scans available module maps for one declaring the name used at the import site (&lt;code&gt;import Rudder_Firebase&lt;&#x2F;code&gt;) and finds it. No component ever compared the declared name against the framework name.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Fix&lt;&#x2F;p&gt;
&lt;p&gt;First-party files, so no patch machinery: renamed the module to &lt;code&gt;RudderFirebase&lt;&#x2F;code&gt; in both platform slices&#x27; module maps and updated the single &lt;code&gt;import&lt;&#x2F;code&gt; statement. A sweep of all other vendored xcframeworks found no further mismatches.&lt;&#x2F;p&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;General rule:&lt;&#x2F;strong&gt; for imported frameworks, declared module name must equal the framework&#x2F;library name. Implicit mode treats this as a convention; explicit mode treats it as a contract.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;issue&quot;&gt;
&lt;summary&gt;&lt;span class=&quot;num&quot;&gt;08&lt;&#x2F;span&gt;&lt;span&gt;&lt;span class=&quot;t&quot;&gt;Mixed-language module — &lt;code&gt;cannot find &#x27;RudderBrazeFactory&#x27; in scope&lt;&#x2F;code&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;s&quot;&gt;-import-underlying-module is invisible to the aspect · fixed via per-target opt-out&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;summary&gt;
&lt;div class=&quot;ibody&quot;&gt;
&lt;p class=&quot;q&quot;&gt;Failing action&lt;&#x2F;p&gt;
&lt;p&gt;The Swift compile of a tracking module&#x27;s Swift half. The target is one half of a &lt;em&gt;mixed-language module&lt;&#x2F;em&gt;: ObjC and Swift halves sharing one module name. The Swift half sees the ObjC half via two hand-written copts: &lt;code&gt;-import-underlying-module&lt;&#x2F;code&gt; (&quot;load the Clang module with my own name&quot;) and &lt;code&gt;-Xcc -fmodule-map-file=…&lt;&#x2F;code&gt; (pointing at a hand-maintained module map).&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Root cause&lt;&#x2F;p&gt;
&lt;p&gt;That module map is referenced only from copts — it is not owned by any target the aspect visits, so no PCM action is scheduled for it (Part 3 §5: the aspect attaches actions to &lt;em&gt;targets&lt;&#x2F;em&gt;). Under implicit mode the compiler would simply build it on the fly; under explicit mode on-the-fly compilation is disabled, the underlying module fails to load, and every ObjC declaration is absent from the Swift compilation. The error is a symptom two layers removed from the cause.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Fix&lt;&#x2F;p&gt;
&lt;p&gt;This was the only target in the repository using the pattern. Applied a per-target feature opt-out (Part 3 §2):&lt;&#x2F;p&gt;
&lt;pre&gt;features = [&quot;-swift.use_c_modules&quot;]   &lt;span class=&quot;dim&quot;&gt;# this target compiles with implicit modules&lt;&#x2F;span&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The rest of the graph remains explicit. Long-term resolution: extract the ObjC half into its own named module and replace &lt;code&gt;-import-underlying-module&lt;&#x2F;code&gt; with an ordinary import.&lt;&#x2F;p&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;General rule:&lt;&#x2F;strong&gt; compilation inputs that exist only as flags (not as targets) are invisible to aspects. Per-target &lt;code&gt;-feature&lt;&#x2F;code&gt; disabling scopes exceptions without blocking a migration.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;details class=&quot;issue&quot;&gt;
&lt;summary&gt;&lt;span class=&quot;num&quot;&gt;09&lt;&#x2F;span&gt;&lt;span&gt;&lt;span class=&quot;t&quot;&gt;Legacy app targets — &lt;code&gt;duplicate input file &#x27;Support&#x27;&#x27;&lt;&#x2F;code&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;s&quot;&gt;Whitespace in include paths splits params-file arguments · fixed by renaming directories&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;summary&gt;
&lt;div class=&quot;ibody&quot;&gt;
&lt;p class=&quot;q&quot;&gt;Failing actions&lt;&#x2F;p&gt;
&lt;p&gt;PCM precompiles of two first-party legacy targets whose include paths contain directories with spaces (&lt;code&gt;Archive Support&lt;&#x2F;code&gt;, &lt;code&gt;Report User&lt;&#x2F;code&gt;, &lt;code&gt;Mark as sold process&lt;&#x2F;code&gt;, &lt;code&gt;Seller review&lt;&#x2F;code&gt; — names inherited from Xcode group structure). Long command lines are passed via a &lt;em&gt;params file&lt;&#x2F;em&gt; (a file of arguments referenced as &lt;code&gt;@file.params&lt;&#x2F;code&gt;); the new precompile code path splits these entries on whitespace, turning &lt;code&gt;-I…&#x2F;Archive Support&lt;&#x2F;code&gt; into two invalid tokens, reported as duplicate &#x2F; unopenable input files.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Why it compiled under implicit mode&lt;&#x2F;p&gt;
&lt;p&gt;The pre-existing compile actions quoted these arguments correctly. The failure is specific to argument handling in the SwiftPrecompileCModule path — the paths themselves were always hazardous, but no prior code path mishandled them.&lt;&#x2F;p&gt;
&lt;p class=&quot;q&quot;&gt;Fix&lt;&#x2F;p&gt;
&lt;p&gt;Renamed the four directories to CamelCase and updated the references in the build macro that declares these targets. Renaming removes the failure class; quoting workarounds would leave the hazard for the next tool.&lt;&#x2F;p&gt;
&lt;div class=&quot;lesson&quot;&gt;&lt;strong&gt;General rule:&lt;&#x2F;strong&gt; whitespace in source paths survives only as long as every tool in the chain quotes correctly. A migration that introduces new action types will exercise the hazard.&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;h2 id=&quot;part7&quot;&gt;Part 7 — Root-cause classification&lt;&#x2F;h2&gt;
&lt;p&gt;The nine failures reduce to four categories, each with a distinct diagnostic signature and fix mechanism.&lt;&#x2F;p&gt;
&lt;div class=&quot;diagram&quot;&gt;
&lt;div class=&quot;donut-wrap&quot;&gt;
&lt;svg viewBox=&quot;0 0 220 220&quot; role=&quot;img&quot; aria-label=&quot;Doughnut chart grouping the nine migration failures into four root-cause categories: missing dependency edges, 3 of 9; invalid vendor metadata, 3 of 9; toolchain coverage gaps, 2 of 9; and path hygiene, 1 of 9.&quot;&gt;
  &lt;g stroke=&quot;var(--bg-light)&quot; stroke-width=&quot;3&quot;&gt;
    &lt;path d=&quot;M110,20 A90 90 0 0 1 187.94,155 L156.77,137 A54 54 0 0 0 110,56 Z&quot; fill=&quot;var(--accent)&quot;&gt;&lt;title&gt;Missing dependency edges — 3 of 9&lt;&#x2F;title&gt;&lt;&#x2F;path&gt;
    &lt;path d=&quot;M187.94,155 A90 90 0 0 1 32.06,155 L63.23,137 A54 54 0 0 0 156.77,137 Z&quot; fill=&quot;var(--link)&quot;&gt;&lt;title&gt;Invalid vendor metadata — 3 of 9&lt;&#x2F;title&gt;&lt;&#x2F;path&gt;
    &lt;path d=&quot;M32.06,155 A90 90 0 0 1 52.15,41.06 L75.29,68.64 A54 54 0 0 0 63.23,137 Z&quot; fill=&quot;var(--accent-light)&quot;&gt;&lt;title&gt;Toolchain coverage gaps — 2 of 9&lt;&#x2F;title&gt;&lt;&#x2F;path&gt;
    &lt;path d=&quot;M52.15,41.06 A90 90 0 0 1 110,20 L110,56 A54 54 0 0 0 75.29,68.64 Z&quot; fill=&quot;var(--text-light)&quot;&gt;&lt;title&gt;Path hygiene — 1 of 9&lt;&#x2F;title&gt;&lt;&#x2F;path&gt;
  &lt;&#x2F;g&gt;
  &lt;text x=&quot;110&quot; y=&quot;105&quot; font-size=&quot;34&quot; font-weight=&quot;800&quot; fill=&quot;var(--fg-color)&quot; text-anchor=&quot;middle&quot;&gt;9&lt;&#x2F;text&gt;
  &lt;text x=&quot;110&quot; y=&quot;128&quot; font-size=&quot;12&quot; fill=&quot;var(--text-light)&quot; text-anchor=&quot;middle&quot;&gt;failures&lt;&#x2F;text&gt;
&lt;&#x2F;svg&gt;
&lt;ul class=&quot;legend&quot; id=&quot;xmodLegend&quot;&gt;
&lt;li&gt;&lt;span class=&quot;ldot&quot; style=&quot;background:var(--accent)&quot;&gt;&lt;&#x2F;span&gt;&lt;span&gt;&lt;strong&gt;Missing dependency edges — 3&lt;&#x2F;strong&gt;&lt;span class=&quot;ld&quot;&gt;Cases 1, 3, 5. Header-level imports with no corresponding deps edge. Fix: target_deps.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;span class=&quot;ldot&quot; style=&quot;background:var(--link)&quot;&gt;&lt;&#x2F;span&gt;&lt;span&gt;&lt;strong&gt;Invalid vendor metadata — 3&lt;&#x2F;strong&gt;&lt;span class=&quot;ld&quot;&gt;Cases 2, 4, 7. Module maps and headers that were wrong but never compiled. Fix: rules patch, source patch, rename.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;span class=&quot;ldot&quot; style=&quot;background:var(--accent-light)&quot;&gt;&lt;&#x2F;span&gt;&lt;span&gt;&lt;strong&gt;Toolchain coverage gaps — 2&lt;&#x2F;strong&gt;&lt;span class=&quot;ld&quot;&gt;Cases 6, 8. Inputs carried in copts or flags that the aspect cannot see. Fix: include rewrite, per-target opt-out.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;span class=&quot;ldot&quot; style=&quot;background:var(--text-light)&quot;&gt;&lt;&#x2F;span&gt;&lt;span&gt;&lt;strong&gt;Path hygiene — 1&lt;&#x2F;strong&gt;&lt;span class=&quot;ld&quot;&gt;Case 9. Whitespace in paths mishandled by the new action&#x27;s params files. Fix: rename.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;part8&quot;&gt;Part 8 — Diagnostic reference&lt;&#x2F;h2&gt;
&lt;p&gt;Lookup table for future &lt;code&gt;SwiftPrecompileCModule&lt;&#x2F;code&gt; failures.&lt;&#x2F;p&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Diagnostic&lt;&#x2F;th&gt;&lt;th&gt;Fix mechanism&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Binary framework header cannot resolve a sibling framework&#x27;s header&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;swift_deps.configure_package(target_deps = {…})&lt;&#x2F;code&gt;. Bare names for same-package targets; public product labels for cross-package.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Module map references nonexistent headers (headerless framework)&lt;&#x2F;td&gt;&lt;td&gt;rules_apple patch: skip SwiftInteropInfo when the imported framework ships no headers.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Required dep edge would create a cycle&lt;&#x2F;td&gt;&lt;td&gt;Source patch: guard the import with &lt;code&gt;__has_include&lt;&#x2F;code&gt;.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Quote includes resolvable only via copts &lt;code&gt;-I&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Rewrite to includer-relative paths, or move the dirs to the &lt;code&gt;includes&lt;&#x2F;code&gt; attribute. copts &lt;code&gt;-I&lt;&#x2F;code&gt; does not reach the PCM action.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Mixed module using &lt;code&gt;-import-underlying-module&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Per-target &lt;code&gt;features = [&quot;-swift.use_c_modules&quot;]&lt;&#x2F;code&gt;; restructure into separate modules later.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;no module named X declared in module map&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Align the declared module name with the framework&#x2F;library name; update import statements.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Malformed argument errors (e.g. &lt;code&gt;duplicate input file &#x27;Support&#x27;&#x27;&lt;&#x2F;code&gt;)&lt;&#x2F;td&gt;&lt;td&gt;A path containing whitespace is being split in the params file. Rename the directory.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Determining a binary framework&#x27;s full dependency set&lt;&#x2F;td&gt;&lt;td&gt;Audit three sources: ObjC headers, generated &lt;code&gt;-Swift.h&lt;&#x2F;code&gt;, and &lt;code&gt;.swiftinterface&lt;&#x2F;code&gt; imports.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;
&lt;&#x2F;table&gt;
&lt;p class=&quot;dim&quot; style=&quot;font-size:.88rem;&quot;&gt;Constraint on patches: &lt;code&gt;configure_package(patches)&lt;&#x2F;code&gt; are applied to package &lt;em&gt;sources&lt;&#x2F;em&gt; after clone but &lt;strong&gt;before&lt;&#x2F;strong&gt; binary artifacts are downloaded. They can modify source files (case 4) but can never modify xcframework contents — which is why case 2 required a rules-level patch instead.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;part9&quot;&gt;Part 9 — FAQ&lt;&#x2F;h2&gt;
&lt;details&gt;
&lt;summary&gt;A PCM precompile fails with &quot;file not found&quot;, but the same import compiles inside the app target. What does this indicate?&lt;&#x2F;summary&gt;
&lt;p&gt;A missing dependency edge. The app compile succeeds because its merged search paths (all deps&#x27; CcInfo) cover the header; the isolated PCM action sees only the owning target&#x27;s declared deps. Identify which target owns the missing header and declare the edge — for SPM binary targets, via &lt;code&gt;target_deps&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details&gt;
&lt;summary&gt;Does adding &lt;code&gt;-IPath&#x2F;To&#x2F;Headers&lt;&#x2F;code&gt; to a target&#x27;s copts fix a precompile resolution error?&lt;&#x2F;summary&gt;
&lt;p&gt;No. copts never enter CcInfo, and the aspect builds the PCM action&#x27;s command from CcInfo — forwarding only &lt;code&gt;-D&lt;&#x2F;code&gt; defines from copts. Use the &lt;code&gt;includes&lt;&#x2F;code&gt; attribute, a dependency whose CcInfo carries the path, or includer-relative includes in the headers.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details&gt;
&lt;summary&gt;A vendor header produces diagnostics inconsistent with code that demonstrably works elsewhere. What is the likely cause?&lt;&#x2F;summary&gt;
&lt;p&gt;An &lt;code&gt;__has_include&lt;&#x2F;code&gt; fallback branch is being compiled for the first time. The isolated PCM action cannot see a header that all prior compilations could, so the preprocessor selects a branch never previously compiled — which may be invalid. Make the primary branch&#x27;s header reachable in the PCM action (declare the edge).&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details&gt;
&lt;summary&gt;Why is the error attributed to an &quot;aspect&quot; rather than to my target?&lt;&#x2F;summary&gt;
&lt;p&gt;PCM actions are not declared by the failing target&#x27;s rule; they are attached by &lt;code&gt;swift_clang_module_aspect&lt;&#x2F;code&gt; as it walks the deps graph. The message &lt;em&gt;&quot;from aspects […] on objc_library rule target X&quot;&lt;&#x2F;em&gt; means: the aspect, visiting target X, registered this precompile action, and the action failed. The target to fix is X (or its missing dependency), not the top-level target you asked Bazel to build.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;details&gt;
&lt;summary&gt;What does explicit module compilation provide over implicit?&lt;&#x2F;summary&gt;
&lt;p&gt;PCM compilations become declared Bazel actions: remote-cacheable (one compilation per module per configuration, shared across all machines and CI), deterministic (independent of import order and consumer flags), schedulable in parallel ahead of Swift compiles, and validating (undeclared dependencies fail instead of resolving incidentally). Every failure in Part 6 corresponds to a defect that implicit compilation masked.&lt;&#x2F;p&gt;
&lt;&#x2F;details&gt;
&lt;h2 id=&quot;part10&quot;&gt;Part 10 — Glossary&lt;&#x2F;h2&gt;
&lt;dl class=&quot;gloss&quot;&gt;
&lt;dt&gt;module map&lt;&#x2F;dt&gt;&lt;dd&gt;File (&lt;code&gt;module.modulemap&lt;&#x2F;code&gt;) declaring that a set of headers forms a named Clang module. Framework modules resolve headers in &lt;code&gt;Headers&#x2F;&lt;&#x2F;code&gt;; an &lt;em&gt;umbrella header&lt;&#x2F;em&gt; defines contents by transitive inclusion.&lt;&#x2F;dd&gt;
&lt;dt&gt;PCM&lt;&#x2F;dt&gt;&lt;dd&gt;Precompiled module — serialized binary AST produced by compiling a module map&#x27;s headers once. Importers deserialize it instead of re-parsing headers.&lt;&#x2F;dd&gt;
&lt;dt&gt;implicit modules&lt;&#x2F;dt&gt;&lt;dd&gt;Compilation mode in which the compiler builds needed PCMs on demand, inside whichever compilation first imports them, inheriting that compilation&#x27;s flags. PCMs live in a compiler-private cache outside the build system.&lt;&#x2F;dd&gt;
&lt;dt&gt;explicit modules&lt;&#x2F;dt&gt;&lt;dd&gt;Compilation mode in which the build system schedules a dedicated, sandboxed action per module map (&lt;code&gt;SwiftPrecompileCModule&lt;&#x2F;code&gt;) and passes finished PCMs to consumers via &lt;code&gt;-fmodule-file=&lt;&#x2F;code&gt;. Enabled in rules_swift by &lt;code&gt;swift.emit_c_module&lt;&#x2F;code&gt; + &lt;code&gt;swift.use_c_modules&lt;&#x2F;code&gt;.&lt;&#x2F;dd&gt;
&lt;dt&gt;-I &#x2F; -F&lt;&#x2F;dt&gt;&lt;dd&gt;Header search path flags: &lt;code&gt;-I dir&lt;&#x2F;code&gt; for plain includes, &lt;code&gt;-F dir&lt;&#x2F;code&gt; for framework-style includes (&lt;code&gt;&amp;lt;Name&#x2F;Header.h&amp;gt;&lt;&#x2F;code&gt; → &lt;code&gt;dir&#x2F;Name.framework&#x2F;Headers&#x2F;Header.h&lt;&#x2F;code&gt;).&lt;&#x2F;dd&gt;
&lt;dt&gt;quote vs. angle include&lt;&#x2F;dt&gt;&lt;dd&gt;&lt;code&gt;&quot;X.h&quot;&lt;&#x2F;code&gt; searches the includer&#x27;s directory first, then &lt;code&gt;-I&lt;&#x2F;code&gt; dirs. &lt;code&gt;&amp;lt;Y&#x2F;Z.h&amp;gt;&lt;&#x2F;code&gt; searches &lt;code&gt;-I&lt;&#x2F;code&gt;, then &lt;code&gt;-F&lt;&#x2F;code&gt;, then system paths.&lt;&#x2F;dd&gt;
&lt;dt&gt;__has_include&lt;&#x2F;dt&gt;&lt;dd&gt;Preprocessor operator testing whether a header is resolvable under the current compilation&#x27;s search paths. Evaluates differently per compilation; guards become first-compiled code paths under explicit modules.&lt;&#x2F;dd&gt;
&lt;dt&gt;target &#x2F; label &#x2F; deps&lt;&#x2F;dt&gt;&lt;dd&gt;Bazel&#x27;s unit of build, its address (&lt;code&gt;&#x2F;&#x2F;pkg:name&lt;&#x2F;code&gt;, external: &lt;code&gt;@repo&#x2F;&#x2F;pkg:name&lt;&#x2F;code&gt;), and the attribute forming the dependency graph.&lt;&#x2F;dd&gt;
&lt;dt&gt;action&lt;&#x2F;dt&gt;&lt;dd&gt;A concrete command with declared inputs&#x2F;outputs, run in a sandbox containing only declared inputs. Unit of caching and parallelism.&lt;&#x2F;dd&gt;
&lt;dt&gt;CcInfo &#x2F; SwiftInfo&lt;&#x2F;dt&gt;&lt;dd&gt;Providers carrying compilation context along deps edges. CcInfo: headers, defines, search paths (from &lt;code&gt;includes&lt;&#x2F;code&gt;, not &lt;code&gt;copts&lt;&#x2F;code&gt;). SwiftInfo: Swift modules and, under explicit mode, Clang module artifacts.&lt;&#x2F;dd&gt;
&lt;dt&gt;aspect&lt;&#x2F;dt&gt;&lt;dd&gt;A graph traversal attaching extra actions&#x2F;providers to existing targets. &lt;code&gt;swift_clang_module_aspect&lt;&#x2F;code&gt; attaches PCM precompile actions to every module-map-owning target reachable through deps.&lt;&#x2F;dd&gt;
&lt;dt&gt;copts vs. includes&lt;&#x2F;dt&gt;&lt;dd&gt;&lt;code&gt;copts&lt;&#x2F;code&gt;: private flags for the target&#x27;s own object compilations; not propagated, not visible to PCM actions (except &lt;code&gt;-D&lt;&#x2F;code&gt;). &lt;code&gt;includes&lt;&#x2F;code&gt;: search dirs entering CcInfo, visible to dependents and PCM actions.&lt;&#x2F;dd&gt;
&lt;dt&gt;xcframework&lt;&#x2F;dt&gt;&lt;dd&gt;Distribution bundle containing one prebuilt framework per platform slice. Imported into Bazel by &lt;code&gt;apple_static_xcframework_import&lt;&#x2F;code&gt; &#x2F; &lt;code&gt;apple_dynamic_xcframework_import&lt;&#x2F;code&gt;.&lt;&#x2F;dd&gt;
&lt;dt&gt;RSPM &#x2F; target_deps&lt;&#x2F;dt&gt;&lt;dd&gt;rules_swift_package_manager: generates Bazel BUILD files for SPM packages. &lt;code&gt;configure_package(target_deps)&lt;&#x2F;code&gt; (≥1.17) injects dependency edges into generated targets — required for binary targets, which SPM manifests cannot give deps.&lt;&#x2F;dd&gt;
&lt;dt&gt;-Swift.h&lt;&#x2F;dt&gt;&lt;dd&gt;Generated ObjC header exposing a framework&#x27;s Swift API to ObjC. Part of the umbrella; may contain &lt;code&gt;@import&lt;&#x2F;code&gt; of other modules, contributing to the module dependency set.&lt;&#x2F;dd&gt;
&lt;dt&gt;.swiftinterface&lt;&#x2F;dt&gt;&lt;dd&gt;Textual Swift module interface shipped in binary frameworks; compiled by the build into a binary swiftmodule. Its &lt;code&gt;import&lt;&#x2F;code&gt; statements must all be resolvable, contributing to the dependency set.&lt;&#x2F;dd&gt;
&lt;dt&gt;-import-underlying-module&lt;&#x2F;dt&gt;&lt;dd&gt;Swift flag loading the Clang module with the same name as the module being compiled — the mixed-language module pattern. The module map involved is supplied by flags, not owned by a target, making it invisible to aspects.&lt;&#x2F;dd&gt;
&lt;dt&gt;params file&lt;&#x2F;dt&gt;&lt;dd&gt;File holding a long argument list, passed to a tool as &lt;code&gt;@file.params&lt;&#x2F;code&gt; to avoid OS command-length limits. Argument splitting bugs in params handling surface as malformed-path errors.&lt;&#x2F;dd&gt;
&lt;&#x2F;dl&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;div class=&quot;callout&quot;&gt;
&lt;p class=&quot;rule&quot;&gt;Header resolution is a function of the running compilation&#x27;s flags. Implicit mode builds modules inside consumer compilations, so undeclared dependencies resolve through borrowed flags. Explicit mode builds each module in a sandboxed action with only declared inputs, so the module graph and the target graph must agree. Each of the nine failures was a point of disagreement: an undeclared edge, invalid metadata never previously evaluated, an input invisible to the aspect, or a path hazard exercised by the new action type.&lt;&#x2F;p&gt;
&lt;p class=&quot;corollary&quot;&gt;App target green with &lt;code&gt;swift.use_c_modules&lt;&#x2F;code&gt; · June 2026&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;script&gt;
function xmodShowMode(mode) {
  var isImplicit = mode === &#x27;implicit&#x27;;
  document.getElementById(&#x27;panelImplicit&#x27;).classList.toggle(&#x27;active&#x27;, isImplicit);
  document.getElementById(&#x27;panelExplicit&#x27;).classList.toggle(&#x27;active&#x27;, !isImplicit);
  document.getElementById(&#x27;btnImplicit&#x27;).classList.toggle(&#x27;active&#x27;, isImplicit);
  document.getElementById(&#x27;btnExplicit&#x27;).classList.toggle(&#x27;active&#x27;, !isImplicit);
  document.getElementById(&#x27;btnImplicit&#x27;).setAttribute(&#x27;aria-selected&#x27;, isImplicit);
  document.getElementById(&#x27;btnExplicit&#x27;).setAttribute(&#x27;aria-selected&#x27;, !isImplicit);
}
&lt;&#x2F;script&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Thoughts on Agentic tooling and future of engineering</title>
        <published>2026-03-15T00:00:00+00:00</published>
        <updated>2026-03-15T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/blog/agent-world-skills/"/>
        <id>https://imostfa.github.io/Blog/blog/agent-world-skills/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/blog/agent-world-skills/">&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;imostfa.github.io&#x2F;Blog&#x2F;blog&#x2F;agent-world-skills&#x2F;cover.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;With the current rise of agentic coding tools, many (and I’m included) are starting to wonder, what skills will actually matter for engineers in the future? If agents can write code, tests, and even design systems, what does that leave for us? My take on this varies depending on the topic. Here I’m trying to break down my thoughts.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;how-did-you-become-highly-skilled&quot;&gt;How Did You Become Highly Skilled?&lt;&#x2F;h3&gt;
&lt;p&gt;No matter how you started programming, likely the process of learning is the same:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;You start by learning the syntax (the building blocks)&lt;&#x2F;li&gt;
&lt;li&gt;You make small projects, and you learn how to use the building blocks to create something useful.&lt;&#x2F;li&gt;
&lt;li&gt;You try to create more complex projects, make mistakes, and learn from them over time. -Repeat-&lt;&#x2F;li&gt;
&lt;li&gt;You start to develop deep understanding of how the tools work, and study&#x2F;understand the details. -Repeat-&lt;&#x2F;li&gt;
&lt;li&gt;Your scope gets wider, and you start to build wider&#x2F;more complex systems, and you learn how to design systems -Repeat-&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;And eventually you become a skilled engineer, and start to move from thinking about code to thinking about systems, and how to design them, and how to make them work together.&lt;&#x2F;p&gt;
&lt;p&gt;What truly makes you skilled is the cycle of learning, making mistakes, and growing from them.&lt;&#x2F;p&gt;
&lt;p&gt;We (engineers) used to say that “Programming isn’t all about writing code, it’s more than this” which is true, and false at the same time.&lt;&#x2F;p&gt;
&lt;p&gt;When you are building an MVP, it’s actually more about writing code, and less about design, because you are trying to quickly validate an idea, and you don’t care about the design at this stage. As you start to grow the project, you notice that you are writing less code and thinking more about design, and how to make the system work together, and how to make it scalable, maintainable, and so on.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;most-projects-are-simpler-than-you-think&quot;&gt;Most Projects Are Simpler Than You Think&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;repetitive-knowledge-and-tools&quot;&gt;Repetitive Knowledge and Tools&lt;&#x2F;h4&gt;
&lt;p&gt;The reality is that many projects don’t require extensive knowledge or deep expertise. Often, they involve repeating the same basic concepts and tools you are already familiar with.&lt;&#x2F;p&gt;
&lt;p&gt;To elaborate, think about the last project you worked on. How many times did you find yourself using the same libraries, frameworks, and design patterns? The truth is that many projects are built on top of existing tools and technologies, and they don’t require deep expertise to be successful.&lt;&#x2F;p&gt;
&lt;p&gt;Because of this, agentic tools can handle these projects easily.&lt;&#x2F;p&gt;
&lt;p&gt;LLMs are trained on these problems 100x more than any human, so they’re likely faster and better at solving them.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;just-fix-it&quot;&gt;Just Fix It&lt;&#x2F;h4&gt;
&lt;p&gt;As projects grow, you start hitting harder problems - scaling, performance, or features that keep breaking each other. These are the problems where real expertise starts to matter.&lt;&#x2F;p&gt;
&lt;p&gt;LLMs are good at solving these problems, but they’re not great at weighing trade-offs or thinking about cost and efficiency. For any problem, there are multiple solutions. Agents tend to pick one that works - often ignoring cost, efficiency, or long-term impact.&lt;&#x2F;p&gt;
&lt;p&gt;If you’re dealing with a simple project (like a CRUD app), this might not be a concern. You could easily rely on tools like Claude, OpenCode, or Codex to solve these issues. However, what is often overlooked in this approach is the efficiency (time) and cost at which the solution is produced. How confident you are in that solution.&lt;&#x2F;p&gt;
&lt;p&gt;Some projects never need to worry about this - they work fine without any optimization. But as a project grows, these trade-offs start to matter more. Bigger systems need more careful decisions to stay healthy over time.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;what-still-needs-to-be-yours&quot;&gt;What Still Needs to Be Yours&lt;&#x2F;h4&gt;
&lt;h5 id=&quot;rejection-meter&quot;&gt;Rejection Meter&lt;&#x2F;h5&gt;
&lt;p&gt;Knowing when to reject an agent’s suggestion is what ultimately matters. To say no, you must have a clear understanding of the issue you are solving. It is easy to fall into the trap of relying on agents entirely, but this approach only works up to a certain point.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Example 1: Writing tests is more enjoyable than ever, but I once found a generated test case that used reflection to access private fields. While the test passed, it was brittle and coupled to implementation details - a clear case where saying &lt;code&gt;no&lt;&#x2F;code&gt; and asking for a better approach was the right call.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h5 id=&quot;the-fundamentals&quot;&gt;The Fundamentals&lt;&#x2F;h5&gt;
&lt;p&gt;Understanding how computer systems work still matters. Keep in mind that agents are good at coding because of their ability to detect patterns and replicate them very well. They mimic thinking by replaying patterns from their training data. But they don’t actually understand how computer systems work.&lt;&#x2F;p&gt;
&lt;p&gt;Example 2:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;While analyzing a performance issue in a project, the agentic tools said that based on the profiling information there’s a bottleneck in one function, and it’s caused by a very big switch case statement. A big switch case is O(N) time complexity, and it is not efficient. So the agentic tool suggested to refactor the code to use a hash map instead of a switch case, which is O(1) time complexity, and it will improve the performance of the function.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;While the function was indeed a bottleneck according to the profiler, what caught my attention was that a switch case isn’t O(N), instead in modern languages, it’s implemented as a jump table, which is O(1) time complexity. (think of a HashMap &#x2F;dictionary)&lt;&#x2F;p&gt;
&lt;p&gt;I’ve seen many mistakes like this. The lesson: you need to understand the technology to solve problems efficiently. If I hadn’t caught this, I would have refactored the code and released it - only to find it didn’t help. Or worse, the agent would have kept trying different theories that were also wrong.&lt;&#x2F;p&gt;
&lt;h5 id=&quot;stackoverflow-era-vs-agentic-coding-era&quot;&gt;Stackoverflow Era vs Agentic coding era&lt;&#x2F;h5&gt;
&lt;p&gt;Back in the old days (that I hope you still remember), many devs relied heavily on StackOverflow to solve their coding problems. They would search for solutions, copy and paste code snippets, and call it a day, and depending on what you work on, maybe you can live with this forever.&lt;&#x2F;p&gt;
&lt;p&gt;And I don’t see any difference between this and relying on agentic coding tools, it’s just way easier, and way cheaper (compared to the output you get) to get things working, and issues fixed.&lt;&#x2F;p&gt;
&lt;h5 id=&quot;10-10-multiplier&quot;&gt;[-10…10] Multiplier&lt;&#x2F;h5&gt;
&lt;p&gt;The output you get from coding agents depends heavily on your own expertise. The multiplier isn’t always positive - if you blindly accept bad suggestions, it can go negative. If you are skilled at the problem you are solving, you will get 10x results; if not, expect 10x worse results - and the gap compounds over time.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;so-are-agents-going-to-replace-us&quot;&gt;So are agents going to replace us?&lt;&#x2F;h3&gt;
&lt;p&gt;Agents have already replaced a lot of the repetitive parts of software development, and they’ll keep doing so. But they won’t replace the parts of the work that require deep expertise, real systems thinking, and careful design decisions.&lt;&#x2F;p&gt;
&lt;p&gt;Many will disagree - some already feel that agents have replaced the need for developers entirely. This is simply because they’re working on projects that LLMs already solve easily, and they haven’t yet hit the point where trade-offs or design decisions require deep understanding. Efficiency isn’t a concern for them either, because whatever they’re facing, it’s still cheaper to solve with agents than with humans.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-should-i-focus-on-learning&quot;&gt;What should i focus on learning?&lt;&#x2F;h3&gt;
&lt;ol&gt;
&lt;li&gt;Understanding the details of what you are working on.&lt;&#x2F;li&gt;
&lt;li&gt;Understadning the trade-offs and implications of different solutions.&lt;&#x2F;li&gt;
&lt;li&gt;Understanding how to use the new tools effectively, and how to evaluate their output critically.&lt;&#x2F;li&gt;
&lt;li&gt;Find and invest into making the validation of soltions faster and easier, if creating solutions is easier, validating them should be easier too. This is where the real bottleneck is, and where the real value is.&lt;&#x2F;li&gt;
&lt;li&gt;Widen your scope, and learn how to design systems how to be a product owner, and how to make sure the system is healthy over time, and how to make sure it can grow and scale as needed.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Stop Using Tools That Are Just Good Enough</title>
        <published>2026-03-15T00:00:00+00:00</published>
        <updated>2026-03-15T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/blog/upgrade-your-tools/"/>
        <id>https://imostfa.github.io/Blog/blog/upgrade-your-tools/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/blog/upgrade-your-tools/">&lt;p&gt;Every day you spend using a tool that’s merely “good enough” is time you’re paying a tax you don’t have to pay — slower edits, unnecessary friction, workflows that almost fit but never quite do. The cost is invisible because you never see the version of you that switched earlier.&lt;&#x2F;p&gt;
&lt;p&gt;Here I will share two tools that have impacted my workflow and made it more efficient, and I think you should give them a try.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;vim-motions&quot;&gt;Vim Motions&lt;&#x2F;h2&gt;
&lt;p&gt;I spend most of my day in text — writing code, editing prompts, drafting messages. Even with the rise of LLMs and agentic tooling, you’re still the one navigating and shaping the words. When I started using vim motions, I stopped thinking about &lt;em&gt;how&lt;&#x2F;em&gt; to move and started thinking about &lt;em&gt;what&lt;&#x2F;em&gt; to say.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;why-vim-motions&quot;&gt;Why Vim Motions?&lt;&#x2F;h3&gt;
&lt;p&gt;Most text operations are repetitive and can be described in two words: delete word, change inside quotes, select around argument. Without vim motions, you reach for the mouse or arrow keys — it works, but it’s slow and pulls your hands out of rhythm.&lt;&#x2F;p&gt;
&lt;p&gt;The key insight is that motions &lt;em&gt;compose&lt;&#x2F;em&gt;. You learn a handful of verbs (&lt;code&gt;d&lt;&#x2F;code&gt;, &lt;code&gt;c&lt;&#x2F;code&gt;, &lt;code&gt;y&lt;&#x2F;code&gt;) and a handful of nouns (&lt;code&gt;w&lt;&#x2F;code&gt;, &lt;code&gt;b&lt;&#x2F;code&gt;, &lt;code&gt;i&lt;&#x2F;code&gt;, &lt;code&gt;aa&lt;&#x2F;code&gt;), and suddenly you have a vocabulary for almost any edit. The combinations multiply your ability without multiplying what you have to memorize.&lt;&#x2F;p&gt;
&lt;p&gt;You don’t need to switch to Neovim. Just enable vim mode in your current editor — VS Code, JetBrains, or even Claude’s prompt input supports it.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;To delete a word, you press &lt;code&gt;dw&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;To select an argument of a function call, you press &lt;code&gt;vaa&lt;&#x2F;code&gt; (visual around argument) — then press &lt;code&gt;d&lt;&#x2F;code&gt; to delete it&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;script src=&quot;https:&#x2F;&#x2F;asciinema.org&#x2F;a&#x2F;Ge366jSGE4jhndLK.js&quot; id=&quot;asciicast-Ge366jSGE4jhndLK&quot; async=&quot;true&quot;&gt;&lt;&#x2F;script&gt;
&lt;p&gt;This is a short clip where i’m trying to show you how fast and fluid it is to edit text with vim motions. (note: the contextual visual selecting is
powered by a plugin in Neovim, and i can’t live without it)&lt;&#x2F;p&gt;
&lt;p&gt;It takes time to get used to, but once you do, you won’t want to go back. And if you’ve ever played a game where movement is muscle memory, you already know how this feels — it’s the same rewiring, just for text instead of a character. Getting comfortable with vim motions is honestly easier than adopting the next tool I’m going to talk about — and that one will challenge everything you thought you knew about version control.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;jujutsu-jj&quot;&gt;Jujutsu (JJ)&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;Jujutsu&lt;&#x2F;code&gt; is a version control system that’s &lt;code&gt;git&lt;&#x2F;code&gt; compatible, but it’s way better than &lt;code&gt;git&lt;&#x2F;code&gt;. Before you say “but I already know git, why should I learn a new tool? git works” — I want you to believe me. I used to be what they call a git wizard; things like &lt;code&gt;rerere&lt;&#x2F;code&gt;, &lt;code&gt;git reflog&lt;&#x2F;code&gt;, &lt;code&gt;merge drivers&lt;&#x2F;code&gt; were part of my workflow every day.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;why-jujutsu&quot;&gt;Why Jujutsu?&lt;&#x2F;h3&gt;
&lt;p&gt;Git has become the English language of version control, and it’s a great tool, but it’s not perfect. Here I will list some of the problems with the design of git itself. None of these problems are a dealbreaker, but they are small things that add up to make your workflow less efficient and more cognitively overloaded.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;In Git you create a commit after you finish working, not before you start working on it&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Did it happen to you that you start working on something, and once you finish you find that you ended up with many files that you have to break down into smaller commits?&lt;&#x2F;p&gt;
&lt;p&gt;This problem usually happens because in git you don’t have a way to describe what you will work on beforehand. In many cases you already know what you are going to work on before you start.&lt;&#x2F;p&gt;
&lt;p&gt;In Jujutsu, you start your upcoming work by doing &lt;code&gt;jj new -m &quot;Description of the work&quot;&lt;&#x2F;code&gt; — this creates a new empty change (think: an empty box that you start adding changes to).&lt;&#x2F;p&gt;
&lt;p&gt;Once you finish, you can start working on something else with &lt;code&gt;jj new&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Notice:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;You didn’t need to stage your files — they are staged by default&lt;&#x2F;li&gt;
&lt;li&gt;It’s mentally easier to break down your work since you can already see the description of what you are doing&lt;&#x2F;li&gt;
&lt;li&gt;You can always do &lt;code&gt;jj describe -m&lt;&#x2F;code&gt; if you find that you are going to do more things, or &lt;code&gt;jj split&lt;&#x2F;code&gt; to split your changes into smaller ones&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;script src=&quot;https:&#x2F;&#x2F;asciinema.org&#x2F;a&#x2F;cBjymthUiJo4zNlU.js&quot; id=&quot;asciicast-cBjymthUiJo4zNlU&quot; async=&quot;true&quot;&gt;&lt;&#x2F;script&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;You don’t need a branch name until you need to push your work upstream (GitHub PR)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;How many times have you created a branch name just to start working on something, only to realize that you don’t need to push it upstream?
Branch names are only useful when you want to push upstream (share work in general).&lt;&#x2F;p&gt;
&lt;p&gt;JJ is branchless by default. You can set a name for a commit (change) only if you need to (mostly to push to GitHub); otherwise, you can use the change ID (think: commit SHA) to switch between changes.&lt;&#x2F;p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;You don’t really need the staging area&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;The issue with the staging area is that it creates two states for any file. If most of the time you do &lt;code&gt;git add .&lt;&#x2F;code&gt;, then staging is just an extra step that’s mostly useless.&lt;&#x2F;p&gt;
&lt;p&gt;In JJ there’s no staging area — everything is staged by default, and you have the following tools instead.&lt;&#x2F;p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;Conflicts shouldn’t be blocking for your workflow&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;In Git, conflicts are a blocking state — you can’t do anything until you resolve the conflict, which isn’t usually what you want. Maybe you want to do other things first, then resolve conflicts.&lt;&#x2F;p&gt;
&lt;p&gt;In JJ it informs you that there’s a conflict, and you can decide to solve it now or ignore it.
This is why in Jujutsu, rebases are effortless — I just solve them after the rebase process is over.&lt;&#x2F;p&gt;
&lt;p&gt;These four problems are just the ones worth naming — there are more. The point isn’t that git is bad; it’s that you’ve been absorbing its rough edges for so long you’ve stopped noticing them. I’ve been using Jujutsu for a while now and I can’t imagine going back. Give it a try.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>About</title>
        <published>2025-03-14T00:00:00+00:00</published>
        <updated>2025-03-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/about/"/>
        <id>https://imostfa.github.io/Blog/about/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/about/">&lt;p&gt;Hey, I’m &lt;strong&gt;Mostafa&lt;&#x2F;strong&gt; - a computer engineer who roams across many domains, always chasing the &lt;em&gt;why&lt;&#x2F;em&gt; behind how things work. When I’m not in code, I’m into music and BodyPump classes.&lt;&#x2F;p&gt;
&lt;p&gt;Most of what I post here is what I’m actively learning - writing is the best way I know to document it.&lt;&#x2F;p&gt;
&lt;p&gt;I’m particularly drawn to fundamentals: how systems are built, how abstractions leak, and what sits underneath what we take for granted.&lt;&#x2F;p&gt;
&lt;p&gt;I graduated in Computer Engineering from Egypt (2016–2021). My graduation project was an electrical circuit solver powered by machine learning and computer vision - which also sparked my early obsession with CV.&lt;&#x2F;p&gt;
&lt;p&gt;My first real language was Objective-C (yes, before Swift existed - and yes, HTML doesn’t count). From there I wandered into Computer Vision, then Embedded programming, and now I find myself deep in compilers, Rust, LLMs, and hermetic build systems. The interests shift; the curiosity doesn’t.&lt;&#x2F;p&gt;
&lt;p&gt;Find me on &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;iMostfa&quot;&gt;GitHub&lt;&#x2F;a&gt; · &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;x.com&#x2F;KallamMostafa&quot;&gt;Twitter&lt;&#x2F;a&gt; · &lt;a href=&quot;&#x2F;resume.pdf&quot;&gt;Resume&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Curiosity has always driven me more than staying in a lane.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;currently-into&quot;&gt;Currently into&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;Compilers&lt;&#x2F;li&gt;
&lt;li&gt;Rust&lt;&#x2F;li&gt;
&lt;li&gt;LLMs - how to build with them, and how they actually work&lt;&#x2F;li&gt;
&lt;li&gt;Hermetic builds (Bazel)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;previously-into&quot;&gt;Previously into&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;Computer Vision&lt;&#x2F;li&gt;
&lt;li&gt;Embedded programming&lt;&#x2F;li&gt;
&lt;li&gt;Objective-C (my first real language)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>How to Implement Volume Casting in SceneKit</title>
        <published>2022-08-07T00:00:00+00:00</published>
        <updated>2022-08-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/blog/volume-casting-scenekit/"/>
        <id>https://imostfa.github.io/Blog/blog/volume-casting-scenekit/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/blog/volume-casting-scenekit/">&lt;h1 id=&quot;how-to-implement-volume-casting-in-scenekit&quot;&gt;How to Implement Volume Casting in SceneKit&lt;&#x2F;h1&gt;
&lt;p&gt;In this post we’ll talk about implementing a feature that doesn’t exist natively in SceneKit. To get there, we need to understand the concept first, identify the problem, then figure out how to solve it in code.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-is-a-ray&quot;&gt;What is a Ray?&lt;&#x2F;h2&gt;
&lt;p&gt;In the context of computer graphics, a &lt;strong&gt;ray&lt;&#x2F;strong&gt; is a point with a direction - another name for a vector.&lt;&#x2F;p&gt;
&lt;p&gt;Imagine a laser pointer in your hand: it has a position and a direction (where it’s pointing).&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;rPos = ray position in space&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;rDir = ray direction in space&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;what-is-ray-casting&quot;&gt;What is Ray Casting?&lt;&#x2F;h2&gt;
&lt;p&gt;Ray casting is the process of determining whether a ray intersects with anything in a 3D scene.&lt;&#x2F;p&gt;
&lt;p&gt;Since a ray has a position and a direction, you can extend it through space - until it hits something. In that example, the ray gets extended until it intersects the sphere in the middle. Now you know the ray is hitting that sphere.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;a-practical-example&quot;&gt;A Practical Example&lt;&#x2F;h2&gt;
&lt;p&gt;One of the most common uses of ray casting is with mouse&#x2F;touch input.&lt;&#x2F;p&gt;
&lt;p&gt;Imagine you have a screen full of 3D objects, and you want to know which object the user tapped. When the user taps, a ray is fired from the tap location - it travels until it hits an object. If it hits nothing, the user tapped empty space.&lt;&#x2F;p&gt;
&lt;p&gt;The challenge here: the tap is in 2D screen space, but the objects live in a 3D world. Ray casting is what bridges the two - it converts the 2D tap into a 3D ray and checks what it intersects.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;hit-testing-in-game-engines&quot;&gt;Hit Testing in Game Engines&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;unity-ray-casting&quot;&gt;Unity Ray Casting&lt;&#x2F;h3&gt;
&lt;p&gt;Since hit testing is fundamental, every game engine has it built in. In Unity:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ray ray &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; Camera.main.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;ScreenPointToRay&lt;&#x2F;span&gt;&lt;span&gt;(Input.mousePosition);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;RaycastHit hit;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (Physics.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;Raycast&lt;&#x2F;span&gt;&lt;span&gt;(ray, out hit, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Debug.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;Log&lt;&#x2F;span&gt;&lt;span&gt;(hit.transform.name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Debug.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;Log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;hit&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;Physics.Raycast&lt;&#x2F;code&gt; handles all the math.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;scenekit-hit-testing&quot;&gt;SceneKit Hit Testing&lt;&#x2F;h3&gt;
&lt;p&gt;In SceneKit (Apple calls it “hit testing”):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; locationOfTapIn2D &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; tapRecognizer.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;location&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span&gt;: sceneView)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; hitResults &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; diagramView.sceneView.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;hitTest&lt;&#x2F;span&gt;&lt;span&gt;(locationOfTapIn2D, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;options&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; nil&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; tappedNode &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; hitResults.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;first&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;.node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt; &#x2F;&#x2F; the object you have tapped&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;what-ray-casting-can-t-do&quot;&gt;What Ray Casting Can’t Do&lt;&#x2F;h2&gt;
&lt;p&gt;Imagine your app needs to find the &lt;strong&gt;closest objects&lt;&#x2F;strong&gt; to a tap, not just the one directly under it.&lt;&#x2F;p&gt;
&lt;p&gt;Say the user taps in empty space. You want to know which objects are nearby - the 4 or 5 closest ones. This becomes even more useful when objects overlap and you can’t tell which one the user intended.&lt;&#x2F;p&gt;
&lt;p&gt;For example: the user taps near a small gray area that partially overlaps a large orange shape. Did they mean to tap the gray one or the orange one? Standard hit testing can only tell you which one is directly under the ray - which may not be what the user wanted.&lt;&#x2F;p&gt;
&lt;p&gt;Ray casting fires a single ray from the tap point. When it hits something, it stops. So if the tap falls in empty space between objects, it finds nothing at all.&lt;&#x2F;p&gt;
&lt;p&gt;What we want is something like &lt;strong&gt;widening the ray’s diameter&lt;&#x2F;strong&gt; - so instead of a single-point pierce, we get a volume that can intersect multiple nearby objects at once.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;other-types-of-casting&quot;&gt;Other Types of Casting&lt;&#x2F;h2&gt;
&lt;p&gt;Some game engines solve this by letting you cast a &lt;strong&gt;shape&lt;&#x2F;strong&gt; instead of a ray:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;BoxCast 📦&lt;&#x2F;li&gt;
&lt;li&gt;CapsuleCast 💊&lt;&#x2F;li&gt;
&lt;li&gt;SphereCast 🏀&lt;&#x2F;li&gt;
&lt;li&gt;RigidBodyCast 🥎&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The idea: instead of casting a ray, place a box at the hit point and check which objects it overlaps. That’s &lt;strong&gt;volume casting&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;volume-casting-in-scenekit&quot;&gt;Volume Casting in SceneKit&lt;&#x2F;h2&gt;
&lt;p&gt;SceneKit doesn’t have volume casting built in. Here’s how to approximate it:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Steps:&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Detect the touch location using normal hit testing (convert 2D → 3D)&lt;&#x2F;li&gt;
&lt;li&gt;Place an invisible box at that 3D point&lt;&#x2F;li&gt;
&lt;li&gt;Check which objects intersect with the box&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;technical-implementation&quot;&gt;Technical Implementation&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;1-detect-touch-location&quot;&gt;1. Detect Touch Location&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;&#x2F;&#x2F; Inside the tap gesture selector&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; locationOfTapIn2D &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; tapRecognizer.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;location&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span&gt;: sceneView)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; hitResults &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; diagramView.sceneView.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;hitTest&lt;&#x2F;span&gt;&lt;span&gt;(locationOfTapIn2D, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;options&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; nil&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; firstTappedNode &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; hitResults.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;first&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt; &#x2F;&#x2F; the object you have tapped&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;2-place-the-box&quot;&gt;2. Place the Box&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; boxHeight &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; 12&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; boxGeometry &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt; SCNBox&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;width&lt;&#x2F;span&gt;&lt;span&gt;: boxHeight,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;                         height&lt;&#x2F;span&gt;&lt;span&gt;: boxHeight,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;                         length&lt;&#x2F;span&gt;&lt;span&gt;: boxHeight,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;                         chamferRadius&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; boxNode &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt; SCNNode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;geometry&lt;&#x2F;span&gt;&lt;span&gt;: boxGeometry)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;&#x2F;&#x2F; Add to root node (world space)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sceneView&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;.scene&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;.rootNode.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;addChildNode&lt;&#x2F;span&gt;&lt;span&gt;(boxNode)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;&#x2F;&#x2F; Place the box at the touch coordinates&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;boxNode.worldPosition&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; firstHit.worldCoordinates&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;boxNode.name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;VOLUME_CASTING_BOX&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;&#x2F;&#x2F; Hide it - the user shouldn&amp;#39;t see it&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;boxNode.isHidden&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;&#x2F;&#x2F; Physics body is required so we can check intersections&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;boxNode.physicsBody&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;init&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span&gt;: .kinematic,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;                             shape&lt;&#x2F;span&gt;&lt;span&gt;: .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;init&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;node&lt;&#x2F;span&gt;&lt;span&gt;: boxNode, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;options&lt;&#x2F;span&gt;&lt;span&gt;: [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;]))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;3-check-intersections&quot;&gt;3. Check Intersections&lt;&#x2F;h3&gt;
&lt;p&gt;The function &lt;code&gt;contactTest(with:)&lt;&#x2F;code&gt; returns an array of &lt;code&gt;SCNPhysicsContact&lt;&#x2F;code&gt; - each contact represents one object intersecting with our box.&lt;&#x2F;p&gt;
&lt;p&gt;Each &lt;code&gt;SCNPhysicsContact&lt;&#x2F;code&gt; gives you &lt;code&gt;nodeA&lt;&#x2F;code&gt; and &lt;code&gt;nodeB&lt;&#x2F;code&gt;. In our case, &lt;code&gt;nodeA&lt;&#x2F;code&gt; will be our box, and &lt;code&gt;nodeB&lt;&#x2F;code&gt; will be the intersecting object.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; boxContacts &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; scene.physicsWorld.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;contactTest&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;with&lt;&#x2F;span&gt;&lt;span&gt;: boxNode.physicsBody&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;options&lt;&#x2F;span&gt;&lt;span&gt;: [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; boxContacts.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;count&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F; Get the names of intersecting nodes&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; nodesNames &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; boxContacts&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt; $0&lt;&#x2F;span&gt;&lt;span&gt;.nodeB.name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; ??&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;NO_OBJECT_NAME&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt; $0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;VOLUME_CASTING_BOX&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span&gt;(nodesNames)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F; You also have access to the nodes themselves&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F; - highlight them, scale them, do whatever you need&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    boxContacts.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;forEach&lt;&#x2F;span&gt;&lt;span&gt; { contactWithBox &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;in&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;        print&lt;&#x2F;span&gt;&lt;span&gt;(contactWithBox.nodeB)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;thread-safety&quot;&gt;Thread Safety&lt;&#x2F;h2&gt;
&lt;p&gt;If you run the above code on the main thread, it will likely crash. SceneKit runs on its own render thread, and accessing it from the main thread creates race conditions. The safest approach: always access SceneKit from its own thread.&lt;&#x2F;p&gt;
&lt;p&gt;How? Conform to &lt;code&gt;SCNSceneRendererDelegate&lt;&#x2F;code&gt; and put your code inside:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt; renderer&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB86C;font-style: italic;&quot;&gt; renderer&lt;&#x2F;span&gt;&lt;span&gt;: SCNSceneRenderer, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt;willRenderScene&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB86C;font-style: italic;&quot;&gt; scene&lt;&#x2F;span&gt;&lt;span&gt;: SCNScene, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt;atTime&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB86C;font-style: italic;&quot;&gt; time&lt;&#x2F;span&gt;&lt;span&gt;: TimeInterval) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F; This is called every frame - all SceneKit access should go here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F; This boolean is set to true when the user taps (from the gesture selector)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F; Note: this isn&amp;#39;t fully race-condition safe either, but works in practice&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; isCheckingCloseObjectsToTap,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;       let&lt;&#x2F;span&gt;&lt;span&gt; node &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; scene.rootNode.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;childNode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;withName&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;VOLUME_CASTING_BOX&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;recursively&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; true&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;        defer&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;            &#x2F;&#x2F; Reset after checking - we only want to check for one frame&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            isCheckingCloseObjectsToTap &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; boxContacts &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; scene.physicsWorld.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;contactTest&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;with&lt;&#x2F;span&gt;&lt;span&gt;: node.physicsBody&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;options&lt;&#x2F;span&gt;&lt;span&gt;: [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; boxContacts.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;count&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span&gt; nodesNames &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; boxContacts&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt; $0&lt;&#x2F;span&gt;&lt;span&gt;.nodeB.name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; ??&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt; $0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;VOLUME_CASTING_BOX&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;            print&lt;&#x2F;span&gt;&lt;span&gt;(nodesNames)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            boxContacts.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;forEach&lt;&#x2F;span&gt;&lt;span&gt; { contactWithBox &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;in&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;                print&lt;&#x2F;span&gt;&lt;span&gt;(contactWithBox.nodeB)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since this delegate method is called every frame, you shouldn’t put heavy work in it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;wrap-up&quot;&gt;Wrap Up&lt;&#x2F;h2&gt;
&lt;p&gt;Using this approach, we can approximate Unity-style volume casting in SceneKit. I say “approximate” because I’m not sure there’s a better way - but this works well in practice. From here, you can ask the user which nearby object they intended to tap, and present them as options.&lt;&#x2F;p&gt;
&lt;p&gt;SceneKit is a solid and capable framework in the Apple ecosystem, but it lacks many of the features and the documentation support you’d find in Unity. As far as I know, no one has written about a volume casting implementation in SceneKit before - so I may write this up in English too, since it’s content that simply doesn’t exist online yet.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Managing Combine Subscriptions</title>
        <published>2022-07-28T00:00:00+00:00</published>
        <updated>2022-07-28T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/blog/combine-subscriptions/"/>
        <id>https://imostfa.github.io/Blog/blog/combine-subscriptions/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/blog/combine-subscriptions/">&lt;p&gt;When you use Combine inside a &lt;code&gt;ViewController&lt;&#x2F;code&gt; or &lt;code&gt;ViewModel&lt;&#x2F;code&gt;, you’re responsible for managing and retaining your subscriptions (a.k.a. disposals).&lt;&#x2F;p&gt;
&lt;p&gt;Take this example of a publisher updating the UI:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt; ViewController&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; UIViewController&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; disposeBag:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Set&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;AnyCancellable&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; []&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt; viewDidLoad&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB86C;font-style: italic;&quot;&gt; animated&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt;Bool&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt;  super&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;viewDidLoad&lt;&#x2F;span&gt;&lt;span&gt;(animated)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  publisher.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;sink&lt;&#x2F;span&gt;&lt;span&gt; { newValue &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;in&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt;    self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;updateUI&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;with&lt;&#x2F;span&gt;&lt;span&gt;: newValue)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;store&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;disposeBag)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What’s happening here: when we subscribe, we store the &lt;code&gt;AnyCancellable&lt;&#x2F;code&gt; in a &lt;code&gt;Set&lt;&#x2F;code&gt; that acts as our dispose bag.&lt;&#x2F;p&gt;
&lt;p&gt;Why? Because if you don’t hold a reference to the &lt;code&gt;AnyCancellable&lt;&#x2F;code&gt; in an Array, variable, or Set - ARC will immediately deallocate it. In Combine, you must keep the subscription alive to keep receiving values.&lt;&#x2F;p&gt;
&lt;p&gt;Think of it as having to hold onto the wire connecting you to the publisher sending data.&lt;&#x2F;p&gt;
&lt;p&gt;Now - say you’ve received what you needed, and the user is about to leave this screen. How do you cancel the subscription and stop receiving values? Does it happen automatically? What if you leave the &lt;code&gt;AnyCancellable&lt;&#x2F;code&gt; stored and never remove it?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;option-1-cancel-on-disappear&quot;&gt;Option 1: Cancel on disappear&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt; ViewController&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; UIViewController&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;  deinit&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;i&amp;#39;m leaving the memory&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;  var&lt;&#x2F;span&gt;&lt;span&gt; disposeBag:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Set&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;AnyCancellable&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; []&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;  func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt; viewDidDisappear&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB86C;font-style: italic;&quot;&gt; animated&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt;Bool&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;viewDidDisappear&lt;&#x2F;span&gt;&lt;span&gt;(animated)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    disposeBag.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;removeAll&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;  func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt; viewDidLoad&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB86C;font-style: italic;&quot;&gt; animated&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt;Bool&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;viewDidLoad&lt;&#x2F;span&gt;&lt;span&gt;(animated)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    publisher.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;sink&lt;&#x2F;span&gt;&lt;span&gt; { newValue &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;in&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt;      self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;updateUI&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;with&lt;&#x2F;span&gt;&lt;span&gt;: newValue)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;store&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;disposeBag)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When the view controller disappears (which usually means the user is done with it, though not always), you empty the dispose bag. All stored subscriptions get cancelled, and the view controller can be freed from memory.&lt;&#x2F;p&gt;
&lt;p&gt;You can confirm this by checking that &lt;code&gt;deinit&lt;&#x2F;code&gt; gets called when you close the screen.&lt;&#x2F;p&gt;
&lt;p&gt;The downside: this puts the burden on you to call &lt;code&gt;removeAll()&lt;&#x2F;code&gt; in every &lt;code&gt;viewDidDisappear&lt;&#x2F;code&gt;. And sometimes you don’t actually want to cancel subscriptions when the view disappears.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;option-2-let-arc-handle-it&quot;&gt;Option 2: Let ARC handle it&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt; ViewController&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; UIViewController&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;  deinit&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;i&amp;#39;m leaving the memory&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;  var&lt;&#x2F;span&gt;&lt;span&gt; disposeBag:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Set&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;AnyCancellable&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; []&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;  func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt; viewDidLoad&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB86C;font-style: italic;&quot;&gt; animated&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt;Bool&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;viewDidLoad&lt;&#x2F;span&gt;&lt;span&gt;(animated)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    publisher.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;sink&lt;&#x2F;span&gt;&lt;span&gt; { [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;weak&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;] newValue &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;in&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #BD93F9;font-style: italic;&quot;&gt;      self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;updateUI&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;with&lt;&#x2F;span&gt;&lt;span&gt;: newValue)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;store&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;disposeBag)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This actually solves two problems at once. The &lt;code&gt;sink&lt;&#x2F;code&gt; closure captures &lt;code&gt;self&lt;&#x2F;code&gt; strongly by default - which can cause a memory leak if you never empty the dispose bag. But when you capture &lt;code&gt;self&lt;&#x2F;code&gt; weakly, ARC can release the view controller, which releases the &lt;code&gt;disposeBag&lt;&#x2F;code&gt; with it, which cancels all the subscriptions.&lt;&#x2F;p&gt;
&lt;p&gt;So the best approach when you’re inside a &lt;code&gt;ViewController&lt;&#x2F;code&gt; or &lt;code&gt;ViewModel&lt;&#x2F;code&gt;: &lt;strong&gt;don’t strongly capture &lt;code&gt;self&lt;&#x2F;code&gt; in closures&lt;&#x2F;strong&gt;. Actually, as a general rule, avoid strong captures in closures altogether 😂&lt;&#x2F;p&gt;
&lt;p&gt;This applies to any reactive framework - RxSwift, ReactiveSwift, you name it.&lt;&#x2F;p&gt;
&lt;p&gt;This post is an expanded version of &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;62964220&#x2F;5253913&quot;&gt;my answer on Stack Overflow&lt;&#x2F;a&gt; on the same question.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Technical Interviews - Instabug</title>
        <published>2022-07-28T00:00:00+00:00</published>
        <updated>2022-07-28T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/blog/interview-instabug/"/>
        <id>https://imostfa.github.io/Blog/blog/interview-instabug/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/blog/interview-instabug/">&lt;p&gt;This one’s short. It’s not particularly informative since I didn’t make it far in the process - but I’m sharing it anyway, because some details might still be useful.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;when-i-applied-to-instabug&quot;&gt;When I applied to Instabug&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;I applied by reaching out directly to engineer Hisham at Instabug (whose code I deeply respect - it’s seriously impressive). He had posted a tweet that they were looking for people. I sent him my CV via Twitter DM, waited for a reply, and he later sent me a link to schedule a call with him.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-meeting&quot;&gt;The meeting&lt;&#x2F;h3&gt;
&lt;p&gt;The meeting was in Arabic. It started with him asking me to introduce myself, then we talked through my past and current experience, what I was working on at my current company, and whether I’d used any Instabug products before. He also told me about the products and goals they were working on.&lt;&#x2F;p&gt;
&lt;p&gt;At the end, he told me the position required significantly more years of experience than I had.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;important-details-and-my-mistakes&quot;&gt;Important details and my mistakes&lt;&#x2F;h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;My CV wasn’t well organized - this caused confusion because he couldn’t clearly follow the timeline. My LinkedIn also had different dates than my CV. I’m sure this kind of inconsistency affects decisions, so I fixed it afterward.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;A company like Instabug wants people who’ve worked on products and apps with a very large user base. I had some small contributions to Firefox and Apple, which is good, but more would have been more convincing.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;At a company like Instabug, what matters most is a deep understanding of internals and compilers at some level - because iOS work there isn’t primarily about UI like at most companies.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Had I made it further, the take-home task they give involves building a Logger with testing and mocking - I know because I later saw that same task during an internship.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;After this, I saw a Facebook post confirming they needed 4–5 years of experience, which I obviously didn’t have yet. Instabug is genuinely one of those companies you dream of working at someday, alongside a really sharp team.&lt;&#x2F;p&gt;
&lt;p&gt;Sharing this because I want to help anyone who needs to understand how things work - because I didn’t, and I always wished someone would talk about it openly.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Why is it Called BehaviorSubject?</title>
        <published>2022-07-27T00:00:00+00:00</published>
        <updated>2022-07-27T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/blog/rxswift-behaviorsubject/"/>
        <id>https://imostfa.github.io/Blog/blog/rxswift-behaviorsubject/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/blog/rxswift-behaviorsubject/">&lt;h3 id=&quot;intro&quot;&gt;Intro&lt;&#x2F;h3&gt;
&lt;p&gt;I’m a fan of the ideas and concepts that reactive programming brings - it often makes code easier to reason about and eliminates certain categories of bugs. Lately I started reading about RxSwift, even though my main reactive framework is Combine.&lt;&#x2F;p&gt;
&lt;p&gt;I came across a &lt;code&gt;Subject&lt;&#x2F;code&gt; called &lt;code&gt;BehaviorSubject&lt;&#x2F;code&gt; - it’s a subject that emits the last value it received to any new subscriber, and then continues to emit any future values.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;why-the-name&quot;&gt;Why the name?&lt;&#x2F;h3&gt;
&lt;p&gt;I found myself asking: where’s the “behavior” in this? Why is the word “behavior” in the name at all? Especially since the equivalent in Combine has a much more self-explanatory name: &lt;code&gt;CurrentValueSubject&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;That name is obvious - it’s a subject that holds and emits the current value. So why isn’t the naming in RxSwift as clear? The answer I found:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;In the world of functional reactive programming, a behavior is a value that changes over time. This is exactly what a &lt;code&gt;BehaviorSubject&lt;&#x2F;code&gt; represents: when you subscribe you get the current value, and then you can continue to observe the changes.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;So the word “behavior” in the reactive programming world means &lt;em&gt;a value being observed as it changes over time&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;A small thing I learned and wanted to share. I’ll probably write more about RxSwift as I dig deeper into the differences between it and Combine.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Technical Interviews - SWVL</title>
        <published>2022-06-12T00:00:00+00:00</published>
        <updated>2022-06-12T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/blog/interview-swvl/"/>
        <id>https://imostfa.github.io/Blog/blog/interview-swvl/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/blog/interview-swvl/">&lt;h3 id=&quot;when-i-applied-to-swvl&quot;&gt;When I applied to SWVL&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;I applied to SWVL at the end of 2021, and the interview process started in January&#x2F;February. The timing matters for two reasons:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;A lot happened at SWVL after that - they went public on Nasdaq, and then laid off 35% of their workforce.&lt;&#x2F;li&gt;
&lt;li&gt;I’m writing this in June, about 5 months after applying. I’m working from notes I kept, but I may have forgotten some details.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;how-i-applied&quot;&gt;How I applied&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;I applied through their website after seeing a tweet from engineer &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;muhammadhani89&quot;&gt;Mohamed Hany&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I genuinely respect him a lot, and I’m grateful to him - that’ll be clear throughout the post.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;who-is-swvl&quot;&gt;Who is SWVL&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;SWVL is a mass-transit company that started in Egypt, then moved to Dubai. There used to be complaints about the work environment at SWVL, but from what I heard things had improved.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;interview-process-overview&quot;&gt;Interview process overview&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;The iOS interview process at SWVL was:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;HR assessment&lt;&#x2F;li&gt;
&lt;li&gt;Technical 1: Multiple Choice Questions + Simple Problem Solving&lt;&#x2F;li&gt;
&lt;li&gt;Meeting with Engineering Manager&lt;&#x2F;li&gt;
&lt;li&gt;Technical 2: With iOS Team, Swift&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;the-hr-call&quot;&gt;The HR call&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;On February 2nd I got an email from HR to schedule the interview - it was on a Friday afternoon.&lt;&#x2F;p&gt;
&lt;p&gt;The HR person was Ali Imam. Since he was from Pakistan, the interview was in English.&lt;&#x2F;p&gt;
&lt;p&gt;Questions included:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;How do you learn new things?&lt;&#x2F;li&gt;
&lt;li&gt;How do you deal with conflicts?&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Overall it was a pleasant call - no video because the internet on both ends was a bit shaky. Afterward he walked me through the interview stages and sent me a link to the assessment shortly after.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-swift-ios-assessment&quot;&gt;The Swift&#x2F;iOS Assessment&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;The second stage was a link to a Swift&#x2F;iOS exam.&lt;&#x2F;p&gt;
&lt;p&gt;The questions covered most Swift and programming fundamentals.&lt;&#x2F;p&gt;
&lt;p&gt;The multiple choice questions covered:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Dynamic Dispatching vs Static Dispatching&lt;&#x2F;li&gt;
&lt;li&gt;Synchronous execution vs Asynchronous execution&lt;&#x2F;li&gt;
&lt;li&gt;SOLID Principles&lt;&#x2F;li&gt;
&lt;li&gt;How to detect keyboard appearance&#x2F;disappearance&lt;&#x2F;li&gt;
&lt;li&gt;How to store an object in UserDefaults&lt;&#x2F;li&gt;
&lt;li&gt;Memory leak in Classes &#x2F; Deinit call&lt;&#x2F;li&gt;
&lt;li&gt;Set data structure&lt;&#x2F;li&gt;
&lt;li&gt;Passing values to functions by &lt;code&gt;inout&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Value capturing&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;the-technical-challenge&quot;&gt;The Technical Challenge&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;blockquote&gt;
&lt;p&gt;Have the function &lt;code&gt;StarRating(str)&lt;&#x2F;code&gt; take the &lt;code&gt;str&lt;&#x2F;code&gt; parameter being passed which will be an average rating between 0.00 and 5.00, and convert this rating into a list of 5 image names to be displayed in a user interface to represent the rating as a list of stars and half stars. Ratings should be rounded up to the nearest half. There are 3 image file names available: “full.jpg”, “half.jpg”, “empty.jpg”. The output will be the name of the 5 images (without the extension), from left to right, separated by spaces. For example: if str is “2.36” then your program should return the string “full full half empty empty”.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h3 id=&quot;result-of-the-first-assessment&quot;&gt;Result of the first assessment&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;I passed, and the next step was a meeting with the Engineering Manager - Mohamed Hany himself. He’s genuinely a great person. I learned a lot from him.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;meeting-with-the-em-at-swvl&quot;&gt;Meeting with the EM at SWVL&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;The meeting was really enjoyable and relaxed - more of a conversation than a formal interview. It included questions like:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;How do you deal with conflicts?&lt;&#x2F;li&gt;
&lt;li&gt;What do you know about AGILE?&lt;&#x2F;li&gt;
&lt;li&gt;What do you love about SWVL B2B?&lt;&#x2F;li&gt;
&lt;li&gt;Tell me about something hard you’ve worked on solving.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;He also listened to my questions. Overall I felt the feedback was very positive from how we connected - but the final result was a rejection. Why?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;why-i-was-rejected&quot;&gt;Why I was rejected&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;I won’t pretend it didn’t sting. Especially since the call felt like it went well. I reached out to Mohamed Hany and asked for feedback - because I knew it would help me in future interviews. He called me and shared his perspective:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;In most of my answers, I kept talking about one specific domain (Computer Graphics applications) or one specific branch of programming. This gave the impression that I didn’t have breadth across iOS in general. From my perspective, I was bringing it up because it’s a complex and hard area - so if I could handle that, I should be fine elsewhere. But from his perspective, it was better to showcase a wider variety of things I’d worked on to demonstrate range. This was something I started doing in every interview after.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;I needed to work on projects that impacted more people and had real users. The startup I was at hadn’t released their app publicly, and the projects I’d worked on before didn’t have thousands of users. For a company the size of SWVL, they wanted someone experienced working on apps with large user bases. He also suggested I start looking at other companies - this was honestly the first time someone had directly told me I should be exploring other opportunities. I’d been at my company for a year and two months. Good advice that I’m grateful for.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;My years of experience were less than what they needed.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;He did mention that he felt if I’d made it to the technical interview, I probably would have passed.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-i-took-away&quot;&gt;What I took away&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;A lot. The feedback from Mohamed clarified things I wasn’t thinking about. It was also my first time doing a multiple-choice iOS interview - and I think I got everything right, but my problem-solving wasn’t optimized. And I realized more clearly what an EM thinks about when interviewing someone.&lt;&#x2F;p&gt;
&lt;p&gt;All of this genuinely helped me in the interview I did after this - with Google.&lt;&#x2F;p&gt;
&lt;p&gt;Sharing this experience is my way of helping anyone who needs to understand how things actually work - because I didn’t know, and I always wished someone would talk about it openly.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Technical Interviews - Introduction</title>
        <published>2022-06-12T00:00:00+00:00</published>
        <updated>2022-06-12T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/blog/interviews-introduction/"/>
        <id>https://imostfa.github.io/Blog/blog/interviews-introduction/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/blog/interviews-introduction/">&lt;p&gt;In this series, I’ll talk about my experience interviewing at several well-known companies - Google, SWVL, Instabug, and companies in Europe 🇪🇸🇩🇪.&lt;&#x2F;p&gt;
&lt;p&gt;The reason I’m writing this series is to share my experience so we can learn from each other. At the start of my interviewing journey - and even now - there were a lot of things I didn’t know: how to answer well, how to present myself, what’s the right approach. I’m still learning. I used to wish there were people writing about or sharing what the interview process actually looks like at each company.&lt;&#x2F;p&gt;
&lt;p&gt;Not all of these interviews went well - but I took something away from every single one.&lt;&#x2F;p&gt;
&lt;p&gt;What I’ll try to do is write down all the steps and the questions I was asked. The exact dates aren’t pinned for technical reasons, but all of these happened in 2022.&lt;&#x2F;p&gt;
&lt;p&gt;Each company will get its own post, covering the full process from start to wherever I made it. Some companies I got through multiple rounds - like SWVL, Google, and Robusta. Others fewer. In every one I learned something and met really sharp people who gave me confidence and taught me a lot.&lt;&#x2F;p&gt;
&lt;p&gt;Everything I write is purely my personal experience. As of 2022, I had just graduated in June 2021 - so I was pretty fresh 😂. If anything’s wrong, I’d genuinely appreciate being told.&lt;&#x2F;p&gt;
&lt;p&gt;The kinds of things I’ll share in these posts:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;HR questions 🧗🏻‍♂️&lt;&#x2F;li&gt;
&lt;li&gt;Technical questions - mobile, especially iOS 📱&lt;&#x2F;li&gt;
&lt;li&gt;Behavioral questions 🤝&lt;&#x2F;li&gt;
&lt;li&gt;How I worked on take-home tasks 🏎&lt;&#x2F;li&gt;
&lt;li&gt;My attempts at studying data structures and algorithms 🏗&lt;&#x2F;li&gt;
&lt;li&gt;Anything else that comes to mind&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>How to Know the Size of Your Types in Swift</title>
        <published>2020-11-07T00:00:00+00:00</published>
        <updated>2020-11-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/blog/memory-alignment-swift/"/>
        <id>https://imostfa.github.io/Blog/blog/memory-alignment-swift/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/blog/memory-alignment-swift/">&lt;h1 id=&quot;how-to-know-the-size-of-your-types-in-swift&quot;&gt;How to Know the Size of Your Types in Swift&lt;&#x2F;h1&gt;
&lt;p&gt;In Swift, any object or type has 3 properties that describe how memory interacts with it. In this post we’ll look at two of them - &lt;code&gt;size&lt;&#x2F;code&gt; and &lt;code&gt;alignment&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;size-package&quot;&gt;Size 📦&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Say you have two &lt;code&gt;struct&lt;&#x2F;code&gt;s in your program like these:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Student&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; age:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Int&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Professor&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; age:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Int&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; phoneNumber:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Int&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; mostfa &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt; Student&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; ahmed &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt; Professor&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;phoneNumber&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; 010&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If I asked you which object is larger - &lt;code&gt;mostfa&lt;&#x2F;code&gt; or &lt;code&gt;ahmed&lt;&#x2F;code&gt; - logic says &lt;code&gt;ahmed&lt;&#x2F;code&gt;, because it’s made of two variables: &lt;code&gt;age&lt;&#x2F;code&gt; and &lt;code&gt;phoneNumber&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;But how do we confirm that with actual numbers? We use &lt;code&gt;MemoryLayout&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;computer-memory-layout&quot;&gt;💻 Memory Layout&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Swift has a type called &lt;code&gt;MemoryLayout&lt;&#x2F;code&gt; that lets us check the sizes of our types - like &lt;code&gt;Student&lt;&#x2F;code&gt;, &lt;code&gt;Professor&lt;&#x2F;code&gt;, or any built-in type like &lt;code&gt;String&lt;&#x2F;code&gt; or &lt;code&gt;Bool&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; studentSize &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; MemoryLayout&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;Student&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;size&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; professorSize &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; MemoryLayout&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;Professor&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;size&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(studentSize)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt; &#x2F;&#x2F; prints 8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(professorSize)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt; &#x2F;&#x2F;prints 16 😉&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As expected, &lt;code&gt;Professor&lt;&#x2F;code&gt; takes up more memory.&lt;&#x2F;p&gt;
&lt;p&gt;If we want to check specific instances like &lt;code&gt;mostfa&lt;&#x2F;code&gt; and &lt;code&gt;ahmed&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; mostfaSize &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; MemoryLayout&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;size&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;ofValue&lt;&#x2F;span&gt;&lt;span&gt;: mostfa)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; ahmedSize &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; MemoryLayout&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;size&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;ofValue&lt;&#x2F;span&gt;&lt;span&gt;: ahmed)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(mostfaSize)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt; &#x2F;&#x2F;prints 8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(ahmedSize)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt; &#x2F;&#x2F;also prints 16&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But why 8? And why 16? Let’s dig into size a bit more.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;back-to-size-package&quot;&gt;Back to Size 📦&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;In Swift, calculating the size of a struct is straightforward - &lt;strong&gt;the size of a struct equals the sum of the sizes of its properties&lt;&#x2F;strong&gt;. So:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Student&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;     let&lt;&#x2F;span&gt;&lt;span&gt; age:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Int&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; inSchool:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Bool&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; let&lt;&#x2F;span&gt;&lt;span&gt; intSize &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; MemoryLayout&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;size&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F;prints out 8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; let&lt;&#x2F;span&gt;&lt;span&gt; boolSize &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; MemoryLayout&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt;Bool&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;size&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;  &#x2F;&#x2F;prints out 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; studentSize &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; MemoryLayout&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;Student&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;size&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt; &#x2F;&#x2F;prints out 9 ( 1 + 8 )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The struct has one &lt;code&gt;Int&lt;&#x2F;code&gt; and one &lt;code&gt;Bool&lt;&#x2F;code&gt;, so its size is 8 + 1 = 9. Easy, right? 🦹🏻‍♂️&lt;&#x2F;p&gt;
&lt;p&gt;Now let’s verify with another example:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; OtherStudent&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; inSchool:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Bool&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; age:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Int&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is the same as &lt;code&gt;Student&lt;&#x2F;code&gt; above - just with the properties in a different order. You’d expect the same size, right?&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; OtherStudentSize &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; MemoryLayout&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;OtherStudent&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;size&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt; &#x2F;&#x2F; 16 !&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It’s 16, not 9. That’s because of &lt;strong&gt;alignment&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;alignment&quot;&gt;Alignment&lt;&#x2F;h2&gt;
&lt;p&gt;The alignment of any &lt;code&gt;Int&lt;&#x2F;code&gt; in Swift is 8. What does that mean? It means the memory address where an &lt;code&gt;Int&lt;&#x2F;code&gt; is stored must be a &lt;strong&gt;multiple of 8&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Say we have:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; A &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; 88&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; b &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; 17&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Both &lt;code&gt;A&lt;&#x2F;code&gt; and &lt;code&gt;b&lt;&#x2F;code&gt; must be placed at memory addresses that are multiples of 8. If &lt;code&gt;A&lt;&#x2F;code&gt; is at address 0 (a multiple of 8) and &lt;code&gt;b&lt;&#x2F;code&gt; is at address 8 (also a multiple of 8) - no problem.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;When do we have a problem? In this situation:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;&#x2F;*&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;A lot of other code in the app which will be saved in memory&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;*&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; A &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt; 88&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If other data already occupies addresses 0 through 4, and we try to place &lt;code&gt;A&lt;&#x2F;code&gt; at address 5 - we can’t. Because 5 is not a multiple of 8. So the compiler places &lt;code&gt;A&lt;&#x2F;code&gt; at address 8 instead, leaving addresses 5, 6, and 7 empty (wasted as padding).&lt;&#x2F;p&gt;
&lt;p&gt;This brings us back to the original question: why does &lt;code&gt;OtherStudent&lt;&#x2F;code&gt; have a different size than &lt;code&gt;Student&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;alignment-and-size-bowling&quot;&gt;Alignment and Size 🎳&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; boolAlignment &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; MemoryLayout&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt;Bool&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;alignment&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F;prints out 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; IntegerAlignment &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; MemoryLayout&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;alignment&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;  &#x2F;&#x2F;prints out 8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Student&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;     let&lt;&#x2F;span&gt;&lt;span&gt; age:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F; alignment: 8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; inSchool:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Bool&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;  &#x2F;&#x2F; alignment: 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since &lt;code&gt;Bool&lt;&#x2F;code&gt; has alignment 1, it can go at any address (every number is a multiple of 1). Since &lt;code&gt;Int&lt;&#x2F;code&gt; has alignment 8, it must go at a multiple-of-8 address.&lt;&#x2F;p&gt;
&lt;p&gt;For &lt;code&gt;Student&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Int&lt;&#x2F;code&gt; goes at address 0 (✅ multiple of 8)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Bool&lt;&#x2F;code&gt; goes at address 8 (✅ multiple of 1)&lt;&#x2F;li&gt;
&lt;li&gt;Total size = 9&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;hr &#x2F;&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; OtherStudent&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; inSchool:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Bool&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;  &#x2F;&#x2F; alignment: 1 - goes at address 0 ✅&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; age:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; Int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;        &#x2F;&#x2F; alignment: 8 - can&amp;#39;t go at address 1 ❌&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For &lt;code&gt;OtherStudent&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Bool&lt;&#x2F;code&gt; goes at address 0 (✅ multiple of 1)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Int&lt;&#x2F;code&gt; &lt;strong&gt;cannot&lt;&#x2F;strong&gt; go at address 1 (❌ not a multiple of 8)&lt;&#x2F;li&gt;
&lt;li&gt;The compiler leaves addresses 1–7 as padding&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Int&lt;&#x2F;code&gt; goes at address 8 (✅ multiple of 8)&lt;&#x2F;li&gt;
&lt;li&gt;Total size = 16 🤯&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;So the &lt;strong&gt;order of properties in a struct matters&lt;&#x2F;strong&gt; - it can affect the size due to alignment padding.&lt;&#x2F;p&gt;
&lt;p&gt;To be continued …&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>What is Unit Testing?</title>
        <published>2020-11-02T00:00:00+00:00</published>
        <updated>2020-11-02T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Mostafa
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://imostfa.github.io/Blog/blog/unit-testing-intro/"/>
        <id>https://imostfa.github.io/Blog/blog/unit-testing-intro/</id>
        
        <content type="html" xml:base="https://imostfa.github.io/Blog/blog/unit-testing-intro/">&lt;h1 id=&quot;what-is-unit-testing&quot;&gt;What is Unit Testing?&lt;&#x2F;h1&gt;
&lt;p&gt;In this post we’ll talk about what unit testing is as a general concept, with some simplified code examples.&lt;&#x2F;p&gt;
&lt;p&gt;If we had to define unit testing in one sentence, we could say: it’s writing code that verifies the functions you wrote always work the way you intend, and always fulfill their purpose.&lt;&#x2F;p&gt;
&lt;p&gt;Why bother? There are many reasons, but here are a few:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Sometimes we write code, come back to it after a long time not remembering what it does, modify it, and the app seems to work fine - but we didn’t notice we broke something else.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;ol&gt;
&lt;li&gt;Sometimes we have a bug, fix it by changing some old working code, and the bug gets fixed - but we didn’t realize we broke something else in the process.&lt;&#x2F;li&gt;
&lt;li&gt;When a team is working together, one person might write working code, then a teammate edits it mid-task and doesn’t realize they changed something fundamental.&lt;&#x2F;li&gt;
&lt;li&gt;You can catch bugs before your QA tester or the client does.&lt;&#x2F;li&gt;
&lt;li&gt;It reduces unnecessary code in your functions and encourages writing cleaner, better code - this is especially true with practices like TDD.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Now that we have a basic definition and some reasons to write unit tests, let’s look at an example. And let’s never forget our goal:&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-goal-any-function-we-write-should-always-work-as-we-intend-and-always-fulfill-its-purpose&quot;&gt;The goal: any function we write should always work as we intend and always fulfill its purpose&lt;&#x2F;h2&gt;
&lt;p&gt;What does that mean? Imagine we have this function:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt; insertDot&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt;to&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB86C;font-style: italic;&quot;&gt; sentance&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F;if the last character in sentance is &amp;quot;.&amp;quot; will return the sentance without any modification&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    guard&lt;&#x2F;span&gt;&lt;span&gt; sentance.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;last&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; return&lt;&#x2F;span&gt;&lt;span&gt; sentance }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F;return the senance with a dot added at the end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span&gt; sentance &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;this-function-s-purpose-is-to-add-a-period-at-the-end-of-a-sentence&quot;&gt;This function’s purpose is to add a period at the end of a sentence&lt;&#x2F;h3&gt;
&lt;p&gt;How does it fulfill that purpose? Through two behaviors:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;If the sentence already ends with a period, don’t add another one.&lt;&#x2F;li&gt;
&lt;li&gt;If the sentence doesn’t end with a period, add one.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;What we want to do is protect those two behaviors - in other words, write code that verifies this function always achieves both goals. That code is called a “test”, or more precisely a “unit test”. We’ll need to write 2 unit tests, one for each goal.&lt;&#x2F;p&gt;
&lt;p&gt;Here’s the code we need to verify both goals - without worrying about where exactly this code lives in the project, just focusing on the concept:&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;The unit test for the first goal:&lt;&#x2F;p&gt;
&lt;p&gt;The goal of this test: when the function receives &lt;code&gt;&quot;Mostfa.&quot;&lt;&#x2F;code&gt;, the output should be &lt;code&gt;&quot;Mostfa.&quot;&lt;&#x2F;code&gt; - unchanged.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt; test_NoDublicate_dots&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt; &#x2F;&#x2F;1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; output &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt; insertDot&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;to&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;Mostfa.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt; &#x2F;&#x2F;2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;           XCTAssertEqual&lt;&#x2F;span&gt;&lt;span&gt;(output, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;Mostfa.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;No dot&amp;#39;s should be added&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt; &#x2F;&#x2F;3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In &lt;code&gt;&#x2F;&#x2F;1&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;p&gt;We name the function to describe what it’s testing. The name here says we don’t want duplicate dots in the output.&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;&#x2F;&#x2F;2&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;p&gt;We call the function we’re testing and store the result in &lt;code&gt;output&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Notice that the sentence we’re passing already ends with a period - so the function should return it unchanged, without adding a second period.&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;&#x2F;&#x2F;3&lt;&#x2F;code&gt;: We use &lt;code&gt;XCTAssertEqual&lt;&#x2F;code&gt;, a function from the testing framework, to declare our expectation. We’re saying: &lt;code&gt;output&lt;&#x2F;code&gt; should equal &lt;code&gt;&quot;Mostfa.&quot;&lt;&#x2F;code&gt; - that’s our goal.&lt;&#x2F;p&gt;
&lt;p&gt;You can think of this function as doing this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt; test_NoDublicate_dots&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; output &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt; insertDot&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;to&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;Mostfa.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;        XCTAssertEqual&lt;&#x2F;span&gt;&lt;span&gt;(output, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;Mostfa.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;No dots should be added&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; output &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;Mostfa.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;            &#x2F;&#x2F;we are happy, no errors!&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;            assertionFailure&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;stop here!, we are not happy, that&amp;#39;s not supposed to happen&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When you run this test, you’ll get a green checkmark ✅ because &lt;code&gt;insertDot&lt;&#x2F;code&gt; correctly doesn’t add a period to a sentence that already ends with one.&lt;&#x2F;p&gt;
&lt;p&gt;Congrats, that’s your first test!&lt;&#x2F;p&gt;
&lt;p&gt;Now imagine someone went and tampered with the original function like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt; insertDot&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt;to&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB86C;font-style: italic;&quot;&gt; sentance&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F;if the last character in sentance is &amp;quot;.&amp;quot; will return it with any modification&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    guard&lt;&#x2F;span&gt;&lt;span&gt; sentance.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;last&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; return&lt;&#x2F;span&gt;&lt;span&gt; sentance }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F;return the senance with a dot added at the end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span&gt; sentance &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we run our test, we’ll see a red X ❌&lt;&#x2F;p&gt;
&lt;p&gt;Why? Because the test found that the output was &lt;code&gt;&quot;Mostfa..&quot;&lt;&#x2F;code&gt; - not what we expected, and not what the function is supposed to do.&lt;&#x2F;p&gt;
&lt;p&gt;In practice, after every change we make to the project, we run the test functions. If everything is green, we’re good. If something is red, we know we broke something and we go find out what.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Now let’s write the second test - which is actually more intuitive:&lt;&#x2F;p&gt;
&lt;p&gt;Here we verify that if the sentence doesn’t already end with a period, one gets added:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt; test_dot_isAdded&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; output &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt; insertDot&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;to&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;Essam&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8BE9FD;&quot;&gt;        XCTAssertEqual&lt;&#x2F;span&gt;&lt;span&gt;(output, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;Essam.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Simple - we call the function with &lt;code&gt;&quot;Essam&quot;&lt;&#x2F;code&gt; and assert that the output is always &lt;code&gt;&quot;Essam.&quot;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now if someone goes and modifies the original function like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #282A36;&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt; insertDot&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #50FA7B;&quot;&gt;to&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB86C;font-style: italic;&quot;&gt; sentance&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8BE9FD;font-style: italic;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F;if the last character in sentance is &amp;quot;.&amp;quot; will return it with any modification&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    guard&lt;&#x2F;span&gt;&lt;span&gt; sentance.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BD93F9;&quot;&gt;last&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt; return&lt;&#x2F;span&gt;&lt;span&gt; sentance }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6272A4;&quot;&gt;    &#x2F;&#x2F;return the senance with a dot added at the end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span&gt; sentance &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF79C6;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F1FA8C;&quot;&gt;💕&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E9F284;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we run the test, it fails - and that’s expected. It tells us the output should be &lt;code&gt;&quot;Essam.&quot;&lt;&#x2F;code&gt;, not &lt;code&gt;&quot;Essam💕&quot;&lt;&#x2F;code&gt;. So we go look at the function and figure out what changed, and fix it so the test passes again.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;That’s the end of today’s post. We covered an introduction to testing with a very simple example that shows how to think about it and what to do to write a test.&lt;&#x2F;p&gt;
&lt;p&gt;There are things still missing - like where exactly the test code lives in a project, and how to test something more complex. We can cover all of that in a follow-up post, and eventually write tests for our API layer in a more advanced way.&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
