I can’t quite remember how I stumbled across this book, “iOS and macOS Performance Tuning”, a couple of weeks ago (Twitter, basically). But regardless, it is awesome. Written by Marcel Weiher, it has a couple of simple premises, well explained, that really provides a great roadmap for thinking about performance in iOS.
I will actually write up a couple of blogposts out of this book, because I like it so much and want the lessons to take root. This post will focus on something that I had never really explored carefully, which is working with
NSData rather than just initializing Foundation objects with
NSData and working with the resulting objects. The two places where this is a natural thing to do is with strings and with images.
I am going to do a very basic demonstration of his principles at work in reading and parsing a large text file, complete with benchmarks!
One of the primary lessons from this book is that regardless of the continuing speedup of devices and storage, the cost of using objects and their associated methods to process data does add up.1 If you are doing relatively complex transformations on a small number of objects, then using the most abstracted models and methods completely makes sense. But, for operations on large numbers of objects, there are opportunities to achieve profound speedup.
A second lesson is that for most operations and code, Objective-C still is faster than the equivalent Swift code. This is an important point that can get lost. Swift is incredibly powerful, and absolutely lends itself to modern programming practices that can lead to a better, safer, more stable codebase. But when it comes to raw performance, it is still difficult to optimize Swift as thoroughly as Objective-C