iOS and macOS™ Performance Tuning: Cocoa®, Cocoa Touch®, Objective-C®, and Swift™

Book description

In iOS and macOS™ Performance Tuning, Marcel Weiher drills down to the code level to help you systematically optimize CPU, memory, I/O, graphics, and program responsiveness in any Objective-C, Cocoa, or CocoaTouch program.

This up-to-date guide focuses entirely on performance optimization for macOS and iOS. Drawing on more than 25 years of experience optimizing Apple device software, Weiher identifies concrete performance problems that can be discovered empirically via measurement. Then, based on a deep understanding of fundamental principles, he presents specific techniques for solving them.

Weiher presents insights you won’t find anywhere else, most of them applying to both macOS and iOS development. Throughout, he reveals common pitfalls and misconceptions about Apple device performance, explains the realities, and helps you reflect those realities in code that performs beautifully.

  • Understand optimization principles, measurement, tools, pitfalls, and techniques

  • Recognize when to carefully optimize, and when it isn’t worth your time

  • Balance performance and encapsulation to create efficient object representations, communication, data access, and computation

  • Avoid mistakes that slow down Objective-C programs and hinder later optimization

  • Fix leaks and other problems with memory and resource management

  • Address I/O issues associated with drives, networking, serialization, and SQLite

  • Code graphics and UIs that don’t overwhelm limited iOS device resources

  • Learn what all developers need to know about Swift performance

  • This book’s source code can be downloaded at github.com/mpw/iOS-macOS-performance.

    Register your product at informit.com/register for convenient access to downloads, updates, and corrections as they become available.

    Table of contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Contents at a Glance
    5. Contents
    6. About the Author
    7. Introduction
    8. 1. CPU: Principles
      1. A Simple Example
        1. The Perils of (Micro-)Benchmarking
        2. More Integer Summing
        3. Swift
        4. Other Languages
      2. The Power of Hybrids
      3. Trends
      4. Cost of Operations
      5. Computational Complexity
      6. Summary
    9. 2. CPU: Measurement and Tools
      1. Command-Line Tools
        1. top
        2. time
        3. sample
      2. Xcode Gauges
      3. Instruments
        1. Setup and Data Gathering
        2. Profiling Options
        3. Basic Analysis
        4. Source Code
        5. Data Mining I: Focus
        6. Data Mining II: Pruning
      4. Internal Measurement
        1. Testing
      5. Dtrace
      6. Optimization Beyond the Call of Duty
      7. Summary
    10. 3. CPU: Pitfalls and Techniques
      1. Representation
        1. Primitive Types
        2. Strings
      2. Objects
        1. Accessors
        2. Public Access
        3. Object Creation and Caching
        4. Mutability and Caching
        5. Lazy Evaluation
        6. Caching Caveats
        7. Pitfall: Generic (Intermediate) Representations
        8. Arrays and Bulk Processing
        9. Dictionaries
      3. Messaging
        1. IMP Caching
        2. Forwarding
        3. Uniformity and Optimization
      4. Methods
        1. Pitfall: CoreFoundation
      5. Multicore
        1. Threads
        2. Work Queues
      6. Mature Optimization
    11. 4. CPU Example: XML Parsing
      1. An HTML Scanner
      2. Mapping Callbacks to Messages
      3. Objects
      4. Objects, Cheaply
      5. Evaluation
      6. Tune-Ups
      7. Optimizing the Whole Widget: MAX
      8. MAX Implementation
      9. Summary
    12. 5. Memory: Principles
      1. The Memory Hierarchy
      2. Mach Virtual Memory
      3. Heap and Stack
        1. Stack Allocation
        2. Heap Allocation with malloc()
      4. Resource Management
        1. Garbage Collection
        2. Foundation Object Ownership
        3. Tracing GC
        4. Automatic Reference Counting
        5. Process-Level Resource Reclamation
      5. Summary
    13. 6. Memory: Measurement and Tools
      1. Xcode Gauges
      2. Command-Line Tools
        1. top
        2. heap
        3. leaks and malloc_debug
      3. Internal Measurement
      4. Memory Instruments
        1. Leaks Instrument
        2. Allocations
        3. VM Tracker
        4. Counters/PM Events
      5. Summary
    14. 7. Memory: Pitfalls and Techniques
      1. Reference Counting
        1. Avoiding Leaks
      2. Foundation Objects vs. Primitives
      3. Smaller Structures
        1. But What About Y2K?
        2. Compression
        3. Purgeable Memory
      4. Memory and Concurrency
      5. Architectural Considerations
      6. Temporary Allocations and Object Caching
      7. NSCache and libcache
      8. Memor y-Mapped Files
        1. madvise
      9. iOS Considerations
      10. Optimizing ARC
      11. Summary
    15. 8. Memory Example: FilterStreams
      1. Unix Pipes and Filters
      2. Object-Oriented Filters
      3. DescriptionStream
        1. Eliminating the Infinite Recursion from description
      4. Stream Hierarchy
      5. Summary
    16. 9. Swift
      1. Swift Performance: Claims
        1. language Characteristics
        2. Benchmarks
      2. Assessing Swift Performance
        1. Basic Performance Characteristics
        2. Collections
      3. Larger Examples
        1. nginx HTTP Parser
        2. Freddy JSON Parser
        3. Image Processing
        4. Observations
        5. Compile Times
        6. Type Inference
        7. Generics Specialization
        8. Whole-Module Optimization
        9. Controlling Compile Times
      4. Optimizer-Oriented Programming
      5. A Sufficiently Smart Compiler
        1. The Death of Optimizing Compilers
        2. Practical Advice
        3. Alternatives
      6. Summary
    17. 10. I/O: Principles
      1. Hardware
        1. Disk Drives
        2. Solid-State Disks
        3. Network
      2. Operating System
      3. Abstraction: Byte Streams
      4. File I/O
        1. The Network Stack
      5. Summary
    18. 11. I/O: Measurement and Tools
      1. Negative Space: top and time
      2. Summary Information: iostat and netstat
      3. Instruments
      4. Detailed Tracing: fs_usage
      5. Summary
    19. 12. I/O: Pitfalls and Techniques
      1. Pushing Bytes with NSData
        1. A Memory-Mapping Anomaly
        2. How Chunky?
      2. Unixy I/O
      3. Network I/O
        1. Overlapping Transfers
        2. Throttling Requests
        3. Data Handling
        4. Asynchronous I/O
        5. HTTP Serving
      4. Serialization
        1. Memory Dumps
        2. A Simple XML Format
        3. Property Lists
        4. Archiving
        5. Serialization Summary
      5. CoreData
        1. Create and Update in Batches
        2. Fetch and Fault Techniques
        3. Object Interaction
        4. Subsetting
        5. Analysis
      6. SQLite
        1. Relational and Other Databases
      7. Event Posting
        1. Hybrid Forms
      8. Segregated Stores
      9. Summary
    20. 13. I/O: Examples
      1. iPhone Game Dictionary
      2. Fun with Property Lists
        1. A Binary Property List Reader
        2. Lazy Reading
      3. Avoiding Intermediate Representations
      4. Comma-Separated Values
      5. Public Transport Schedule Data
      6. Stops
        1. Stop Time Lookup
        2. Stop Time Import
      7. Faster CSV Parsing
        1. Object Allocation
        2. Push vs. Pull
      8. Keys of Interest
        1. Parallelization
      9. Summary
    21. 14. Graphics and UI: Principles
      1. Responsiveness
      2. Software and APIs
      3. Quartz and the PostScript Imaging Model
      4. OpenGL
      5. Metal
      6. Graphics Hardware and Acceleration
      7. From Quartz Extreme to Core Animation
      8. Summary
    22. 15. Graphics and UI: Measurement and Tools
      1. CPU Profiling with Instruments
      2. Quartz Debug
      3. Core Animation Instrument
        1. When the CPU Is Not the Problem
      4. What Am I Measuring?
      5. Summary
    23. 16. Graphics and UI: Pitfalls and Techniques
      1. Pitfalls
      2. Techniques
      3. Too Much Communication Slows Down Installation
        1. The Display Throttle
        2. Working with the Display Throttle
        3. Installers and Progress Reporting Today
      4. Overwhelming an iPhone
      5. It’s Just an Illusion
        1. Image Scaling and Cropping
        2. Thumbnail Drawing
        3. How Definitely Not to Draw Thumbnails
        4. How to Not Really Draw Thumbnails
        5. How to Draw Non-Thumbnails
      6. Line Drawing on iPhone
      7. Summary
    24. 17. Graphics and UI: Examples
      1. Beautiful Weather App
        1. An Update
        2. Fun with PNG
        3. Brainstorming
        4. Data Points to JPEG
        5. A Measuring Hiccup
      2. JPNG and JPJP
        1. A Beautiful Launch
      3. Wunderlist 3
        1. Wunderlist 2
        2. Overall Architecture
        3. URIs and In-Process REST
        4. An Eventually Consistent Asynchronous Data Store
        5. RESTOperation Queues
        6. A Smooth and Responsive UI
        7. Wunderlist in Short
      4. Summary
    25. Index
    26. Code Snippets

    Product information

    • Title: iOS and macOS™ Performance Tuning: Cocoa®, Cocoa Touch®, Objective-C®, and Swift™
    • Author(s): Marcel Weiher
    • Release date: February 2017
    • Publisher(s): Addison-Wesley Professional
    • ISBN: 9780133085501