iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS...
Transcript of iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS...
![Page 1: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/1.jpg)
iOS Performance and Concurrency
Patrick Thomson
![Page 2: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/2.jpg)
Performance Matters
• iOS devices are resource-constrained
• Users will notice performance issues
• The deciding factor between a good and an awful app
![Page 3: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/3.jpg)
Demo
![Page 4: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/4.jpg)
Obligatory Documentation Mention
“Concurrency Programming Guide”
![Page 5: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/5.jpg)
Why is This Slow?
Draw graphics
Receive input
Handle input
run loop
![Page 6: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/6.jpg)
Draw windows and process image data
Receive input
Handle input
![Page 7: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/7.jpg)
Process data
Draw windows
Receive input
Handle input
![Page 8: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/8.jpg)
Concurrency is Tricky
• Order of execution varies
• Bugs may appear and disappear at random
• Coordinating tasks and stateful data is hard
• Ergo, writing correct concurrent code is complicated
![Page 9: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/9.jpg)
Deadlock
![Page 10: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/10.jpg)
Nondeterministic Execution
Core 2Core 1
![Page 11: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/11.jpg)
Nondeterministic Execution
Core 2Core 1
NSLog(@"hi");NSLog(@"bye");
![Page 12: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/12.jpg)
Nondeterministic Execution
Core 2Core 1NSLog(@"hi");NSLog(@"bye");
![Page 13: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/13.jpg)
Nondeterministic Execution
Core 2Core 1
![Page 14: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/14.jpg)
Race Conditions
if (flag == NO) { flag = YES; launchMissiles();}
![Page 15: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/15.jpg)
Race Conditions
if (flag == NO) { flag = YES; launchMissiles();}
![Page 16: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/16.jpg)
Race Conditions
if (flag == NO) { flag = YES; launchMissiles();}
![Page 17: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/17.jpg)
Race Conditions
if (flag == NO) { flag = YES; launchMissiles();}
![Page 18: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/18.jpg)
Race Conditions
if (flag == NO) { flag = YES; launchMissiles();}
![Page 19: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/19.jpg)
Race Conditions
if (flag == NO) { flag = YES; launchMissiles();}
![Page 20: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/20.jpg)
iOS Concurrency Tools
• iOS is an amalgam of many historical technologies, and it shows
• Too many options
• Luckily, you generally only need one
![Page 21: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/21.jpg)
Use Grand Central Dispatch!
Always, always reach for GCD first!
![Page 22: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/22.jpg)
So What is GCD?
• Set of concurrency primitives
• Based around asynchronicity
• Adjusts itself to leverage the capabilities of the devices it’s running on
![Page 23: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/23.jpg)
Why Use GCD?
• Transparently manages the minutiae of threading details
• Works in C, Objective-C, and C++
• Built on simple, easy-to-understand concepts
• Gorgeous and simple API
• Absurdly fast
![Page 24: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/24.jpg)
How it Works
Thread
![Page 25: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/25.jpg)
How it Works
Thread
Task
![Page 26: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/26.jpg)
How it Works
Thread
Task Task Task
![Page 27: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/27.jpg)
How it Works
Thread
Task Task Task
Thread
![Page 28: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/28.jpg)
How it Works
Thread
Task Task Task Task
Thread
![Page 29: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/29.jpg)
How it Works
Thread Thread
Task Task Task Task
Thread
![Page 30: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/30.jpg)
How it Works
Thread Thread
Task Task
Thread
![Page 31: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/31.jpg)
How it Works
Thread Thread
Task Task
![Page 32: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/32.jpg)
How it Works
Thread Thread
![Page 33: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/33.jpg)
How it Works
Thread
![Page 34: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/34.jpg)
Tasks
• Implemented in terms of C blocks
• Fundamental units of work
• Tasks have no return value: if you want any computed values to persist, you must do so within the task
^{ NSLog(@"Hello!"); sleep(2); NSLog(@"Goodbye!");}
![Page 35: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/35.jpg)
Serial Queues
• Execute tasks one at a time
• First-in, first-out order
• Very lightweight (~160 bytes)
• Can replace locks and mutexes
Queue
Task Task Task Task
![Page 36: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/36.jpg)
Serial Queues
• Execute tasks one at a time
• First-in, first-out order
• Very lightweight (~160 bytes)
• Can replace locks and mutexes
Queue
![Page 37: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/37.jpg)
'a' 'b' 'c'
![Page 38: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/38.jpg)
'a' 'b' 'c'
![Page 39: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/39.jpg)
'a' 'b' 'c'
![Page 40: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/40.jpg)
'a' 'b' 'c'
![Page 41: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/41.jpg)
Queue
'a' 'b' 'c'
![Page 42: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/42.jpg)
Queue
'a' 'b' 'c'
![Page 43: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/43.jpg)
Queue
'a' 'b' 'c'
![Page 44: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/44.jpg)
Queue
'a' 'b' 'c'
![Page 45: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/45.jpg)
dispatch_queue_t ourQueue;ourQueue = dispatch_queue_create("edu.cmu.dataviz.example", NULL);
dispatch_queue_t dispatch_queue_create(const char *label, dispatch_queue_attr_t attrs);
![Page 46: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/46.jpg)
Enqueuing Tasks
• dispatch_async(queue, task) schedules task for execution on queue and returns immediately
• dispatch_sync(queue, task) schedules task on queue and does not return until task completes.
![Page 47: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/47.jpg)
dispatch_queue_t queue = dispatch_queue_create("edu.cmu.dataviz.example", NULL);
dispatch_async(queue, ^{ NSLog(@"Task beginning execution..."); sleep(2); NSLog(@"Task ending...");});
dispatch_release(queue);
![Page 48: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/48.jpg)
dispatch_queue_t queue = dispatch_queue_create("edu.cmu.dataviz.example", NULL);
dispatch_async(queue, ^{ NSLog(@"Task beginning execution..."); sleep(2); NSLog(@"Task ending...");});
dispatch_release(queue);
don't actually call sleep()
![Page 49: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/49.jpg)
Retain and Release
Any dispatch object you create must be managed with dispatch_retain and dispatch_release.
dispatch_queue_t ourQueue;ourQueue = dispatch_queue_create("edu.cmu.dataviz.example", NULL);
// blah blah blah
dispatch_release(ourQueue);
![Page 50: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/50.jpg)
Concurrent Queues
• May execute tasks simultaneously
• No guarantee as to the order of task execution
• Use your own or the provided app-wide concurrent queues
Queue
Task Task Task Task
![Page 51: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/51.jpg)
Concurrent Queues
• May execute tasks simultaneously
• No guarantee as to the order of task execution
• Use your own or the provided app-wide concurrent queues
Queue
![Page 52: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/52.jpg)
Queue Priorities
DISPATCH_QUEUE_PRIORITY_HIGHDISPATCH_QUEUE_PRIORITY_DEFAULTDISPATCH_QUEUE_PRIORITY_LOW
DISPATCH_QUEUE_PRIORITY_BACKGROUND
![Page 53: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/53.jpg)
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, NULL);
dispatch_queue_create("edu.cmu.example", DISPATCH_QUEUE_CONCURRENT);
versus
![Page 54: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/54.jpg)
Main Queue
• Associated with the run loop
• Useful for drawing, UI updating, thread-unsafe frameworks
• Avoid it if possible
Draw graphics
Receive input
Handle input
![Page 55: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/55.jpg)
Main Queue
• Associated with the run loop
• Useful for drawing, UI updating, thread-unsafe frameworks
• Avoid it if possible
Draw graphics
Receive input
Handle input
Main Queue
![Page 56: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/56.jpg)
Coordinating Tasks
Queue
Task Task Task Task
0
• Often one dispatches asynchronous work and then waits for its completion
• Groups count when tasks enter and exit for a queue
• Can wait until all tasks are done and execute a block at that point
![Page 57: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/57.jpg)
Coordinating Tasks
Queue
Task
Task Task Task
1
• Often one dispatches asynchronous work and then waits for its completion
• Groups count when tasks enter and exit for a queue
• Can wait until all tasks are done and execute a block at that point
![Page 58: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/58.jpg)
Coordinating Tasks
Queue
0
• Often one dispatches asynchronous work and then waits for its completion
• Groups count when tasks enter and exit for a queue
• Can wait until all tasks are done and execute a block at that point
![Page 59: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/59.jpg)
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, NULL);dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{ /* work goes here */ });
dispatch_group_async(group, queue, ^{ /* more work */ });
dispatch_group_async(group, queue, ^{ /* even more work! */ });
dispatch_group_notify(group, ^{ /* executes when all work is done */ });
// wait until all the work is done
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
![Page 60: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/60.jpg)
Enumeration
• Spawn tasks asynchronously and wait until each completes
• For loops: dispatch_apply
• Foreach loops: enumerateObjectsWithOptions:usingBlock: with NSEnumerationConcurrent
dispatch_apply(size_t iterations, dispatch_queue_t queue, void (^block)(size_t));
![Page 61: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/61.jpg)
Doing Things Once
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ launchMissiles(); });
![Page 62: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/62.jpg)
Measuring Time
dispatch_time_t dispatch_time(dispatch_time_t base, uint64_t nanoseconds);
dispatch_time_t DISPATCH_TIME_NOW;dispatch_time_t DISPATCH_TIME_FOREVER;
![Page 63: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/63.jpg)
dispatch_after()
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, NULL);
double delayInSeconds = 2.0;dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(delay, queue, ^{ NSLog(@"Hello, from two seconds in the future!");});
![Page 64: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/64.jpg)
Suspending and Resuming
void dispatch_suspend(dispatch_object_t object);void dispatch_resume(dispatch_object_t object);
You can prevent queues from executing blocks by suspending them.
These do not suspend the execution of any running tasks.
![Page 65: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/65.jpg)
Things We Didn’t Cover
• Sources: monitor the file system, running processes, signals and timers, and execute blocks appropriately
• Dispatch I/O: highly efficient, adaptable, non-blocking I/O
• Barriers: enforce invariants on your concurrent queues
• Semaphores: counted, multicore safe, faster than POSIX
![Page 66: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/66.jpg)
“Grand Central Dispatch Reference”
![Page 67: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/67.jpg)
Tips, Best Practices, and Hints
![Page 68: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/68.jpg)
Plan Ahead
• Factor your work into its most discrete units
• Keep it simple
• Instruments is your friend
![Page 69: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/69.jpg)
Favor Immutability
• Avoid sharing mutable data structures
• stick to NSArray, NSDictionary, NSSet rather than their mutable counterparts
• If you absolutely can’t avoid mutable shared state, guard access to it with a serial queue
![Page 70: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/70.jpg)
Working with Graphics
• Don’t be afraid to spawn queues
• Only draw on the main thread
• dispatch_get_main_queue();
![Page 71: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/71.jpg)
Other Concurrency Options
![Page 72: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/72.jpg)
NSTimer
• Performs a given selector repeatedly with a provided interval
• Good for simple repeated invocations
• Doesn’t support blocks
• Supports preemptive cancellation
![Page 73: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/73.jpg)
NSOperationQueue
• An object-oriented take on the notion of queues and tasks
• Precedes GCD; now implemented on top of it
• Heavyweight – requires a lot of subclassing
• Useful if tasks must support cancellation
• Probably won’t need this, outside of API’s such as CMMotionManager
![Page 74: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/74.jpg)
NSThread
• In the past, concurrency meant invoking or subclassing NSThread
• No longer recommended
• Still has a few useful helper functions – [NSThread isMainThread]
![Page 75: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/75.jpg)
performSelector and friends
• - performSelector: withObject: afterDelay:
• - performSelectorOnMainThread: withObject: waitUntilDone:
• - performSelectorInBackground: withObject:
• + cancelPreviousPerformRequestsWithTarget:
![Page 76: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/76.jpg)
Do not use these!
• Since they take selectors, you sacrifice type-checking
• They sweep complexity under the rug, which is a Bad Move in general
• Everything you can do with these methods you can do with GCD
![Page 77: iOS Performance and Concurrency - Apple Inc.a1.phobos.apple.com/us/r30/CobaltPublic/v4/c7/15/8...iOS Performance and Concurrency Patrick Thomson. Performance Matters • iOS devices](https://reader030.fdocuments.us/reader030/viewer/2022040917/5e91864c347e544afe6c27d8/html5/thumbnails/77.jpg)
Questions?