Swift and Compiler Magic

swift

No matter what area you work in, I’ve always felt it is important to have a diverse knowledge of your domain.  So though I am primarily an Android Developer, I like to stay up to date on whats going on in iOS land.  I’ve done some iOS development for my current company, and enjoy Android / iOS shop talk with the other devs.  It has all helped me be a much better developer!

So, naturally, I was live-streaming the WWDC keynote yesterday.  And, naturally, my jaw HIT THE FLOOR when Swift was announced!  I keep re-watching the first part of this clip; the mix of excitement and terror in the developers’ shouts is just hilarious.  This is such a huge game changer.*  And from what I’ve been hearing from devs on Twitter, Apple’s claims about Swift’s efficiency are really true.  I don’t know how they did it, but it is pretty amazing that they did.

Back in undergrad, I took a crazy fun class called “Computers as Components.”  During this course we wrote primarily in assembly, and talked a lot about compiler optimization.  Once we’d gotten the hang of what the heck was going on in assembly, we had some tasks where we wrote a program in assembly, then wrote the same program in C.  Then, we took at the assembly generated by the compiler from our C code.  It was cool to see how the compiler optimized certain aspects of the C code, and how in certain cases, the compiler’s optimization algorithms were actually hurting more than helping, and our handwritten assembly was superior!

Our professor told us that a smart human will always be able to optimize assembly better than a compiler.  The problem is that in the end, it might not be worth it; it is way more efficient for the compiler to miss some optimizations then to have a human write everything in assembly.  Not only is assembly PAINSTAKING to write, it is highly prone to errors.  Most embedded firmware engineers will write primarily in C, and then do some ‘inline assembly’ when they are really relying on the exactness that assembly provides.

But…. what if humans could write in super high level languages, yet have the exactness and awesomeness of writing in assembly?  That is what compiler optimization research is all about.

You see, the higher level a language is (ie, easier for a human to read), the further away you are getting from assembly (an ultimately, from machine code…. 1’s and 0’s).  Meaning, you are carrying around a LOT more baggage than you need, and your program is running way slower than it needs to.

For example, consider what happens for a new method call in a basic processor. For each new method call, you have a jump to a new location in memory.  With every jump you’ve probably got to save off some current variables in your processor’s registers.  If you need to save off more variables than you have registers, you’ve got to copy this overflow information into another spot in memory and make one of your registers point to the new location in memory.  Then when you return from the method, you’ve got restore all the values you just shuffled around.  Now think of all the memory swapping and reads and writes that are going to happen if you call (and return from) a method in a for-loop a few hundred times!

When you start thinking about what the processor is actually doing, programming can really start to blow your mind!  “It does all that for me?!  In how many milliseconds!?”

Android and iOS devices have some pretty beefy processors, but higher level (scripting) languages still bring too much baggage along for the ride.  The thing that is so crazy about Swift is that it FEELS like a scripting language when you are writing it.  But, Swift is still a compiled language!  The compiler is doing the heavy lifting here and optimizing this code like crazy, so that the program runs efficiently (and apparently, MORE effieciently than Objective C… what!?).

This is a really exciting move, and I am still giddy about it.  The compiler optimization that must have had to happen for Swift to be possible is truly some phenomenal engineering.  Chris Lattner’s homepage has been updated since yesterday’s announcement, and specifically gives kudos to the “compiler optimization ninjas” who worked on the project.   I would love to meet one of them some day! Wow!

With Swift, Apple has opened up the developing market to a whole new group of developers that didn’t want to deal with Objective C.  I can’t wait to see how this plays out!

* And to think…. all the media outlets were obsessed with Apple’s lack of an iWatch release yesterday, with Swift as just a minor bullet point in their WWDC keynote re-caps.  Ha!  Looks like they are starting to realize today, that Swift is an enormous announcement!

This entry was posted in Compilers, iOS, Low Level. Bookmark the permalink.

1 Response to Swift and Compiler Magic

  1. 芒果乾 says:

    Amazing website, thank You !!

Leave a Reply

Your email address will not be published. Required fields are marked *