SlideShare a Scribd company logo
Dr Alex Blewitt @alblueSwift 2 Under the Hood
Swift 2 Under the Hood
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
 ▸ About This Talk
• Overview
• Where did Swift come from?
• What makes Swift fast?
• Where is Swift going?
• Alex Blewitt @alblue
• NeXT owner and veteran Objective-C programmer
• Author of Swift Essentials http://swiftessentials.org
Based on Swift 2.1,
the public release in
December 2015
Dr Alex Blewitt @alblueSwift 2 Under the Hood
Where did Swift
come from?
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Pre-history
• Story starts in 1983 with Objective-C
• Created as a Smalltalk like runtime on top of C
• NeXT licensed Objective-C in 1988
• NextStep released in 1989 (and NS prefix)
• Apple bought NeXT in 1996
• OSX Server in 1999
• OSX 10.0 Beta in 2000, released in 2001
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Objective-C
• Originally implemented as a pre-processor for C
• Rewrote Objective-C code as C code
• Enhanced and merged into GCC
• Compiler integrated under GPL
• Runtime libraries open source (and GNUStep)
http://www.opensource.apple.com/source/
objc4/objc4-208/runtime/objc.h
/*
* Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
* objc.h
* Copyright 1988-1996, NeXT Software, Inc.
*/
Dr Alex Blewitt @alblueSwift 2 Under the Hood
Timeline
C
1972
Objective-C
1983
Smalltalk
1972
Objective-C
2.0 2007
C++
1983
C++07
2007
C++11
2011
C++14
2014
LLVM 1.0
2003
Clang 1.0
2009
Swift 1.0
2014
Static dispatch
Dynamic dispatch
Swift 2.1
2015
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
A lot has changed …
• CPU speed has risen for most of the prior decades
• Plateaued about 3GHz for desktops
• Mobile devices still rising; around 1-2GHz today
• More performance has come from more cores
• Most mobiles have dual-core, some have more
• Mobiles tend to be single-socket/single CPU
• Memory has not increased as fast
Dr Alex Blewitt @alblueSwift 2 Under the Hood
CPU speed
"Computer Architecture: A Quantitive Approach"
Copyright (c) 2011, Elsevier Inc
http://booksite.elsevier.com/9780123838728/
[[objc alloc] init]
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Memory latency
• Memory latency is a significant bottleneck
• CPU stores near-level caches for memory
• L1 - per core 64k instruction / 64k data (~1ns)
• L2 - 1-3Mb per CPU (~10ns)
• L3 - 4-8Mb shared with GPU (~50-80ns)
• Main memory 1-2Gb (~180ns)
Numbers based on
the iPhone 6 and
iPhone 6s (A8 and A9)
Core
L1i
L1d
L2
Core
L1i
L1d
L3
Dr Alex Blewitt @alblueSwift 2 Under the Hood
Memory latency
AnandTech review of iPhone 6s
http://www.anandtech.com/show/9686/the-apple-iphone-6s-and-
iphone-6s-plus-review/4
L1 Cache
L2 Cache
L3 Cache
Main memory
Dr Alex Blewitt @alblueSwift 2 Under the Hood
Why Swift?
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Why Swift?
• Language features
• Namespaces/Modules
• Reference or Struct value types
• Functional constructs
• Importantly
• Interoperability with Objective-C
• No undefined behaviour or nasal daemons
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Modules
• Modules provide a namespace and function partition
• Objective-C
• Foundation, UIKit, SpriteKit
• C wrappers
• Dispatch, simd, Darwin
• Swift
• Swift (automatically imported), Builtin
Builtin provides bindings
with native types e.g.
Builtin.Int256
Darwin provides
bindings with native C
libraries e.g. random()
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Types
• Reference types: class (either Swift or Objective-C)
• Value types: struct
• Protocols: provides an interface for values/references
• Extensions: add methods/protocols to existing type
Any
AnyObjectclassstruct
@objc class
NonObjective
CBase
NSObject
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Numeric values
• Numeric values are represented as structs
• Copied by value into arguments
• Structs can inherit protocols and extensions
public struct Int : SignedIntegerType, Comparable {
public var value: Builtin.Int64
public static var max: Int { get }
public static var min: Int { get }
}
public struct UInt: UnsignedIntegerType, Comparable {
public var value: Builtin.Int64
public static var max: Int { get }
public static var min: Int { get }
}
sizeof(Int.self) == 8
sizeof(UInt.self) == 8
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Protocols
• Most methods are defined as protocols on structs
Any
struct
Int8 UInt8
Comparable
Equatable
Int32 UInt32
IntegerType
Signed
IntegerType
Unsigned
IntegerType
Int UInt
typealias Any protocol<>
Dr Alex Blewitt @alblueSwift 2 Under the Hood
What makes
Swift fast?
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Memory optimisation
• Contiguous arrays of data vs objects
• NSArray
• Diverse
• Memory fragmentation
• Limited memory load benefits for locality
• Array<…>
• Iteration is more performant over memory
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Static and Dynamic?
• Static dispatch (used by C, C++, Swift)
• Function calls are known precisely
• Compiler generates call/callq to direct symbol
• Fastest, and allows for optimisations
• Dynamic dispatch (used by Objective-C, Swift)
• Messages are dispatched through objc_msgSend
• Effectively call(cache["methodName"])
Swift can generate
Objective-C classes
and use runtime
Dr Alex Blewitt @alblueSwift 2 Under the Hood
Static Dispatch
a() -> b() -> c()
a b c
Dynamic Dispatch
[a:] -> [b:] -> [c:]
a b c
objc_msgSend objc_msgSend
Optimises
to abc
Cannot be
optimised
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
objc_msgSend
• Every Objective-C message calls objc_msgSend
• Hand tuned assembly – fast, but still overhead
40
50
60
70
80
90
100
110
Leopard Snow Leopard Lion Mountain Lion Mavericks Yosemite
107
104
47 47
44
50
Removal of special-
case GC handling
CPU, registers
(_cmd, self),
energy🔋
Non-pointer isa
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Optimisations
• Most optimisations rely on inlining
• Instead of a() -> b(), have ab() instead
• Reduces function prologue/epilog (stack/reg spill)
• Reduces branch miss and memory jumps
• May unlock peephole optimisations
• func foo(i:Int) {if i<0 {return}…}
• foo(-1) foo(negative) can be
optimised away completely
Increases code size
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Whole Module Optimisation
• Whole Module Optimisation/Link Time Optimisation
• Instead of writing out x86_64 .o files, writes LLVM
• LLVM linker reads all files, optimises
• Can see optimisations where single file cannot
• final methods and data structures can be inlined
• Structs are always final (no subclassing)
• private (same file) internal (same module)
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Swift and LLVM
• Swift and clang are both built on LLVM
• Originally stood for Low Level Virtual Machine
• Family of tools (compiler, debugger, linker etc.)
• Abstract assembly language
• Intermediate Representation (IR), Bitcode (BC)
• Infinite register RISC typed instruction set
• Call and return convention agnostic
Bad name, wasn't
really VMs
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Swift compile pipeline
• AST - Abstract Syntax Tree representation
• Parsed AST - Types resolved
• SIL - Swift Intermediate Language, high-level IR
• Platform agnostic (Builtin.Word abstracts size)
• IR - LLVM Intermediate Representation
• Platform dependencies (e.g. word size)
• Output formats (assembly, bitcode, library output)
Dr Alex Blewitt @alblueSwift 2 Under the Hood
Swift compile pipeline
print("Hello World")
ASTParse Sema AST' SILGen SIL SILOpt IRGen IR LLVM
.o .dylib
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Example C based IR
• The ubiquitous Hello World program…
#include <stdio.h>
int main() {
puts("Hello World")
}
@.str = private unnamed_addr constant [12 x i8] ⤦
c"Hello World00", align 1
define i32 @main() #0 {
%1 = call i32 @puts(i8* getelementptr inbounds
([12 x i8]* @.str, i32 0, i32 0))
ret i32 0
}
clang helloWorld.c -emit-llvm -c -S -o -
Dr Alex Blewitt @alblueSwift 2 Under the Hood
@.str = private unnamed_addr constant [12 x i8] ⤦
c"Hello World00", align 1
define i32 @main() #0 {
%1 = call i32 @puts(i8* getelementptr inbounds
([12 x i8]* @.str, i32 0, i32 0))
ret i32 0
}
clang helloWorld.c -emit-assembly -S -o -
_main
pushq %rbp
movq %rsp, %rbp
leaq L_.str(%rip), %rdi
callq _puts
xorl %eax, %eax
popq %rbp
retq
.section __TEXT
L_.str: ## was @.str
.asciz "Hello World"
stack management
rdi = &L_.str
puts(rdi)
eax = 0
return(eax)
L_.str = "Hello World"
main function
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Advantages of IR
• LLVM IR can still be understood when compiled
• Allows for more accurate transformations
• Inlining across method/function calls
• Elimination of unused code paths
• Optimisation phases that are language agnostic
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Example Swift based IR
• The ubiquitous Hello World program…
print("Hello World")
@0 = private unnamed_addr constant [12 x i8] ⤦
c"Hello World00"
define i32 @main(i32, i8**) {
…
call void
@_TFSs5printFTGSaP__9separatorSS10terminatorSS_T_(
%swift.bridge* %6, i8* %17, i64 %18, i64 %19,
i8* %21, i64 %22, i64 %23)
ret i32 0
}
swiftc helloWorld.swift -emit-ir —o -
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Name Mangling
• Name Mangling is source → assembly identifiers
• C name mangling: main → _main
• C++ name mangling: main → __Z4mainiPPc
• __Z = C++ name
• 4 = 4 characters following for name (main)
• i = int
• PPc = pointer to pointer to char (i.e. char**)
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Swift Name Mangling
• With the Swift symbol
_TFSs5printFTGSaP__9separatorSS10terminatorSS_T_
• _T = Swift symbol
• F = function
• Ss = "Swift" (module, as in Swift.print)
• 5print = "print" (function name)
• TGSaP___ = tuple containing generic array protocol ([protocol<>])
• 9separator = "separator" (argument name)
• SS = Swift.String (special case)
• T_ = empty tuple () (return type)
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Swift Name Mangling
• With the Swift symbol
_TFSs5printFTGSaP__9separatorSS10terminatorSS_T_
• _T = Swift symbol
• F = function
• Ss = "Swift" (module, as in Swift.print)
• 5print = "print" (function name)
• TGSaP___ = tuple containing generic array protocol ([protocol<>])
• 9separator = "separator" (argument name)
• SS = Swift.String (special case)
• T_ = empty tuple () (return type)
$ echo "_TFSs5printFTGSaP__9separatorSS10terminatorSS_T_" |
xcrun swift-demangle
Swift.print ([protocol<>],
separator : Swift.String,
terminator : Swift.String) -> ()
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Swift Intermediate Language
• Similar to IL, but with some Swift specifics
print("Hello World")
sil_stage canonical
import Builtin
import Swift
import SwiftShims
// main
sil @main : $@convention(c) (Int32,
UnsafeMutablePointer<UnsafeMutablePointer<Int8>>) ->
Int32 {
// function_ref Swift.print (Swift.Array<protocol<>>,
separator : Swift.String, terminator : Swift.String) ->
swiftc helloWorld.swift -emit-sil —o -
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Swift vTables
• Method lookup in Swift is like C++ with vTable
class World { func hello() {…} }
sil_stage canonical
import Builtin; import Swift; import SwiftShims
…
sil_vtable World {
// main.World.hello (main.World)() -> ()
#World.hello!1: _TFC4main5World5hellofS0_FT_T_
// main.World.__deallocating_deinit
#World.deinit!deallocator: _TFC4main5WorldD
// main.World.init (main.World.Type)() -> main.World
#World.init!initializer.1: _TFC4main5WorldcfMS0_FT_S0_
}
swiftc helloWorld.swift -emit-sil —o -
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
SIL Inspector
• Allows Swift SIL to be inspected
• Available at GitHub
• https://github.com/alblue/SILInspector
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
SwiftObject and ObjC
• Swift objects can also be used in Objective-C
• Swift instance in memory has an isa pointer
• Objective-C can call Swift code with no changes
• Swift classes have @objc to use dynamic dispatch
• Reduces optimisations
• Automatically applied when using ObjC
• Protocols, Superclasses
Dr Alex Blewitt @alblueSwift 2 Under the Hood
Where is Swift going?
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Is Swift swift yet?
• Is Swift as fast as C?
• Wrong question
• Is Swift as fast, or faster than Objective-C?
• As fast or faster than Objective-C
• Can be faster for data/struct processing
• More optimisation possibilities in future
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Swift
• Being heavily developed – 3 releases in a year
• Provides a transitional mechanism from ObjC
• Existing libraries/frameworks will continue to work
• Can drop down to native calls when necessary
• Used as replacement language in LLDB
• Future of iOS development?
• Future of server-side development?
@alblueDr Alex Blewitt @alblueSwift 2 Under the Hood
Summary
• Swift has a long history coming from LLVM roots
• Prefers static dispatch but also supports objective-c
• Values can be laid out in memory efficiently
• In-lining leads to further optimisations
• Whole-module optimisation will only get better
• Modular compile pipeline allows for optimisations

More Related Content

What's hot

NkSIP: The Erlang SIP application server
NkSIP: The Erlang SIP application serverNkSIP: The Erlang SIP application server
NkSIP: The Erlang SIP application server
Carlos González Florido
 
An Introduction to Twisted
An Introduction to TwistedAn Introduction to Twisted
An Introduction to Twisted
sdsern
 
Tutorial for developing SILOptimizer Pass
Tutorial for developing SILOptimizer PassTutorial for developing SILOptimizer Pass
Tutorial for developing SILOptimizer Pass
秋 勇紀
 
Kubernetes Internals
Kubernetes InternalsKubernetes Internals
Kubernetes Internals
Shimi Bandiel
 
Docker, Atomic Host and Kubernetes.
Docker, Atomic Host and Kubernetes.Docker, Atomic Host and Kubernetes.
Docker, Atomic Host and Kubernetes.
Jooho Lee
 
Low Level Exploits
Low Level ExploitsLow Level Exploits
Low Level Exploitshughpearse
 
Neodev
NeodevNeodev
Project Basecamp: News From Camp 4
Project Basecamp: News From Camp 4Project Basecamp: News From Camp 4
Project Basecamp: News From Camp 4
Digital Bond
 
Intro. to static analysis
Intro. to static analysisIntro. to static analysis
Intro. to static analysis
Chong-Kuan Chen
 
Hello istio
Hello istioHello istio
Hello istio
Jooho Lee
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
Shimi Bandiel
 
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Codemotion
 
Nerves Project Intro to ErlangDC
Nerves Project Intro to ErlangDCNerves Project Intro to ErlangDC
Nerves Project Intro to ErlangDC
Frank Hunleth
 
Exploit techniques and mitigation
Exploit techniques and mitigationExploit techniques and mitigation
Exploit techniques and mitigation
Yaniv Shani
 
From V8 to Modern Compilers
From V8 to Modern CompilersFrom V8 to Modern Compilers
From V8 to Modern Compilers
Min-Yih Hsu
 
RubyStack: the easiest way to deploy Ruby on Rails
RubyStack: the easiest way to deploy Ruby on RailsRubyStack: the easiest way to deploy Ruby on Rails
RubyStack: the easiest way to deploy Ruby on Railselliando dias
 
第2回クラウドネットワーク研究会 「OpenFlowコントローラとその実装」
第2回クラウドネットワーク研究会 「OpenFlowコントローラとその実装」第2回クラウドネットワーク研究会 「OpenFlowコントローラとその実装」
第2回クラウドネットワーク研究会 「OpenFlowコントローラとその実装」Sho Shimizu
 
ESIL - Universal IL (Intermediate Language) for Radare2
ESIL - Universal IL (Intermediate Language) for Radare2ESIL - Universal IL (Intermediate Language) for Radare2
ESIL - Universal IL (Intermediate Language) for Radare2
Anton Kochkov
 
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」
Sho Shimizu
 
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
Hackito Ergo Sum
 

What's hot (20)

NkSIP: The Erlang SIP application server
NkSIP: The Erlang SIP application serverNkSIP: The Erlang SIP application server
NkSIP: The Erlang SIP application server
 
An Introduction to Twisted
An Introduction to TwistedAn Introduction to Twisted
An Introduction to Twisted
 
Tutorial for developing SILOptimizer Pass
Tutorial for developing SILOptimizer PassTutorial for developing SILOptimizer Pass
Tutorial for developing SILOptimizer Pass
 
Kubernetes Internals
Kubernetes InternalsKubernetes Internals
Kubernetes Internals
 
Docker, Atomic Host and Kubernetes.
Docker, Atomic Host and Kubernetes.Docker, Atomic Host and Kubernetes.
Docker, Atomic Host and Kubernetes.
 
Low Level Exploits
Low Level ExploitsLow Level Exploits
Low Level Exploits
 
Neodev
NeodevNeodev
Neodev
 
Project Basecamp: News From Camp 4
Project Basecamp: News From Camp 4Project Basecamp: News From Camp 4
Project Basecamp: News From Camp 4
 
Intro. to static analysis
Intro. to static analysisIntro. to static analysis
Intro. to static analysis
 
Hello istio
Hello istioHello istio
Hello istio
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
 
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
 
Nerves Project Intro to ErlangDC
Nerves Project Intro to ErlangDCNerves Project Intro to ErlangDC
Nerves Project Intro to ErlangDC
 
Exploit techniques and mitigation
Exploit techniques and mitigationExploit techniques and mitigation
Exploit techniques and mitigation
 
From V8 to Modern Compilers
From V8 to Modern CompilersFrom V8 to Modern Compilers
From V8 to Modern Compilers
 
RubyStack: the easiest way to deploy Ruby on Rails
RubyStack: the easiest way to deploy Ruby on RailsRubyStack: the easiest way to deploy Ruby on Rails
RubyStack: the easiest way to deploy Ruby on Rails
 
第2回クラウドネットワーク研究会 「OpenFlowコントローラとその実装」
第2回クラウドネットワーク研究会 「OpenFlowコントローラとその実装」第2回クラウドネットワーク研究会 「OpenFlowコントローラとその実装」
第2回クラウドネットワーク研究会 「OpenFlowコントローラとその実装」
 
ESIL - Universal IL (Intermediate Language) for Radare2
ESIL - Universal IL (Intermediate Language) for Radare2ESIL - Universal IL (Intermediate Language) for Radare2
ESIL - Universal IL (Intermediate Language) for Radare2
 
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」
 
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
 

Similar to Swift 2 Under the Hood - Gotober 2015

Search at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, TwitterSearch at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, Twitter
Lucidworks
 
One Shellcode to Rule Them All: Cross-Platform Exploitation
One Shellcode to Rule Them All: Cross-Platform ExploitationOne Shellcode to Rule Them All: Cross-Platform Exploitation
One Shellcode to Rule Them All: Cross-Platform Exploitation
Quinn Wilton
 
Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCD
Prashant Rane
 
Игорь Фесенко "Direction of C# as a High-Performance Language"
Игорь Фесенко "Direction of C# as a High-Performance Language"Игорь Фесенко "Direction of C# as a High-Performance Language"
Игорь Фесенко "Direction of C# as a High-Performance Language"
Fwdays
 
Using Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systemsUsing Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systemsSerge Stinckwich
 
Infinit filesystem, Reactor reloaded
Infinit filesystem, Reactor reloadedInfinit filesystem, Reactor reloaded
Infinit filesystem, Reactor reloaded
Infinit
 
Embedded Rust on IoT devices
Embedded Rust on IoT devicesEmbedded Rust on IoT devices
Embedded Rust on IoT devices
Lars Gregori
 
Swift Install Workshop - OpenStack Conference Spring 2012
Swift Install Workshop - OpenStack Conference Spring 2012Swift Install Workshop - OpenStack Conference Spring 2012
Swift Install Workshop - OpenStack Conference Spring 2012
Joe Arnold
 
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
Jung Kim
 
Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing Landscape
Kernel TLV
 
stackconf 2022: Cluster Management: Heterogeneous, Lightweight, Safe. Pick Three
stackconf 2022: Cluster Management: Heterogeneous, Lightweight, Safe. Pick Threestackconf 2022: Cluster Management: Heterogeneous, Lightweight, Safe. Pick Three
stackconf 2022: Cluster Management: Heterogeneous, Lightweight, Safe. Pick Three
NETWAYS
 
Brief Introduction to Parallella
Brief Introduction to ParallellaBrief Introduction to Parallella
Brief Introduction to Parallella
Somnath Mazumdar
 
"Making OpenCV Code Run Fast," a Presentation from Intel
"Making OpenCV Code Run Fast," a Presentation from Intel"Making OpenCV Code Run Fast," a Presentation from Intel
"Making OpenCV Code Run Fast," a Presentation from Intel
Edge AI and Vision Alliance
 
DefCon 2012 - Rooting SOHO Routers
DefCon 2012 - Rooting SOHO RoutersDefCon 2012 - Rooting SOHO Routers
DefCon 2012 - Rooting SOHO RoutersMichael Smith
 
A Peek into TFRT
A Peek into TFRTA Peek into TFRT
A Peek into TFRT
Koan-Sin Tan
 
Highly concurrent yet natural programming
Highly concurrent yet natural programmingHighly concurrent yet natural programming
Highly concurrent yet natural programming
Infinit
 
Advanced SOHO Router Exploitation XCON
Advanced SOHO Router Exploitation XCONAdvanced SOHO Router Exploitation XCON
Advanced SOHO Router Exploitation XCON
Lyon Yang
 
Container & kubernetes
Container & kubernetesContainer & kubernetes
Container & kubernetes
Ted Jung
 
Static PIE, How and Why - Metasploit's new POSIX payload: Mettle
Static PIE, How and Why - Metasploit's new POSIX payload: MettleStatic PIE, How and Why - Metasploit's new POSIX payload: Mettle
Static PIE, How and Why - Metasploit's new POSIX payload: Mettle
Brent Cook
 
MattsonTutorialSC14.pdf
MattsonTutorialSC14.pdfMattsonTutorialSC14.pdf
MattsonTutorialSC14.pdf
George Papaioannou
 

Similar to Swift 2 Under the Hood - Gotober 2015 (20)

Search at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, TwitterSearch at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, Twitter
 
One Shellcode to Rule Them All: Cross-Platform Exploitation
One Shellcode to Rule Them All: Cross-Platform ExploitationOne Shellcode to Rule Them All: Cross-Platform Exploitation
One Shellcode to Rule Them All: Cross-Platform Exploitation
 
Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCD
 
Игорь Фесенко "Direction of C# as a High-Performance Language"
Игорь Фесенко "Direction of C# as a High-Performance Language"Игорь Фесенко "Direction of C# as a High-Performance Language"
Игорь Фесенко "Direction of C# as a High-Performance Language"
 
Using Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systemsUsing Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systems
 
Infinit filesystem, Reactor reloaded
Infinit filesystem, Reactor reloadedInfinit filesystem, Reactor reloaded
Infinit filesystem, Reactor reloaded
 
Embedded Rust on IoT devices
Embedded Rust on IoT devicesEmbedded Rust on IoT devices
Embedded Rust on IoT devices
 
Swift Install Workshop - OpenStack Conference Spring 2012
Swift Install Workshop - OpenStack Conference Spring 2012Swift Install Workshop - OpenStack Conference Spring 2012
Swift Install Workshop - OpenStack Conference Spring 2012
 
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
 
Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing Landscape
 
stackconf 2022: Cluster Management: Heterogeneous, Lightweight, Safe. Pick Three
stackconf 2022: Cluster Management: Heterogeneous, Lightweight, Safe. Pick Threestackconf 2022: Cluster Management: Heterogeneous, Lightweight, Safe. Pick Three
stackconf 2022: Cluster Management: Heterogeneous, Lightweight, Safe. Pick Three
 
Brief Introduction to Parallella
Brief Introduction to ParallellaBrief Introduction to Parallella
Brief Introduction to Parallella
 
"Making OpenCV Code Run Fast," a Presentation from Intel
"Making OpenCV Code Run Fast," a Presentation from Intel"Making OpenCV Code Run Fast," a Presentation from Intel
"Making OpenCV Code Run Fast," a Presentation from Intel
 
DefCon 2012 - Rooting SOHO Routers
DefCon 2012 - Rooting SOHO RoutersDefCon 2012 - Rooting SOHO Routers
DefCon 2012 - Rooting SOHO Routers
 
A Peek into TFRT
A Peek into TFRTA Peek into TFRT
A Peek into TFRT
 
Highly concurrent yet natural programming
Highly concurrent yet natural programmingHighly concurrent yet natural programming
Highly concurrent yet natural programming
 
Advanced SOHO Router Exploitation XCON
Advanced SOHO Router Exploitation XCONAdvanced SOHO Router Exploitation XCON
Advanced SOHO Router Exploitation XCON
 
Container & kubernetes
Container & kubernetesContainer & kubernetes
Container & kubernetes
 
Static PIE, How and Why - Metasploit's new POSIX payload: Mettle
Static PIE, How and Why - Metasploit's new POSIX payload: MettleStatic PIE, How and Why - Metasploit's new POSIX payload: Mettle
Static PIE, How and Why - Metasploit's new POSIX payload: Mettle
 
MattsonTutorialSC14.pdf
MattsonTutorialSC14.pdfMattsonTutorialSC14.pdf
MattsonTutorialSC14.pdf
 

Recently uploaded

Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 

Recently uploaded (20)

Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 

Swift 2 Under the Hood - Gotober 2015

  • 1. Dr Alex Blewitt @alblueSwift 2 Under the Hood Swift 2 Under the Hood
  • 2. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood  ▸ About This Talk • Overview • Where did Swift come from? • What makes Swift fast? • Where is Swift going? • Alex Blewitt @alblue • NeXT owner and veteran Objective-C programmer • Author of Swift Essentials http://swiftessentials.org Based on Swift 2.1, the public release in December 2015
  • 3. Dr Alex Blewitt @alblueSwift 2 Under the Hood Where did Swift come from?
  • 4. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Pre-history • Story starts in 1983 with Objective-C • Created as a Smalltalk like runtime on top of C • NeXT licensed Objective-C in 1988 • NextStep released in 1989 (and NS prefix) • Apple bought NeXT in 1996 • OSX Server in 1999 • OSX 10.0 Beta in 2000, released in 2001
  • 5. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Objective-C • Originally implemented as a pre-processor for C • Rewrote Objective-C code as C code • Enhanced and merged into GCC • Compiler integrated under GPL • Runtime libraries open source (and GNUStep) http://www.opensource.apple.com/source/ objc4/objc4-208/runtime/objc.h /* * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. * objc.h * Copyright 1988-1996, NeXT Software, Inc. */
  • 6. Dr Alex Blewitt @alblueSwift 2 Under the Hood Timeline C 1972 Objective-C 1983 Smalltalk 1972 Objective-C 2.0 2007 C++ 1983 C++07 2007 C++11 2011 C++14 2014 LLVM 1.0 2003 Clang 1.0 2009 Swift 1.0 2014 Static dispatch Dynamic dispatch Swift 2.1 2015
  • 7. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood A lot has changed … • CPU speed has risen for most of the prior decades • Plateaued about 3GHz for desktops • Mobile devices still rising; around 1-2GHz today • More performance has come from more cores • Most mobiles have dual-core, some have more • Mobiles tend to be single-socket/single CPU • Memory has not increased as fast
  • 8. Dr Alex Blewitt @alblueSwift 2 Under the Hood CPU speed "Computer Architecture: A Quantitive Approach" Copyright (c) 2011, Elsevier Inc http://booksite.elsevier.com/9780123838728/ [[objc alloc] init]
  • 9. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Memory latency • Memory latency is a significant bottleneck • CPU stores near-level caches for memory • L1 - per core 64k instruction / 64k data (~1ns) • L2 - 1-3Mb per CPU (~10ns) • L3 - 4-8Mb shared with GPU (~50-80ns) • Main memory 1-2Gb (~180ns) Numbers based on the iPhone 6 and iPhone 6s (A8 and A9) Core L1i L1d L2 Core L1i L1d L3
  • 10. Dr Alex Blewitt @alblueSwift 2 Under the Hood Memory latency AnandTech review of iPhone 6s http://www.anandtech.com/show/9686/the-apple-iphone-6s-and- iphone-6s-plus-review/4 L1 Cache L2 Cache L3 Cache Main memory
  • 11. Dr Alex Blewitt @alblueSwift 2 Under the Hood Why Swift?
  • 12. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Why Swift? • Language features • Namespaces/Modules • Reference or Struct value types • Functional constructs • Importantly • Interoperability with Objective-C • No undefined behaviour or nasal daemons
  • 13. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Modules • Modules provide a namespace and function partition • Objective-C • Foundation, UIKit, SpriteKit • C wrappers • Dispatch, simd, Darwin • Swift • Swift (automatically imported), Builtin Builtin provides bindings with native types e.g. Builtin.Int256 Darwin provides bindings with native C libraries e.g. random()
  • 14. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Types • Reference types: class (either Swift or Objective-C) • Value types: struct • Protocols: provides an interface for values/references • Extensions: add methods/protocols to existing type Any AnyObjectclassstruct @objc class NonObjective CBase NSObject
  • 15. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Numeric values • Numeric values are represented as structs • Copied by value into arguments • Structs can inherit protocols and extensions public struct Int : SignedIntegerType, Comparable { public var value: Builtin.Int64 public static var max: Int { get } public static var min: Int { get } } public struct UInt: UnsignedIntegerType, Comparable { public var value: Builtin.Int64 public static var max: Int { get } public static var min: Int { get } } sizeof(Int.self) == 8 sizeof(UInt.self) == 8
  • 16. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Protocols • Most methods are defined as protocols on structs Any struct Int8 UInt8 Comparable Equatable Int32 UInt32 IntegerType Signed IntegerType Unsigned IntegerType Int UInt typealias Any protocol<>
  • 17. Dr Alex Blewitt @alblueSwift 2 Under the Hood What makes Swift fast?
  • 18. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Memory optimisation • Contiguous arrays of data vs objects • NSArray • Diverse • Memory fragmentation • Limited memory load benefits for locality • Array<…> • Iteration is more performant over memory
  • 19. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Static and Dynamic? • Static dispatch (used by C, C++, Swift) • Function calls are known precisely • Compiler generates call/callq to direct symbol • Fastest, and allows for optimisations • Dynamic dispatch (used by Objective-C, Swift) • Messages are dispatched through objc_msgSend • Effectively call(cache["methodName"]) Swift can generate Objective-C classes and use runtime
  • 20. Dr Alex Blewitt @alblueSwift 2 Under the Hood Static Dispatch a() -> b() -> c() a b c Dynamic Dispatch [a:] -> [b:] -> [c:] a b c objc_msgSend objc_msgSend Optimises to abc Cannot be optimised
  • 21. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood objc_msgSend • Every Objective-C message calls objc_msgSend • Hand tuned assembly – fast, but still overhead 40 50 60 70 80 90 100 110 Leopard Snow Leopard Lion Mountain Lion Mavericks Yosemite 107 104 47 47 44 50 Removal of special- case GC handling CPU, registers (_cmd, self), energy🔋 Non-pointer isa
  • 22. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Optimisations • Most optimisations rely on inlining • Instead of a() -> b(), have ab() instead • Reduces function prologue/epilog (stack/reg spill) • Reduces branch miss and memory jumps • May unlock peephole optimisations • func foo(i:Int) {if i<0 {return}…} • foo(-1) foo(negative) can be optimised away completely Increases code size
  • 23. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Whole Module Optimisation • Whole Module Optimisation/Link Time Optimisation • Instead of writing out x86_64 .o files, writes LLVM • LLVM linker reads all files, optimises • Can see optimisations where single file cannot • final methods and data structures can be inlined • Structs are always final (no subclassing) • private (same file) internal (same module)
  • 24. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Swift and LLVM • Swift and clang are both built on LLVM • Originally stood for Low Level Virtual Machine • Family of tools (compiler, debugger, linker etc.) • Abstract assembly language • Intermediate Representation (IR), Bitcode (BC) • Infinite register RISC typed instruction set • Call and return convention agnostic Bad name, wasn't really VMs
  • 25. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Swift compile pipeline • AST - Abstract Syntax Tree representation • Parsed AST - Types resolved • SIL - Swift Intermediate Language, high-level IR • Platform agnostic (Builtin.Word abstracts size) • IR - LLVM Intermediate Representation • Platform dependencies (e.g. word size) • Output formats (assembly, bitcode, library output)
  • 26. Dr Alex Blewitt @alblueSwift 2 Under the Hood Swift compile pipeline print("Hello World") ASTParse Sema AST' SILGen SIL SILOpt IRGen IR LLVM .o .dylib
  • 27. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Example C based IR • The ubiquitous Hello World program… #include <stdio.h> int main() { puts("Hello World") } @.str = private unnamed_addr constant [12 x i8] ⤦ c"Hello World00", align 1 define i32 @main() #0 { %1 = call i32 @puts(i8* getelementptr inbounds ([12 x i8]* @.str, i32 0, i32 0)) ret i32 0 } clang helloWorld.c -emit-llvm -c -S -o -
  • 28. Dr Alex Blewitt @alblueSwift 2 Under the Hood @.str = private unnamed_addr constant [12 x i8] ⤦ c"Hello World00", align 1 define i32 @main() #0 { %1 = call i32 @puts(i8* getelementptr inbounds ([12 x i8]* @.str, i32 0, i32 0)) ret i32 0 } clang helloWorld.c -emit-assembly -S -o - _main pushq %rbp movq %rsp, %rbp leaq L_.str(%rip), %rdi callq _puts xorl %eax, %eax popq %rbp retq .section __TEXT L_.str: ## was @.str .asciz "Hello World" stack management rdi = &L_.str puts(rdi) eax = 0 return(eax) L_.str = "Hello World" main function
  • 29. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Advantages of IR • LLVM IR can still be understood when compiled • Allows for more accurate transformations • Inlining across method/function calls • Elimination of unused code paths • Optimisation phases that are language agnostic
  • 30. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Example Swift based IR • The ubiquitous Hello World program… print("Hello World") @0 = private unnamed_addr constant [12 x i8] ⤦ c"Hello World00" define i32 @main(i32, i8**) { … call void @_TFSs5printFTGSaP__9separatorSS10terminatorSS_T_( %swift.bridge* %6, i8* %17, i64 %18, i64 %19, i8* %21, i64 %22, i64 %23) ret i32 0 } swiftc helloWorld.swift -emit-ir —o -
  • 31. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Name Mangling • Name Mangling is source → assembly identifiers • C name mangling: main → _main • C++ name mangling: main → __Z4mainiPPc • __Z = C++ name • 4 = 4 characters following for name (main) • i = int • PPc = pointer to pointer to char (i.e. char**)
  • 32. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Swift Name Mangling • With the Swift symbol _TFSs5printFTGSaP__9separatorSS10terminatorSS_T_ • _T = Swift symbol • F = function • Ss = "Swift" (module, as in Swift.print) • 5print = "print" (function name) • TGSaP___ = tuple containing generic array protocol ([protocol<>]) • 9separator = "separator" (argument name) • SS = Swift.String (special case) • T_ = empty tuple () (return type)
  • 33. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Swift Name Mangling • With the Swift symbol _TFSs5printFTGSaP__9separatorSS10terminatorSS_T_ • _T = Swift symbol • F = function • Ss = "Swift" (module, as in Swift.print) • 5print = "print" (function name) • TGSaP___ = tuple containing generic array protocol ([protocol<>]) • 9separator = "separator" (argument name) • SS = Swift.String (special case) • T_ = empty tuple () (return type) $ echo "_TFSs5printFTGSaP__9separatorSS10terminatorSS_T_" | xcrun swift-demangle Swift.print ([protocol<>], separator : Swift.String, terminator : Swift.String) -> ()
  • 34. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Swift Intermediate Language • Similar to IL, but with some Swift specifics print("Hello World") sil_stage canonical import Builtin import Swift import SwiftShims // main sil @main : $@convention(c) (Int32, UnsafeMutablePointer<UnsafeMutablePointer<Int8>>) -> Int32 { // function_ref Swift.print (Swift.Array<protocol<>>, separator : Swift.String, terminator : Swift.String) -> swiftc helloWorld.swift -emit-sil —o -
  • 35. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Swift vTables • Method lookup in Swift is like C++ with vTable class World { func hello() {…} } sil_stage canonical import Builtin; import Swift; import SwiftShims … sil_vtable World { // main.World.hello (main.World)() -> () #World.hello!1: _TFC4main5World5hellofS0_FT_T_ // main.World.__deallocating_deinit #World.deinit!deallocator: _TFC4main5WorldD // main.World.init (main.World.Type)() -> main.World #World.init!initializer.1: _TFC4main5WorldcfMS0_FT_S0_ } swiftc helloWorld.swift -emit-sil —o -
  • 36. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood SIL Inspector • Allows Swift SIL to be inspected • Available at GitHub • https://github.com/alblue/SILInspector
  • 37. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood SwiftObject and ObjC • Swift objects can also be used in Objective-C • Swift instance in memory has an isa pointer • Objective-C can call Swift code with no changes • Swift classes have @objc to use dynamic dispatch • Reduces optimisations • Automatically applied when using ObjC • Protocols, Superclasses
  • 38. Dr Alex Blewitt @alblueSwift 2 Under the Hood Where is Swift going?
  • 39. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Is Swift swift yet? • Is Swift as fast as C? • Wrong question • Is Swift as fast, or faster than Objective-C? • As fast or faster than Objective-C • Can be faster for data/struct processing • More optimisation possibilities in future
  • 40. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Swift • Being heavily developed – 3 releases in a year • Provides a transitional mechanism from ObjC • Existing libraries/frameworks will continue to work • Can drop down to native calls when necessary • Used as replacement language in LLDB • Future of iOS development? • Future of server-side development?
  • 41. @alblueDr Alex Blewitt @alblueSwift 2 Under the Hood Summary • Swift has a long history coming from LLVM roots • Prefers static dispatch but also supports objective-c • Values can be laid out in memory efficiently • In-lining leads to further optimisations • Whole-module optimisation will only get better • Modular compile pipeline allows for optimisations