title: Swift's Dictionary is slow even with -Ofast
body: <p>I'm implementing what is essentially a cache using a <code>Dictionary</code> in Swift. The performance is well short of what I would expect. I've read some of the other questions, for example <a href="https://stackoverflow.com/questions/24101718/swift-performance-sorting-arrays">this one about array sorting</a> that seem to suggest that <code>-Ofast</code> is the answer (if you're prepared to accept the changes it brings with it). However, even when compiled <code>-Ofast</code>, performance compares poorly to other languages. I'm using Swift version 1.0 (swift-600.0.34.4.8).</p>
  
  <p>The following is a boiled-down example which illustrates the problem:</p>
  
  <pre><code>import Foundation
  
  class Holder {
      var dictionary = Dictionary&lt;Int, Int&gt;()
  
      func store(#key: Int, value: Int) {
          dictionary[key] = value
      }
  }
  
  let holder = Holder()
  
  let items = 5000
  
  for (var i: Int = 0; i &lt; 5000; i++) {
      holder.store(key: i, value: i)
  }
  </code></pre>
  
  <p>Compiled with <code>-O3</code> it takes more than two seconds to run:</p>
  
  <pre class="lang-sh prettyprint-override"><code>xcrun swift -sdk $(xcrun --show-sdk-path --sdk macosx) -O3 Test.swift &amp;&amp; time ./Test
  
  real    0m2.295s
  user    0m2.176s
  sys     0m0.117s
  </code></pre>
  
  <p>Compiling with <code>-Ofast</code> yields a 3-4x improvement:</p>
  
  <pre class="lang-sh prettyprint-override"><code>xcrun swift -sdk $(xcrun --show-sdk-path --sdk macosx) -Ofast Test.swift &amp;&amp; time ./Test
  
  real    0m0.602s
  user    0m0.484s
  sys     0m0.117s
  </code></pre>
  
  <p>By comparison, this Java implementation:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.util.Map;
  import java.util.HashMap;
  
  public class Test {
      public static void main(String[] args) {
          Holder holder = new Holder();
          int items = 5000;
          for (int i = 0; i &lt; items; i++) {
              holder.store(i, i);
          }
      }
  }
  
  class Holder {
      private final Map&lt;Integer, Integer&gt; map = new HashMap&lt;Integer, Integer&gt;();
  
      public void store(Integer key, Integer value) {
          map.put(key, value);
      }
  }
  </code></pre>
  
  <p>is ~6x faster again:</p>
  
  <pre class="lang-sh prettyprint-override"><code>javac Test.java &amp;&amp; time java Test
  
  real    0m0.096s
  user    0m0.088s
  sys     0m0.021s
  </code></pre>
  
  <p>Is it simply the cost of copying the <code>Dictionary</code> as it's mutated and stored in the <code>Holder</code> instance that's causing Swift to fare so badly? Removing <code>Holder</code> and accessing the <code>Dictionary</code> directly would suggest that it is.</p>
  
  <p>This code:</p>
  
  <pre><code>import Foundation
  
  var dictionary = Dictionary&lt;Int, Int&gt;()
  
  let items = 5000
  
  for (var i: Int = 0; i &lt; 5000; i++) {
      dictionary[i] = i
  }
  </code></pre>
  
  <p>is <em>significantly</em> faster:</p>
  
  <pre><code>$ xcrun swift -sdk $(xcrun --show-sdk-path --sdk macosx) -O3 NoHolder.swift &amp;&amp; time ./NoHolder
  
  real    0m0.011s
  user    0m0.009s
  sys     0m0.002s
  
  $ xcrun swift -sdk $(xcrun --show-sdk-path --sdk macosx) -Ofast NoHolder.swift &amp;&amp; time ./NoHolder
  
  real    0m0.011s
  user    0m0.007s
  sys     0m0.003s
  </code></pre>
  
  <p>While it provides a (hopefully) interesting data point, accessing the Dictionary directly isn't possible in my situation. Is there anything else I can do to get closer to this level of performance with Swift in its current form?</p>