SlideShare a Scribd company logo
Swift under the hood:
Method Dispatching
VLAHO POLUTA
01METHOD DISPATCHING
Method dispatch is the algorithm used to
decide which method should be invoked in
response to a message.
[RECEIVER MESSAGE]
objc_msgSend(receiver, selector)
STATIC DISPATCH
Decided at Compile-time
DYNAMIC DISPATCH
Decided at Run-time
02C++
STATIC DISPATCH
class AnimalStatic
{
public:
void eat() { std::out << “I’m eating food.”; }
}
DYNAMIC DISPATCH
class AnimalVirtual
{
public:
virtual void eat() { std::out << “I’m eating generic food.”; }
}
VTABLE IN C
typedef struct {
PTRFUN* vTablePtr;
const char *x;
const char *y;
} Animal;
receiver→vTable[2]()
03OBJECTIVE C
objc_msgSend(receiver, selector)
struct objc_class {
struct objc_class *isa;
struct objc_class *super_class;
const char *name;
long version;
long info;
long instance_size;
struct objc_ivar_list *ivars;
struct objc_method_list **methodLists;
struct objc_cache *cache;
struct objc_protocol_list *protocols;
};
typedef struct objc_method *Method;
struct objc_method {
SEL method_name;
char *method_types;
IMP method_imp;
};
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
typedef struct objc_cache * Cache;
#define CACHE_BUCKET_NAME(B) ((B)->method_name)
#define CACHE_BUCKET_IMP(B) ((B)->method_imp)
#define CACHE_BUCKET_VALID(B) (B)
#define CACHE_HASH(sel, mask) (((uarith_t)(sel)>>2) & (mask))
struct objc_cache {
unsigned int mask; /* total = mask + 1 */
unsigned int occupied;
Method buckets[1];
};
04SWIFT
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
STATIC DISPATCH
class AnimalStatic {
final func eat() {
print("I’m eating food.")
}
}
final class AnimalStatic {
func eat() {
print("I’m eating food.")
}
}
Swift can generate Obj-C classes and
use runtime
Swift classes are Obj-C classes
NEW ROOT CLASS
SwiftObject
Conforms to the NSObjectProtocol
methodLists
vTable
05WHEN IN DOUBT,
CMD+R!
class AddNameHereClass {
func addSubOne(num: Int) -> Int {/*…*/}
}
let addNameClass = AddNameHereClass()
TEST
let startTime = NSDate()
addNameClass.addSubOne(test) x50000 times
print( NSDate().timeIntervalSinceDate(startTime))
@objc class ObjcClass: NSObject {
func addSubOne(num: Int) -> Int {/*…*/}
}
let objcClass = ObjcClass()
0.0056279…
@objc class ObjcClass: NSObject {
func addSubOne(num: Int) -> Int {/*…*/}
}
let objcClass = ObjcClass()
0.0056279…
class SwiftClass {
func addSubOne(num: Int) -> Int {/*…*/}
}
let swiftClass = SwiftClass()
0.0014960…
@objc class ObjcClass: NSObject {
func addSubOne(num: Int) -> Int {/*…*/}
}
let objcClass = ObjcClass()
0.0056279…
class SwiftClass {
func addSubOne(num: Int) -> Int {/*…*/}
}
let swiftClass = SwiftClass()
0.0014960…
class FinalClass {
final func addSubOne(num: Int) -> Int {/*…*/}
}
let finalClass = FinalClass()
0.0013079…
@objc class ObjcClass: NSObject {
func addSubOne(num: Int) -> Int {/*…*/}
}
let objcClass = ObjcClass()
0.0056279…
class SwiftClass {
func addSubOne(num: Int) -> Int {/*…*/}
}
let swiftClass = SwiftClass()
0.0014960…
class FinalClass {
final func addSubOne(num: Int) -> Int {/*…*/}
}
let finalClass = FinalClass()
0.0013079…
06CONCLUSION
SWIFT
As fast or faster than Obj-C
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta

More Related Content

PDF
JavaScript Looping Statements
PDF
Unity Programing on Boo
PPTX
Hello scala
PDF
DevoxxPL: JRebel Under The Covers
PPT
NS2: Binding C++ and OTcl variables
PDF
Is your profiler speaking the same language as you? -- Docklands JUG
PDF
Spock: A Highly Logical Way To Test
PDF
Ns2: Introduction - Part I
JavaScript Looping Statements
Unity Programing on Boo
Hello scala
DevoxxPL: JRebel Under The Covers
NS2: Binding C++ and OTcl variables
Is your profiler speaking the same language as you? -- Docklands JUG
Spock: A Highly Logical Way To Test
Ns2: Introduction - Part I

What's hot (20)

PDF
Ns2: OTCL - PArt II
PDF
G*におけるソフトウェアテスト・シーズンIII
DOCX
Ns2programs
PPTX
Beirut Java User Group JVM presentation
PDF
Active records before_type_cast
PDF
Grails/Groovyによる開発事例紹介
PDF
Cascadia.js: Don't Cross the Streams
PDF
NS2: AWK and GNUplot - PArt III
PPTX
Reacting with ReactiveUI
ODP
DRb at the Ruby Drink-up of Sophia, December 2011
PPTX
Functional Reactive Programming with RxJS
KEY
Grand Central Dispatch Design Patterns
PDF
Kirk Shoop, Reactive programming in C++
PPT
Ggug spock
PPT
20100712-OTcl Command -- Getting Started
PDF
Jenkins 2を使った究極のpipeline ~ 明日もう一度来てください、本物のpipelineをお見せしますよ ~
ODP
Clojure made really really simple
PDF
Counter Wars (JEEConf 2016)
PPT
iOS Development with Blocks
PDF
devday2012
Ns2: OTCL - PArt II
G*におけるソフトウェアテスト・シーズンIII
Ns2programs
Beirut Java User Group JVM presentation
Active records before_type_cast
Grails/Groovyによる開発事例紹介
Cascadia.js: Don't Cross the Streams
NS2: AWK and GNUplot - PArt III
Reacting with ReactiveUI
DRb at the Ruby Drink-up of Sophia, December 2011
Functional Reactive Programming with RxJS
Grand Central Dispatch Design Patterns
Kirk Shoop, Reactive programming in C++
Ggug spock
20100712-OTcl Command -- Getting Started
Jenkins 2を使った究極のpipeline ~ 明日もう一度来てください、本物のpipelineをお見せしますよ ~
Clojure made really really simple
Counter Wars (JEEConf 2016)
iOS Development with Blocks
devday2012
Ad

Viewers also liked (19)

PDF
September2011aftma
PDF
Swift - Under the Hood
PPTX
Swift distributed tracing method and tools v2
PDF
Let's Learn Ruby - Basic
PDF
Control review for iOS
KEY
Action Controller Overview, Season 1
PPT
Ruby on Rails testing with Rspec
PPT
jQuery For Beginners - jQuery Conference 2009
PDF
Learning jQuery in 30 minutes
PDF
A swift introduction to Swift
PPTX
Web application architecture
PPT
Introduction to html
PPT
Boost your influence on Klout!
PPTX
Ecosystem
PPTX
Black and White Fashion by Vadim Stein
PDF
4ta psico jueves medidas de dispersion
PPTX
K-10714 ABHISHEK(TQM )
PPTX
Biotec Presentation Linkedin
DOCX
Impacto de las tendencias informaticas
September2011aftma
Swift - Under the Hood
Swift distributed tracing method and tools v2
Let's Learn Ruby - Basic
Control review for iOS
Action Controller Overview, Season 1
Ruby on Rails testing with Rspec
jQuery For Beginners - jQuery Conference 2009
Learning jQuery in 30 minutes
A swift introduction to Swift
Web application architecture
Introduction to html
Boost your influence on Klout!
Ecosystem
Black and White Fashion by Vadim Stein
4ta psico jueves medidas de dispersion
K-10714 ABHISHEK(TQM )
Biotec Presentation Linkedin
Impacto de las tendencias informaticas
Ad

Similar to Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta (20)

PPTX
How to add an optimization for C# to RyuJIT
PDF
Objective-C Runtime overview
KEY
Runtime
PPTX
Java byte code in practice
PDF
Swift - One step forward from Obj-C
PDF
Java Performance Puzzlers
PDF
Pavel kravchenko obj c runtime
PDF
Tdd iPhone For Dummies
PDF
Java Concurrency Idioms
PPTX
Binary patching for fun and profit @ JUG.ru, 25.02.2012
PPT
Learning Java 1 – Introduction
PPTX
05. Java Loops Methods and Classes
PDF
ádfasdfasdfasdfasdfasdfsadfsadfasdfasfasdfasdfasdfa
PDF
Live Updating Swift Code
PPT
Thread
DOCX
JAVAPGMS.docx
PPTX
from java to c
PPTX
Grand Central Dispatch in Objective-C
PPT
Java Concepts
PDF
Слава Бобик «NancyFx для самых маленьких»
How to add an optimization for C# to RyuJIT
Objective-C Runtime overview
Runtime
Java byte code in practice
Swift - One step forward from Obj-C
Java Performance Puzzlers
Pavel kravchenko obj c runtime
Tdd iPhone For Dummies
Java Concurrency Idioms
Binary patching for fun and profit @ JUG.ru, 25.02.2012
Learning Java 1 – Introduction
05. Java Loops Methods and Classes
ádfasdfasdfasdfasdfasdfsadfsadfasdfasfasdfasdfasdfa
Live Updating Swift Code
Thread
JAVAPGMS.docx
from java to c
Grand Central Dispatch in Objective-C
Java Concepts
Слава Бобик «NancyFx для самых маленьких»

More from Infinum (20)

PDF
Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
PDF
Infinum Android Talks #20 - DiffUtil
PDF
Infinum Android Talks #20 - Benefits of using Kotlin
PDF
Infinum iOS Talks #4 - Making our VIPER more reactive
PDF
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
PDF
Infinum Android Talks #13 - Using ViewDragHelper
PDF
Infinum Android Talks #14 - Log4j
PDF
Infinum Android Talks #9 - Making your app location-aware
PDF
Infinum Android Talks #14 - Gradle plugins
PDF
Infinum Android Talks #14 - Facebook for Android API
PDF
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
PDF
Infinum Android Talks #18 - Create fun lists by Ivan Marić
PDF
Infinum Android Talks #18 - In-app billing by Ivan Marić
PDF
Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
PDF
Infinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
PDF
Infinum iOS Talks #2 - Xamarin by Ivan Đikić
PDF
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
PDF
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
PDF
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
PDF
Infinum Android Talks #17 - A quest for WebSockets by Zeljko Plesac
Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
Infinum Android Talks #20 - DiffUtil
Infinum Android Talks #20 - Benefits of using Kotlin
Infinum iOS Talks #4 - Making our VIPER more reactive
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
Infinum Android Talks #13 - Using ViewDragHelper
Infinum Android Talks #14 - Log4j
Infinum Android Talks #9 - Making your app location-aware
Infinum Android Talks #14 - Gradle plugins
Infinum Android Talks #14 - Facebook for Android API
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
Infinum Android Talks #18 - Create fun lists by Ivan Marić
Infinum Android Talks #18 - In-app billing by Ivan Marić
Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
Infinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
Infinum iOS Talks #2 - Xamarin by Ivan Đikić
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - A quest for WebSockets by Zeljko Plesac

Recently uploaded (20)

PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
medical staffing services at VALiNTRY
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Nekopoi APK 2025 free lastest update
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
history of c programming in notes for students .pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
AI in Product Development-omnex systems
PDF
System and Network Administraation Chapter 3
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPT
Introduction Database Management System for Course Database
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
ManageIQ - Sprint 268 Review - Slide Deck
medical staffing services at VALiNTRY
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Nekopoi APK 2025 free lastest update
ISO 45001 Occupational Health and Safety Management System
history of c programming in notes for students .pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
Odoo POS Development Services by CandidRoot Solutions
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Design an Analysis of Algorithms II-SECS-1021-03
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
L1 - Introduction to python Backend.pptx
AI in Product Development-omnex systems
System and Network Administraation Chapter 3
Softaken Excel to vCard Converter Software.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Introduction Database Management System for Course Database

Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta