About This E-Book
EPUB is an open, industry-standard format for e-books. However, support for EPUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the e-book in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
iOS and macOS Performance Tuning
Cocoa, Cocoa Touch, Objective-C, and Swift
Marcel Weiher
Boston Columbus Indianapolis New York San Francisco Amsterdam Cape Town
Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City
So Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact
Library of Congress Number: 2016961010
Copyright 2017 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/.
ISBN-13: 978-0-321-84284-8
ISBN-10: 0-321-84284-7
1 17
Editor-in-Chief
Greg Wiegand
Senior Acquisitions Editor
Trina MacDonald
Development Editor
Songlin Qiu
Managing Editor
Sandra Schroeder
Full-Service Production
Manager
Julie B. Nahil
Project Manager
Melissa Panagos
Copy Editor
Stephanie Geels
Indexer
Jack Lewis
Proofreader
Melissa Panagos
Technical Reviewers
Christian Brunschen
BJ Miller
Christian Neuss
Dominik Wagner
Editorial Assistant
Olivia Basegio
Cover Designer
Chuti Prasertsith
Compositor
Lori Hughes
About the Author
Marcel Weiher is a software engineer and researcher with more than 25 years of experience with Cocoa-related technologies. Marcels work has always been performance-focused, ranging from solving impossible pre-press problems on the machines of the day via optimizing one of the worlds busiest Web properties at the BBC to helping other Apple engineers improve the performance of their code on Apples Mac OS X performance team.
In addition to helping established companies and start-ups create award-winning software and turn around development teams, Marcel also teaches, blogs, speaks at conferences, contributes to open source, and invents new techniques such as Higher Order Messaging. He also works on programming languages, starting with an Objective-C implementation in 1987 and culminating in the Objective-Smalltalk architecture research language. Marcel currently works as a principal software engineer at Microsoft Berlin and maintains his own software and consulting company, metaobject ltd.
Introduction
Performance is one of the most important qualities of software programs. You cant have world-beating software without world-beating performance. For a long time, hardware improvements meant that worrying about software performance seemed a waste of time, but with Moores Law no longer automatically providing significant automatic performance improvements, performance optimization is coming back to the forefront of both computer science and engineering.
In addition, performance for end users seems to have gotten only marginally better, whereas the performance of the underlying hardware has improved by many orders of magnitude. Bill Gates quipped that the speed of software halves every 18 months, whereas Wirths law in A Plea for Lean Software states, Software is getting slower more rapidly than hardware becomes faster.
We are so used to this sorry state of affairs that industry veterans were surprised at the original iPads fluid UI, despite having a CPU with only 1 GHz. Thats more than 1,000 times faster than my Apple ][, and 40 times faster than my NeXT cube that had a larger screen to deal with. If anything, the surprise should have been that it wasnt faster, especially when considering that it also had a GPU to handle the screen.
This book will try to give insights into the underlying reasons for these developments in the context of Objective-C, Cocoa, and CocoaTouch, and attempt to provide techniques for taking full advantage of the raw power of our amazing computing machinespower that we tend to squander with reckless abandon. It will also try to show when it is actually OK to squander that power, and when it is necessary to pay careful attention. Programmer attention is also a scarce resource, too often squandered attempting to optimize parts of the program that do not matter.
General themes will include latency versus bandwidth, and transactions costs (overhead) versus actual work done, themes that are universal and manifest themselves in different forms at every level of the hardware and software stack.
What you will notice is that due to the speed of our machines, any single operation is, in fact, always more than fast enough, so the crucial equation is itemscost. Most optimization is about reducing one or both of the parts of that formula, usually by breaking it up first.
One frequent method for reducing cost is to realize that cost is actually composed of two separate costs, cost1 and cost2, and only one of these needs to be applied to all items: items (cost1 + cost