<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Unformed Delta - optimization</title>
    <description>A place to collect the things I learn, figure out, or find interesting.
</description>
    <link>https://unformeddelta.wiki/</link>
    <atom:link href="https://unformeddelta.wiki/feed/optimization.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sat, 09 May 2026 14:44:19 +0000</pubDate>
    <lastBuildDate>Sat, 09 May 2026 14:44:19 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
    <item>
      <title>Swift computed properties returning a constant aren&apos;t slower than stored constants</title>
      <description>&lt;p&gt;&lt;a href=&quot;https://iosdev.space/@alpennec/115729444379908949&quot;&gt;Axel Le Pennec on Mastodon&lt;/a&gt;, noticed that using stored properties cause more recompilation when using Xcode Previews. In our discussion about this toot at work, a colleague suggested that while it’s likely that incremental builds are faster for computed properties, this might come with a performance hit.&lt;/p&gt;

&lt;p&gt;I had a hunch that the compiler should almost certainly optimize away any extra costs associated with a computed properties that returns a constant.&lt;/p&gt;

&lt;p&gt;To make a fairer comparison for cross-module compilation situations (like in a large real world app), consider the assembly generated from &lt;a href=&quot;https://godbolt.org/z/evoqo5njz&quot;&gt;just this snippet&lt;/a&gt; (no main function, to simulate exposing this from a Framework):&lt;/p&gt;
&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Constants&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;computedProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;storedProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Compiling with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-Osize&lt;/code&gt; (optimize for executable size), we get the following relevant assembly:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-arm&quot;&gt;static output.Constants.computedProperty.getter : Swift.Int:
        mov     w0, #3
        ret

output.Constants.storedProperty.unsafeMutableAddressor : Swift.Int:
        adrp    x0, (static output.Constants.storedProperty : Swift.Int)
        add     x0, x0, :lo12:(static output.Constants.storedProperty : Swift.Int)
        ret

static output.Constants.storedProperty.getter : Swift.Int:
        mov     w0, #3
        ret
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The computed property generates exactly the same code for accessing the property in both cases: just a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov&lt;/code&gt; instruction. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;storedProperty&lt;/code&gt; also has an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unsafeMutableAddressor&lt;/code&gt; that is generated. I’m not exactly sure what it does, but a colleague suggested that it is used for Swift’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;borrow&lt;/code&gt; machinery, see e.g. &lt;a href=&quot;https://forums.swift.org/t/unsafeaddress-and-unsafemutableaddress/79804/3&quot;&gt;this thread&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Mon, 05 Jan 2026 20:19:00 +0000</pubDate>
      <link>https://unformeddelta.wiki/m1hm9H9NxeLD/swift-computed-properties-returning-a-constant-arent-slower-than-stored-constants</link>
      <guid isPermaLink="true">https://unformeddelta.wiki/m1hm9H9NxeLD/swift-computed-properties-returning-a-constant-arent-slower-than-stored-constants</guid>
      
      <category>swift</category>
      
      <category>optimization</category>
      
      <category>recommended</category>
      
    </item>
    
  </channel>
</rss>
