title: Swift Beta performance: sorting arrays
body: <p>I was implementing an algorithm in Swift Beta and noticed that the performance was very poor. After digging deeper I realized that one of the bottlenecks was something as simple as sorting arrays. The relevant part is here:</p>
  
  <pre><code>let n = 1000000
  var x =  [Int](repeating: 0, count: n)
  for i in 0..&lt;n {
      x[i] = random()
  }
  // start clock here
  let y = sort(x)
  // stop clock here
  </code></pre>
  
  <p>In C++, a similar operation takes <strong>0.06s</strong> on my computer.</p>
  
  <p>In Python, it takes <strong>0.6s</strong> (no tricks, just y = sorted(x) for a list of integers).</p>
  
  <p>In Swift it takes <strong>6s</strong> if I compile it with the following command:</p>
  
  <pre><code>xcrun swift -O3 -sdk `xcrun --show-sdk-path --sdk macosx`
  </code></pre>
  
  <p>And it takes as much as <strong>88s</strong> if I compile it with the following command:</p>
  
  <pre><code>xcrun swift -O0 -sdk `xcrun --show-sdk-path --sdk macosx`
  </code></pre>
  
  <p>Timings in Xcode with "Release" vs. "Debug" builds are similar.</p>
  
  <p>What is wrong here? I could understand some performance loss in comparison with C++, but not a 10-fold slowdown in comparison with pure Python.</p>
  
  <hr>
  
  <p><strong>Edit:</strong> weather noticed that changing <code>-O3</code> to <code>-Ofast</code> makes this code run almost as fast as the C++ version! However, <code>-Ofast</code> changes the semantics of the language a lot — in my testing, it <strong>disabled the checks for integer overflows and array indexing overflows</strong>. For example, with <code>-Ofast</code> the following Swift code runs silently without crashing (and prints out some garbage):</p>
  
  <pre><code>let n = 10000000
  print(n*n*n*n*n)
  let x =  [Int](repeating: 10, count: n)
  print(x[n])
  </code></pre>
  
  <p>So <code>-Ofast</code> is not what we want; the whole point of Swift is that we have the safety nets in place. Of course, the safety nets have some impact on the performance, but they should not make the programs 100 times slower. Remember that Java already checks for array bounds, and in typical cases, the slowdown is by a factor much less than 2. And in Clang and GCC we have got <code>-ftrapv</code> for checking (signed) integer overflows, and it is not that slow, either.</p>
  
  <p>Hence the question: how can we get reasonable performance in Swift without losing the safety nets?</p>
  
  <hr>
  
  <p><strong>Edit 2:</strong> I did some more benchmarking, with very simple loops along the lines of</p>
  
  <pre><code>for i in 0..&lt;n {
      x[i] = x[i] ^ 12345678
  }
  </code></pre>
  
  <p>(Here the xor operation is there just so that I can more easily find the relevant loop in the assembly code. I tried to pick an operation that is easy to spot but also "harmless" in the sense that it should not require any checks related to integer overflows.)</p>
  
  <p>Again, there was a huge difference in the performance between <code>-O3</code> and <code>-Ofast</code>. So I had a look at the assembly code:</p>
  
  <ul>
  <li><p>With <code>-Ofast</code> I get pretty much what I would expect. The relevant part is a loop with 5 machine language instructions.</p></li>
  <li><p>With <code>-O3</code> I get something that was beyond my wildest imagination. The inner loop spans 88 lines of assembly code. I did not try to understand all of it, but the most suspicious parts are 13 invocations of "callq _swift_retain" and another 13 invocations of "callq _swift_release". That is, <strong>26 subroutine calls in the inner loop</strong>!</p></li>
  </ul>
  
  <hr>
  
  <p><strong>Edit 3:</strong> In comments, Ferruccio asked for benchmarks that are fair in the sense that they do not rely on built-in functions (e.g. sort). I think the following program is a fairly good example:</p>
  
  <pre><code>let n = 10000
  var x = [Int](repeating: 1, count: n)
  for i in 0..&lt;n {
      for j in 0..&lt;n {
          x[i] = x[j]
      }
  }
  </code></pre>
  
  <p>There is no arithmetic, so we do not need to worry about integer overflows. The only thing that we do is just lots of array references. And the results are here—Swift -O3 loses by a factor almost 500 in comparison with -Ofast:</p>
  
  <ul>
  <li>C++ -O3: <strong>0.05 s</strong></li>
  <li>C++ -O0: 0.4 s</li>
  <li>Java: <strong>0.2 s</strong></li>
  <li>Python with PyPy: 0.5 s</li>
  <li>Python: <strong>12 s</strong></li>
  <li>Swift -Ofast: 0.05 s</li>
  <li>Swift -O3: <strong>23 s</strong></li>
  <li>Swift -O0: 443 s</li>
  </ul>
  
  <p>(If you are concerned that the compiler might optimize out the pointless loops entirely, you can change it to e.g. <code>x[i] ^= x[j]</code>, and add a print statement that outputs <code>x[0]</code>. This does not change anything; the timings will be very similar.)</p>
  
  <p>And yes, here the Python implementation was a stupid pure Python implementation with a list of ints and nested for loops. It should be <strong>much</strong> slower than unoptimized Swift. Something seems to be seriously broken with Swift and array indexing.</p>
  
  <hr>
  
  <p><strong>Edit 4:</strong> These issues (as well as some other performance issues) seems to have been fixed in Xcode 6 beta 5.</p>
  
  <p>For sorting, I now have the following timings:</p>
  
  <ul>
  <li>clang++ -O3: 0.06 s</li>
  <li>swiftc -Ofast: 0.1 s</li>
  <li>swiftc -O: 0.1 s</li>
  <li>swiftc: 4 s</li>
  </ul>
  
  <p>For nested loops:</p>
  
  <ul>
  <li>clang++ -O3: 0.06 s</li>
  <li>swiftc -Ofast: 0.3 s</li>
  <li>swiftc -O: 0.4 s</li>
  <li>swiftc: 540 s</li>
  </ul>
  
  <p>It seems that there is no reason anymore to use the unsafe <code>-Ofast</code> (a.k.a. <code>-Ounchecked</code>); plain <code>-O</code> produces equally good code.</p>