You are previewing Swift High Performance.
O'Reilly logo
Swift High Performance

Book Description

Leverage Swift and enhance your code to take your applications to the next level

About This Book

  • Build solid, high performance applications in Swift

  • Increase your efficiency by getting to grips with concurrency and parallel programming

  • Use Swift to design performance-oriented solutions

  • Who This Book Is For

    This book is aimed at experienced Swift developers wanting to optimize their programs on Apple platforms to optimize application performance.

    What You Will Learn

  • Build solid, stable, and reliable applications using Swift

  • Use REPL and Pl to manage and configure relational databases

  • Explore Swift's features including its static type system, value objects, and functional programming Design reusable code for high performance in Swift

  • Use to Xcode LLBD and REPL to debug commands

  • Avoid sharing resources by using concurrency and parallel programming

  • Understand the lazy loading pattern, lazy sequences, and lazy evolution.

  • In Detail

    Swift is one of the most popular and powerful programming languages for building iOS and Mac OS applications, and continues to evolve with new features and capabilities. Swift is considered a replacement to Objective-C and has performance advantages over Objective-C and Python. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible, and more fun.

    Develop Swift and discover best practices that allow you to build solid applications and optimize their performance.

    First, a few of performance characteristics of Swift will be explained. You will implement new tools available in Swift, including Playgrounds and REPL. These will improve your code efficiency, enable you to analyse Swift code, and enhance performance. Next, the importance of building solid applications using multithreading concurrency and multi-core device architecture is covered, before moving on to best practices and techniques that you should utilize when building high performance applications, such as concurrency and lazy-loading. Finally, you will explore the underlying structure of Swift further, and learn how to disassemble and compile Swift code.

    Style and approach

    This is a comprehensive guide to enhancing Swift programming techniques and methodology to enable faster application development.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Swift High Performance
      1. Table of Contents
      2. Swift High Performance
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Exploring Swift's Power and Performance
        1. Swift speed
        2. Welcome to Swift
          1. Why should I learn Swift?
        3. Swift's features and benefits
          1. Clean and beautiful
            1. No semicolons
            2. Type inference
            3. Other clean code Swift features
            4. A clean code summary
          2. Safe
            1. Optionals
            2. Error handling
          3. Rich type system
          4. Powerful value types
          5. A multiparadigm language
            1. Object oriented
            2. Protocol oriented
            3. Functional
            4. Generic purpose
          6. Fast
        4. Swift interoperability
          1. Using Objective-C in Swift
            1. Setup
              1. The application target
              2. The framework target
            2. Calling Objective-C code
          2. Using Swift in Objective-C
          3. Features of Swift that are not available in Objective-C
        5. Performance – meaning and key metrics
          1. Everyday code performance
          2. The importance of performance
          3. The key metrics
        6. Summary
      9. 2. Making a Good Application Architecture in Swift
        1. Making a Swift application
        2. The differences between variables and constants
        3. Immutability
          1. Multithreading
        4. Value types and immutability
          1. Reference types
          2. Value types
            1. The power of structures
            2. Representing the state with classes
        5. Representing the absence of values with optionals
          1. Optional and non-optional types
          2. Safe nil handling
          3. Using optionals
          4. Wrapping up on optionals
        6. Functional programming
          1. Function types
          2. Splitting the code
          3. The closure expression
            1. Type inference
            2. Implicit return type
            3. Shorthand argument names
            4. Trailing closure syntax
          4. The standard library
            1. The map method
              1. map for optionals
            2. The reduce method
            3. The filter method
        7. Generics
          1. Generic functions
          2. Type constraints
          3. The generic type and collections
        8. Safety
          1. Dangerous operations
        9. Summary
      10. 3. Testing and Identifying Slow Code with the Swift Toolkit
        1. REPL
        2. Playgrounds
          1. Interactive documentation
          2. The folder structure
            1. The source folder
            2. Resources
            3. Pages
            4. XCPlayground
        3. LLDB
        4. REPL in Xcode
        5. Console logs
        6. Performance measuring in unit tests
        7. Instruments
        8. A reminder
        9. Summary
      11. 4. Improving Code Performance
        1. Understanding performance optimization
          1. Explicit
          2. Implicit
        2. Optimization checklist
        3. Swift code compilation
        4. Constants and variables
        5. Method calls
          1. Functions and methods
            1. Global functions
            2. Type methods
            3. Static methods
            4. Instance methods
            5. Comparing function speed
          2. Functions and methods usage summary
        6. Intelligent code
          1. Dangerous functions
            1. Console print
            2. Removing print logs
              1. Comment out
              2. Using build configurations
          2. Using nonoptimizable constants
        7. Improving speed
          1. Final
          2. Inline functions
        8. Value objects and reference objects
          1. Memory allocation
        9. Swift arrays and unsafe C arrays
          1. Function parameters
          2. Creating a pointer to an existing variable
          3. Allocating memory for the pointer
          4. Comparing Swift arrays with unsafe C arrays
            1. A summary of working with pointers
        10. Avoiding Objective-C
          1. Avoiding exposing Swift to Objective-C
            1. Dynamic
          2. A summary of avoiding Objective-C
        11. Summary
      12. 5. Choosing the Correct Data Structure
        1. An overview of data structures
        2. Collection types
        3. Swift standard library collections
          1. Arrays
            1. Using arrays
              1. Fast operations
              2. Slower operations
              3. Search
              4. Sort
          2. Sets
            1. Using sets
              1. Set operations
          3. Dictionaries
        4. Collection memory allocation
          1. Empty
          2. Reserve capacity
          3. Default values
        5. The CollectionType protocol methods
          1. Protocol extensions
        6. Accelerate and Surge
        7. Other collections
        8. Summary
      13. 6. Architecting Applications for High Performance
        1. Achieving high performance
          1. Device architecture
        2. Concurrency overview
          1. Threads
            1. Thread complexity
            2. Solution for threads
          2. GCD
            1. Queues
              1. Main queues
              2. Concurrent queues
              3. Own queues
            2. Tasks
            3. Adding tasks to the queue
              1. Synchronous dispatch
              2. Asynchronous dispatch
          3. Operation queues
        3. Designing asynchronous code
        4. Avoiding state
          1. Capture list
          2. Immutable state
        5. Divide and conquer
        6. Controlling the lifetime
          1. Applying the @autoclosure and @noescape attributes
          2. @autoclosure
          3. @noescape
          4. @autoclosure (escaping)
        7. Summary
      14. 7. The Importance of Being Lazy
        1. The lazy mindset
          1. Separation
          2. Do work on demand
          3. Approximate results
        2. Lazy loading
          1. Global constants and variables
          2. Type properties
          3. Lazy properties
          4. Computed properties
        3. Lazy collections and evaluation
          1. Sequences and collections
            1. Sequences
            2. Collections
            3. Implementing our own sequence and collection
            4. Using lazy
            5. Using the lazy sequence
            6. Using a lazy collection
        4. Summary
      15. 8. Discovering All the Underlying Swift Power
        1. How Swift is so fast
        2. Swift command line tools
          1. xcrun
        3. The Swift compiler
          1. swift
          2. swiftc
        4. The Swift compilation process and swiftc
          1. Swift AST
          2. SIL
          3. LLVM IR
          4. Other swiftc options
        5. Analyzing executable files
        6. Summary
        7. Final thoughts
      16. Index