How Facebook made News Feed 50 percent faster on iOS

shutterstock_188792909

Have you noticed a faster experience, iOS users? Facebook’s engineering team explained how switching from HTML5 to native iOS code improved the speed of the News Feed.

Facebook also rewrote the code for timeline, groups, pages, search and more, but noticed that News Feed got slower with each release.

Facebook’s Adam Ernst, in a detailed blog post, explained that the problem was in the data model layer:

First, let’s talk about how News Feed was designed to work on iOS. The Facebook APIs we use serve as a JSON representation of the stories in your News Feed. Because we didn’t want UIViews to consume JSON directly — there are no type safety or hints about what fields you can expect to get from the server — we create intermediate data models from JSON and used those to power the user interface. Like most iOS apps, we chose to use the system default framework for managing data models: Core Data. Already built into iOS and very well documented, it allowed us to get the native rewrite out the door without reinventing the wheel.

Returning to our performance problems, though, we found that Core Data had a quirk. As we ported more features, our Core Data database slowed down. We started with only a few dozen entities in Core Data, but this had ballooned to hundreds. Some of those entities had a lot of fields — Pages, for example, had more than 100!

Under the hood, Apple’s Core Data framework uses SQLite to store data. As we added more entities and more fields, the number of tables, columns, and indexes grew. Core Data stores data in a fully normalized format, so each time we encountered a Pages object in JSON, we would have to perform a fetch-or-create in Core Data and then update the page. Saving would touch dozens of indexes in SQLite, thanks to an enormous number of relationships (i.e., how many things reference people or Pages objects on Facebook).

Readers: What else do you want to see in the iOS app?

Image courtesy of Neirfy / Shutterstock.com.