<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Tudor Turcanu - Blog (full posts)</title><description>Writing about iOS development, Swift, SwiftUI, and on-device AI. Every post in full.</description><link>https://tudorturcanu.ch/</link><language>en</language><atom:link href="https://tudorturcanu.ch/rss-full.xml" rel="self" type="application/rss+xml"/><item><title>Copy-on-Write, Visually: The Copy You Wrote Isn&apos;t the Copy You Pay For</title><link>https://tudorturcanu.ch/blog/copy-on-write-visually/</link><guid isPermaLink="true">https://tudorturcanu.ch/blog/copy-on-write-visually/</guid><description>Swift arrays are values, so every assignment is a copy - except it isn&apos;t, until it suddenly is. An interactive look at when the buffer actually gets duplicated, and the three ordinary-looking patterns that make it happen on every mutation.</description><pubDate>Sun, 01 Nov 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In &lt;a href=&quot;https://tudorturcanu.ch/blog/wmo-what-gets-deleted/&quot;&gt;the whole-module optimization post&lt;/a&gt; the theme was that the abstraction you write isn’t the code that runs. Copy-on-write is the same bargain on the data side: you write &lt;code&gt;var b = a&lt;/code&gt; and think “copy”, and the runtime quietly does something much cheaper - right up until the moment it does something much more expensive than you expected.&lt;/p&gt;
&lt;p&gt;Both halves are worth seeing, because the first half is why value types are practical at all, and the second half is where quadratic loops come from.&lt;/p&gt;
&lt;h2 id=&quot;two-different-things-called-copy&quot;&gt;Two different things called “copy”&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Dictionary&lt;/code&gt;, &lt;code&gt;Set&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt; are structs, but the struct itself is tiny: it’s essentially one pointer to a heap buffer that holds the elements. Assigning an array copies &lt;em&gt;the pointer&lt;/em&gt; and bumps the buffer’s reference count. That’s it - one retain, regardless of whether the array holds three elements or three million.&lt;/p&gt;
&lt;p&gt;The real copy is deferred to the first mutation, and it’s conditional. Before writing, the array asks one question: &lt;em&gt;am I the only one holding this buffer?&lt;/em&gt; If yes, mutate in place. If no, allocate a new buffer, copy every element across, and then mutate the private copy.&lt;/p&gt;
&lt;p&gt;Play with it. Share the buffer, then append through different names, and watch which actions move the “elements copied” counter - and which leave a flat green line in the chart:&lt;/p&gt;
&lt;figure class=&quot;cowviz&quot; id=&quot;cow-viz&quot;&gt;
  &lt;div class=&quot;cowviz-controls&quot;&gt;
    &lt;div class=&quot;cowviz-group&quot;&gt;&lt;span class=&quot;cowviz-group-label&quot;&gt;Assign&lt;/span&gt;&lt;div class=&quot;cowviz-buttons&quot; id=&quot;cow-assign&quot;&gt;&lt;/div&gt;&lt;/div&gt;
    &lt;div class=&quot;cowviz-group&quot;&gt;&lt;span class=&quot;cowviz-group-label&quot;&gt;Mutate&lt;/span&gt;&lt;div class=&quot;cowviz-buttons&quot; id=&quot;cow-mutate&quot;&gt;&lt;/div&gt;&lt;/div&gt;
    &lt;button type=&quot;button&quot; class=&quot;cowviz-reset&quot; id=&quot;cow-reset&quot;&gt;Reset&lt;/button&gt;
  &lt;/div&gt;
  &lt;label class=&quot;cowviz-toggle&quot;&gt;&lt;input type=&quot;checkbox&quot; role=&quot;switch&quot; id=&quot;cow-observer&quot;&gt;&lt;span class=&quot;cowviz-switch&quot; aria-hidden=&quot;true&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;code&gt;a&lt;/code&gt; has a &lt;code&gt;didSet&lt;/code&gt; that reads &lt;code&gt;oldValue&lt;/code&gt;&lt;/span&gt;&lt;/label&gt;
  &lt;div class=&quot;cowviz-mem&quot; id=&quot;cow-mem&quot;&gt;
    &lt;div class=&quot;cowviz-col&quot;&gt;&lt;span class=&quot;cowviz-col-label&quot;&gt;Stack&lt;/span&gt;&lt;div class=&quot;cowviz-vars&quot; id=&quot;cow-vars&quot;&gt;&lt;/div&gt;&lt;/div&gt;
    &lt;div class=&quot;cowviz-col&quot;&gt;&lt;span class=&quot;cowviz-col-label&quot;&gt;Heap&lt;/span&gt;&lt;div class=&quot;cowviz-buffers&quot; id=&quot;cow-buffers&quot; aria-live=&quot;polite&quot;&gt;&lt;/div&gt;&lt;/div&gt;
    &lt;svg class=&quot;cowviz-wires&quot; id=&quot;cow-wires&quot; aria-hidden=&quot;true&quot;&gt;&lt;/svg&gt;
  &lt;/div&gt;
  &lt;div class=&quot;cowviz-step&quot; id=&quot;cow-step&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cowviz-bottom&quot;&gt;
    &lt;div class=&quot;cowviz-readout&quot;&gt;
      &lt;div&gt;&lt;span class=&quot;stat-k&quot;&gt;Assignments&lt;/span&gt;&lt;span class=&quot;stat-v&quot; id=&quot;cow-stat-assign&quot;&gt;0&lt;/span&gt;&lt;/div&gt;
      &lt;div&gt;&lt;span class=&quot;stat-k&quot;&gt;Real copies&lt;/span&gt;&lt;span class=&quot;stat-v&quot; id=&quot;cow-stat-copies&quot;&gt;0&lt;/span&gt;&lt;/div&gt;
      &lt;div&gt;&lt;span class=&quot;stat-k&quot;&gt;Elements copied&lt;/span&gt;&lt;span class=&quot;stat-v paid&quot; id=&quot;cow-stat-elems&quot;&gt;0&lt;/span&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;cowviz-chart-wrap&quot;&gt;
      &lt;span class=&quot;stat-k&quot;&gt;Elements copied per append&lt;/span&gt;
      &lt;div class=&quot;cowviz-chart&quot; id=&quot;cow-chart&quot; role=&quot;img&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;figcaption&gt;A simulation, not Swift running in your browser. Each name on the left is an array struct - one pointer. Each card on the right is a heap buffer. A name that mutates a shared buffer leaves it for a private copy, and a buffer nobody points at is freed.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;script src=&quot;https://tudorturcanu.ch/scripts/cow-viz.js&quot; defer&gt;&lt;/script&gt;
&lt;style&gt;
  .cowviz, .cow-dance { --cow-free: #2b7a4b; --cow-paid: var(--accent); --cow-shared: #9a6a00; }
  @media (prefers-color-scheme: dark) { .cowviz, .cow-dance { --cow-free: #5cc98a; --cow-shared: #e3b341; } }
  .cowviz { border: 1px solid var(--border); border-radius: 12px; background: var(--bg-soft); padding: 1rem 1rem 1.1rem; margin: 2rem 0; }
  .cowviz-controls { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .6rem 1.1rem; margin-bottom: .8rem; }
  .cowviz-group { display: flex; flex-direction: column; gap: .3rem; }
  .cowviz-group-label, .cowviz-col-label, .cowviz .stat-k { color: var(--text-soft); font-size: .68rem; text-transform: uppercase; letter-spacing: .06em; }
  .cowviz-buttons { display: flex; flex-wrap: wrap; gap: .35rem; }
  .cowviz button { font-family: var(--font-mono); font-size: .78rem; cursor: pointer; padding: .36rem .7rem; border-radius: 7px; border: 1px solid var(--border-strong); background: var(--bg); color: var(--text); transition: border-color .15s, transform .1s; }
  .cowviz button:hover:not(:disabled) { border-color: var(--accent); }
  .cowviz button:active:not(:disabled) { transform: translateY(1px); }
  .cowviz button:disabled { opacity: .35; cursor: default; background: transparent; }
  .cowviz .cowviz-reset { margin-left: auto; font-family: inherit; background: transparent; color: var(--text-soft); }
  .cowviz-toggle { position: relative; display: inline-flex; gap: .55rem; align-items: center; font-size: .84rem; color: var(--text-soft); margin-bottom: 1rem; cursor: pointer; }
  .cowviz-toggle input { position: absolute; opacity: 0; width: 1px; height: 1px; }
  .cowviz-switch { flex: none; width: 2.1rem; height: 1.2rem; border-radius: 999px; background: var(--border-strong); position: relative; transition: background-color .2s; }
  .cowviz-switch::after { content: &apos;&apos;; position: absolute; top: .15rem; left: .15rem; width: .9rem; height: .9rem; border-radius: 50%; background: var(--bg); transition: transform .2s var(--ease-out); }
  .cowviz-toggle input:checked + .cowviz-switch { background: var(--cow-paid); }
  .cowviz-toggle input:checked + .cowviz-switch::after { transform: translateX(.9rem); }
  .cowviz-toggle input:focus-visible + .cowviz-switch { outline: 2px solid var(--accent); outline-offset: 2px; }
  .cowviz-mem { position: relative; display: grid; grid-template-columns: minmax(6.5rem, 9rem) 1fr; gap: 1rem 3.2rem; padding: .9rem; border-radius: 10px; background: var(--bg); border: 1px solid var(--border); }
  .cowviz-col { display: flex; flex-direction: column; gap: .55rem; min-width: 0; }
  .cowviz-vars { display: flex; flex-direction: column; gap: .5rem; }
  .cowviz-buffers { display: flex; flex-direction: column; gap: .6rem; min-height: 6rem; }
  .cowviz-wires { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
  .cow-wire { fill: none; stroke: var(--text-soft); stroke-width: 1.5; opacity: .75; }
  .cow-wire.paid { stroke: var(--cow-paid); stroke-width: 2; opacity: 1; }
  .cow-arrowhead { fill: var(--text-soft); }
  .cow-arrowhead.paid { fill: var(--cow-paid); }
  .cow-slot { display: flex; align-items: baseline; justify-content: space-between; gap: .5rem; font-family: var(--font-mono); padding: .4rem .6rem; border-radius: 8px; border: 1px solid var(--border-strong); background: var(--bg-soft); }
  .cow-slot.empty { border-style: dashed; opacity: .55; }
  .cow-slot-name { font-weight: 700; font-size: .9rem; }
  .cow-slot-ptr { font-size: .72rem; color: var(--text-soft); white-space: nowrap; }
  .cow-buffer { align-self: flex-start; max-width: 100%; border: 1px solid var(--border-strong); border-radius: 9px; background: var(--bg-soft); padding: .5rem .6rem .6rem; }
  .cow-buffer.fresh { border-color: var(--cow-paid); box-shadow: 0 0 0 3px color-mix(in srgb, var(--cow-paid) 18%, transparent); animation: cow-pop .35s var(--ease-out); }
  .cow-buffer.freed { border-style: dashed; opacity: .45; animation: cow-fade .6s ease; }
  .cow-buffer-head { display: flex; flex-wrap: wrap; align-items: center; gap: .45rem; font-family: var(--font-mono); font-size: .7rem; color: var(--text-soft); margin-bottom: .45rem; }
  .cow-buffer-id { color: var(--text); font-weight: 700; }
  .cow-tag { font-family: var(--font-sans); font-size: .64rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; padding: .1rem .45rem; border-radius: 999px; border: 1px solid currentColor; }
  .cow-tag.shared { color: var(--cow-shared); }
  .cow-tag.paid { color: var(--cow-paid); }
  .cow-tag.free { color: var(--cow-free); }
  .cow-tag.freed, .cow-tag.neutral { color: var(--text-soft); }
  .cow-cells { display: flex; flex-wrap: wrap; gap: 3px; }
  .cow-cell { font-family: var(--font-mono); font-size: .74rem; min-width: 24px; height: 24px; line-height: 24px; padding: 0 3px; text-align: center; border-radius: 4px; background: color-mix(in srgb, var(--text) 9%, transparent); }
  .cow-cell.copied { background: color-mix(in srgb, var(--cow-paid) 20%, transparent); animation: cow-copy .3s var(--ease-out) both; animation-delay: calc(var(--i) * 45ms); }
  .cow-cell.new { background: var(--cow-free); color: var(--bg); font-weight: 700; animation: cow-pop .35s var(--ease-out) both; }
  .cow-buffer.fresh .cow-cell.new { background: var(--cow-paid); animation-delay: calc(var(--i, 0) * 45ms + 60ms); }
  .cow-cell.more { color: var(--text-soft); background: transparent; border: 1px dashed var(--border-strong); }
  @keyframes cow-copy { from { opacity: 0; transform: translateX(-10px); } to { opacity: 1; transform: none; } }
  @keyframes cow-pop { from { opacity: 0; transform: scale(.85); } to { opacity: 1; transform: none; } }
  @keyframes cow-fade { from { opacity: 1; } to { opacity: .45; } }
  .cowviz-step { margin: .9rem 0 0; min-height: 5.2em; }
  .cow-step-head { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; margin-bottom: .35rem; }
  .cow-step-code { font-family: var(--font-mono); font-size: .82rem; font-weight: 600; }
  .cow-step-text { font-size: .86rem; color: var(--text-soft); margin: 0; }
  .cowviz-bottom { display: grid; grid-template-columns: auto 1fr; gap: 1rem 1.6rem; align-items: end; margin-top: .8rem; padding-top: .9rem; border-top: 1px solid var(--border); }
  .cowviz-readout { display: flex; flex-direction: column; gap: .5rem; }
  .cowviz .stat-k { display: block; }
  .cowviz .stat-v { font-family: var(--font-mono); font-size: 1.15rem; font-weight: 700; font-variant-numeric: tabular-nums; }
  .cowviz .stat-v.paid { color: var(--cow-paid); }
  .cowviz-chart { display: flex; align-items: flex-end; gap: 4px; height: 7.5rem; margin-top: .4rem; padding-bottom: 1.1rem; border-bottom: 1px solid var(--border-strong); position: relative; }
  .cow-bar-col { flex: 1 1 0; max-width: 2rem; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: stretch; position: relative; }
  .cow-bar { border-radius: 3px 3px 0 0; position: relative; transition: height .3s var(--ease-out); }
  .cow-bar.paid { background: var(--cow-paid); }
  .cow-bar.free { height: 4px; background: var(--cow-free); border-radius: 2px; }
  .cow-bar-n { position: absolute; bottom: 100%; left: 0; right: 0; text-align: center; font-family: var(--font-mono); font-size: .62rem; color: var(--cow-paid); padding-bottom: 1px; }
  .cow-bar-label { position: absolute; top: 100%; left: 0; right: 0; text-align: center; font-family: var(--font-mono); font-size: .64rem; color: var(--text-soft); padding-top: 2px; }
  .cow-chart-empty { align-self: center; margin: auto; font-size: .78rem; color: var(--text-soft); }
  .cowviz figcaption { font-size: .8rem; color: var(--text-soft); margin-top: .9rem; }
  @media (max-width: 34rem) {
    .cowviz-mem { grid-template-columns: 4.2rem 1fr; gap: .6rem 1.9rem; padding: .7rem; }
    .cow-slot { flex-direction: column; align-items: flex-start; gap: 0; padding: .35rem .5rem; }
    .cow-slot.empty .cow-slot-ptr { font-size: 0; }
    .cow-slot.empty .cow-slot-ptr::after { content: &apos;-&apos;; font-size: .72rem; }
    .cowviz-bottom { grid-template-columns: 1fr; }
    .cowviz-readout { flex-direction: row; flex-wrap: wrap; gap: 1.2rem; }
  }
  .cow-dance { margin: 2rem 0; }
  .cow-dance-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr)); gap: .7rem; }
  .cow-dance-step { border: 1px solid var(--border); border-radius: 12px; background: var(--bg-soft); padding: .8rem .85rem .75rem; display: flex; flex-direction: column; gap: .45rem; min-width: 0; }
  .cow-dance-step.fix { border-color: color-mix(in srgb, var(--cow-free) 55%, var(--border)); }
  .cow-dance-n { margin: 0; font-size: .68rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--text-soft); }
  .cow-dance-step.fix .cow-dance-n { color: var(--cow-free); }
  .cow-dance-code { font-family: var(--font-mono); font-size: .76rem; font-weight: 600; overflow-wrap: anywhere; }
  .cow-dance svg { width: 100%; height: auto; display: block; }
  .cow-dance-note { margin: 0; font-size: .8rem; color: var(--text-soft); }
  .cow-dance figcaption { font-size: .8rem; color: var(--text-soft); margin-top: .7rem; }
  .cow-dance text { font-family: var(--font-mono); }
  .cow-dance text.d-cap { font-family: var(--font-sans); }
  .d-holder { fill: var(--bg); stroke: var(--border-strong); }
  .d-holder-t { font-size: 11px; font-weight: 700; fill: var(--text); }
  .d-buf { fill: var(--bg); stroke: var(--border-strong); }
  .d-buf.paid { stroke: var(--cow-paid); stroke-width: 1.5; }
  .d-buf.freed { stroke-dasharray: 4 3; opacity: .55; }
  .d-cell { fill: color-mix(in srgb, var(--text) 9%, transparent); }
  .d-cell.copied { fill: color-mix(in srgb, var(--cow-paid) 22%, transparent); }
  .d-cell.new { fill: var(--cow-free); }
  .d-buf.paid ~ .d-cell.new, .cow-dance-step:not(.fix) .d-cell.new { fill: var(--cow-paid); }
  .d-cell.freed, .d-cell-t.freed { opacity: .45; }
  .d-cell-t { font-size: 10px; fill: var(--text); }
  .d-cell-t.new { fill: var(--bg); font-weight: 700; }
  .d-cap { font-family: var(--font-sans); font-size: 9.5px; fill: var(--text-soft); }
  .d-cap.paid { fill: var(--cow-paid); font-weight: 700; }
  .d-cap.free { fill: var(--cow-free); font-weight: 700; }
  .d-cap.shared { fill: var(--cow-shared); font-weight: 700; }
  .d-wire { fill: none; stroke: var(--text-soft); stroke-width: 1.4; }
  .d-wire.paid { stroke: var(--cow-paid); stroke-width: 1.8; }
  .d-head { fill: var(--text-soft); }
  .d-head.paid { fill: var(--cow-paid); }
  @media (prefers-reduced-motion: reduce) { .cowviz *, .cowviz *::after { animation: none !important; transition: none !important; } }
&lt;/style&gt;
&lt;p&gt;Two things to notice. First, assignments are free - you can hand an array to five owners and the counter doesn’t move. Second, the copy is paid by &lt;em&gt;whoever mutates a shared buffer first&lt;/em&gt;, and only once: after &lt;code&gt;b&lt;/code&gt; takes its private copy, &lt;code&gt;b&lt;/code&gt; is unique again and further appends are in place. One shared mutation costs one O(n) copy. That’s a good deal.&lt;/p&gt;
&lt;p&gt;The bad deal is when something keeps the buffer shared &lt;em&gt;during every mutation&lt;/em&gt;. Then every append is O(n), and a loop of appends is O(n²). Flip the &lt;code&gt;didSet&lt;/code&gt; switch in the figure and hammer &lt;code&gt;a.append&lt;/code&gt; to see it - the bars stop being flat and climb by one more element each time. That staircase is what O(n²) looks like.&lt;/p&gt;
&lt;h2 id=&quot;three-ways-to-pay-on-every-mutation&quot;&gt;Three ways to pay on every mutation&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;A &lt;code&gt;didSet&lt;/code&gt; that reads &lt;code&gt;oldValue&lt;/code&gt;.&lt;/strong&gt; This is the one in the figure. To hand you &lt;code&gt;oldValue&lt;/code&gt;, Swift has to keep the old array alive across the mutation - which means a second reference to the buffer, which means the uniqueness check fails, which means a full copy. Every time.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; items: [Item] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; [] {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    didSet&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; items.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; !=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; oldValue.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;refresh&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;() } }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;didSet&lt;/code&gt; that never mentions &lt;code&gt;oldValue&lt;/code&gt; doesn’t have this problem (that was the point of SE-0268), so the fix is usually to not ask for it: track the count yourself, or compare something cheaper.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The read-modify-write dance.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; bucket &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; groups[key] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;??&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; []   &lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;// second reference to the buffer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;bucket.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(item)              &lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;// shared → full copy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;groups[key] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; bucket             &lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;// old buffer freed&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Three innocent lines, one full copy per item. The dictionary still holds the buffer while you append to your local, so your local is never unique. The in-place spelling has no local and no copy:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;groups[key, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: []].&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(item)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;figure class=&quot;cow-dance&quot;&gt;
&lt;div class=&quot;cow-dance-grid&quot;&gt;
  &lt;div class=&quot;cow-dance-step&quot;&gt;
    &lt;p class=&quot;cow-dance-n&quot;&gt;Step 1&lt;/p&gt;
    &lt;code class=&quot;cow-dance-code&quot;&gt;var bucket = groups[key] ?? []&lt;/code&gt;
    &lt;svg viewBox=&quot;0 0 260 124&quot; role=&quot;img&quot; aria-label=&quot;groups[key] and bucket both point at one buffer holding 1, 2, 3, refcount 2.&quot;&gt;&lt;defs&gt;&lt;marker id=&quot;d-arrow-Step1&quot; viewBox=&quot;0 0 8 8&quot; refX=&quot;7&quot; refY=&quot;4&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L8,4 L0,8 z&quot; class=&quot;d-head&quot;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;marker id=&quot;d-arrow-Step1-paid&quot; viewBox=&quot;0 0 8 8&quot; refX=&quot;7&quot; refY=&quot;4&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L8,4 L0,8 z&quot; class=&quot;d-head paid&quot;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;/defs&gt;&lt;rect x=&quot;6&quot; y=&quot;19&quot; width=&quot;96&quot; height=&quot;26&quot; rx=&quot;6&quot; class=&quot;d-holder&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;54&quot; y=&quot;36&quot; text-anchor=&quot;middle&quot; class=&quot;d-holder-t&quot;&gt;groups[key]&lt;/text&gt;&lt;rect x=&quot;6&quot; y=&quot;73&quot; width=&quot;96&quot; height=&quot;26&quot; rx=&quot;6&quot; class=&quot;d-holder&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;54&quot; y=&quot;90&quot; text-anchor=&quot;middle&quot; class=&quot;d-holder-t&quot;&gt;bucket&lt;/text&gt;&lt;rect x=&quot;146&quot; y=&quot;43&quot; width=&quot;73&quot; height=&quot;27&quot; rx=&quot;6&quot; class=&quot;d-buf&quot;&gt;&lt;/rect&gt;&lt;rect x=&quot;154&quot; y=&quot;48&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;162.5&quot; y=&quot;60.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;1&lt;/text&gt;&lt;rect x=&quot;174&quot; y=&quot;48&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;182.5&quot; y=&quot;60.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;2&lt;/text&gt;&lt;rect x=&quot;194&quot; y=&quot;48&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;202.5&quot; y=&quot;60.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;3&lt;/text&gt;&lt;text x=&quot;147&quot; y=&quot;82&quot; class=&quot;d-cap shared&quot;&gt;refcount 2 · shared&lt;/text&gt;&lt;path d=&quot;M102,32 C122.5,32 122.5,51 143,51&quot; class=&quot;d-wire&quot; marker-end=&quot;url(#d-arrow-Step1)&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M102,86 C122.5,86 122.5,62 143,62&quot; class=&quot;d-wire&quot; marker-end=&quot;url(#d-arrow-Step1)&quot;&gt;&lt;/path&gt;&lt;/svg&gt;
    &lt;p class=&quot;cow-dance-note&quot;&gt;Free: the local is a second pointer to the dictionary’s buffer.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;cow-dance-step&quot;&gt;
    &lt;p class=&quot;cow-dance-n&quot;&gt;Step 2&lt;/p&gt;
    &lt;code class=&quot;cow-dance-code&quot;&gt;bucket.append(item)&lt;/code&gt;
    &lt;svg viewBox=&quot;0 0 260 124&quot; role=&quot;img&quot; aria-label=&quot;groups[key] still points at the old buffer 1, 2, 3. bucket now points at a new copy 1, 2, 3, 4.&quot;&gt;&lt;defs&gt;&lt;marker id=&quot;d-arrow-Step2&quot; viewBox=&quot;0 0 8 8&quot; refX=&quot;7&quot; refY=&quot;4&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L8,4 L0,8 z&quot; class=&quot;d-head&quot;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;marker id=&quot;d-arrow-Step2-paid&quot; viewBox=&quot;0 0 8 8&quot; refX=&quot;7&quot; refY=&quot;4&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L8,4 L0,8 z&quot; class=&quot;d-head paid&quot;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;/defs&gt;&lt;rect x=&quot;6&quot; y=&quot;19&quot; width=&quot;96&quot; height=&quot;26&quot; rx=&quot;6&quot; class=&quot;d-holder&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;54&quot; y=&quot;36&quot; text-anchor=&quot;middle&quot; class=&quot;d-holder-t&quot;&gt;groups[key]&lt;/text&gt;&lt;rect x=&quot;6&quot; y=&quot;73&quot; width=&quot;96&quot; height=&quot;26&quot; rx=&quot;6&quot; class=&quot;d-holder&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;54&quot; y=&quot;90&quot; text-anchor=&quot;middle&quot; class=&quot;d-holder-t&quot;&gt;bucket&lt;/text&gt;&lt;rect x=&quot;146&quot; y=&quot;16&quot; width=&quot;73&quot; height=&quot;27&quot; rx=&quot;6&quot; class=&quot;d-buf&quot;&gt;&lt;/rect&gt;&lt;rect x=&quot;154&quot; y=&quot;21&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;162.5&quot; y=&quot;33.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;1&lt;/text&gt;&lt;rect x=&quot;174&quot; y=&quot;21&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;182.5&quot; y=&quot;33.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;2&lt;/text&gt;&lt;rect x=&quot;194&quot; y=&quot;21&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;202.5&quot; y=&quot;33.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;3&lt;/text&gt;&lt;text x=&quot;147&quot; y=&quot;55&quot; class=&quot;d-cap&quot;&gt;refcount 1&lt;/text&gt;&lt;rect x=&quot;146&quot; y=&quot;70&quot; width=&quot;93&quot; height=&quot;27&quot; rx=&quot;6&quot; class=&quot;d-buf paid&quot;&gt;&lt;/rect&gt;&lt;rect x=&quot;154&quot; y=&quot;75&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell copied&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;162.5&quot; y=&quot;87.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;1&lt;/text&gt;&lt;rect x=&quot;174&quot; y=&quot;75&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell copied&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;182.5&quot; y=&quot;87.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;2&lt;/text&gt;&lt;rect x=&quot;194&quot; y=&quot;75&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell copied&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;202.5&quot; y=&quot;87.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;3&lt;/text&gt;&lt;rect x=&quot;214&quot; y=&quot;75&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell new&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;222.5&quot; y=&quot;87.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t new&quot;&gt;4&lt;/text&gt;&lt;text x=&quot;147&quot; y=&quot;109&quot; class=&quot;d-cap paid&quot;&gt;new copy · 3 copied&lt;/text&gt;&lt;path d=&quot;M102,32 C122.5,32 122.5,32 143,32&quot; class=&quot;d-wire&quot; marker-end=&quot;url(#d-arrow-Step2)&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M102,86 C122.5,86 122.5,86 143,86&quot; class=&quot;d-wire paid&quot; marker-end=&quot;url(#d-arrow-Step2-paid)&quot;&gt;&lt;/path&gt;&lt;/svg&gt;
    &lt;p class=&quot;cow-dance-note&quot;&gt;Not unique, so the whole bucket is copied before the append.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;cow-dance-step&quot;&gt;
    &lt;p class=&quot;cow-dance-n&quot;&gt;Step 3&lt;/p&gt;
    &lt;code class=&quot;cow-dance-code&quot;&gt;groups[key] = bucket&lt;/code&gt;
    &lt;svg viewBox=&quot;0 0 260 124&quot; role=&quot;img&quot; aria-label=&quot;Both point at the new buffer 1, 2, 3, 4. The old buffer is freed.&quot;&gt;&lt;defs&gt;&lt;marker id=&quot;d-arrow-Step3&quot; viewBox=&quot;0 0 8 8&quot; refX=&quot;7&quot; refY=&quot;4&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L8,4 L0,8 z&quot; class=&quot;d-head&quot;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;marker id=&quot;d-arrow-Step3-paid&quot; viewBox=&quot;0 0 8 8&quot; refX=&quot;7&quot; refY=&quot;4&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L8,4 L0,8 z&quot; class=&quot;d-head paid&quot;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;/defs&gt;&lt;rect x=&quot;6&quot; y=&quot;19&quot; width=&quot;96&quot; height=&quot;26&quot; rx=&quot;6&quot; class=&quot;d-holder&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;54&quot; y=&quot;36&quot; text-anchor=&quot;middle&quot; class=&quot;d-holder-t&quot;&gt;groups[key]&lt;/text&gt;&lt;rect x=&quot;6&quot; y=&quot;73&quot; width=&quot;96&quot; height=&quot;26&quot; rx=&quot;6&quot; class=&quot;d-holder&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;54&quot; y=&quot;90&quot; text-anchor=&quot;middle&quot; class=&quot;d-holder-t&quot;&gt;bucket&lt;/text&gt;&lt;rect x=&quot;146&quot; y=&quot;16&quot; width=&quot;73&quot; height=&quot;27&quot; rx=&quot;6&quot; class=&quot;d-buf freed&quot;&gt;&lt;/rect&gt;&lt;rect x=&quot;154&quot; y=&quot;21&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell freed&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;162.5&quot; y=&quot;33.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t freed&quot;&gt;1&lt;/text&gt;&lt;rect x=&quot;174&quot; y=&quot;21&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell freed&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;182.5&quot; y=&quot;33.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t freed&quot;&gt;2&lt;/text&gt;&lt;rect x=&quot;194&quot; y=&quot;21&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell freed&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;202.5&quot; y=&quot;33.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t freed&quot;&gt;3&lt;/text&gt;&lt;text x=&quot;147&quot; y=&quot;55&quot; class=&quot;d-cap muted&quot;&gt;freed&lt;/text&gt;&lt;rect x=&quot;146&quot; y=&quot;70&quot; width=&quot;93&quot; height=&quot;27&quot; rx=&quot;6&quot; class=&quot;d-buf&quot;&gt;&lt;/rect&gt;&lt;rect x=&quot;154&quot; y=&quot;75&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;162.5&quot; y=&quot;87.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;1&lt;/text&gt;&lt;rect x=&quot;174&quot; y=&quot;75&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;182.5&quot; y=&quot;87.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;2&lt;/text&gt;&lt;rect x=&quot;194&quot; y=&quot;75&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;202.5&quot; y=&quot;87.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;3&lt;/text&gt;&lt;rect x=&quot;214&quot; y=&quot;75&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;222.5&quot; y=&quot;87.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;4&lt;/text&gt;&lt;text x=&quot;147&quot; y=&quot;109&quot; class=&quot;d-cap&quot;&gt;refcount 2 for now&lt;/text&gt;&lt;path d=&quot;M102,32 C122.5,32 122.5,78 143,78&quot; class=&quot;d-wire&quot; marker-end=&quot;url(#d-arrow-Step3)&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M102,86 C122.5,86 122.5,86 143,86&quot; class=&quot;d-wire&quot; marker-end=&quot;url(#d-arrow-Step3)&quot;&gt;&lt;/path&gt;&lt;/svg&gt;
    &lt;p class=&quot;cow-dance-note&quot;&gt;The old buffer is thrown away. Next item: the same full copy again.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;cow-dance-step fix&quot;&gt;
    &lt;p class=&quot;cow-dance-n&quot;&gt;The fix&lt;/p&gt;
    &lt;code class=&quot;cow-dance-code&quot;&gt;groups[key, default: []].append(item)&lt;/code&gt;
    &lt;svg viewBox=&quot;0 0 260 124&quot; role=&quot;img&quot; aria-label=&quot;groups[key] is the only pointer to its buffer; 4 is appended in place.&quot;&gt;&lt;defs&gt;&lt;marker id=&quot;d-arrow-Thefix&quot; viewBox=&quot;0 0 8 8&quot; refX=&quot;7&quot; refY=&quot;4&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L8,4 L0,8 z&quot; class=&quot;d-head&quot;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;marker id=&quot;d-arrow-Thefix-paid&quot; viewBox=&quot;0 0 8 8&quot; refX=&quot;7&quot; refY=&quot;4&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto&quot;&gt;&lt;path d=&quot;M0,0 L8,4 L0,8 z&quot; class=&quot;d-head paid&quot;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;/defs&gt;&lt;rect x=&quot;6&quot; y=&quot;19&quot; width=&quot;96&quot; height=&quot;26&quot; rx=&quot;6&quot; class=&quot;d-holder&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;54&quot; y=&quot;36&quot; text-anchor=&quot;middle&quot; class=&quot;d-holder-t&quot;&gt;groups[key]&lt;/text&gt;&lt;rect x=&quot;146&quot; y=&quot;43&quot; width=&quot;93&quot; height=&quot;27&quot; rx=&quot;6&quot; class=&quot;d-buf&quot;&gt;&lt;/rect&gt;&lt;rect x=&quot;154&quot; y=&quot;48&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;162.5&quot; y=&quot;60.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;1&lt;/text&gt;&lt;rect x=&quot;174&quot; y=&quot;48&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;182.5&quot; y=&quot;60.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;2&lt;/text&gt;&lt;rect x=&quot;194&quot; y=&quot;48&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;202.5&quot; y=&quot;60.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t&quot;&gt;3&lt;/text&gt;&lt;rect x=&quot;214&quot; y=&quot;48&quot; width=&quot;17&quot; height=&quot;17&quot; rx=&quot;3&quot; class=&quot;d-cell new&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;222.5&quot; y=&quot;60.5&quot; text-anchor=&quot;middle&quot; class=&quot;d-cell-t new&quot;&gt;4&lt;/text&gt;&lt;text x=&quot;147&quot; y=&quot;82&quot; class=&quot;d-cap free&quot;&gt;refcount 1 · in place&lt;/text&gt;&lt;path d=&quot;M102,32 C122.5,32 122.5,51 143,51&quot; class=&quot;d-wire&quot; marker-end=&quot;url(#d-arrow-Thefix)&quot;&gt;&lt;/path&gt;&lt;/svg&gt;
    &lt;p class=&quot;cow-dance-note&quot;&gt;No local, so the buffer stays unique. Nothing is copied.&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;
  &lt;figcaption&gt;The read-modify-write version copies the whole bucket for every item, so building a bucket of n items copies about n²/2 elements. The subscript-with-default version mutates the dictionary&apos;s storage directly.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;&lt;strong&gt;Your own structs don’t get this for free.&lt;/strong&gt; A struct made of arrays inherits copy-on-write from its members. A struct that wraps a class reference gets nothing - copies share the object and mutations leak between them, which is the opposite bug. If you’re building a value type on top of reference storage, the uniqueness check is yours to write:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;isKnownUniquelyReferenced&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;storage) { storage &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; storage.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;copy&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;() }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That single line is the whole mechanism the figure animates. There’s no magic underneath it.&lt;/p&gt;
&lt;h2 id=&quot;what-to-take-from-this&quot;&gt;What to take from this&lt;/h2&gt;
&lt;p&gt;Don’t avoid value types to avoid copies - the copies you can see in the source are almost all fake. The ones that cost you are invisible in the source: a buffer kept alive by an observer, a local, an iterator, a captured closure. When a loop that “just appends” shows up hot in Instruments with &lt;code&gt;memmove&lt;/code&gt; underneath it, the question isn’t “why is Array slow?” It’s “who else is holding this buffer?”&lt;/p&gt;</content:encoded><category>swift</category><category>performance</category><author>tudor.turcanu@gmail.com (Tudor Turcanu)</author></item><item><title>Helvi and Own AI Now Have Their Own Websites</title><link>https://tudorturcanu.ch/blog/helvi-and-own-ai-get-their-own-sites/</link><guid isPermaLink="true">https://tudorturcanu.ch/blog/helvi-and-own-ai-get-their-own-sites/</guid><description>Two of my iPhone apps moved out of my portfolio and onto their own domains. helvi.online is for finding what&apos;s on in Switzerland, ownai.online is for running AI models on your phone without sending anything anywhere.</description><pubDate>Tue, 13 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Two of my apps now have their own homes on the web: &lt;a href=&quot;https://helvi.online&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;helvi.online&lt;/a&gt; for Helvi, and &lt;a href=&quot;https://ownai.online&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;ownai.online&lt;/a&gt; for Own AI.&lt;/p&gt;
&lt;p&gt;Until now both lived as a page inside this portfolio, next to everything else I’ve shipped. That worked for a list, but neither app is really a list item. Each one has a few things worth showing properly, and a link you can send to someone who has never heard of me. So they got their own domains, their own pages, and their own privacy policy and terms.&lt;/p&gt;
&lt;h2 id=&quot;helvi-whats-on-this-weekend&quot;&gt;Helvi: what’s on this weekend&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://helvi.online&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;helvi.online&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Helvi is a catalogue of events across Switzerland. Family days out, village markets, festivals, concerts, the odd alpine thing you only hear about if you know someone local. Right now it has more than 210 upcoming events across 90-plus towns and cities, and it isn’t limited to Zürich and Geneva.&lt;/p&gt;
&lt;p&gt;The part I care most about is the part before you’ve decided anything. The feed starts with the questions people really ask: what’s on this weekend, what’s near me, what’s free. Open an event and you get the dates, the venue, the prices and a countdown, then a day itinerary, reminders and a packing list if you want them. There’s a map where every event is a pin, and a month view with a dot on each day something is happening.&lt;/p&gt;
&lt;p&gt;A few smaller things that I’m fond of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Search treats Genf, Genève and Ginevra as the same place, because in Switzerland they are.&lt;/li&gt;
&lt;li&gt;The app and the event details come in English, German, French and Italian.&lt;/li&gt;
&lt;li&gt;Event pages show parking, step-free access, family facilities, food and the weather forecast.&lt;/li&gt;
&lt;li&gt;A home screen widget counts down the days to your next event.&lt;/li&gt;
&lt;li&gt;You can put two events side by side when you can’t pick.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It’s free, it has no ads, and an account is optional (you only need one if you want favourites synced between devices). It needs iOS 18 or later, and it’s on the Swiss App Store.&lt;/p&gt;
&lt;h2 id=&quot;own-ai-a-language-model-that-lives-on-the-phone&quot;&gt;Own AI: a language model that lives on the phone&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://ownai.online&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;ownai.online&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Own AI runs language and vision models on the iPhone itself. Your question, your documents, your photos and your voice get processed on the device, so there’s no prompt going to a model provider, because there is no model provider in the loop. There’s no account to make either.&lt;/p&gt;
&lt;p&gt;The app ships with a small Qwen3 0.6B model already inside, so the first chat works with no download. From there you can pick from a catalogue of 88 MLX models across 13 families, some under 1 GB, some that only make sense on an iPad Pro or a Mac. You can also search Hugging Face for community models. Each one is checked against your device before it downloads (architecture, files, chat template, quantization, memory), and a short self-test afterwards proves it loads and replies.&lt;/p&gt;
&lt;p&gt;What it does with them:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Documents.&lt;/strong&gt; Attach PDFs, DOCX, TXT, RTF or code files and ask questions. Retrieval runs on the phone and the answer shows which pages it drew from.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Math.&lt;/strong&gt; Answers render LaTeX, group derivations into steps, and open in a formula inspector that can plot in 2D and 3D.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Voice.&lt;/strong&gt; Speech recognition on the device, and replies read aloud by local text-to-speech.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Study sets.&lt;/strong&gt; A document becomes flashcards and quizzes, each with its source quote, coming back after 1, 3, 7, 14 and 30 days.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Siri, Shortcuts, widgets and an Apple Watch companion&lt;/strong&gt;, for when you don’t want to open the app.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Only two things ever touch the network: a model you choose to download, and a web search you choose to run. With a local model everything else works in airplane mode, which is the quickest way to check I’m not making this up.&lt;/p&gt;
&lt;p&gt;It’s free with a daily allowance of up to five messages, and Pro lifts the limits. It needs iOS 18, and an A14 chip or newer (iPhone 12 or later) to run local models. The interface comes in English, German, Spanish and French.&lt;/p&gt;
&lt;h2 id=&quot;why-separate-sites&quot;&gt;Why separate sites&lt;/h2&gt;
&lt;p&gt;Mostly because the pages can be honest about one thing at a time. The Own AI page has a section on how the engine keeps memory under control on a phone, with small animations, which would be strange inside a general portfolio. The Helvi page shows real events from the catalogue, which is much more convincing than a screenshot and a paragraph.&lt;/p&gt;
&lt;p&gt;It also means each app has a stable address and its own legal pages, which is what App Store listings and support links ought to point at.&lt;/p&gt;
&lt;p&gt;This portfolio keeps the full list of what I’ve built, and both apps are still on &lt;a href=&quot;https://tudorturcanu.ch/apps/&quot;&gt;the apps page&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you try either one and something is unclear, broken or missing, the &lt;a href=&quot;https://tudorturcanu.ch/contact/&quot;&gt;contact page&lt;/a&gt; is the way to tell me.&lt;/p&gt;</content:encoded><category>apps</category><category>announcement</category><category>ios</category><author>tudor.turcanu@gmail.com (Tudor Turcanu)</author></item><item><title>What Whole-Module Optimization Actually Deletes</title><link>https://tudorturcanu.ch/blog/wmo-what-gets-deleted/</link><guid isPermaLink="true">https://tudorturcanu.ch/blog/wmo-what-gets-deleted/</guid><description>You write protocols and generics for the humans; the release binary runs like hand-written C. Here&apos;s a stage-by-stage look at what the optimizer removes on the way - specialization, devirtualization, inlining, ARC - and why the abstraction you wrote never reaches the CPU.</description><pubDate>Fri, 18 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In &lt;a href=&quot;https://tudorturcanu.ch/blog/swift-guardrails-ai/&quot;&gt;the compiler guardrails post&lt;/a&gt; I claimed that the abstraction you write for the humans never makes it to the CPU - that &lt;code&gt;swiftc&lt;/code&gt; melts your protocols and generics into something a C programmer in a hurry would recognize. A few people asked what that melting actually looks like, step by step. Fair question, because the answer is one of the most satisfying things the toolchain does, and it happens invisibly on every archive build.&lt;/p&gt;
&lt;p&gt;So let’s take one small, ordinary function and watch the optimizer take it apart.&lt;/p&gt;
&lt;h2 id=&quot;the-specimen&quot;&gt;The specimen&lt;/h2&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt; total&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;S&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;Sequence&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; xs: S) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;-&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt; Double&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; where&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; S.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;Element&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt; Double&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; sum &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; xs { sum &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; x }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; sum&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; sum &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt; total&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(prices)   &lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;// prices: [Double]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Nice and abstract: any sequence of doubles, one implementation. Exactly the kind of code Swift wants you to write - and, naively compiled, exactly the kind of code that should be slow. Every iteration goes through a protocol witness table to find &lt;code&gt;next()&lt;/code&gt;, every element arrives behind a layer of generic indirection, and the loop can’t be reasoned about because the compiler doesn’t know what &lt;code&gt;S&lt;/code&gt; is.&lt;/p&gt;
&lt;p&gt;Unless it does. Step through what each optimization pass removes:&lt;/p&gt;
&lt;figure class=&quot;wviz&quot; id=&quot;wmo-viz&quot;&gt;
  &lt;div class=&quot;wviz-stages&quot; role=&quot;tablist&quot; aria-label=&quot;Optimization stage&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;wviz-panel&quot;&gt;
    &lt;pre class=&quot;wviz-code&quot; id=&quot;wmo-code&quot;&gt;&lt;/pre&gt;
    &lt;p class=&quot;wviz-annot&quot; id=&quot;wmo-annot&quot;&gt;&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;wviz-meters&quot; id=&quot;wmo-meters&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;wviz-nav&quot;&gt;
    &lt;button type=&quot;button&quot; id=&quot;wmo-prev&quot;&gt;← Prev&lt;/button&gt;
    &lt;button type=&quot;button&quot; id=&quot;wmo-next&quot;&gt;Next stage →&lt;/button&gt;
  &lt;/div&gt;
  &lt;figcaption&gt;Pseudocode, not real SIL - the shapes and the counts are what matter, not the spelling. Meter values are illustrative for a 1,000-element array.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;script src=&quot;https://tudorturcanu.ch/scripts/wmo-viz.js?v=2&quot; defer&gt;&lt;/script&gt;
&lt;style&gt;
  .wviz { border: 1px solid var(--border); border-radius: 10px; background: var(--bg-soft); padding: 1rem 1rem 1.1rem; margin: 2rem 0; }
  .wviz-stages { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: .9rem; }
  .wviz-stage { font: inherit; font-size: .8rem; cursor: pointer; padding: .32rem .65rem; border-radius: 999px; border: 1px solid var(--border-strong); background: transparent; color: var(--text-soft); }
  .wviz-stage[aria-selected=&quot;true&quot;] { background: var(--accent); border-color: var(--accent); color: var(--bg); font-weight: 600; }
  .wviz-code { font-family: var(--font-mono); font-size: .78rem; line-height: 1.55; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: .8rem .9rem; overflow-x: auto; margin: 0; white-space: pre; }
  .wviz-code .gone { opacity: .38; text-decoration: line-through; }
  .wviz-annot { font-size: .85rem; color: var(--text-soft); margin: .7rem 0 0; }
  .wviz-meters { display: grid; grid-template-columns: max-content 1fr max-content; gap: .35rem .7rem; align-items: center; margin-top: .9rem; padding-top: .8rem; border-top: 1px solid var(--border); font-size: .78rem; }
  .wviz-meter-label { color: var(--text-soft); font-family: var(--font-mono); white-space: nowrap; }
  .wviz-meter-track { background: color-mix(in srgb, var(--border) 60%, transparent); border-radius: 999px; height: 8px; overflow: hidden; }
  .wviz-meter-fill { height: 100%; border-radius: 999px; background: var(--accent); transition: width .35s ease; }
  .wviz-meter-fill.zero { background: color-mix(in srgb, var(--text-soft) 40%, transparent); }
  .wviz-meter-val { font-family: var(--font-mono); font-weight: 600; min-width: 4.5ch; text-align: right; }
  .wviz-nav { display: flex; justify-content: space-between; margin-top: .9rem; }
  .wviz-nav button { font: inherit; cursor: pointer; padding: .35rem .8rem; border-radius: 6px; border: 1px solid var(--border-strong); background: transparent; color: var(--text); }
  .wviz-nav button:disabled { opacity: .4; cursor: default; }
  .wviz figcaption { font-size: .8rem; color: var(--text-soft); margin-top: .7rem; }
&lt;/style&gt;
&lt;h2 id=&quot;why-whole-module-is-the-load-bearing-word&quot;&gt;Why “whole-module” is the load-bearing word&lt;/h2&gt;
&lt;p&gt;Every pass in that sequence depends on the compiler &lt;em&gt;seeing&lt;/em&gt; both sides of the call: the generic function and the concrete call site. Compile them in isolation - one file at a time, the old default - and &lt;code&gt;total&lt;/code&gt; has to be compiled as the fully-generic, witness-table-driven version, because some unknown caller somewhere might pass it a linked list.&lt;/p&gt;
&lt;p&gt;Whole-module optimization is just the permission slip: with the entire module visible at once, the compiler knows &lt;em&gt;every&lt;/em&gt; caller, sees that &lt;code&gt;S&lt;/code&gt; is only ever &lt;code&gt;[Double]&lt;/code&gt;, and can commit to the specialized version - then let devirtualization, inlining, and ARC elimination cascade from there, each pass exposing the next. Cross-module optimization and &lt;code&gt;@inlinable&lt;/code&gt; extend the same visibility across package boundaries, which is a big part of why SPM-heavy codebases care about their settings here.&lt;/p&gt;
&lt;h2 id=&quot;the-part-worth-internalizing&quot;&gt;The part worth internalizing&lt;/h2&gt;
&lt;p&gt;The stage-4 loop - pointer, stride, add, no calls, no reference counting - is what you’d have written in C if you were optimizing by hand. The difference is that your version states its intent (&lt;code&gt;any sequence of doubles, summed&lt;/code&gt;), gets checked by the type system, and &lt;em&gt;still compiles down to that loop&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;That’s the deal I keep coming back to: abstraction in Swift is a source-code phenomenon. You pay for it in compile time, occasionally in binary size - and almost never, in a release build, at runtime. Which means the common performance instinct of “avoid protocols, avoid generics, they’re slow” is optimizing against a cost model the optimizer deleted years ago. Write the clear version; the compiler’s half of the bargain is better than yours.&lt;/p&gt;</content:encoded><category>swift</category><category>xcode</category><author>tudor.turcanu@gmail.com (Tudor Turcanu)</author></item><item><title>There Has Never Been a Better Time to Learn a New Programming Language</title><link>https://tudorturcanu.ch/blog/best-time-to-learn-a-new-language/</link><guid isPermaLink="true">https://tudorturcanu.ch/blog/best-time-to-learn-a-new-language/</guid><description>I&apos;m learning Kotlin for Android after a decade of Swift, and it&apos;s the easiest language pickup of my life - not because I got smarter. The cost of learning a new language just collapsed.</description><pubDate>Tue, 11 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I’m learning Kotlin. After a decade of Swift, Android was always the thing I’d get to “eventually” - and eventually arrived this year, mostly out of curiosity about how the other half ships. It has been the easiest language pickup of my life.&lt;/p&gt;
&lt;p&gt;Not because I got smarter. Because the economics of learning a language just changed completely.&lt;/p&gt;
&lt;h2 id=&quot;syntax-was-never-the-expensive-part&quot;&gt;Syntax was never the expensive part&lt;/h2&gt;
&lt;p&gt;You can learn any language’s syntax from a book in a weekend. That was true in 2005 and it’s true now. The expensive parts were always somewhere else:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Idiom.&lt;/strong&gt; Knowing what the language &lt;em&gt;can&lt;/em&gt; write versus what its community &lt;em&gt;does&lt;/em&gt; write. Every language has a dialect that marks you as local, and books are always five years behind it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The toolchain.&lt;/strong&gt; Nobody warns you that learning Android is 20% Kotlin and 80% Gradle.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The under-the-hood questions.&lt;/strong&gt; The gap between using a language and knowing it is a thousand small questions - &lt;em&gt;what does this actually compile to? where does this allocate? why is this fast and that slow?&lt;/em&gt; - that used to take years of scattered blog posts and Stack Overflow archaeology to answer.&lt;/p&gt;
&lt;p&gt;AI didn’t make syntax easier. It made those three things cheap. That’s the whole story.&lt;/p&gt;
&lt;h2 id=&quot;learn-by-translation-not-from-scratch&quot;&gt;Learn by translation, not from scratch&lt;/h2&gt;
&lt;p&gt;Here’s the trick that’s made Kotlin feel easy: I never ask “how does Kotlin work?” I ask “in Swift I’d write this - what’s the idiomatic Kotlin, and why is it shaped differently?”&lt;/p&gt;
&lt;p&gt;An AI that knows both languages fluently is a personal translator between mental models. And a second language learned by translation goes fast, because you’re not learning programming - you’re learning a mapping. &lt;code&gt;let&lt;/code&gt; becomes &lt;code&gt;val&lt;/code&gt;. Protocols plus extensions become interfaces plus extension functions. &lt;code&gt;async/await&lt;/code&gt; becomes coroutines. Swift and Kotlin grew up watching each other, and it shows; some days the delta feels closer to an accent than a language.&lt;/p&gt;
&lt;p&gt;Where the mapping breaks is exactly where it gets interesting - and that’s where an endless, patient explainer changes everything. Nobody at work rolls their eyes when I ask my fourth “but &lt;em&gt;why&lt;/em&gt; is it like that?” of the morning.&lt;/p&gt;
&lt;h2 id=&quot;go-under-the-hood---it-finally-costs-nothing&quot;&gt;Go under the hood - it finally costs nothing&lt;/h2&gt;
&lt;p&gt;This is the part I care most about. Knowing a language means knowing what’s beneath it, and those questions used to be the hardest ones to get answered:&lt;/p&gt;
&lt;p&gt;What does a &lt;code&gt;suspend&lt;/code&gt; function compile to? (A state machine with a continuation parameter - the same broad strategy Swift uses for &lt;code&gt;async&lt;/code&gt;, expressed through Kotlin’s coroutine machinery.) What actually runs my Kotlin on Android - the JVM? (No: ART, with its own mix of ahead-of-time and JIT compilation. The JVM performance rules you may know do not transfer cleanly.) What does &lt;code&gt;data class&lt;/code&gt; generate? Where do captured variables in a lambda live?&lt;/p&gt;
&lt;p&gt;Ten years ago, chasing one of those to the bottom was an evening. Now it’s a conversation - and each answer survives cross-examination: &lt;em&gt;show me the bytecode, compare it to what swiftc does, when would this bite me?&lt;/em&gt; Learning a language while understanding its machinery used to be a luxury for people with unusual patience. Now it’s the default way in.&lt;/p&gt;
&lt;h2 id=&quot;the-one-rule-ai-explains-you-type&quot;&gt;The one rule: AI explains, you type&lt;/h2&gt;
&lt;p&gt;The discipline that makes any of this work: the AI writes none of the learning code. Every line, I type. The moment an agent writes my Kotlin, I’m no longer learning to write Kotlin - I’m learning to review it, which is a different and shallower skill. It’s the same logic as keeping the agent in a sandbox and integrating by hand: the friction isn’t waste, the friction is where the learning lives.&lt;/p&gt;
&lt;p&gt;Use it as the tireless senior colleague instead - the one who explains, never judges the question, and has read every compiler source dump ever posted.&lt;/p&gt;
&lt;h2 id=&quot;so-pick-the-one-youve-been-postponing&quot;&gt;So pick the one you’ve been postponing&lt;/h2&gt;
&lt;p&gt;Learning a language is thousands of small questions, and the price per question just dropped to roughly zero. The language you’ve been putting off for years costs a fraction of what it did when you first postponed it.&lt;/p&gt;
&lt;p&gt;Mine was Kotlin. It took a few evenings to feel at home, and the surprise wasn’t that it was possible. It’s that it was &lt;em&gt;pleasant&lt;/em&gt;.&lt;/p&gt;</content:encoded><category>ai</category><category>kotlin</category><category>learning</category><author>tudor.turcanu@gmail.com (Tudor Turcanu)</author></item><item><title>Every Compiler Error Is a Bug That Didn&apos;t Ship</title><link>https://tudorturcanu.ch/blog/swift-guardrails-ai/</link><guid isPermaLink="true">https://tudorturcanu.ch/blog/swift-guardrails-ai/</guid><description>Sendable, actors, optionals, exhaustive switches - Swift&apos;s strictness used to feel like friction. Now that half our code starts life in an AI agent, the compiler quietly became the most valuable tool in the chain.</description><pubDate>Thu, 30 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;We spend a lot of time these days being amazed that an AI can turn a sentence into working code. Fair enough - it is amazing. But somewhere along the way we stopped being amazed by the machine that’s been doing the harder half of that trick for decades: the compiler, quietly turning our high-level intentions into assembly that’s often better than what we’d write by hand.&lt;/p&gt;
&lt;h2 id=&quot;the-magic-we-stopped-noticing&quot;&gt;The magic we stopped noticing&lt;/h2&gt;
&lt;p&gt;Take what &lt;code&gt;swiftc&lt;/code&gt; does to a release build. You write a generic function against a protocol - nice, abstract, readable. The compiler looks at how it’s actually used and &lt;em&gt;specializes&lt;/em&gt; it: the generic melts away into concrete, direct code for each type, the protocol lookup disappears, calls become direct, small functions get folded into their callers, and unnecessary ARC bookkeeping gets removed. Whole-module optimization lets it see across files to do all of this at once.&lt;/p&gt;
&lt;p&gt;The abstraction you wrote for the humans never makes it to the CPU. You get to think in protocols and generics; the binary runs like someone wrote it in a hurry, in C, with twenty years of experience. That bargain - &lt;em&gt;you write it safe and clear, I’ll make it fast&lt;/em&gt; - is one of the best deals in software, and we collect on it silently with every archive build.&lt;/p&gt;
&lt;p&gt;I bring this up because the same underappreciated machine has a second act. It doesn’t just transform your code. It &lt;em&gt;judges&lt;/em&gt; it - and that judgment is the part that just became precious.&lt;/p&gt;
&lt;h2 id=&quot;the-checks-are-the-feature&quot;&gt;The checks are the feature&lt;/h2&gt;
&lt;p&gt;There’s a complaint every Swift developer has made at least once, probably during a Swift 6 migration, probably at 6 pm: &lt;em&gt;the compiler is getting in my way&lt;/em&gt;. Sendable warnings on types you swear are fine. Actor isolation errors three layers deep. The optional you have to unwrap even though “it can’t be nil here.”&lt;/p&gt;
&lt;p&gt;I want to argue the opposite position, and more strongly than I would have two years ago: that strictness is the most valuable feature the language has. And the reason it got &lt;em&gt;more&lt;/em&gt; valuable is sitting in your terminal running an agent.&lt;/p&gt;
&lt;h2 id=&quot;what-it-compiles-actually-promises&quot;&gt;What “it compiles” actually promises&lt;/h2&gt;
&lt;p&gt;Languages differ wildly in what a successful compile means.&lt;/p&gt;
&lt;p&gt;In C, “it compiles” promises almost nothing. This compiles without a single warning on default settings, and it may crash the moment it runs:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;name &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt; NULL&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;Hello, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;%s\\&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;n&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, name);&lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;  // uses a value that isn&apos;t there&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Swift, that value would be an optional. You have to decide what to do when it is &lt;code&gt;nil&lt;/code&gt; before the compiler lets you use it. The whole category is gone, not discouraged: gone.&lt;/p&gt;
&lt;p&gt;Swift keeps doing this, category after category. Optionals make “forgot the nil check” a type error. Exhaustive switches make “forgot the new enum case” a build break instead of a silent fallthrough. Definite initialization kills the read-before-write. And with strict concurrency, &lt;code&gt;Sendable&lt;/code&gt; and actors can catch data races before they turn into the kind of production bugs that are notoriously hard to reproduce.&lt;/p&gt;
&lt;p&gt;Each of those checks is a pattern the compiler &lt;em&gt;enforces&lt;/em&gt; rather than recommends. That distinction always mattered. It matters ten times more now.&lt;/p&gt;
&lt;h2 id=&quot;the-reviewer-that-scales&quot;&gt;The reviewer that scales&lt;/h2&gt;
&lt;p&gt;Here’s what changed: code got cheap. An agent writes in an afternoon what a team used to write in a sprint. Whatever you think of that code’s quality, its &lt;em&gt;volume&lt;/em&gt; is real - and review capacity didn’t grow with it. The bottleneck moved from writing code to catching what’s wrong with it.&lt;/p&gt;
&lt;p&gt;A human reviewer has limited time and attention, and plausible-looking code can still hide mistakes. The compiler applies the same checks to every line, every time. When code production scales up, the only reviewer that scales with it is the one built into the toolchain.&lt;/p&gt;
&lt;p&gt;This is where language choice quietly became an AI-era decision. An agent producing C can hand you something that compiles cleanly and still corrupts memory, races on a shared int, or leaks - because C’s compiler was never asked to check for any of that. An agent producing Swift under strict concurrency has a far harder time handing you a data race that compiles unnoticed. The guarantee doesn’t come from the model being smart. It comes from the language refusing to accept the bad program.&lt;/p&gt;
&lt;h2 id=&quot;the-error-message-is-a-prompt&quot;&gt;The error message is a prompt&lt;/h2&gt;
&lt;p&gt;There’s a second effect that took me longer to notice. Watch an agent work in a strict language and you see a loop: write, compile, read the error, correct. Every one of those errors is course-correction toward the enforced pattern - &lt;em&gt;this closure captures non-Sendable state; this case isn’t handled; this value might be nil.&lt;/em&gt; The compiler isn’t just rejecting bad code. It’s steering the generation, error by error, toward the shape the language wants.&lt;/p&gt;
&lt;p&gt;In a permissive language that loop barely exists. The code compiles on the first try - including the versions that are wrong - so the model never gets pushed. Swift’s strictness gives the agent something C can’t: a tight, truthful feedback signal. The stricter the checks, the more “it finally compiles” converges on “it’s actually right.”&lt;/p&gt;
&lt;p&gt;That reframes the migration pain, too. All that work making your codebase Sendable-clean wasn’t just paying down your own bug debt - it was building the guardrails that every future contributor runs inside, including the ones that aren’t human. Those checks are also getting easier to live with: weak &lt;code&gt;let&lt;/code&gt;, clearer isolation, and &lt;code&gt;Task&lt;/code&gt; warnings for ignored errors. Each small strictness improvement is compounding infrastructure now.&lt;/p&gt;
&lt;h2 id=&quot;watch-the-escape-hatches&quot;&gt;Watch the escape hatches&lt;/h2&gt;
&lt;p&gt;The honest caveat: guardrails only work if you don’t override them, and Swift gives you ways to do that - &lt;code&gt;@unchecked Sendable&lt;/code&gt;, &lt;code&gt;try!&lt;/code&gt;, force unwraps, &lt;code&gt;nonisolated(unsafe)&lt;/code&gt;. They exist for good reasons and they’re all one keystroke away.&lt;/p&gt;
&lt;p&gt;Agents will reach for them, because agents optimize for making the error go away, and the escape hatch &lt;em&gt;is&lt;/em&gt; the shortest path. My house rule, for humans and machines alike: an escape hatch in a diff is a finding, not a fix. If the review checklist contains one item, make it “search the diff for &lt;code&gt;unsafe&lt;/code&gt;, &lt;code&gt;unchecked&lt;/code&gt;, and &lt;code&gt;!&lt;/code&gt;.” This is exactly the kind of rule worth putting in a project skill, so an agent checks its own diff before calling the task done. The compiler can only protect the product if the annotations telling it to stop looking stay rare.&lt;/p&gt;
&lt;p&gt;Writing code is becoming cheap. Confidence in that code is not. Swift’s compiler does not replace tests or human review, but it gives both people and agents a stronger baseline before either begins.&lt;/p&gt;
&lt;p&gt;In an AI-assisted codebase, that is not friction. It is leverage: every check that catches a mistake before the app runs gives the rest of the engineering process something better to build on.&lt;/p&gt;</content:encoded><category>swift</category><category>concurrency</category><category>ai</category><author>tudor.turcanu@gmail.com (Tudor Turcanu)</author></item><item><title>Migrating to Swift Testing Without a Heroic Weekend</title><link>https://tudorturcanu.ch/blog/migrating-to-swift-testing/</link><guid isPermaLink="true">https://tudorturcanu.ch/blog/migrating-to-swift-testing/</guid><description>The reason nobody&apos;s finished migrating off XCTest was never the syntax - it was that a half-migrated suite used to fail silently. Xcode 27&apos;s interop mode is the part that actually unblocks this.</description><pubDate>Wed, 22 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I’ve started this migration three times. Rewriting &lt;code&gt;XCTAssertEqual(a, b)&lt;/code&gt; as &lt;code&gt;#expect(a == b)&lt;/code&gt; was never the hard part - a find-and-replace with a bit of judgment handles most of it. The hard part was that XCTest and Swift Testing didn’t actually understand each other. An &lt;code&gt;XCTAssert&lt;/code&gt; failure that happened to run inside a Swift Testing context, or a &lt;code&gt;#expect&lt;/code&gt; called from inside an &lt;code&gt;XCTestCase&lt;/code&gt; helper, just didn’t register. You could migrate a file, watch the suite stay green, and have genuinely lost coverage without a single red X to tell you.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2026/267/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Session 267&lt;/a&gt; at WWDC 2026 is Apple’s answer to exactly that problem, and it’s less about new syntax and more about closing that hole.&lt;/p&gt;
&lt;h2 id=&quot;what-interop-actually-fixes&quot;&gt;What interop actually fixes&lt;/h2&gt;
&lt;p&gt;Xcode 27 turns on interoperability by default: a Swift Testing test can call a helper that internally does &lt;code&gt;XCTFail&lt;/code&gt;, and that failure now surfaces as a real test issue. An &lt;code&gt;XCTestCase&lt;/code&gt; can call &lt;code&gt;#expect&lt;/code&gt; and get a real, visible failure too. The two frameworks stop being fully separate worlds - which is the thing that makes incremental migration safe instead of theoretical.&lt;/p&gt;
&lt;p&gt;There are two modes, and which one your project is in matters:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Limited mode&lt;/strong&gt; - cross-framework issues are reported as warnings. Every test plan created before Xcode 27 inherits this mode automatically, so existing projects don’t get surprise failures the day they upgrade.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Complete mode&lt;/strong&gt; - the same cross-framework issues are errors. New projects default here.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can move a project from limited to complete deliberately, once you trust the migrated portions of the suite. Don’t do it on day one of upgrading Xcode - do it once you’ve actually migrated something and want the stricter guarantee.&lt;/p&gt;
&lt;h2 id=&quot;the-actual-migration-strategy&quot;&gt;The actual migration strategy&lt;/h2&gt;
&lt;p&gt;Apple’s own recommendation, and it matches what worked the three times I tried this manually: don’t schedule a rewrite. Leave existing &lt;code&gt;XCTestCase&lt;/code&gt; classes in place. Write every new test in Swift Testing. When you’re already touching a test file for an unrelated reason, migrate it then, not before.&lt;/p&gt;
&lt;p&gt;The mechanical part, for whichever files you do touch:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Replace &lt;code&gt;XCTFail(message)&lt;/code&gt; with &lt;code&gt;Issue.record(message)&lt;/code&gt;, and carry over the source location parameter - this is what keeps failures pointing at the right line instead of at whatever helper called it.&lt;/li&gt;
&lt;li&gt;Nested loops driving repeated assertions are almost always a parameterized test in disguise:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;@Test&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(arguments&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; [Rocket.saturnV, Rocket.falcon9, Rocket.starship])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt; fuelingSequenceCompletes&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; rocket: Rocket) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; throws&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;    #expect&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; rocket.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;fuel&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;().isComplete)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Anywhere you have a &lt;code&gt;preconditionFailure&lt;/code&gt; or &lt;code&gt;fatalError&lt;/code&gt; path with no test coverage - because there was no good way to assert a crash in XCTest - Swift Testing’s exit tests cover it directly:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;@Test&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; func&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt; rejectsNegativeThrust&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    await&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt; #expect&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;processExitsWith&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: .failure) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        engine.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;setThrust&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;-1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That last one is worth going back for even outside a broader migration. “This crashes on bad input” was one of the harder things to actually prove in XCTest, and now it’s a normal test.&lt;/p&gt;
&lt;h2 id=&quot;what-id-actually-do-this-week&quot;&gt;What I’d actually do this week&lt;/h2&gt;
&lt;p&gt;Turn on Xcode 27’s default interop, leave your project in limited mode, and migrate the next test file you touch for an unrelated reason - not the whole suite. Once a meaningful chunk is migrated and you trust it, flip to complete mode so cross-framework issues start failing the build instead of just warning. That’s the whole plan. The framework finally makes “a little at a time” a safe strategy instead of a hopeful one.&lt;/p&gt;</content:encoded><category>swift</category><category>testing</category><category>wwdc</category><author>tudor.turcanu@gmail.com (Tudor Turcanu)</author></item><item><title>Scaling an iOS Codebase: Features Vertical, Logic Horizontal</title><link>https://tudorturcanu.ch/blog/features-vertical-logic-horizontal/</link><guid isPermaLink="true">https://tudorturcanu.ch/blog/features-vertical-logic-horizontal/</guid><description>Layers or features? Wrong question. Split product code vertically, platform code horizontally, and let the compiler enforce the boundary.</description><pubDate>Thu, 16 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Someone wants to split the app into layers. Someone else wants to split it into features. Both sides have conference talks to back them up.&lt;/p&gt;
&lt;p&gt;I’ve shipped code both ways, and this is where I landed.&lt;/p&gt;
&lt;h2 id=&quot;the-two-ways-everyone-does-it&quot;&gt;The two ways everyone does it&lt;/h2&gt;
&lt;p&gt;Slice &lt;strong&gt;horizontally&lt;/strong&gt; and you get layers: UI, domain, networking, persistence. This is the Clean Architecture diagram everyone has seen. It looks great on a whiteboard. Then you need to change one screen and you’re editing four modules. Your PR touches everything, your teammate’s PR touches everything, and you spend the week resolving conflicts in the domain layer.&lt;/p&gt;
&lt;p&gt;Slice &lt;strong&gt;vertically&lt;/strong&gt; and you get feature modules instead: &lt;code&gt;Checkout&lt;/code&gt;, &lt;code&gt;Profile&lt;/code&gt;, &lt;code&gt;Orders&lt;/code&gt;. Each one owns its screens, its logic, its storage. This fixes the collisions. It also means that six months in, three features have three slightly different retry implementations. Or someone got tired of duplicating code and made Checkout import Profile, and now nothing builds in isolation anymore.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://tudorturcanu.ch/images/blog/slicing-strategies.svg&quot; alt=&quot;Horizontal slicing spreads one feature across every layer; vertical slicing isolates features but tempts them to borrow each other&amp;#x27;s code&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;760&quot; height=&quot;380&quot;&gt;&lt;/p&gt;
&lt;p&gt;For years I assumed I just had to pick which of these problems I’d rather live with. I don’t think that anymore.&lt;/p&gt;
&lt;h2 id=&quot;not-all-code-is-the-same-kind-of-code&quot;&gt;Not all code is the same kind of code&lt;/h2&gt;
&lt;p&gt;Here’s what both camps miss: an app contains two kinds of code, and they want different treatment.&lt;/p&gt;
&lt;p&gt;There’s code that changes because the &lt;em&gt;product&lt;/em&gt; changes. The checkout flow. Onboarding. The settings screen that product keeps redesigning.&lt;/p&gt;
&lt;p&gt;And there’s code that changes because the &lt;em&gt;platform&lt;/em&gt; changes. Networking. Persistence. Auth. The design system. Nobody redesigns your keychain wrapper for a spring campaign.&lt;/p&gt;
&lt;p&gt;Product code wants to live in vertical slices because that’s how you work on it, one feature at a time. Platform code wants to be horizontal because everyone needs it and it should exist exactly once. So stop forcing one shape on both:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://tudorturcanu.ch/images/blog/hybrid-architecture.svg&quot; alt=&quot;The hybrid architecture: the app target composes vertical feature modules, which sit on horizontal capability layers over a shared core, with sideways dependencies between features forbidden&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;790&quot; height=&quot;460&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;one-rule-holds-it-together&quot;&gt;One rule holds it together&lt;/h2&gt;
&lt;p&gt;Features depend on capabilities. Features never depend on each other. Capabilities never depend on features.&lt;/p&gt;
&lt;p&gt;That’s the whole thing. In &lt;code&gt;Package.swift&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;targets&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;    // Capabilities: horizontal, shared&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    .&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;target&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;CoreModels&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    .&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;target&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;Networking&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;,   &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;dependencies&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;CoreModels&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    .&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;target&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;Persistence&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;,  &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;dependencies&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;CoreModels&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    .&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;target&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;DesignSystem&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;    // Features: vertical, isolated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    .&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;target&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;Checkout&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;dependencies&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;Networking&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;Persistence&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;DesignSystem&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    .&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;target&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;Profile&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;,  &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;dependencies&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;Networking&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;DesignSystem&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    .&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;target&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;Orders&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;,   &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;dependencies&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;Networking&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;Persistence&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;DesignSystem&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now &lt;code&gt;import Profile&lt;/code&gt; inside Checkout is a compile error. I like that a lot more than a written convention, because conventions lose arguments with deadlines. The compiler doesn’t.&lt;/p&gt;
&lt;h2 id=&quot;but-checkout-needs-to-open-profile&quot;&gt;”But Checkout needs to open Profile”&lt;/h2&gt;
&lt;p&gt;No, it doesn’t. It needs to &lt;em&gt;say&lt;/em&gt; that the user wants to edit their profile. Actually opening the profile screen is someone else’s job.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;// In the Checkout feature, with no import of Profile anywhere&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; enum&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt; CheckoutExit&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt; finished&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(orderID: OrderID)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt; editProfileRequested&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt; CheckoutView&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt;View &lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; let&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; onExit: (CheckoutExit) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;-&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt; Void&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;    // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;// In the app target, the only place that knows every feature&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;CheckoutView&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; { exit &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    switch&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; exit {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;finished&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; orderID)&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    router.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;show&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;OrderConfirmationView&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;orderID&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: orderID))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; .editProfileRequested&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;     router.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;show&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;ProfileView&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://tudorturcanu.ch/images/blog/exit-intents.svg&quot; alt=&quot;Checkout emits an exit intent up to the app target, which routes to Profile. There is never a direct import between the two features&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;760&quot; height=&quot;360&quot;&gt;&lt;/p&gt;
&lt;p&gt;The app target is allowed to import everything because composing things is its whole job. It stays small, handling routing, wiring, and dependency injection. Everything interesting lives in modules you can build and test on their own.&lt;/p&gt;
&lt;p&gt;When two features need the same logic, say Checkout and Orders both format prices, that’s not a reason to link them. Price formatting is telling you it’s actually a capability. Move it down a layer. Shared code moves down, never sideways.&lt;/p&gt;
&lt;h2 id=&quot;things-i-learned-the-hard-way&quot;&gt;Things I learned the hard way&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Don’t start with forty modules.&lt;/strong&gt; I’ve watched a project spend two weeks setting up a module graph before writing a single screen. Start with &lt;code&gt;Core&lt;/code&gt;, &lt;code&gt;DesignSystem&lt;/code&gt;, and two or three features. Split a module when it starts hurting, not because a diagram said so.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Keep product knowledge out of capabilities.&lt;/strong&gt; The day &lt;code&gt;Networking&lt;/code&gt; learns what a “checkout session” is, it stops being reusable. Capabilities deal in generic requests and generic storage; features give them meaning.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Test hardest at the bottom.&lt;/strong&gt; A bug in a capability breaks every feature, so that’s where the thorough unit tests go. Features get quicker tests against mocked capabilities. If your capability interfaces are protocols, this costs almost nothing.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;It works solo, too.&lt;/strong&gt; This isn’t just a big-team thing. Most of my own apps share the same &lt;code&gt;Networking&lt;/code&gt;, &lt;code&gt;Persistence&lt;/code&gt;, and &lt;code&gt;DesignSystem&lt;/code&gt; packages. A new app is mostly one new feature slice on top of code I already trust. That is the only reason shipping twenty-something apps as one person was possible at all.&lt;/p&gt;
&lt;h2 id=&quot;if-you-remember-one-thing&quot;&gt;If you remember one thing&lt;/h2&gt;
&lt;p&gt;Stop asking “layers or features?” and ask “which kind of code is this?” instead. Product code goes in a vertical slice. Platform code goes in a horizontal layer. Features never touch each other.&lt;/p&gt;
&lt;p&gt;That’s the whole trick.&lt;/p&gt;</content:encoded><category>architecture</category><category>swift</category><category>spm</category><category>modularization</category><author>tudor.turcanu@gmail.com (Tudor Turcanu)</author></item><item><title>Streaming LLM Responses with Swift Concurrency</title><link>https://tudorturcanu.ch/blog/streaming-llm-swift-concurrency/</link><guid isPermaLink="true">https://tudorturcanu.ch/blog/streaming-llm-swift-concurrency/</guid><description>Token streams are a natural fit for AsyncSequence, until cancellation, actor isolation, and UI update frequency get involved. Here&apos;s the shape that works.</description><pubDate>Wed, 08 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A chat UI that waits for the full LLM response feels broken, even when it’s fast. Streaming tokens as they’re generated is table stakes now. The good news: token streams and Swift Concurrency fit together almost suspiciously well. The bad news: the naive version has three bugs you won’t notice until users do.&lt;/p&gt;
&lt;h2 id=&quot;the-stream-itself&quot;&gt;The stream itself&lt;/h2&gt;
&lt;p&gt;An LLM generating tokens is an &lt;code&gt;AsyncSequence&lt;/code&gt; that produces strings and can fail. &lt;code&gt;AsyncThrowingStream&lt;/code&gt; is the bridge between the inference engine’s callback world and async/await:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt; generate&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt;prompt&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;-&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; AsyncThrowingStream&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;Error&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;    AsyncThrowingStream&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; { continuation &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;        let&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; task &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; engine.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;startGeneration&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;prompt&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: prompt) { event &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;            switch&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; event {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;            case&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;token&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; text)&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  continuation.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;yield&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(text)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;            case&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; .finished&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;         continuation.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;finish&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;            case&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;failed&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; err)&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  continuation.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;finish&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;throwing&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: err)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        continuation.onTermination &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; in&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;            task.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;cancel&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;()   &lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;// &amp;#x3C;- the line everyone forgets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;onTermination&lt;/code&gt; handler is the first bug in most implementations. Without it, the stream and the generation have different lifetimes: the user leaves the screen, the &lt;code&gt;for await&lt;/code&gt; loop ends, and the model keeps generating tokens into the void, burning battery and, on-device, holding megabytes of KV cache for a conversation nobody is reading.&lt;/p&gt;
&lt;h2 id=&quot;consuming-it&quot;&gt;Consuming it&lt;/h2&gt;
&lt;p&gt;On the view model side, the loop is almost embarrassingly simple:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;@MainActor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;final&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt; ChatViewModel&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt;ObservableObject &lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    @Published&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; reply &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt; &quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; generation: Task&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;Void&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;Never&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    func&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt; send&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; prompt: &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        generation&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;cancel&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;()   &lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;// one generation at a time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        generation &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt; Task&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;            do&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;                for&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; try&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; token &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; llm.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;generate&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;prompt&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: prompt) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;                    reply &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;            } &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;catch&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; is&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt; CancellationError&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;                // user interrupted: keep the partial reply&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;            } &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;catch&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;                reply &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt;⚠️ Generation failed.&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    func&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt; stop&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;() { generation&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;cancel&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;() }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cancelling the &lt;code&gt;Task&lt;/code&gt; ends the &lt;code&gt;for await&lt;/code&gt; loop, which terminates the stream, which, thanks to &lt;code&gt;onTermination&lt;/code&gt;, actually stops the engine. One cancellation path, top to bottom. When someone reports “the stop button doesn’t stop it,” it’s because one link in that chain is missing.&lt;/p&gt;
&lt;p&gt;Note the &lt;code&gt;CancellationError&lt;/code&gt; case: interruption is a &lt;em&gt;feature&lt;/em&gt; in a chat app, not an error. The user tapped stop because the answer was already good enough, or already wrong. Keep the partial text.&lt;/p&gt;
&lt;h2 id=&quot;the-bug-youll-find-in-instruments&quot;&gt;The bug you’ll find in Instruments&lt;/h2&gt;
&lt;p&gt;Here’s the subtle one. A model can produce a great many tokens per second, and &lt;code&gt;reply += token&lt;/code&gt; publishes a SwiftUI update for every one of them. Each update re-renders a growing &lt;code&gt;Text&lt;/code&gt;, re-layouts the bubble, re-measures the scroll view. Early in a response nobody notices. Two thousand tokens into a long answer, the main thread is spending its entire budget on layout and the scroll starts hitching.&lt;/p&gt;
&lt;p&gt;The fix isn’t clever, it’s a buffer: accumulate tokens and flush to the UI on a fixed cadence:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; buffer &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt; &quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; lastFlush &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; ContinuousClock.now&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; try&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; token &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; llm.&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;generate&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;prompt&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: prompt) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    buffer &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; ContinuousClock.now &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; lastFlush &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;milliseconds&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        reply &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; buffer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        buffer &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#9ECBFF&quot;&gt; &quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        lastFlush &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; .now&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;reply &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; buffer   &lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;// whatever&apos;s left when the stream ends&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Twenty UI updates a second is indistinguishable from per-token updates to a human eye, and it’s the difference between a smooth stream and a slideshow. This one change matters more than anything else in this post.&lt;/p&gt;
&lt;h2 id=&quot;keep-the-engine-behind-an-actor&quot;&gt;Keep the engine behind an actor&lt;/h2&gt;
&lt;p&gt;On-device inference is stateful: a loaded model, a KV cache, sometimes a single non-reentrant C API. Two overlapping generations will corrupt state or crash. An actor makes the exclusivity structural instead of disciplinary:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;actor&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt; InferenceEngine&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; model: Model&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;    func&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt; generate&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#B392F0&quot;&gt;prompt&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F97583&quot;&gt;-&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt; AsyncThrowingStream&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#79B8FF&quot;&gt;Error&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#6A737D&quot;&gt;        // callers queue here automatically&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Same argument as making illegal imports a compile error: rules enforced by the language don’t need code review to survive.&lt;/p&gt;
&lt;h2 id=&quot;test-with-a-fake-stream&quot;&gt;Test with a fake stream&lt;/h2&gt;
&lt;p&gt;The last win: everything above depends on &lt;code&gt;AsyncThrowingStream&amp;#x3C;String, Error&gt;&lt;/code&gt;, not on a real model. So tests inject a fake that yields scripted tokens with scripted delays, including a stream that fails halfway, and one that gets cancelled mid-word. Those two scenarios are exactly the ones you can’t reliably reproduce with a live model, and exactly the ones users hit daily.&lt;/p&gt;
&lt;p&gt;The streaming layer is the part of an AI app you can get completely right before the model ever loads. Worth doing, because it’s also the part users actually touch.&lt;/p&gt;</content:encoded><category>swift</category><category>concurrency</category><category>ai</category><category>on-device</category><author>tudor.turcanu@gmail.com (Tudor Turcanu)</author></item></channel></rss>