Optimizing Swift Performance Session 409

Similar documents
Implementing UI Designs in Interface Builder

Improving your Existing Apps with Swift

Advanced Debugging and the Address Sanitizer

What s New in LLDB. Debug your way to fame and glory #WWDC15. Developer Tools. Session 402

Understanding Undefined Behavior

Building Faster in Xcode

Using and Extending the Xcode Source Editor

Finding Bugs Using Xcode Runtime Tools

Extending Your Apps with SiriKit

Thread Sanitizer and Static Analysis

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

Profiling in Depth. Do you know where your code is? Session 412. Kris Markel Performance Tools Engineer Chad Woolf Performance Tools Engineer

Introduction to Programming Using Java (98-388)

What s New in Xcode App Signing

What s New in MapKit. App Frameworks #WWDC17. Fredrik Olsson

Swift. Introducing swift. Thomas Woodfin

Mastering UIKit on tvos

Monetize and Promote Your App with iad

Reverse Engineering Swift Apps. Michael Gianarakis Rootcon X 2016

Team: The Cimple Programming Language

iphone Application Programming Lecture 3: Swift Part 2

Core ML in Depth. System Frameworks #WWDC17. Krishna Sridhar, Core ML Zach Nation, Core ML

Building Watch Apps #WWDC15. Featured. Session 108. Neil Desai watchos Engineer

SWIFT! init(title: String) { self.title = title } // required initializer w/ named parameter

Introducing On Demand Resources

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Enabling Your App for CarPlay

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Seamless Linking to Your App

ios Accessibility Developing for everyone Session 201 Ian Fisch ios Accessibility

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

WatchKit In-Depth, Part 2

Advanced Scrollviews and Touch Handling Techniques

Accessibility on OS X

Introduction to Siri Shortcuts

Xcode & Swift: Introduction

Data Delivery with Drag and Drop

Power, Performance, and Diagnostics

Mysteries of Auto Layout, Part 1

Kevin van Vechten Core OS

Working with Metal Overview

Creating Extensions for Safari

Short Notes of CS201

15CS45 : OBJECT ORIENTED CONCEPTS

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

5/23/2015. Core Java Syllabus. VikRam ShaRma

Porting Objective-C to Swift. Richard Ekle

Getting the Most out of Playgrounds in Xcode

C++ (Non for C Programmer) (BT307) 40 Hours

iphone Application Programming Lecture 3: Swift Part 2

CS201 - Introduction to Programming Glossary By

Getting to Know Swift Package Manager

Accessibility on ios. Developing for everyone. Frameworks #WWDC14. Session 210 Clare Kasemset ios Accessibility

What's New in UIKit Dynamics and Visual Effects Session 229

Memory management COSC346

Leveraging Touch Input on ios

Introducing Swift Playgrounds

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

The Mysterious Swift Performance

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

Forth Meets Smalltalk. A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman

ITP 342 Mobile App Dev. Fundamentals

ios Application Development Lecture 2: Seminar and Unit 1

CLASS DESIGN. Objectives MODULE 4

Creating Complications with ClockKit Session 209

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

COMP-520 GoLite Tutorial

Introduce C# as Object Oriented programming language. Explain, tokens,

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

C# and Java. C# and Java are both modern object-oriented languages

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

What s New in Swift #WWDC18. Ted Kremenek, Languages & Runtimes Manager Slava Pestov, Swift Compiler Engineer

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Modern User Interaction on ios

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

1 Lexical Considerations

Mobile Application Programming. Swift Classes

Media and Gaming Accessibility

(CONDITIONALS_BOUNDARY)

ios Memory Deep Dive #WWDC18 Kyle Howarth, Software Engineer James Snee, Software Engineer Kris Markel, Software Engineer

A program execution is memory safe so long as memory access errors never occur:

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Managing Documents In Your ios Apps

(infinitespeak.wordpress.com) Classes and Structs. Dr. Sarah Abraham

New UIKit Support for International User Interfaces

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

What s New in watchos

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

Introducing the Photos Frameworks

Building Visually Rich User Experiences

Expressing high level optimizations within LLVM. Artur Pilipenko

New Ways to Work with Workouts

VIRTUAL FUNCTIONS Chapter 10

App Extension Best Practices

Using Grouped Notifications

CMSC 132: Object-Oriented Programming II

What s New in tvos #WWDC16. App Frameworks. Session 206. Hans Kim tvos Engineer

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

Quick Interaction Techniques for watchos

Transcription:

Developer Tools #WWDC15 Optimizing Swift Performance Session 409 Nadav Rotem Manager, Swift Performance Team Michael Gottesman Engineer, Swift Performance Team Joe Grzywacz Engineer, Performance Tools 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

Agenda Swift 2.0 performance update Understanding Swift performance Using Instruments to analyze the performance of Swift programs

Swift is a Flexible, Safe Programming Language with ARC

Swift is a Flexible, Safe Programming Language with ARC Flexible function signature specializations global variable optimizations lock-less metadata caches generics specializations class hierarchy analysis closure optimizations SSA optimizations call graph analysis loop optimizations devirtualization function inliner Safe overflow checks removal bounds checks elimination obj-c bridge optimizations checked casting optimizations ARC ARC optimizer copy forwarding heap to stack code motion alias analysis reference counting analysis copy-on-write optimizations

Swift is a Flexible, Safe Programming Language with ARC Flexible function signature specializations global variable optimizations lock-less metadata caches generics specializations class hierarchy analysis closure optimizations SSA optimizations call graph analysis loop optimizations devirtualization function inliner Safe overflow checks removal bounds checks elimination obj-c bridge optimizations checked casting optimizations ARC ARC optimizer copy forwarding heap to stack code motion alias analysis reference counting analysis copy-on-write optimizations

Array Bounds Checks Optimizations Swift ensures that array access happen in bounds Swift can lift checks out of loops for i in 0..<n { A[i] ^= 13 O(n) checks become O(1)

Array Bounds Checks Optimizations Swift ensures that array access happen in bounds Swift can lift checks out of loops O(n) checks become O(1) for i in 0..<n { precondition (i < length) A[i] ^= 13

Array Bounds Checks Optimizations Swift ensures that array access happen in bounds precondition (n length) for i in 0..<n { Swift can lift checks out of loops O(n) checks become O(1) A[i] ^= 13

Swift is a Flexible, Safe Programming Language with ARC Flexible Safe ARC function signature specializations overflow checks removal ARC optimizer global variable optimizations bounds checks elimination copy forwarding lock-less metadata caches obj-c bridge optimizations heap to stack generics specializations checked casting optimizations code motion class hierarchy analysis closure optimizations SSA optimizations alias analysis reference counting analysis copy-on-write optimizations call graph analysis loop optimizations devirtualization function inliner

Performance Improvements Since 1.0 Optimized programs (higher is better) Richards 7.50 GenericStack 5.34 $.function 4.65 LinkedList 2.53 NSStringConvert 1.78 1x 2x 3x 4x 5x 6x 7x 8x

Performance Improvements Since 1.0 Unoptimized programs (higher is better) Richards 5.80 GenericStack 7.80 $.function 4.70 LinkedList 9.10 NSStringConvert 2.10 1x 2x 3x 4x 5x 6x 7x 8x

Swift vs. Objective-C Program speed (higher is better) 2.67 Swift Objective-C DeltaBlue 1 4.29 Richards 1 1x 2x 3x 4x 5x 6x 7x 8x

Swift Compilation Xcode compiles files independently, in parallel Re-compile only files that need to be updated File1.swift File2.swift Optimizer is limited to scope of one file Compiler Compiler File1.o File2.o

Whole Module Optimizations Compilation is not limited to the scope of one file File1.swift File2.swift Analyzing the whole module allows better optimizations Whole Module Optimization greatly Compiler improved in Swift 2.0 Better optimizations Parallel code generation File.o

Performance Improvements Due to WMO Swift 2 vs Swift 2 + WMO (higher is better) FFT 4.70 NBody 3.60 2x 3x 4x 5x 6x 7x 8x

New Optimization Level Configurations

New Optimization Level Configurations

Writing High Performance Swift Code Michael Gottesman Engineer, Swift Performance Team

Overview Reference Counting Generics Dynamic Dispatch

Overview Reference Counting Generics Dynamic Dispatch

How Reference Counting Works

How Reference Counting Works class C {... func foo(c: C?) {... var x: C? = C() var y: C? = x foo(y) y = nil x = nil

How Reference Counting Works class C {... func foo(c: C?) {... var x: C? = C() var y: C? = x foo(y) X Class C 21 Boolean Float Double y = nil x = nil

How Reference Counting Works class C {... func foo(c: C?) {... var x: C? = C() var y: C? = x foo(y) X Y Class C 32 Boolean Float Double y = nil x = nil

How Reference Counting Works class C {... func foo(c: C?) {... var x: C? = C() var y: C? = x foo(y) X Y c Class C 3 Boolean Float Double y = nil x = nil

How Reference Counting Works class C {... func foo(c: C?) {... var x: C? = C() var y: C? = x foo(y) X Y Class C 32 Boolean Float Double y = nil x = nil

How Reference Counting Works class C {... func foo(c: C?) {... var x: C? = C() var y: C? = x foo(y) X Class C 21 Boolean Float Double y = nil x = nil

How Reference Counting Works class C {... func foo(c: C?) {... var x: C? = C() var y: C? = x foo(y) Class C 10 Boolean Float Double y = nil x = nil

How Reference Counting Works class C {... func foo(c: C?) {... var x: C? = C() var y: C? = x foo(y) y = nil x = nil

Classes That Do Not Contain References class Point { var x, y: Float

Classes That Do Not Contain References class Point { var x, y: Float Point Point Point Point Reference Reference Reference Reference Array

Classes That Do Not Contain References class Point { var x, y: Float Point Point Point Point var array: [Point] =... for p in array { Reference Reference Reference Reference... Array

Classes That Do Not Contain References class Point { var x, y: Float Point Point Point Point var array: [Point] =... for p in array { Reference Reference Reference Reference increment... Array

Classes That Do Not Contain References class Point { var x, y: Float Point Point Point Point var array: [Point] =... for p in array { Reference Reference Reference Reference increment... decrement Array

Structs That Do Not Contain References struct Point { var x, y: Float Point Point Point Point var array: [Point] =... for p in array { Reference Point Reference Point Reference Point Reference Point increment... decrement Array

Structs That Do Not Contain References struct Point { var x, y: Float var array: [Point] =... for p in array { Reference Point Reference Point Reference Point Reference Point increment... decrement Array

Structs That Do Not Contain References struct Point { var x, y: Float var array: [Point] =... for p in array { Point Point Point Point... Array All reference counting operations eliminated

Structs Containing a Reference

Structs Containing a Reference A Struct requires reference counting if its properties require reference counting

Structs Containing a Reference A Struct requires reference counting if its properties require reference counting Struct Point Float Float

Structs Containing a Reference A Struct requires reference counting if its properties require reference counting Struct Point Float Float UIColor

Structs Containing a Reference A Struct requires reference counting if its properties require reference counting Struct Point Float Float UIColor Class 1

Structs Containing a Reference A Struct requires reference counting if its properties require reference counting Struct Point Float Float UIColor Struct Point Float Float UIColor Class 1

Structs Containing a Reference A Struct requires reference counting if its properties require reference counting Struct Point Float Float UIColor Struct Point Float Float UIColor Class 12

Structs Containing Many References Struct User String String String Array Dictionary

Structs Containing Many References Class Struct User String String String Array Dictionary 1 Class 1 Class 1 Class 1 Class 1

Structs Containing Many References Class Struct User String String String Array Dictionary 1 Class 1 Class 1 Class 1 Class 1 Struct User String String String Array Dictionary

Structs Containing Many References Class Struct User String String String Array Dictionary 12 Class 12 Class 12 Class 12 Class 12 Struct User String String String Array Dictionary

Use a Wrapper Class Reference Wrapper Class 1 Struct User String String String Array Dictionary Class 1 Class 1 Class 1 Class 1 Class 1

Use a Wrapper Class Reference Reference Wrapper Class 12 Struct User String String String Array Dictionary Class 1 Class 1 Class 1 Class 1 Class 1

Use a Wrapper Class Reference Reference Wrapper Class 12 Struct User String String String Array Dictionary Class 1 Class 1 Class 1 Class 1 Class 1 Building Better Apps with Value Types in Swift Mission Friday 2:30PM

Overview Reference Counting Generics Dynamic Dispatch

Overview Reference Counting Generics Dynamic Dispatch

How Generics Work func min<t : Comparable>(x: T, y: T) -> T { return y < x? y : x

How Generics Work func min<t : Comparable>(x: T, y: T) -> T { return y < x? y : x f func min<t : Comparable>(x: T, y: T) -> T { return y < x? y : x

How Generics Work func min<t : Comparable>(x: T, y: T) -> T { return y < x? y : x func min<t : Comparable>(x: T, y: T, FTable: FunctionTable) -> T { let xcopy = FTable.copy(x) let ycopy = FTable.copy(y) let m = FTable.lessThan(yCopy, xcopy)? y : x FTable.release(x) FTable.release(y) return m

How Generics Work func min<t : Comparable>(x: T, y: T) -> T { return y < x? y : x func min<t : Comparable>(x: T, y: T, FTable: FunctionTable) -> T { let xcopy = FTable.copy(x) let ycopy = FTable.copy(y) let m = FTable.lessThan(yCopy, xcopy)? y : x FTable.release(x) FTable.release(y) return m

How Generics Work func min<t : Comparable>(x: T, y: T) -> T { return y < x? y : x func min<t : Comparable>(x: T, y: T, FTable: FunctionTable) -> T { let xcopy = FTable.copy(x) let ycopy = FTable.copy(y) let m = FTable.lessThan(yCopy, xcopy)? y : x FTable.release(x) FTable.release(y) return m

Generic Specialization func foo() { let x: Int =... let y: Int =... let r = min (x, y)...

Generic Specialization func foo() { let x: Int =... let y: Int =... let r = min (x, y)...

Generic Specialization func foo() { let x: Int =... let y: Int =... let r = min (x, y)... func min<t : Comparable>(x: T, y: T, FTable: FunctionTable) -> T { let xcopy = FTable.copy(x) let ycopy = FTable.copy(y) let m = FTable.lessThan(yCopy, xcopy)? y : x FTable.release(x) FTable.release(y) return m

Generic Specialization func foo() { let x: Int =... let y: Int =... let r = min (x, y)... func min<int>(x: Int, y: Int, FTable: FunctionTable) -> Int { let xcopy = FTable.copy(x) let ycopy = FTable.copy(y) let m = FTable.lessThan(yCopy, xcopy)? y : x FTable.release(x) FTable.release(y) return m

Generic Specialization func foo() { let x: Int =... let y: Int =... let r = min (x, y)... func min<int>(x: Int, y: Int) -> Int { return y < x? y : x

Generic Specialization func foo() { let x: Int =... let y: Int =... let r = min <Int> (x, y)... func min<int>(x: Int, y: Int) -> Int { return y < x? y : x

Specialization is Limited by Visibility Module A File1.swift func compute(...) -> Int {... return min(x, y) File2.swift func min<t: Comparable>(x: T, y: T) -> T { return y < x? y : x

Specialization is Limited by Visibility Module A File1.swift func compute(...) -> Int {... return min(x, y) Passing Int to min<t> File2.swift func min<t: Comparable>(x: T, y: T) -> T { return y < x? y : x

Specialization is Limited by Visibility Module A File1.swift func compute(...) -> Int {... return min(x, y) Passing Int to min<t> File2.swift func min<t: Comparable>(x: T, y: T) -> T { return y < x? y : x Definition not visible in File1

Specialization is Limited by Visibility Module A File1.swift func compute(...) -> Int {... return min(x, y) Passing Int to min<t> Must call min<t> File2.swift func min<t: Comparable>(x: T, y: T) -> T { return y < x? y : x Definition not visible in File1

Whole Module Optimization Module A File1.swift func compute(...) -> Int {... return min(x, y) File2.swift func min<t: Comparable>(x: T, y: T) -> T { return y < x? y : x Definition not visible in File1

Whole Module Optimization Module A File1.swift func compute(...) -> Int {... return min(x, y) File2.swift func min<t: Comparable>(x: T, y: T) -> T { return y < x? y : x Definition is visible in File1 Definition not visible in File1

Whole Module Optimization Module A File1.swift func compute(...) -> Int {... return min(x, y) Can call min<int> File2.swift func min<t: Comparable>(x: T, y: T) -> T { return y < x? y : x Definition is visible in File1 Definition not visible in File1

Overview Reference Counting Generics Dynamic Dispatch

Overview Reference Counting Generics Dynamic Dispatch

Dynamic Dispatch

Dynamic Dispatch public class Pet func noise() var name func noiseimpl() class Dog override func noise()

Dynamic Dispatch public class Pet func makenoise(p: Pet) { print("my name is \(p.name)") func noise() var name func noiseimpl() p.noise() class Dog override func noise()

Dynamic Dispatch public class Pet func makenoise(p: Pet) { print("my name is \(p.name)") func noise() var name p.noise() func noiseimpl() func makenoise(p: Pet) { print("my name is \(p.name)") p.noise() class Dog override func noise()

Dynamic Dispatch public class Pet func makenoise(p: Pet) { print("my name is \(p.name)") func noise() var name p.noise() func noiseimpl() func makenoise(p: Pet) { class Dog override func noise()

Dynamic Dispatch public class Pet func makenoise(p: Pet) { print("my name is \(p.name)") func noise() var name p.noise() func noiseimpl() func makenoise(p: Pet) { class Dog override func noise() let namegetter = Pet.nameGetter(p) print("my name is \(namegetter(p))") let noisemethod = Pet.noiseMethod(p) noisemethod(p)

Dynamic Dispatch public class Pet func makenoise(p: Pet) { print("my name is \(p.name)") func noise() var name p.noise() func noiseimpl() func makenoise(p: Pet) { class Dog override func noise() let namegetter = Pet.nameGetter(p) print("my name is \(namegetter(p))") let noisemethod = Pet.noiseMethod(p) noisemethod(p) Can only emit direct calls if it is known that the method is not overridden

Communicate API Constraints

Communicate API Constraints Inheritance

Communicate API Constraints Inheritance Access Control

Inheritance public class Pet func noise() var name func noiseimpl() func makenoise(p: Pet) { print("my let namegetter is = Pet.getNameGetter(p) \(p.name)") print("my name is \(namegetter(p))") let noisemethod = Pet.getNoiseMethod(p) noisemethod(p) class Dog override func noise()

Inheritance public class Pet func noise() var name func noiseimpl() func makenoise(p: Pet) { print("my let namegetter is = Pet.getNameGetter(p) \(p.name)") print("my name is \(namegetter(p))") let noisemethod = Pet.getNoiseMethod(p) noisemethod(p) class Dog override func noise()

Inheritance public class Pet func noise() final var var name name func noiseimpl() func makenoise(p: Pet) { print("my let namegetter is = Pet.getNameGetter(p) \(p.name)") print("my name is \(namegetter(p))") let noisemethod = Pet.getNoiseMethod(p) noisemethod(p) class Dog override func noise()

Inheritance public class Pet func noise() final var var name name func noiseimpl() func makenoise(p: Pet) { print("my name is \(p.name)") let noisemethod = Pet.getNoiseMethod(p) noisemethod(p) class Dog override func noise()

Access Control public class Pet func noise() final var name override func noiseimpl()? func noiseimpl() class Dog override func noise() override func noiseimpl()?

Access Control Module A Pet.swift public class Pet func noise() final var name override func noiseimpl()? func noiseimpl() Dog.swift class Dog override func noise() override func noiseimpl()?

Access Control Module A Pet.swift Module B Cat.swift public class Pet func noise() final var name class Cat override func noise() override func noiseimpl()? func noiseimpl() Dog.swift class Dog override func noise() override func noiseimpl()?

Access Control Module A Pet.swift Module B Cat.swift public class Pet func noise() final var name class Cat override func noise() override func noiseimpl()? func noiseimpl() Dog.swift class Dog override func noise() override func noiseimpl()?

Access Control Module A Pet.swift Module B Cat.swift public class Pet func noise() final var name class Cat override func noise() override func noiseimpl() func noiseimpl() Dog.swift class Dog override func noise() override func noiseimpl()

Access Control Module A Pet.swift Module B Cat.swift public class Pet func noise() final var name class Cat override func noise() override func noiseimpl() private func func noiseimpl() Dog.swift class Dog override func noise() override func noiseimpl()

Access Control Module A Pet.swift Module B Cat.swift public class Pet func noise() class Cat override func noise() final var name private func func noiseimpl() Dog.swift class Dog override func noise()

Whole Module Optimization Module A Pet.swift public class Pet func noise() final var name private func func noiseimpl() Dog.swift class Dog override func noise()

Whole Module Optimization Module A Pet.swift public class Pet func noise() func bark(d: Dog) { d.noise() final var name private func func noiseimpl() Dog.swift class Dog override func noise()

Whole Module Optimization Module A Pet.swift public class Pet func noise() func bark(d: Dog) { d.noise() final var name private func func noiseimpl() func bark(d: Dog) { Dog.swift class Dog let noisemethod = Dog.getNoiseMethod() noisemethod(d) override func noise()

Whole Module Optimization Module A Pet.swift public class Pet func noise() func bark(d: Dog) { d.noise() final var name private func func noiseimpl() Dog.swift class Dog override func noise() func bark(d: Dog) { let noisemethod = Dog.getNoiseMethod() noisemethod(d)

Whole Module Optimization Module A Pet.swift public class Pet func noise() func bark(d: Dog) { d.noise() final var name private func func noiseimpl() Dog.swift class Dog func bark(d: Dog) { override func noise()

Whole Module Optimization Module A Pet.swift public class Pet func noise() func bark(d: Dog) { d.noise() final var name private func func noiseimpl() Dog.swift class Dog func bark(d: Dog) { d.noise() override func noise()

Swift vs. Objective-C Program speed (higher is better) 2.67 Swift Objective-C DeltaBlue 1 4.29 Richards 1 1x 2x 3x 4x 5x 6x 7x 8x

Communicate your API Intent Use the final keyword and access control Help the compiler understand your class hierarchy Be aware of breaking existing clients Enable Whole Module Optimization

Demo Joe Grzywacz Engineer, Performance Tools

Summary Swift is a flexible, safe programming language with ARC Write your APIs and code with performance in mind Profile your application with Instruments

More Information Swift Language Documentation http://developer.apple.com/swift Apple Developer Forums http://developer.apple.com/forums Stefan Lesser Developer Tools Evangelist slesser@apple.com

Related Sessions Profiling in Depth Mission Thursday 3:30PM Building Better Apps with Value Types in Swift Mission Friday 2:30PM