77
iOS Performance and Concurrency Patrick Thomson

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

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

'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

'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

'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

'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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

“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

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

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

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

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

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

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

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

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

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

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

Questions?