iOS at Facebook - Simon Whitaker
iOS at Facebook - Simon Whitaker
iOS at Facebook - Simon Whitaker
Simon Whitaker
Software Engineer, Facebook London
Thank Chris/Neal
Introduce myself - quick bit about Protect and Care
We prevent bad stuff from happening on Facebook:
- Spam, revenge porn, child exploitative images, etc
And make sure people feel cared for when bad stuff does happen:
- Reporting flows, etc
I work on iOS within Protect and Care, so I find myself spelunking through various Facebook iOS codebases; the
main Facebook app, Messenger, and a handful of others. So today Im going to talk a little bit about what iOS
engineering at Facebook looks like.
http://quellish.tumblr.com/post/126712999812/how-on-earth-the-facebook-ios-application-is-so
But lets start by addressing the elephant in the room. Like, literally, the elephant.
Many of you probably saw this blog post when it did the rounds a couple of weeks ago. It even featured in the
and finally slot in the iOS Dev Weekly newsletter, which is a rare honour indeed.
The blog post was a response to a question on Reddit, asking why the Facebook app binary was so large. So this
person did a class-dump on the binary to find out more.
I was pretty excited when I saw this. Ive wanted to know the answer since I joined Facebook last year, so I read
with great interest!
So I scrolled (and scrolled, and scrolled) to the bottom for the conclusion.
And here it is. [Read slide.]
But it left me wanting more.
xkcd.com/703/
I mean, the app is large because it has lots of classes is something of a tautology. And honestly quite a relief,
considering the alternative
FBMassiveViewController.h
But clearly this isnt the case; Justin Timberlake left Facebook long before we started writing Objective-C.
So if its not down to Justin Timberlake, then why?
Well, it helps to consider Facebooks culture. The first clue is in the fact that Facebook has managed to scale to
1.5 billion people on
Hack. And that also gives you a bit of a clue regarding Facebooks culture. Only a certain type of company would
invent a PHP-like language and call it Hack.
And lest you think this name was just a fun in-joke among a couple of developers - does anyone know the
address of Facebooks headquarters in California?
1 Hacker Way. Im pretty sure it wasnt called that when Sun Microsystems owned the campus. And that campus
Theres an open square in the middle of the campus, and theres a single word written there, visible from low
orbit.
Hack.
OK, the hacker way isnt just some idea that Facebook has been running with for a few weeks - its deeply
ingrained in the way we develop software.
The hacker spirit is exemplified in posters that pop up all around the office like this one
18,000+ classes
All dierent, all special
Duplication?
Old code?
Awesome stu?
287
401
33
Cocoapods
Homebrew
Linux kernel
Facebook iOS
Here are the stats for the week starting 24 August this year.
In that week:
429 unique contributors
2823 total commits
Doesnt include commits to bump version numbers, add translated strings etc - actual number is over 4,000.
So its big. Its an order of magnitude bigger than the same figure for the Linux kernel in the same week, and
bigger again than popular repos like Homebrew and Cocoapods.
It is frankly the biggest, busiest codebase Ive ever seen. And LOTS of things cant handle this kind of scale.
https://code.facebook.com/videos/859635244094281/f8-2015-facebook-on-ios-inside-the-big-blue-app-/
https://code.facebook.com/videos/859635244094281/f8-2015-facebook-on-ios-inside-the-big-blue-app-/
So we wrote (and open-sourced!) ComponentKit, which takes a functional, declarative approach to building UI.
All layout is done on a background thread, making 60fps achievable even with complex UIs on older devices.
And because this is Facebook, we have at least two other UI frameworks offering alternative takes on background
thread compositing.
React Native focuses on developer efficiency across all the platforms you care about learn once, write
anywhere.
For the same reason, we dont make much use of Auto Layout; it has unpredictable layout scheduling and does its
work on the main thread, so its not a good fit for news feed and other parts of the app where 60fps is important.
http://xkcd.com/303/
Nuclide
https://code.facebook.com/posts/397706937084869
We wrote out own IDE. Seriously. Its called Nuclide, its based on Githubs open-source Atom text editor.
And heres an interesting quote from Michael Bolin, the lead engineer on Nuclide. Once again, its all about
hackability.
OK, so:
- Core Data cant handle our scale
- UIKit cant handle our scale
- AutoLayout cant handle our scale
- Xcode cant handle our scale
What else cant handle our scale?
https://code.facebook.com/posts/218678814984400/scaling-mercurial-at-facebook/
https://code.facebook.com/posts/218678814984400/scaling-mercurial-at-facebook/
Mercurial works better for us. Its more hackable, so were more easily able to dig in and make changes that suit
us.
https://facebook.github.io/watchman/
Using watchman, a local file system monitor that we wrote and open-sourced, we improved performance of
operations that need to determine which files in a directory tree have changed.
Things like:
- hg status
- hg diff
- hg update (like git checkout)
- hg commit
https://bitbucket.org/facebook/remotefilelog
And using remotefilelog, a Mercurial extension that weve also open-sourced, we reduced network I/O by only
pulling down metadata for the majority of commits when pulling or cloning, then fetching the file data on
demand.
So thats it; a brief glimpse into just some of the reasons that we might have ended up with 18,000 classes in our
iOS app.
So, we have a lot of unique challenges at Facebook, and we open-source a lot of our solutions. I just want to
highlight a couple of other open-source projects of ours that you might not be aware of.
Infer
http://fbinfer.com/
A tool to detect bugs in Android and iOS apps before they ship. It reports memory leak problems in Objective-C
and C code. We run Infer as part of our continuous integration suite.
pop
https://code.facebook.com/projects/642915749111632/pop/
An extensible animation engine for iOS and OS X. Written to power the animations in Facebooks Paper app.
https://github.com/facebook/chisel
Good eh?
code.facebook.com/ios
- Blog articles
- Videos (including inside the big blue app, which talks in more depth about mem models and component kit)
- Open source stuff
Welcome to Clowntown
So some of you may now have the impression that Facebook is staffed by superhumans, people who arent afraid
to rewrite iOS from the ground up to squeeze that last bit of performance out of the system.
People who never make mistakes.
Let me disabuse you of that notion.
At Facebook, we talk of a mystical land called Clowntown. Many are called but few are chosen. Clowntown is the
place you visit
from 2013
when you write comments like this and someone finds them two years later
Earlier this year I worked on the iOS implementation of a feature called legacy contact. Its where Facebook lets
you nominate a friend or relative to manage your account after your death. Initially I just rushed out a quick hack
to get the basic functionality in place.
Then this summer I had an intern come to work with me for 3 months, so I tasked her with re-writing my hack
using lots of nice new stuff like ComponentKit and make it properly production ready.
As part of her work, she needed to test her code. And to test her code, she needed create a dead test user.
memorializeUser(687406370, 499891768)
person being
memorialised
There is a UI for memorialising users, but I assured her that the pros simply ran a bit of code in the PHP
debugger. Theres a function that takes two parameters: one the ID of the person being memorialised, the other
the ID of the person doing the memorialising. I gave her a demo to show her how easy it was.
And thats when I entered Clowntown.
I first realised something was wrong when I went back to farting around on Facebook and got prompted to login.
Then when I tried to log in, I was told I couldnt log in to my account because it had been memorialised.
My co-workers started laughing, because theyd discovered that my account now showed Remembering Simon
Whitaker.
And then I thought; Oh god. My wifes going to get the email.
I had set my wife as my legacy contact.
memorializeUser(687406370, 499891768)
person being
memorialised
So in case you havent guessed what I got wrong yet, I managed to get the arguments the wrong way round.
Instead of me memorialising my test user, my test user memorialised me.
memorializeUser(687406370, 499891768)
person being
memorialised
Simon Whitaker
Zombie Mayor of Clowntown
Simon Whitaker
www.facebook.com/simonwhitaker
@s1mn
Questions?