Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is...
Transcript of Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is...
![Page 1: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/1.jpg)
#WWDC17
© 2017 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.
Doug Gregor, Swift Compiler Team Bob Wilson, Swift Performance Team
•What’s New in Swift • Session 402
Developer Tools
Ben Cohen, Swift Standard Library Team John McCall, Swift Compiler Team
![Page 2: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/2.jpg)
![Page 3: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/3.jpg)
![Page 4: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/4.jpg)
Open Source
Swift is developed on GitHub
Open evolution process
![Page 5: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/5.jpg)
Open Source
Swift is developed on GitHub
Open evolution process
Refactoring coming soon • Can contribute new refactorings • Toolchains can add refactorings
to Xcode
![Page 6: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/6.jpg)
Swift Package Manager
Growing ecosystem • 7,000+ packages on GitHub • Popular for server-side Swift
![Page 7: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/7.jpg)
Swift Package Manager
Growing ecosystem • 7,000+ packages on GitHub • Popular for server-side Swift
Significant improvements in Swift 4 • New manifest API • Better development workflow, diagnostics, dependency resolution • Xcode project generation
![Page 8: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/8.jpg)
Agenda
•Language refinements and additions •Source compatibility •Tools and performance •Standard library •Exclusive access to memory
![Page 9: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/9.jpg)
// “Private” Access Control
struct Date: Equatable, Comparable { private let secondsSinceReferenceDate: Double static func ==(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate == rhs.secondsSinceReferenceDate } static func <(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate < rhs.secondsSinceReferenceDate }}
![Page 10: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/10.jpg)
// “Private” Access Control
struct Date { private let secondsSinceReferenceDate: Double} extension Date: Equatable { static func ==(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate == rhs.secondsSinceReferenceDate }} extension Date: Comparable { static func <(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate < rhs.secondsSinceReferenceDate }}
![Page 11: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/11.jpg)
// “Private” Access Control
struct Date { private let secondsSinceReferenceDate: Double} extension Date: Equatable { static func ==(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate == rhs.secondsSinceReferenceDate }} extension Date: Comparable { static func <(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate < rhs.secondsSinceReferenceDate }}
error: ’secondsSinceReferenceDate’ is inaccessible due to private protection level
![Page 12: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/12.jpg)
// “Private” Access Control
struct Date { fileprivate let secondsSinceReferenceDate: Double} extension Date: Equatable { static func ==(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate == rhs.secondsSinceReferenceDate }} extension Date: Comparable { static func <(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate < rhs.secondsSinceReferenceDate }}
![Page 13: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/13.jpg)
// SE-0169: Improve Interaction Between Private Declarations and Extensions
struct Date { private let secondsSinceReferenceDate: Double} extension Date: Equatable { static func ==(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate == rhs.secondsSinceReferenceDate }} extension Date: Comparable { static func <(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate < rhs.secondsSinceReferenceDate }}
NEW
![Page 14: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/14.jpg)
// Composing Classes and Protocols
protocol Shakeable { func shake()} extension UIButton: Shakeable { /* … */ }extension UISlider: Shakeable { /* … */ }
![Page 15: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/15.jpg)
// Composing Classes and Protocols
protocol Shakeable { func shake()} extension UIButton: Shakeable { /* … */ }extension UISlider: Shakeable { /* … */ } func shakeEm(controls: [???]) { for control in controls where control.state.isEnabled { control.shake() }}
![Page 16: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/16.jpg)
// Composing Classes and Protocols
protocol Shakeable { func shake()} extension UIButton: Shakeable { /* … */ }extension UISlider: Shakeable { /* … */ } func shakeEm(controls: [UIControl]) { for control in controls where control.state.isEnabled { control.shake() }}
![Page 17: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/17.jpg)
// Composing Classes and Protocols
protocol Shakeable { func shake()} extension UIButton: Shakeable { /* … */ }extension UISlider: Shakeable { /* … */ } func shakeEm(controls: [UIControl]) { for control in controls where control.state.isEnabled { control.shake() }} error: value of type ‘UIControl’ has no member named ‘shake’
![Page 18: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/18.jpg)
// Composing Classes and Protocols
protocol Shakeable { func shake()} extension UIButton: Shakeable { /* … */ }extension UISlider: Shakeable { /* … */ } func shakeEm(controls: [Shakeable]) { for control in controls where control.state.isEnabled { control.shake() }}
![Page 19: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/19.jpg)
// Composing Classes and Protocols
protocol Shakeable { func shake()} extension UIButton: Shakeable { /* … */ }extension UISlider: Shakeable { /* … */ } func shakeEm(controls: [Shakeable]) { for control in controls where control.state.isEnabled { control.shake() }}
error: value of type ‘Shakeable’ has no member named ‘state’
![Page 20: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/20.jpg)
// SE-0156: Class and Subtype Existentials
protocol Shakeable { func shake()} extension UIButton: Shakeable { /* … */ }extension UISlider: Shakeable { /* … */ } func shakeEm(controls: [UIControl & Shakeable]) { for control in controls where control.state.isEnabled { control.shake() }}
NEW
![Page 21: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/21.jpg)
// Class and Protocol Composition in the SDK
// Objective-C API@interface NSCandidateListTouchBarItem<CandidateType> : NSTouchBarItem@property (nullable, weak) NSView <NSTextInputClient> *client;@end
![Page 22: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/22.jpg)
// Class and Protocol Composition in the SDK
// Objective-C API@interface NSCandidateListTouchBarItem<CandidateType> : NSTouchBarItem@property (nullable, weak) NSView <NSTextInputClient> *client;@end
// Swift 3class NSCandidateListTouchBarItem<CandidateType: AnyObject> : NSTouchBarItem { var client: NSView?}
![Page 23: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/23.jpg)
// Class and Protocol Composition in the SDK
// Objective-C API@interface NSCandidateListTouchBarItem<CandidateType> : NSTouchBarItem@property (nullable, weak) NSView <NSTextInputClient> *client;@end
// Swift 4class NSCandidateListTouchBarItem<CandidateType: AnyObject> : NSTouchBarItem { var client: (NSView & NSTextInputClient)?}
NEW
![Page 24: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/24.jpg)
SE-0161 Smart KeyPaths: Better Key-Value Coding for Swift
SE-0166 Swift Archival & Serialization
SE-0167 Swift Encoders
Swift 4—Improving Cocoa Idioms
What’s New in Foundation Hall 2 Wednesday 11:00AM
![Page 25: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/25.jpg)
•Source Compatibility
![Page 26: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/26.jpg)
Swift 4
Swift 4 largely source-compatible with Swift 3 • Refinements • SDK improvements
Additive features extend existing syntax
![Page 27: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/27.jpg)
Swift 3.2
Compilation mode of the Swift 4 compiler • Not a separate toolchain!
Emulates Swift 3 • Allows Swift 3 syntax that has changed in Swift 4 • “Rolls back” SDK changes
NEW
![Page 28: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/28.jpg)
Swift 3.2
Compilation mode of the Swift 4 compiler • Not a separate toolchain!
Emulates Swift 3 • Allows Swift 3 syntax that has changed in Swift 4 • “Rolls back” SDK changes
Most Swift 3 code should compile unmodified
NEW
![Page 29: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/29.jpg)
Swift 3.2
Compilation mode of the Swift 4 compiler • Not a separate toolchain!
Emulates Swift 3 • Allows Swift 3 syntax that has changed in Swift 4 • “Rolls back” SDK changes
Most Swift 3 code should compile unmodified
Provides most Swift 4 features and new SDKs
NEW
![Page 30: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/30.jpg)
Migrating from Swift 3.2 to Swift 4
![Page 31: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/31.jpg)
Swift 3.2 and Swift 4 Coexistence
Swift language is set per-target
Migrate one target at a time • No need to migrate in dependency order • Your dependencies can migrate asynchronously
![Page 32: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/32.jpg)
Swift Package Manager
Swift package manager will pick the appropriate Swift version for each package
let package = Package( name: "HTTP", ... swiftLanguageVersions: [3, 4])
![Page 33: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/33.jpg)
Bob Wilson, Swift Performance Team
•Build Improvements
![Page 34: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/34.jpg)
New Build System
Xcode 9 includes a preview of a new build system
Fast: lower overhead, especially for large projects
![Page 35: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/35.jpg)
New Build System
Xcode 9 includes a preview of a new build system
Fast: lower overhead, especially for large projects
![Page 36: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/36.jpg)
Precompiled Bridging Headers Bridging header for large mixed-source projects can be slow to parse
Objective-C
Objective-C
Objective-C
Swift
Swift
Swift
![Page 37: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/37.jpg)
Precompiled Bridging Headers Bridging header for large mixed-source projects can be slow to parse
Objective-C
Objective-C
Objective-C
Swift
Swift
Swift
Precompiled Header
![Page 38: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/38.jpg)
Precompiled Bridging Headers Bridging header for large mixed-source projects can be slow to parse
Speeds up Apple’s Music app build by 40%
Objective-C
Objective-C
Objective-C
Swift
Swift
Swift
Precompiled Header
![Page 39: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/39.jpg)
Shared Build for Coverage Testing
![Page 40: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/40.jpg)
Shared Build for Coverage Testing
Xcode 8 builds separately for coverage testing
![Page 41: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/41.jpg)
Shared Build for Coverage Testing
Xcode 8 builds separately for coverage testing
![Page 42: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/42.jpg)
Shared Build for Coverage Testing
Xcode 8 builds separately for coverage testing
![Page 43: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/43.jpg)
Shared Build for Coverage Testing
Xcode 8 builds separately for coverage testing
Coverage instrumentation is very low overhead
![Page 44: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/44.jpg)
Shared Build for Coverage Testing
Xcode 8 builds separately for coverage testing
Coverage instrumentation is very low overhead
Xcode 9 shares the same build → avoid building twice
![Page 45: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/45.jpg)
Indexing While Building
![Page 46: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/46.jpg)
Indexing While Building
![Page 47: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/47.jpg)
Indexing While Building
Background indexing often duplicates effort
Build process now updates the index
More accurate results
![Page 48: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/48.jpg)
•Predictable Performance
![Page 49: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/49.jpg)
// Unpredictable Performance in Swift 3
protocol Ordered { func precedes(_ other: Ordered) -> Bool } func testSort(_ values: inout [Ordered]) { values.sort { $0.precedes($1) }}
![Page 50: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/50.jpg)
Unpredictable Performance in Swift 3
![Page 51: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/51.jpg)
Unpredictable Performance in Swift 3So
rt T
ime
0
5
10
15
20
Struct Size
1 2 3 4
![Page 52: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/52.jpg)
Unpredictable Performance in Swift 3So
rt T
ime
0
5
10
15
20
Struct Size
1 2 3 4
![Page 53: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/53.jpg)
Unpredictable Performance in Swift 3So
rt T
ime
0
5
10
15
20
Struct Size
1 2 3 4
![Page 54: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/54.jpg)
Unpredictable Performance in Swift 3So
rt T
ime
0
5
10
15
20
Struct Size
1 2 3 4
![Page 55: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/55.jpg)
Existential Containers
Understanding Swift Performance WWDC 2016
![Page 56: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/56.jpg)
Existential Containers
Implementation of a value of unknown type
Understanding Swift Performance WWDC 2016
![Page 57: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/57.jpg)
Existential Containers
Implementation of a value of unknown type
Inline value buffer: currently three words
Understanding Swift Performance WWDC 2016
![Page 58: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/58.jpg)
Existential Containers
Implementation of a value of unknown type
Inline value buffer: currently three words
Large values stored on the heap
Understanding Swift Performance WWDC 2016
![Page 59: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/59.jpg)
Existential Containers
Implementation of a value of unknown type
Inline value buffer: currently three words
Large values stored on the heap
Heap allocation is slow
Understanding Swift Performance WWDC 2016
![Page 60: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/60.jpg)
COW Existential Buffers
Why am I here?
![Page 61: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/61.jpg)
COW Existential Buffers
Swift now uses copy-on-write (COW) reference-counted existential buffers
Copied only when modified while not uniquely referenced
Avoids expensive heap allocations
![Page 62: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/62.jpg)
COW Existential Buffers
Sort
Tim
e
0
5
10
15
20
Struct Size
1 2 3 4
![Page 63: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/63.jpg)
Faster Generic Code
Specialization: compiler generates code for specific types
Not always possible: unspecialized generic code is also important
Stack allocation of generic buffers
![Page 64: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/64.jpg)
•Smaller Binaries
![Page 65: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/65.jpg)
// Unused Conformance Removal
struct Date { private let secondsSinceReferenceDate: Double} extension Date: Equatable { static func ==(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate == rhs.secondsSinceReferenceDate }} extension Date: Comparable { static func <(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate < rhs.secondsSinceReferenceDate }}
![Page 66: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/66.jpg)
// Unused Conformance Removal
struct Date { private let secondsSinceReferenceDate: Double} extension Date: Equatable { static func ==(lhs: Date, rhs: Date) -> Bool { return lhs.secondsSinceReferenceDate == rhs.secondsSinceReferenceDate }}
![Page 67: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/67.jpg)
Unused @objc Thunks
class MyClass: NSObject {
}
func print() { … }func show() { print() … }
![Page 68: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/68.jpg)
Unused @objc Thunks
class MyClass: NSObject {
}
print()
show()
func print() { … }func show() { print() … }
![Page 69: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/69.jpg)
Unused @objc Thunks
Swift 3 automatically infers @objc
class MyClass: NSObject {
}
print()
show()
func print() { … }func show() { print() … }
@objc@objc
![Page 70: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/70.jpg)
Unused @objc Thunks
Swift 3 automatically infers @objc
class MyClass: NSObject {
}
print() -[MyClass print]
show() -[MyClass show]
func print() { … }func show() { print() … }
@objc@objc
![Page 71: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/71.jpg)
Unused @objc Thunks
Swift 3 automatically infers @objc
class MyClass: NSObject {
}
print() -[MyClass print]
show() -[MyClass show]
func print() { … }func show() { print() … }
@objc@objc
![Page 72: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/72.jpg)
Unused @objc Thunks
Swift 3 automatically infers @objc
Objective-C thunks are often unused
class MyClass: NSObject {
}
print() -[MyClass print]
show() -[MyClass show]
func print() { … }func show() { print() … }
@objc@objc
![Page 73: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/73.jpg)
SE-0160: Limited @objc Inference
Swift 4 only infers @objc when it is needed • Overriding an Objective-C method • Conforming to an Objective-C protocol
Reduced size of Apple’s Music app by 5.7%
![Page 74: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/74.jpg)
SE-0160: Limited @objc Inference
Use @objc on extension with a group of functions
Compiler will report errors for anything not expressible in Objective-C
@objc extension MyClass { func f(_: String?) { … } func g(_: Int?) { … } }
![Page 75: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/75.jpg)
SE-0160: Limited @objc Inference
Use @objc on extension with a group of functions
Compiler will report errors for anything not expressible in Objective-C
@objc extension MyClass { func f(_: String?) { … } func g(_: Int?) { … } }
error: method cannot be in an @objc extension of a class (without @nonobjc) because the type of the parameter cannot be represented in Objective-C
![Page 76: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/76.jpg)
Migration for Limited @objc Inference
![Page 77: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/77.jpg)
Migration for Minimal Inference
Migrator cannot identify all the functions that need @objc
Inferred Objective-C thunks marked as deprecated to help you find them • Build warnings about deprecated methods • Console messages when running deprecated thunks
![Page 78: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/78.jpg)
Build Warnings
Manually add @objc to fix build warnings
[vc showStatus]; }
warning: Swift method ‘ViewController.showStatus’ uses ‘@objc’ inference deprecated in Swift 4; add ‘@objc’ to provide an Objective-C entrypoint
![Page 79: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/79.jpg)
Build Warnings
Manually add @objc to fix build warnings
[vc showStatus]; }
warning: Swift method ‘ViewController.showStatus’ uses ‘@objc’ inference deprecated in Swift 4; add ‘@objc’ to provide an Objective-C entrypoint
print(“ViewController status:”) if let name = title { print(“ \(name)”) }
func showStatus() {
![Page 80: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/80.jpg)
Build Warnings
Manually add @objc to fix build warnings
[vc showStatus]; }
warning: Swift method ‘ViewController.showStatus’ uses ‘@objc’ inference deprecated in Swift 4; add ‘@objc’ to provide an Objective-C entrypoint
print(“ViewController status:”) if let name = title { print(“ \(name)”) }
func showStatus() {@objc
![Page 81: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/81.jpg)
Run your code, including all your tests, and fix issues logged to the console
Runtime Warnings
2017-05-26 10:00:01.531842-0700 MyApp[48356:20427473] *** /Users/bwilson/Desktop/MyApp/MyApp/ViewController.swift:26:5: implicit Objective-C entrypoint -[MyApp.ViewController showStatus] is deprecated and will be removed in Swift 4; add explicit ‘@objc’ to the declaration to emit
the Objective-C entrypoint in Swift 4 and suppress this message
![Page 82: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/82.jpg)
Run your code, including all your tests, and fix issues logged to the console
Runtime Warnings
2017-05-26 10:00:01.531842-0700 MyApp[48356:20427473] *** /Users/bwilson/Desktop/MyApp/MyApp/ViewController.swift:26:5: implicit Objective-C entrypoint -[MyApp.ViewController showStatus] is deprecated and will be removed in Swift 4; add explicit ‘@objc’ to the declaration to emit
the Objective-C entrypoint in Swift 4 and suppress this message
![Page 83: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/83.jpg)
Finish Migration
Change build setting to Default
Apple’s Music app migration: only a handful of manual changes required
![Page 84: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/84.jpg)
Symbol Size
Swift symbols take up a lot of space
Example: macOS libswiftCore library
![Page 85: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/85.jpg)
Symbol Size
Swift symbols take up a lot of space
Example: macOS libswiftCore library
0MB
1.5MB
3MB
4.5MB
6MB
Swift 3.1 Swift 4
![Page 86: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/86.jpg)
Symbol Size
Swift symbols take up a lot of space
Example: macOS libswiftCore library
0MB
1.5MB
3MB
4.5MB
6MB
Swift 3.1 Swift 4
![Page 87: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/87.jpg)
Symbol Stripping
Linkers use a separate trie structure to find symbols
Swift symbols are rarely needed in the symbol table
New build setting enabled by default
View symbols with “xcrun dyldinfo -export” instead of nm
![Page 88: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/88.jpg)
Symbol Stripping
Swift standard libraries are stripped during App Thinning
New option when exporting project archive
![Page 89: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/89.jpg)
Ben Cohen, Swift Standard Library Team
•Swift Strings •Faster, easier character processing
![Page 90: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/90.jpg)
public typealias CChar = Int8
![Page 91: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/91.jpg)
e0xE9
![Page 92: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/92.jpg)
e ´0x65 0x301
+
![Page 93: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/93.jpg)
# Ruby
one = "\u{E9}" é
two = "\u{65}\u{301}" é
![Page 94: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/94.jpg)
# Ruby
one = "\u{E9}" é
two = "\u{65}\u{301}" é
one.length 1
two.length 2
one == two false
![Page 95: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/95.jpg)
Unicode Correctness by Default In Swift, a Character is a grapheme
![Page 96: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/96.jpg)
Unicode Correctness by Default In Swift, a Character is a grapheme
aéИ 草
!"#
カ ગu
twoCodeUnits.count 1 oneCodeUnit == twoCodeUnits true
![Page 97: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/97.jpg)
// Grapheme Breaking
![Page 98: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/98.jpg)
// Grapheme Breaking
var family = "👩"family += "\u{200D}👩"family += "\u{200D}👧"family += "\u{200D}👦"
print(family) '
family.count 1
![Page 99: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/99.jpg)
Swift 4
Swift 3
Time (ms)
0 60 120 180 240 300
Faster Character Processing Benchmark for Latin-derived characters, Han ideographs, and Kana
![Page 100: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/100.jpg)
// Graphemes can be of arbitrary length
var family = "👩" family += "\u{200D}👩" family += "\u{200D}👧" family += "\u{200D}👦"
print(family) '
family.count 1
![Page 101: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/101.jpg)
// Swift 3 strings had a collection of characters
let values = "one,two,three..."
var i = values.characters.startIndex while let comma = values.characters[i...<values.characters.endIndex].index(of: ",") { if values.characters[i..<comma] == "two" { print("found it!") }. i = values.characters.index(after: comma) }.
![Page 102: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/102.jpg)
// Swift 3 strings had a collection of characters
let values = "one,two,three..."
var i = values.characters.startIndex while let comma = values.characters[i...<values.characters.endIndex].index(of: ",") { if values.characters[i..<comma] == "two" { print("found it!") }. i = values.characters.index(after: comma) }.
![Page 103: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/103.jpg)
// Swift 3 strings had a collection of characters
let values = "one,two,three..."
var i = values.startIndex while let comma = values[i..<values.endIndex].index(of: ",") { if values[i..<comma] == "two" { print("found it!") }. i = values.index(after: comma) }.
![Page 104: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/104.jpg)
// Swift 4 strings are a collection of characters
let values = "one,two,three..."
var i = values.startIndex while let comma = values[i..<values.endIndex].index(of: ",") { if values[i..<comma] == "two" { print("found it!") }. i = values.index(after: comma) }.
![Page 105: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/105.jpg)
// SE-0172: Simpler One-Sided Slicing Syntax
let values = "one,two,three..."
var i = values.startIndex while let comma = values[i..<values.endIndex].index(of: ",") { if values[i..<comma] == "two" { print("found it!") } i = values.index(after: comma) }
![Page 106: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/106.jpg)
// SE-0172: Simpler One-sided Slicing Syntax
let values = "one,two,three..."
var i = values.startIndex while let comma = values[i...].index(of: ",") { if values[i..<comma] == "two" { print("found it!") } i = values.index(after: comma) }
![Page 107: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/107.jpg)
// Using String as a Collection
let asciiTable = zip(65..., "ABCDEFGHIJKLMNOPQRSTUVWXYZ") 65: A for (code, character) in asciiTable { 66: B print(code, character, separator: ": ") 68: C } 69: D …
![Page 108: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/108.jpg)
// Using String as a Collection
"Good luck ( in the game tonight!"
![Page 109: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/109.jpg)
// Using String as a Collection
"Good luck ( in the game tonight!"
extension Unicode.Scalar { var isRegionalIndicator: Bool { return ("🇦"..."🇿").contains(self) } }
![Page 110: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/110.jpg)
// Using String as a Collection
"Good luck ( in the game tonight!"
extension Character { var isFlag: Bool { let scalars = self.unicodeScalars return scalars.count == 2 && scalars.first!.isRegionalIndicator && scalars.last!.isRegionalIndicator } }
![Page 111: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/111.jpg)
// Using String as a Collection
let message = "Looking forward to + vs , game!"
message.contains { $0.isFlag } true
let flags = message.filter { $0.isFlag } "+,"
flags.count 2
![Page 112: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/112.jpg)
// SE-0163: String Slicing
let s = "one,two,three"
s.split(separator: ",")
![Page 113: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/113.jpg)
// SE-0163: String Slicing
let s = "one,two,three"
s.split(separator: ",") ["one","two","three"]: [String]
![Page 114: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/114.jpg)
// SE-0163: String Slicing
let s = "one,two,three"
s.split(separator: ",") ["one","two","three"]: [Substring]
![Page 115: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/115.jpg)
Swift Strings Have Three Properties
Owning class Capacity, reference count
Start
Count = 13
Owner
3 words
H e l l o , w o r l d !
"Hello, world!"
![Page 116: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/116.jpg)
Owner Reference Count Drops to Zero Owner is freed, frees the string buffer
Owning class Capacity, reference count
Start
Count = 13
Owner
H e l l o , w o r l d !
"Hello, world!"
![Page 117: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/117.jpg)
Owner Reference Count Drops to Zero Owner is freed, frees the string buffer
Owning class Capacity, reference count
H e l l o , w o r l d !
![Page 118: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/118.jpg)
Owner Reference Count Drops to Zero Owner is freed, frees the string buffer
![Page 119: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/119.jpg)
Creating a Substring
Owning class Capacity, reference count
Start
Count = 13
Owner
"Hello, world!"
H e l l o , w o r l d !
![Page 120: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/120.jpg)
Creating a Substring
Owning class Capacity, reference count
Start
Count = 13
Owner
"Hello, world!"
H e l l o , w o r l d !
![Page 121: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/121.jpg)
Creating a Substring
Owning class Capacity, reference count
Start
Count = 13
Owner
"Hello, world!"
H e l l o , w o r l d !
Start
Count = 5
Owner
"world"
![Page 122: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/122.jpg)
Original String Goes Out of Scope Owner still referenced, buffer remains
Owning class Capacity, reference count
Start
Count = 13
Owner
"Hello, world!"
H e l l o , w o r l d !
Start
Count = 5
Owner
"world"
![Page 123: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/123.jpg)
Original String Goes Out of Scope Owner still referenced, buffer remains
Owning class Capacity, reference count
H e l l o , w o r l d !
Start
Count = 5
Owner
"world"
![Page 124: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/124.jpg)
// Substrings can waste memory
let big = downloadHugeString() let small = extractTinyString(from: big)
mainView.titleLabel.text = small
![Page 125: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/125.jpg)
// Substrings can waste memory
let big = downloadHugeString() let small = extractTinyString(from: big)
mainView.titleLabel.text = small
![Page 126: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/126.jpg)
// Substrings can waste memory
let big = downloadHugeString() let small = extractTinyString(from: big)
mainView.titleLabel.text = small
error: cannot assign value of type ‘Substring’ to type ‘String’
![Page 127: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/127.jpg)
// String(_:Substring) Copies the Buffer
let big = downloadHugeString() let small = extractTinyString(from: big)
mainView.titleLabel.text = small
![Page 128: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/128.jpg)
// String(_:Substring) copies the buffer
let big = downloadHugeString() let small = extractTinyString(from: big)
mainView.titleLabel.text = String(small)
![Page 129: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/129.jpg)
// Substring and type inference
let keyAndValue = setting.split(":") if keyAndValue.first == "animation", let value = keyAndValue.last view.animate = value == "on" ? true : false }
![Page 130: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/130.jpg)
// SE-0168: Multi-line String Literals
func tellJoke(name: String, character: Character) { let punchline = name.filter { $0 != character } let n = name.count - punchline.count
let joke = "Q: Why does \(name) have \(n) \(character)'s in their name?\nA: I don't know, why does \(name) have \(n) \(character)'s in their name?\nQ: Because otherwise they'd be called \(punchline)." print(joke)
tellJoke(name: "Edward Woodward", character: "d")
}
![Page 131: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/131.jpg)
// SE-0168: Multi-line String Literals
func tellJoke(name: String, character: Character) { let punchline = name.filter { $0 != character } let n = name.count - punchline.count
let joke = """ Q: Why does \(name) have \(n) \(character)'s in their name? A: I don't know, why does \(name) have \(n) \(character)'s in their name? Q: Because otherwise they'd be called \(punchline). """ print(joke)
tellJoke(name: "Edward Woodward", character: "d")
NEW
}
![Page 132: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/132.jpg)
// SE-0168: Multi-line String Literals
func tellJoke(name: String, character: Character) { let punchline = name.filter { $0 != character } let n = name.count - punchline.count
let joke = """ Q: Why does \(name) have \(n) \(character)'s in their name? A: I don't know, why does \(name) have \(n) \(character)'s in their name? Q: Because otherwise they'd be called \(punchline). """ print(joke)
tellJoke(name: "Edward Woodward", character: "d")
\t\t
NEW
}
![Page 133: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/133.jpg)
// SE-0168: Multi-line String Literals
func tellJoke(name: String, character: Character) { let punchline = name.filter { $0 != character } let n = name.count - punchline.count
let joke = """ Q: Why does \(name) have \(n) \(character)'s in their name? A: I don't know, why does \(name) have \(n) \(character)'s in their name? Q: Because otherwise they'd be called \(punchline). """ print(joke)
tellJoke(name: "Edward Woodward", character: "d")
\t\t\t\t\t\t\t\t
NEW
}
![Page 134: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/134.jpg)
// SE-0168: Multi-line String Literals
func tellJoke(name: String, character: Character) { let punchline = name.filter { $0 != character } let n = name.count - punchline.count
Q: Why does \(name) have \(n) \(character)'s in their name? A: I don't know, why does \(name) have \(n) \(character)'s in their name? Q: Because otherwise they'd be called \(punchline).
tellJoke(name: "Edward Woodward", character: "d")
NEW
![Page 135: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/135.jpg)
// SE-0168: Multi-line String Literals
func tellJoke(name: String, character: Character) { let punchline = name.filter { $0 != character } let n = name.count - punchline.count
Q: Why does \(name) have \(n) \(character)'s in their name? A: I don't know, why does \(name) have \(n) \(character)'s in their name? Q: Because otherwise they'd be called \(punchline).
tellJoke(name: "Edward Woodward", character: "d")
NEW
![Page 136: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/136.jpg)
•New Generics Features
![Page 137: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/137.jpg)
// Extending Sequence
![Page 138: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/138.jpg)
// Extending Sequence
!contains { $0 != value }
![Page 139: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/139.jpg)
// Extending Sequence
extension Sequence where Iterator.Element: Equatable { func containsOnly(_ value: (Iterator.Element)->Bool) -> Bool { return } }
!contains { $0 != value }
mySequence.containsOnly(5)
![Page 140: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/140.jpg)
// Extending Sequence
extension Sequence where Iterator.Element: Equatable { func containsOnly(_ value: (Iterator.Element)->Bool) -> Bool { return } }
!contains { $0 != value }
mySequence.containsOnly(5)
![Page 141: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/141.jpg)
// Extending Sequence
extension Sequence where Element: Equatable { func containsOnly(_ value: (Element)->Bool) -> Bool { return } }
![Page 142: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/142.jpg)
// Swift 3 Sequence
protocol Sequence {
associatedtype Iterator: IteratorProtocol
func makeIterator() -> Iterator }
protocol IteratorProtocol { associatedtype Element
mutating func next() -> Element? }
![Page 143: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/143.jpg)
// Swift 3 Sequence
protocol Sequence {
associatedtype Iterator: IteratorProtocol
func makeIterator() -> Iterator }
protocol IteratorProtocol { associatedtype Element
mutating func next() -> Element? }
![Page 144: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/144.jpg)
// SE-0142: Swift 4 Sequence
protocol Sequence { associatedtype Element associatedtype Iterator: IteratorProtocol where Iterator.Element == Element
func makeIterator() -> Iterator }
protocol IteratorProtocol { associatedtype Element
mutating func next() -> Element? }
![Page 145: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/145.jpg)
// SE-0142: Swift 4 Sequence
protocol Sequence { associatedtype Element associatedtype Iterator: IteratorProtocol where Iterator.Element == Element
func makeIterator() -> Iterator }
protocol IteratorProtocol { associatedtype Element
mutating func next() -> Element? }
![Page 146: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/146.jpg)
// SE-0142: Swift 4 Sequence
protocol Sequence { associatedtype Element associatedtype Iterator: IteratorProtocol where Iterator.Element == Element
func makeIterator() -> Iterator }
protocol IteratorProtocol { associatedtype Element
mutating func next() -> Element? }
![Page 147: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/147.jpg)
// Other important constraints
protocol Sequence { associatedtype SubSequence: Sequence
}
![Page 148: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/148.jpg)
// Other important constraints
protocol Sequence { associatedtype SubSequence: Sequence
}
where SubSequence.SubSequence == SubSequence, SubSequence.Element == Element
![Page 149: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/149.jpg)
// Redundant constraints
extension Collection where Element: Equatable, SubSequence: Collection, SubSequence.SubSequence == SubSequence, SubSequence.Element == Element { func containsOnly(_ x: Element) -> Bool { return isEmpty || (first == x && dropFirst().containsOnly(x)) } }
![Page 150: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/150.jpg)
// Redundant constraints
extension Collection where Element: Equatable, SubSequence: Collection, SubSequence.SubSequence == SubSequence, SubSequence.Element == Element { func containsOnly(_ x: Element) -> Bool { return isEmpty || (first == x && dropFirst().containsOnly(x)) } }
warning: redundant same-type constraint 'Self.Element' == 'Self.SubSequence.Element'
![Page 151: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/151.jpg)
// Redundant constraints
extension Collection where Element: Equatable, SubSequence: Collection { func containsOnly(_ x: Element) -> Bool { return isEmpty || (first == x && dropFirst().containsOnly(x)) } }
![Page 152: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/152.jpg)
// SE-0148: Generic Subscripts // Use case: partial ranges
let values = "one,two,three..."
var i = values.startIndex while let comma = values[i...].index(of: ",") { if values[i..<comma] == "two" { print("found it!") } i = values.index(after: comma) }
![Page 153: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/153.jpg)
// SE-0148: Generic Subscripts // Use case: partial ranges
let values = "one,two,three..."
var i = values.startIndex while let comma = values[i...].index(of: ",") { if values[i..<comma] == "two" { print("found it!") } i = values.index(after: comma) }
![Page 154: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/154.jpg)
// RangeExpression
struct PartialRangeFrom<Bound: Comparable> { let lowerBound: Bound }
![Page 155: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/155.jpg)
// RangeExpression
protocol RangeExpression { func relative<C: Collection>(to collection: C) -> Range<Bound> where C.Index == Bound
}
![Page 156: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/156.jpg)
// RangeExpression
extension PartialRangeFrom: RangeExpression { func relative<C: Collection>(to collection: C) -> Range<Bound> where C.Index == Bound { return lowerBound..<collection.endIndex } }
![Page 157: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/157.jpg)
// RangeExpression
extension PartialRangeFrom: RangeExpression { func relative<C: Collection>(to collection: C) -> Range<Bound> where C.Index == Bound { return lowerBound..<collection.endIndex } }
![Page 158: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/158.jpg)
// SE-0148: Generic Subscripts
extension String { subscript<R: RangeExpression>(range: R) -> Substring where R.Bound == Index { return self[range.relative(to: self)] } }
![Page 159: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/159.jpg)
// SE-0148: Generic Subscripts
extension Collection { subscript<R: RangeExpression>(range: R) -> SubSequence where R.Bound == Index { return self[range.relative(to: self)] } }
![Page 160: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/160.jpg)
More Standard Library Features
SE-0104 Protocol-oriented integers
SE-0153 Dictionary & Set enhancements
SE-0163 Improved String C Interop and Transcoding
SE-0170 NSNumber bridging and Numeric types
SE-0173 Add MutableCollection.swapAt(_:_:)
SE-0174 Change filter to return Self for RangeReplaceableCollection
![Page 161: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/161.jpg)
More Standard Library Features
SE-0104 Protocol-oriented integers
SE-0153 Dictionary & Set enhancements
SE-0163 Improved String C Interop and Transcoding
SE-0170 NSNumber bridging and Numeric types
SE-0173 Add MutableCollection.swapAt(_:_:)
SE-0174 Change filter to return Self for RangeReplaceableCollection
![Page 162: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/162.jpg)
John McCall, Swift Compiler Team
•Exclusive Access to Memory
![Page 163: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/163.jpg)
Ownership
Make it easier to reason about local variables
Enable better programmer optimization
Enable better compiler optimization
Enable powerful new language features
![Page 164: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/164.jpg)
var numbers = [1, 2, 3]
numbers[index] *= 2
// numbers == [2, 4, 6]
for index in numbers.indices {
}
![Page 165: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/165.jpg)
extension MutableCollection { mutating func modifyEach(_ body : (inout Element) -> ()) { for index in body(&self[index]) } } }
var numbers = [1, 2, 3] numbers.modifyEach { element in
} // numbers == [2, 4, 6]
for index in self.indices { body(&self[index])
element *= 2
![Page 166: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/166.jpg)
extension MutableCollection { mutating func modifyEach(_ body : (inout Element) -> ()) { for index in self.indices { body(&self[index]) } } }
![Page 167: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/167.jpg)
extension MutableCollection { mutating func modifyEach(_ body : (inout Element) -> ()) { for index in self.indices { body(&self[index]) } } }
![Page 168: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/168.jpg)
extension MutableCollection { mutating func modifyEach(_ body : (inout Element) -> ()) { for index in self.indices { body(&self[index]) } } }
![Page 169: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/169.jpg)
var numbers = [1, 2, 3] numbers.modifyEach { element in element *= 2 } // numbers == [2, 4, 6]
![Page 170: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/170.jpg)
var numbers = [1, 2, 3] numbers.modifyEach { element in element *= 2 numbers.removeLast() } // numbers == ???
![Page 171: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/171.jpg)
extension MutableCollection { mutating func modifyEach(_ body : (inout Element) -> ()) { for index in self.indices { body(&self[index]) } } }
![Page 172: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/172.jpg)
extension MutableCollection { mutating func modifyEach(_ body : (inout Element) -> ()) { var index = self.beginIndex while index != self.endIndex { body(&self[index]) self.formIndex(after: &index) } } }
![Page 173: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/173.jpg)
![Page 174: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/174.jpg)
var numbers = [1, 2, 3] numbers.modifyEach { element in .removeLast() } // numbers == ???
element *= 2numbers
![Page 175: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/175.jpg)
var numbers = [1, 2, 3] numbers.modifyEach { element in = [] }
numberselement *= 2
![Page 176: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/176.jpg)
var numbers = [1, 2, 3] numbers.modifyEach { element in numbers = [] element *= 2 }
![Page 177: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/177.jpg)
var numbers 32
var numbers = [1, 2, 3] numbers.modifyEach { element in numbers = [] element *= 2 }
1
![Page 178: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/178.jpg)
var numbers 32
inout element
var numbers = [1, 2, 3] numbers.modifyEach { element in numbers = [] element *= 2 }
1
![Page 179: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/179.jpg)
var numbers 32
inout element
var numbers = [1, 2, 3] numbers.modifyEach { element in numbers = [] element *= 2 }
1
![Page 180: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/180.jpg)
var numbers
32
inout element
var numbers = [1, 2, 3] numbers.modifyEach { element in numbers = [] element *= 2 }
1
![Page 181: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/181.jpg)
var numbers
32
inout element
?
var numbers = [1, 2, 3] numbers.modifyEach { element in numbers = [] element *= 2 }
1
![Page 182: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/182.jpg)
Non-Exclusive Access to Memory
Hard to reason about
Creates corner cases
Performance problems for libraries
Performance problems for the compiler
![Page 183: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/183.jpg)
SE-0176: Enforcing Exclusive Access to Memory
Read + Read Read + Write Write + Write
NEW
![Page 184: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/184.jpg)
var numbers = [1, 2, 3] numbers.modifyEach { element in element *= 2 numbers.removeLast() } // numbers == ???
![Page 185: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/185.jpg)
var numbers = [1, 2, 3] numbers.modifyEach { element in element *= 2 numbers.removeLast() } // numbers == ???
![Page 186: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/186.jpg)
var numbers = [1, 2, 3] numbers.modifyEach { element in element *= 2 numbers.removeLast() } // numbers == ???
error: simultaneous accesses, but initialization requires exclusive access
![Page 187: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/187.jpg)
Run-time Enforcement
Global variables
Properties of classes
Local variables captured in escaping closures
![Page 188: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/188.jpg)
var numbers = [1, 2, 3] numbers.modifyEach { element in element *= 2 numbers.removeLast() }
numbers.modifyEach { element in element *= 2 numbers.removeLast() }
var numbers = [1, 2, 3]
![Page 189: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/189.jpg)
numbers.modifyEach { element in element *= 2 numbers.removeLast() }
var numbers = [1, 2, 3]
![Page 190: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/190.jpg)
class MyNumbers { var numbers = [1, 2, 3] func double(other: MyNumbers) { other.numbers.modifyEach { element in element *= 2 self.numbers.removeLast() } } }
![Page 191: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/191.jpg)
class MyNumbers { var numbers = [1, 2, 3] func double(other: MyNumbers) { other.numbers.modifyEach { element in element *= 2 self.numbers.removeLast() } } }
![Page 192: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/192.jpg)
class MyNumbers { var numbers = [1, 2, 3] func double(other: MyNumbers) { other.numbers.modifyEach { element in element *= 2 self.numbers.removeLast() } } }
![Page 193: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/193.jpg)
class MyNumbers { var numbers = [1, 2, 3] func double(other: MyNumbers) { other.numbers.modifyEach { element in element *= 2 self.numbers.removeLast() } } }
Simultaneous accesses to 0x1105ac070, but modifications require exclusive access. Fatal access conflict detected.
![Page 194: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/194.jpg)
Multi-threaded Enforcement
Default enforcement only catches single-threaded bugs
Thread Sanitizer catches multi-threaded bugs
Finding Bugs Using Xcode Runtime Tools Executive Ballroom Wednesday 5:10PM
![Page 195: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/195.jpg)
Swift 3 Compatibility
Just a warning in Swift 3.2
Will be an error in later releases
![Page 196: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/196.jpg)
Taking Advantage of Exclusive Access
More reliable performance
Lots of optimization • In libraries • In the compiler • In your code
New language opportunities
https://github.com/apple/swift/blob/master/docs/OwnershipManifesto.md
![Page 197: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/197.jpg)
Enforcement in the Developer Preview
Read the release notes
Compile-time enforcement: • Enabled by default
Run-time enforcement: • Disabled by default • Off in optimized builds
![Page 198: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/198.jpg)
What's New in Swift
Language refinements and additions
Source compatibility
Tools and performance
Standard library
Exclusive access to memory
![Page 199: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/199.jpg)
Related Sessions
What's New In Foundation Hall 2 Wednesday 11:00AM
Finding Bugs Using Xcode Runtime Tools Executive Ballroom Wednesday 5:10PM
What’s New in Swift Playgrounds Hall 3 Thursday 10:00AM
Understanding Undefined Behavior Grand Ballroom B Thursday 9:00AM
What's New in LLVM Hall 2 Thursday 4:10PM
Efficient Interactions with Frameworks Hall 2 Friday 1:50PM
Understanding Swift Performance WWDC 2016
![Page 200: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/200.jpg)
Labs
Swift Open Hours Technology Lab E Tue 3:10PM–6:00PM
Swift Open Hours Technology Lab E Wed 9:00AM–12:00PM
Swift Open Hours Technology Lab K Thu 9:00AM–11:00AM
Swift Open Hours Technology Lab D Fri 12:00PM–1:30PM
![Page 201: Doug Gregor, Swift Compiler Team Ben Cohen, Swift ... always possible: unspecialized generic code is also important Stack allocation of generic buffers •Smaller Binaries // Unused](https://reader031.fdocuments.in/reader031/viewer/2022030507/5ab638667f8b9a86428d7eaf/html5/thumbnails/201.jpg)