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 manufacturer’s 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 São 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 [email protected] or (800) 382-3419. For government sales inquiries, please contact [email protected]. For questions about sales outside the U.S., please contact [email protected]. Visit us on the Web: informit.com/aw 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 Contents at a Glance Contents About the Author Introduction 1 CPU: Principles 2 CPU: Measurement and Tools 3 CPU: Pitfalls and Techniques 4 CPU Example: XML Parsing 5 Memory: Principles 6 Memory: Measurement and Tools 7 Memory: Pitfalls and Techniques 8 Memory Example: FilterStreams 9 Swift 10 I/O: Principles 11 I/O: Measurement and Tools 12 I/O: Pitfalls and Techniques 13 I/O: Examples 14 Graphics and UI: Principles 15 Graphics and UI: Measurement and Tools 16 Graphics and UI: Pitfalls and Techniques 17 Graphics and UI: Examples Index Contents About the Author Introduction 1 CPU: Principles A Simple Example The Perils of (Micro-)Benchmarking More Integer Summing Swift Other Languages The Power of Hybrids Trends Cost of Operations Computational Complexity Summary 2 CPU: Measurement and Tools Command-Line Tools top time sample Xcode Gauges Instruments Setup and Data Gathering Profiling Options Basic Analysis Source Code Data Mining I: Focus Data Mining II: Pruning Internal Measurement Testing Dtrace Optimization Beyond the Call of Duty Summary 3 CPU: Pitfalls and Techniques Representation Primitive Types Strings Objects Accessors Public Access Object Creation and Caching Mutability and Caching Lazy Evaluation Caching Caveats Pitfall: Generic (Intermediate) Representations Arrays and Bulk Processing Dictionaries Messaging IMP Caching Forwarding Uniformity and Optimization Methods Pitfall: CoreFoundation Multicore Threads Work Queues Mature Optimization 4 CPU Example: XML Parsing An HTML Scanner Mapping Callbacks to Messages Objects Objects, Cheaply Evaluation Tune-Ups Optimizing the Whole Widget: MAX MAX Implementation Summary 5 Memory: Principles The Memory Hierarchy Mach Virtual Memory Heap and Stack Stack Allocation Heap Allocation with malloc() Resource Management Garbage Collection Foundation Object Ownership Tracing GC Automatic Reference Counting Process-Level Resource Reclamation Summary 6 Memory: Measurement and Tools Xcode Gauges Command-Line Tools top heap leaks and malloc_debug Internal Measurement Memory Instruments Leaks Instrument Allocations VM Tracker Counters/PM Events Summary 7 Memory: Pitfalls and Techniques Reference Counting Avoiding Leaks Foundation Objects vs. Primitives Smaller Structures But What About Y2K? Compression Purgeable Memory Memory and Concurrency Architectural Considerations Temporary Allocations and Object Caching NSCache and libcache Memory-Mapped Files madvise iOS Considerations Optimizing ARC Summary 8 Memory Example: FilterStreams Unix Pipes and Filters Object-Oriented Filters DescriptionStream Eliminating the Infinite Recursion from description Stream Hierarchy Summary 9 Swift Swift Performance: Claims Language Characteristics
Description: