Slides, video and links related to my Swift Summit talk on Swift performance. Video should be available later is now available and it might be worth watching Airspeed's talk that preceded mine before watching it when it is available I'm also expecting a blog post from him shortly on his static vs. compile time talk which is also highly relevant to optimisation.
The key point in Swift is that as the compiler gets better there is no need to choose between nice code and fast code. With a little thought and knowledge it is usually possible to get close to the speed of lowish level C code with nice abstractions. Some may say that C++ is there already but I enjoy writing Swift code much more and C++ is hampered by it's C legacy and choices made many years ago (for good reasons) from becoming a truly nice language in my view (non-Nullable by default would be hard to retrofit for example).
There were also many other excellent talks at Swift Summit, and all were at least good. I'm looking forward to watching several again.
Context for Slides
First I want to add a little note for those who were there. I'm a little bit afraid that I may have been a little too positive about Swift's performance. With a little work you can usually get close to C/C++ in my experience (although as I noted there are significant exceptions) but to reach this level some work may be required rather than just taking first approach even if you are using structs. There are also things which further work from the Swift time will help.
Secondly I plan to post a proper blog post with lots of information about performance and what I know about the Swift optimiser within the next couple of weeks. I recognise that this presentation gives only some headlines and not the context of the talk, be especially with the list of optimisation approaches for which various details apply. Feel free to get in touch with particular questions, read one of the linked stories for details. The talk video will also be posted by Swift Summit soon I think.
Optimisation Levels Video (80MB)
Additional Notes
This sort of optimisation is a side project for me. I haven't needed it in real production apps. I've been looking at interesting cases where Swift has performed poorly either in real world situations or benchmark comparisions and trying to understand why and what can be done. In most cases I have been able to speed up the Swift significantly and at least put it in contention with C/C++ for comparisions especially with the Xcode 6.3 betas (and Beta2 automatically applies some of the optimisations in many cases).
Links
- My initial reaction to Xcode 6.3 Beta 2 performance. I have learnt more since then which will be in my next blog post.
- Gray Scott Cellular Automata as used in the video. This branch isn't the most optimised. I went back to an early modestly optimised version and updated for Swift 1.2.
- I've blogged several times and spoken about it in the past. Please note that things have moved on in the latest Xcode 6.3 betas and the UnsafeBuffer optimisations no longer seem necessary and final can often be applied automatically.
- Primate Labs' Geekbench performance comparison could do with a Beta2/3 and for interest a version of the C++ test without fast-math enabled just to see how much of an impact that is having on GEMM.
- David Owen's render test. This test is right up C's street in my view and the C code is quite highly optimised. Note also that -Os compilation is about three and a half times slower than -Ofast C compilation although still twice as fasts as Swift at the moment. There is definitely room for the Swift compiler to improve here.
- Not mentioned in the talk but there is a big language shootout with Swift second to C++ at the moment but I expect the order to change over time (it has changed twice this week). I almost doubled the Swift result with about 10minutes work a couple of nights ago before C++ was added and I suspect there is more performance on the table both in Swift and the other languages.
- A slightly outdated post about final. The key update is that in many cases the Swift compiler can infer finality since Xcode 6.3 Beta 2.