It’s easier than you think with this one weird trick…
Every Swift developer knows that Swift code compile times are best measured in ice ages, and historically a number of candidates have been put forward as possible culprits: slow type inference checking, string interpolation, and a lack of headers.
However, despite the community’s best efforts we’ve only ever been able to get Swift compile times down by 20% or so, and that’s even after removing all the culprits above. That left us wondering: just what is taking so long?
At long last, we can reveal not only what causes such slow Swift compile times, but provide a quick fix that will literally cut your Swift compile times by half if not more.
The original implementation of Swift was designed to be a simple replacement of Objective-C, but shortly after the first public release of Swift a secret front-end flag was inserted.
On the surface this flag was supposed to be there to help make Objective-C tests work better, but the reality was that Apple had taken the initial step towards becoming the world’s first trillion dollar company: Apple had created a fork of Bitcoin called SwiftCoin.
But Apple had a unique approach: they knew developers were deeply envious of language features such as generics, operator overloading, and first-class functions, so they crafted a plan to introduce a new language to desperate developers – a language that would also secretly mine SwiftCoin while pretending to compile.
Responding to the news that SwiftCoin’s existence had leaked, Swift project lead Ed Kermenek issued an official statement from his personal aircraft carrier somewhere in the Pacific: “Hahahaha – and you really thought type inference could be that slow? I thought the plan would fail early when some folks realized compiling was slower than Objective-C, but then Kris [Latener; original creator of Swift] had the idea of making so many language changes every year that folks would be too distracted to investigate.”
It’s hard to tell how many Apple engineers benefited from the scheme, but I managed to track down Jay Gorff – one of the lead engineers on the Swift project. I found him in a mountain fortress in Aspen, Colorado, where he was burning piles of $100 dollar notes on an open fire. “It’s all true,” he said. “We literally have so much money now that I’m out of space – I’m going to have to buy my dogs bigger ski chalets just to have somewhere to put it all.”
Fortunately for all of us, the newly released Xcode 9.3 comes with an option to disable SwiftCoin mining entirely. Doing so immediately halves all Swift compilation time, and will dramatically lower your household energy bills.
To try it for yourself, simply run this command from the terminal app: defaults write org.swift.dt.compiler MineSwiftCoin -bool NO – make sure you restart Xcode afterwards, and you should be done.
If all developers upgrade to Xcode 9.3 and disable SwiftCoin mining, experts have estimated energy savings could reach up to 1.21 gigawatts – energy that could be far better used bikeshedding Swift Evolution proposals on the Swift Forums.
Paul Hudson is the creator of Hacking with Swift, the most comprehensive series of Swift books in the world. He's also the editor of Swift Developer News, the maintainer of the Swift Knowledge Base, and a speaker at Swift events around the world. If you're curious you can learn more here.
Link copied to your pasteboard.