SlideShare une entreprise Scribd logo
1  sur  57
Télécharger pour lire hors ligne
CS193P - Lecture 10
        iPhone Application Development

        Performance




Monday, February 8, 2010                 1
Announcements
        • Paparazzi 2 is due next Wednesday at 11:59pm
        • Friday section tomorrow at 4 PM, Building 260 Room 113
            ■   Yelp




Monday, February 8, 2010                                           2
A little more Core Data
        • NSFetchedResultsController
            ■ Interacts with the Core Data database on your behalf
            ■ [fetchedResultsController objectAtIndexPath:] gets at row data

            ■ [fetchedResultsController sections] gets at section data


        • NSFetchedResultsSectionInfo
            ■   Protocol defining methods that you can call from your
                UITableViewDataSource methods
                 ■ numberOfSectionsInTableView:
                 ■ tableView:numberOfRowsInSection:

                 ■ tableView:cellForRowAtIndexPath:




Monday, February 8, 2010                                                       3
Today’s Topics
        • Memory Usage
            ■ Leaks
            ■ Autorelease

            ■ System warnings


        • Concurrency
            ■ Threads
            ■ Operations and queues


        • Additional Tips & Tricks




Monday, February 8, 2010              4
iPhone Performance Overview
        • iPhone applications must work with...
            ■ Limited memory
            ■ Slow or unavailable network resources

            ■ Less powerful hardware


        • Write your code with these constraints in mind
        • Use performance tools to figure out where to invest




Monday, February 8, 2010                                        5
Memory Usage




Monday, February 8, 2010   6
Memory on the iPhone
        • Starting points for performance
            ■ Load lazily
            ■ Don’t leak

            ■ Watch your autorelease footprint

            ■ Reuse memory


        • System memory warnings are a last resort
            ■   Respond to warnings or be terminated




Monday, February 8, 2010                               7
Loading Lazily
        • Pervasive in Cocoa frameworks
        • Do only as much work as is required
            ■   Application launch time!
        • Think about where your code really belongs
        • Use multiple NIBs for your user interface




Monday, February 8, 2010                               8
Loading a Resource Too Early
        • What if it’s not needed until much later? Or not at all?
           - (id)init
           {
             self = [super init];
             if (self) {
                // Too early...
                myImage = [self readSomeHugeImageFromDisk];
             }
             return self;
           }




Monday, February 8, 2010                                             9
Loading a Resource Lazily
        • Wait until someone actually requests it, then create it
           - (UIImage *)myImage
           {
             if (myImage == nil) {
                myImage = [self readSomeHugeImageFromDisk];
             }
           }

        • This pattern benefits both memory and launch time
        • Not always the right move, consider your specific situation
        • Notice that above implementation is not thread-safe!




Monday, February 8, 2010                                                10
Plugging Leaks
        • Memory leaks are very bad
            ■   Especially in code that runs often
        • Luckily, leaks are easy to find with the right tools




Monday, February 8, 2010                                         11
Method Naming and Object Ownership
        • If a method’s name contains alloc, copy or new,
          then it returns a retained object
        • Balance calls to alloc, copy, new or retain with calls to release or
          autorelease
            ■   Early returns can make this very difficult to do!




Monday, February 8, 2010                                                         12
Finding Leaks
        • Use Instruments with the Leaks recorder




Monday, February 8, 2010                            13
Identifying Leaks in Instruments
        • Each leak comes with a backtrace
        • Leaks in system code do exist, but they’re rare
            ■   If you find one, tell us at http://bugreport.apple.com
        • Consider your own application code first




Monday, February 8, 2010                                                 14
Caught in the Act




Monday, February 8, 2010                       15
Demo:
        Finding Leaks with Instruments




Monday, February 8, 2010                 16
Autorelease and You
        • Autorelease simplifies your code
            ■   Worry less about the scope and lifetime of objects
        • When an autorelease pool is drained, it calls -release on each
          object
        • An autorelease pool is created automatically for each iteration
          of your application’s run loop




Monday, February 8, 2010                                                    17
So What’s the Catch?
        • What if many objects are autoreleased before the pool pops?
        • Consider the maximum memory footprint of your application




Monday, February 8, 2010                                                18
A Crowded Pool...




Monday, February 8, 2010                       19
Reducing Your High-Water Mark
        • When many objects will be autoreleased, create and release
          your own pool
            ■ Usually not necessary, don’t do this without thinking!
            ■ Tools can help identify cases where it’s needed

            ■ Loops are the classic case




Monday, February 8, 2010                                               20
Autorelease in a Loop
        • Remember that many methods return autoreleased objects
           for (int i = 0; i < someLargeNumber; i++) {
             NSString *string = ...;
             string = [string lowercaseString];
             string = [string stringByAppendingString:...];
             NSLog(@“%@”, string);
           }




Monday, February 8, 2010                                           21
Creating an Autorelease Pool
        • One option is to create and release for each iteration
           for (int i = 0; i < someLargeNumber; i++) {
             NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

               NSString *string = ...;
               string = [string lowercaseString];
               string = [string stringByAppendingString:...];
               NSLog(@“%@”, string);

               [pool release];
           }




Monday, February 8, 2010                                                   22
Outliving the Autorelease Pool
        • What if some object is needed outside the scope of the pool?
           NSString *stringToReturn = nil;

           for (int i = 0; i < someLargeNumber; i++) {
             NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

               NSString *string = ...;
               string = [string stringByAppendingString:...];

               if ([string someCondition]) {
                 stringToReturn = [string retain];
               }

               [pool release];
               if (stringToReturn) break;
           }

           return [stringToReturn autorelease];

Monday, February 8, 2010                                                   23
Reducing Use of Autorelease
        • Another option is to cut down on use of autoreleased objects
            ■   Not always possible if you’re callling into someone else’s code
        • When it makes sense, switch to alloc/init/release
        • In previous example, perhaps use a single NSMutableString?




Monday, February 8, 2010                                                          24
Demo:
        Measuring Your High-Water Mark




Monday, February 8, 2010                 25
Object Creation Overhead
        • Most of the time, creating and deallocating objects is not a
          insignificant hit to application performance
        • In a tight loop, though, it can become a problem...
            for (int i = 0; i < someLargeNumber; i++) {
              MyObject *object = [[MyObject alloc] initWithValue:...];
              [object doSomething];
              [object release];
            }




Monday, February 8, 2010                                                 26
Reusing Objects
        • Update existing objects rather than creating new ones
        • Combine intuition and evidence to decide if it’s necessary
           MyObject *myObject = [[MyObject alloc] init];

           for (int i = 0; i < someLargeNumber; i++) {
             myObject.value = ...;
             [myObject doSomething];
           }

           [myObject release];

        • Remember -[UITableView dequeueReusableCellWithIdentifier]




Monday, February 8, 2010                                               27
Memory Warnings
        • Coexist with system applications
        • Memory warnings issued when memory runs out
        • Respond to memory warnings or face dire consequences!




Monday, February 8, 2010                                          28
Responding to Memory Warnings
        • Every view controller gets -didReceiveMemoryWarning
            ■ By default, releases the view if it’s not visible
            ■ Release other expensive resources in your subclass


                - (void)didReceiveMemoryWarning
                {
                  // Always call super
                  [super didReceiveMemoryWarning];

                    // Release expensive resources
                    [expensiveResource release];
                    expensiveResource = nil;
                }



          • App Delegate gets -applicationDidReceiveMemoryWarning:

Monday, February 8, 2010                                             29
What Other Resources Do I Release?
        • Images
        • Sounds
        • Cached data




Monday, February 8, 2010                     30
Use SQLite/Core Data for Large Data Sets
        • Many data formats keep everything in memory
        • SQLite can work with your data in chunks




Monday, February 8, 2010                                31
More on Memory Performance
        • “Memory Usage Performance Guidelines”
          https://developer.apple.com/iphone/library/documentation/
          Performance/Conceptual/ManagingMemory/




Monday, February 8, 2010                                              32
Concurrency




Monday, February 8, 2010   33
Why Concurrency?
        • With a single thread, long-running operations may interfere
          with user interaction
        • Multiple threads allow you to load resources or perform
          computations without locking up your entire application




Monday, February 8, 2010                                                34
Threads on the iPhone
        • Based on the POSIX threading API
            ■   /usr/include/pthread.h
        • Higher-level wrappers in the Foundation framework




Monday, February 8, 2010                                      35
NSThread Basics
        • Run loop automatically instantiated for each thread
        • Each NSThread needs to create its own autorelease pool
        • Convenience methods for messaging between threads




Monday, February 8, 2010                                           36
Typical NSThread Use Case
            - (void)someAction:(id)sender
            {
              // Fire up a new thread
              [NSThread detachNewThreadSelector:@selector(doWork:)
                                      withTarget:self object:someData];
            }

            - (void)doWork:(id)someData
            {
              NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

                [someData doLotsOfWork];

                // Message back to the main thread
                [self performSelectorOnMainThread:@selector(allDone:)
                       withObject:[someData result] waitUntilDone:NO];

                [pool release];
            }

Monday, February 8, 2010                                                    37
UIKit and Threads
        • Unless otherwise noted, UIKit classes are not threadsafe
            ■   Objects must be created and messaged from the main thread
        • You can create a UIImage on a background thread
            ■   But you can’t set it on a UIImageView




Monday, February 8, 2010                                                    38
Demo:
        Threads and Xcode




Monday, February 8, 2010    39
Locks
        • Protect critical sections of code, mediate access to shared data
        • NSLock and subclasses
            - (void)init
            {
              myLock = [[NSLock alloc] init];
            }

            - (void)someMethod
            {
              [myLock lock];
              // We only want one thread executing this code at once
              [myLock unlock]
            }




Monday, February 8, 2010                                                     40
Conditions
        • NSCondition is useful for producer/consumer model
           // On the producer thread       // On the consumer thread
           - (void)produceData             - (void)consumeData
           {                               {
             [condition lock];               [condition lock];
                                             while(!newDataExists) {
               // Produce new data              [condition wait];
               newDataExists = YES;          }

               [condition signal];             // Consume the new data
               [condition unlock];             newDataExists = NO;
           }
                                               [condition unlock];
                                           }


        • Wait is equivalent to: unlock, sleep until signalled, lock

Monday, February 8, 2010                                                 41
The Danger of Locks
        • Very difficult to get locking right!
        • All it takes is one poorly behaved client
            ■ Accessing shared data outside of a lock
            ■ Deadlocks

            ■ Priority inversion




Monday, February 8, 2010                                42
Threading Pitfalls
        • Subtle, nondeterministic bugs may be introduced
        • Code may become more difficult to maintain
        • In the worst case, more threads can mean slower code




Monday, February 8, 2010                                         43
Alternatives to Threading
        • Asynchronous (nonblocking) functions
            ■ Specify target/action or delegate for callback
            ■ NSURLConnection has synchronous and asynchronous variants


        • Timers
            ■ One-shot or recurring
            ■ Specify a callback method

            ■ Managed by the run loop


        • Higher level constructs like operations




Monday, February 8, 2010                                                  44
NSOperation
        • Abstract superclass
        • Manages thread creation and lifecycle
        • Encapsulate a unit of work in an object
        • Specify priorities and dependencies




Monday, February 8, 2010                            45
Creating an NSOperation Subclass
        • Define a custom init method
            - (id)initWithSomeObject:(id)someObject
            {
              self = [super init];
              if (self) {
                 self.someObject = someObject;
              }
              return self;
            }


        • Override -main method to do work
            - (void)main
            {
              [someObject doLotsOfTimeConsumingWork];
            }




Monday, February 8, 2010                                46
NSOperationQueue
        • Operations are typically scheduled by adding to a queue
        • Choose a maximum number of concurrent operations
        • Queue runs operations based on priority and dependencies




Monday, February 8, 2010                                             47
Using an NSInvocationOperation
        • Concrete subclass of NSOperation
        • For lightweight tasks where creating a subclass is overkill
           - (void)someAction:(id)sender
           {
             NSInvocationOperation *operation =
               [[NSInvocationOperation alloc] initWithTarget:self
                                      selector:@selector(doWork:)
                                         object:someObject];

               [queue addObject:operation];

               [operation release];
           }




Monday, February 8, 2010                                                48
Demo:
        Threaded Flickr Loading




Monday, February 8, 2010          49
More on Concurrent Programming
        • “Threading Programming Guide”
          https://developer.apple.com/iphone/library/documentation/
          Cocoa/Conceptual/Multithreading




Monday, February 8, 2010                                              50
Additional Tips & Tricks




Monday, February 8, 2010           51
Drawing Performance
        • Avoid transparency when possible
            ■ Opaque views are much faster to draw than transparent views
            ■ Especially important when scrolling




        • Don’t call -drawRect: yourself
        • Use -setNeedsDisplayInRect: instead of -setNeedsDisplay
        • Use CoreAnimation Instrument




Monday, February 8, 2010                                                    52
Reuse Table View Cells
        • UITableView provides mechanism for reusing table view cells
           - (UITableViewCell *)tableView:(UITableView *)tableView
           cellForRowAtIndexPath:(NSIndexPath *)indexPath
           {
             // Ask the table view if it has a cell we can reuse
             UITableViewCell *cell =
             [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

               if (!cell) { // If not, create one with our identifier
                 cell = [[UITableViewCell alloc] initWithFrame:CGRectZero
                                                identifier:MyIdentifier];
                 [cell autorelease];
               }

               return cell;
           }


Monday, February 8, 2010                                                    53
Get notified
        • Don’t continuously poll!
            ■   Unless you must, which is rare
        • Hurts both responsiveness and battery life
        • Look in the documentation for a notification, delegate callback
          or other asynchronous API




Monday, February 8, 2010                                                    54
Take Samples
        • Instrument that lets you monitor CPU usage
        • Backtrace taken every fraction of a second
        • Higher samples = better candidates for optimization




Monday, February 8, 2010                                        55
Recap
        • Performance is an art and a science
            ■   Combine tools & concrete data with intuition & best practices
        • Don’t waste memory
        • Concurrency is tricky, abstract it if possible
        • Drawing is expensive, avoid unnecessary work




Monday, February 8, 2010                                                        56
Questions?




Monday, February 8, 2010   57

Contenu connexe

Similaire à Lecture 10 slides (february 4, 2010)

Optimizing mobile applications - Ian Dundore, Mark Harkness
Optimizing mobile applications - Ian Dundore, Mark HarknessOptimizing mobile applications - Ian Dundore, Mark Harkness
Optimizing mobile applications - Ian Dundore, Mark Harknessozlael ozlael
 
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15Raunak Talwar
 
The Reluctant SysAdmin : 360|iDev Austin 2010
The Reluctant SysAdmin : 360|iDev Austin 2010The Reluctant SysAdmin : 360|iDev Austin 2010
The Reluctant SysAdmin : 360|iDev Austin 2010Voxilate
 
10x Performance Improvements - A Case Study
10x Performance Improvements - A Case Study10x Performance Improvements - A Case Study
10x Performance Improvements - A Case StudyRonald Bradford
 
Introduction to Dependency Injection
Introduction to Dependency InjectionIntroduction to Dependency Injection
Introduction to Dependency InjectionSolTech, Inc.
 
Memory Management in RubyMotion
Memory Management in RubyMotionMemory Management in RubyMotion
Memory Management in RubyMotionMichael Denomy
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]Kuba Břečka
 
Vulnerability, exploit to metasploit
Vulnerability, exploit to metasploitVulnerability, exploit to metasploit
Vulnerability, exploit to metasploitTiago Henriques
 
Browser Fuzzing with a Twist (and a Shake) -- ZeroNights 2015
Browser Fuzzing with a Twist (and a Shake) -- ZeroNights 2015Browser Fuzzing with a Twist (and a Shake) -- ZeroNights 2015
Browser Fuzzing with a Twist (and a Shake) -- ZeroNights 2015Jeremy Brown
 
Sqlpo Presentation
Sqlpo PresentationSqlpo Presentation
Sqlpo Presentationsandook
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidStanojko Markovik
 
The Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To TestingThe Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To TestingTim Duckett
 
Owasp joy of proactive security
Owasp joy of proactive securityOwasp joy of proactive security
Owasp joy of proactive securityScott Behrens
 

Similaire à Lecture 10 slides (february 4, 2010) (20)

0507 057 01 98 * Adana Cukurova Klima Servisleri
0507 057 01 98 * Adana Cukurova Klima Servisleri0507 057 01 98 * Adana Cukurova Klima Servisleri
0507 057 01 98 * Adana Cukurova Klima Servisleri
 
Optimizing mobile applications - Ian Dundore, Mark Harkness
Optimizing mobile applications - Ian Dundore, Mark HarknessOptimizing mobile applications - Ian Dundore, Mark Harkness
Optimizing mobile applications - Ian Dundore, Mark Harkness
 
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
 
The Reluctant SysAdmin : 360|iDev Austin 2010
The Reluctant SysAdmin : 360|iDev Austin 2010The Reluctant SysAdmin : 360|iDev Austin 2010
The Reluctant SysAdmin : 360|iDev Austin 2010
 
10x Performance Improvements - A Case Study
10x Performance Improvements - A Case Study10x Performance Improvements - A Case Study
10x Performance Improvements - A Case Study
 
An Introduction to
An Introduction to An Introduction to
An Introduction to
 
Using SQLite
Using SQLiteUsing SQLite
Using SQLite
 
Introduction to Dependency Injection
Introduction to Dependency InjectionIntroduction to Dependency Injection
Introduction to Dependency Injection
 
Memory Management in RubyMotion
Memory Management in RubyMotionMemory Management in RubyMotion
Memory Management in RubyMotion
 
Noit ocon-2010
Noit ocon-2010Noit ocon-2010
Noit ocon-2010
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
 
Vulnerability, exploit to metasploit
Vulnerability, exploit to metasploitVulnerability, exploit to metasploit
Vulnerability, exploit to metasploit
 
Browser Fuzzing with a Twist (and a Shake) -- ZeroNights 2015
Browser Fuzzing with a Twist (and a Shake) -- ZeroNights 2015Browser Fuzzing with a Twist (and a Shake) -- ZeroNights 2015
Browser Fuzzing with a Twist (and a Shake) -- ZeroNights 2015
 
Oscon 2010
Oscon 2010Oscon 2010
Oscon 2010
 
Tori ORM for PyCon 2014
Tori ORM for PyCon 2014Tori ORM for PyCon 2014
Tori ORM for PyCon 2014
 
Sqlpo Presentation
Sqlpo PresentationSqlpo Presentation
Sqlpo Presentation
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with android
 
The Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To TestingThe Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To Testing
 
Owasp joy of proactive security
Owasp joy of proactive securityOwasp joy of proactive security
Owasp joy of proactive security
 
Reef - ESUG 2010
Reef - ESUG 2010Reef - ESUG 2010
Reef - ESUG 2010
 

Dernier

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Dernier (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Lecture 10 slides (february 4, 2010)

  • 1. CS193P - Lecture 10 iPhone Application Development Performance Monday, February 8, 2010 1
  • 2. Announcements • Paparazzi 2 is due next Wednesday at 11:59pm • Friday section tomorrow at 4 PM, Building 260 Room 113 ■ Yelp Monday, February 8, 2010 2
  • 3. A little more Core Data • NSFetchedResultsController ■ Interacts with the Core Data database on your behalf ■ [fetchedResultsController objectAtIndexPath:] gets at row data ■ [fetchedResultsController sections] gets at section data • NSFetchedResultsSectionInfo ■ Protocol defining methods that you can call from your UITableViewDataSource methods ■ numberOfSectionsInTableView: ■ tableView:numberOfRowsInSection: ■ tableView:cellForRowAtIndexPath: Monday, February 8, 2010 3
  • 4. Today’s Topics • Memory Usage ■ Leaks ■ Autorelease ■ System warnings • Concurrency ■ Threads ■ Operations and queues • Additional Tips & Tricks Monday, February 8, 2010 4
  • 5. iPhone Performance Overview • iPhone applications must work with... ■ Limited memory ■ Slow or unavailable network resources ■ Less powerful hardware • Write your code with these constraints in mind • Use performance tools to figure out where to invest Monday, February 8, 2010 5
  • 7. Memory on the iPhone • Starting points for performance ■ Load lazily ■ Don’t leak ■ Watch your autorelease footprint ■ Reuse memory • System memory warnings are a last resort ■ Respond to warnings or be terminated Monday, February 8, 2010 7
  • 8. Loading Lazily • Pervasive in Cocoa frameworks • Do only as much work as is required ■ Application launch time! • Think about where your code really belongs • Use multiple NIBs for your user interface Monday, February 8, 2010 8
  • 9. Loading a Resource Too Early • What if it’s not needed until much later? Or not at all? - (id)init { self = [super init]; if (self) { // Too early... myImage = [self readSomeHugeImageFromDisk]; } return self; } Monday, February 8, 2010 9
  • 10. Loading a Resource Lazily • Wait until someone actually requests it, then create it - (UIImage *)myImage { if (myImage == nil) { myImage = [self readSomeHugeImageFromDisk]; } } • This pattern benefits both memory and launch time • Not always the right move, consider your specific situation • Notice that above implementation is not thread-safe! Monday, February 8, 2010 10
  • 11. Plugging Leaks • Memory leaks are very bad ■ Especially in code that runs often • Luckily, leaks are easy to find with the right tools Monday, February 8, 2010 11
  • 12. Method Naming and Object Ownership • If a method’s name contains alloc, copy or new, then it returns a retained object • Balance calls to alloc, copy, new or retain with calls to release or autorelease ■ Early returns can make this very difficult to do! Monday, February 8, 2010 12
  • 13. Finding Leaks • Use Instruments with the Leaks recorder Monday, February 8, 2010 13
  • 14. Identifying Leaks in Instruments • Each leak comes with a backtrace • Leaks in system code do exist, but they’re rare ■ If you find one, tell us at http://bugreport.apple.com • Consider your own application code first Monday, February 8, 2010 14
  • 15. Caught in the Act Monday, February 8, 2010 15
  • 16. Demo: Finding Leaks with Instruments Monday, February 8, 2010 16
  • 17. Autorelease and You • Autorelease simplifies your code ■ Worry less about the scope and lifetime of objects • When an autorelease pool is drained, it calls -release on each object • An autorelease pool is created automatically for each iteration of your application’s run loop Monday, February 8, 2010 17
  • 18. So What’s the Catch? • What if many objects are autoreleased before the pool pops? • Consider the maximum memory footprint of your application Monday, February 8, 2010 18
  • 19. A Crowded Pool... Monday, February 8, 2010 19
  • 20. Reducing Your High-Water Mark • When many objects will be autoreleased, create and release your own pool ■ Usually not necessary, don’t do this without thinking! ■ Tools can help identify cases where it’s needed ■ Loops are the classic case Monday, February 8, 2010 20
  • 21. Autorelease in a Loop • Remember that many methods return autoreleased objects for (int i = 0; i < someLargeNumber; i++) { NSString *string = ...; string = [string lowercaseString]; string = [string stringByAppendingString:...]; NSLog(@“%@”, string); } Monday, February 8, 2010 21
  • 22. Creating an Autorelease Pool • One option is to create and release for each iteration for (int i = 0; i < someLargeNumber; i++) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *string = ...; string = [string lowercaseString]; string = [string stringByAppendingString:...]; NSLog(@“%@”, string); [pool release]; } Monday, February 8, 2010 22
  • 23. Outliving the Autorelease Pool • What if some object is needed outside the scope of the pool? NSString *stringToReturn = nil; for (int i = 0; i < someLargeNumber; i++) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *string = ...; string = [string stringByAppendingString:...]; if ([string someCondition]) { stringToReturn = [string retain]; } [pool release]; if (stringToReturn) break; } return [stringToReturn autorelease]; Monday, February 8, 2010 23
  • 24. Reducing Use of Autorelease • Another option is to cut down on use of autoreleased objects ■ Not always possible if you’re callling into someone else’s code • When it makes sense, switch to alloc/init/release • In previous example, perhaps use a single NSMutableString? Monday, February 8, 2010 24
  • 25. Demo: Measuring Your High-Water Mark Monday, February 8, 2010 25
  • 26. Object Creation Overhead • Most of the time, creating and deallocating objects is not a insignificant hit to application performance • In a tight loop, though, it can become a problem... for (int i = 0; i < someLargeNumber; i++) { MyObject *object = [[MyObject alloc] initWithValue:...]; [object doSomething]; [object release]; } Monday, February 8, 2010 26
  • 27. Reusing Objects • Update existing objects rather than creating new ones • Combine intuition and evidence to decide if it’s necessary MyObject *myObject = [[MyObject alloc] init]; for (int i = 0; i < someLargeNumber; i++) { myObject.value = ...; [myObject doSomething]; } [myObject release]; • Remember -[UITableView dequeueReusableCellWithIdentifier] Monday, February 8, 2010 27
  • 28. Memory Warnings • Coexist with system applications • Memory warnings issued when memory runs out • Respond to memory warnings or face dire consequences! Monday, February 8, 2010 28
  • 29. Responding to Memory Warnings • Every view controller gets -didReceiveMemoryWarning ■ By default, releases the view if it’s not visible ■ Release other expensive resources in your subclass - (void)didReceiveMemoryWarning { // Always call super [super didReceiveMemoryWarning]; // Release expensive resources [expensiveResource release]; expensiveResource = nil; } • App Delegate gets -applicationDidReceiveMemoryWarning: Monday, February 8, 2010 29
  • 30. What Other Resources Do I Release? • Images • Sounds • Cached data Monday, February 8, 2010 30
  • 31. Use SQLite/Core Data for Large Data Sets • Many data formats keep everything in memory • SQLite can work with your data in chunks Monday, February 8, 2010 31
  • 32. More on Memory Performance • “Memory Usage Performance Guidelines” https://developer.apple.com/iphone/library/documentation/ Performance/Conceptual/ManagingMemory/ Monday, February 8, 2010 32
  • 34. Why Concurrency? • With a single thread, long-running operations may interfere with user interaction • Multiple threads allow you to load resources or perform computations without locking up your entire application Monday, February 8, 2010 34
  • 35. Threads on the iPhone • Based on the POSIX threading API ■ /usr/include/pthread.h • Higher-level wrappers in the Foundation framework Monday, February 8, 2010 35
  • 36. NSThread Basics • Run loop automatically instantiated for each thread • Each NSThread needs to create its own autorelease pool • Convenience methods for messaging between threads Monday, February 8, 2010 36
  • 37. Typical NSThread Use Case - (void)someAction:(id)sender { // Fire up a new thread [NSThread detachNewThreadSelector:@selector(doWork:) withTarget:self object:someData]; } - (void)doWork:(id)someData { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [someData doLotsOfWork]; // Message back to the main thread [self performSelectorOnMainThread:@selector(allDone:) withObject:[someData result] waitUntilDone:NO]; [pool release]; } Monday, February 8, 2010 37
  • 38. UIKit and Threads • Unless otherwise noted, UIKit classes are not threadsafe ■ Objects must be created and messaged from the main thread • You can create a UIImage on a background thread ■ But you can’t set it on a UIImageView Monday, February 8, 2010 38
  • 39. Demo: Threads and Xcode Monday, February 8, 2010 39
  • 40. Locks • Protect critical sections of code, mediate access to shared data • NSLock and subclasses - (void)init { myLock = [[NSLock alloc] init]; } - (void)someMethod { [myLock lock]; // We only want one thread executing this code at once [myLock unlock] } Monday, February 8, 2010 40
  • 41. Conditions • NSCondition is useful for producer/consumer model // On the producer thread // On the consumer thread - (void)produceData - (void)consumeData { { [condition lock]; [condition lock]; while(!newDataExists) { // Produce new data [condition wait]; newDataExists = YES; } [condition signal]; // Consume the new data [condition unlock]; newDataExists = NO; } [condition unlock]; } • Wait is equivalent to: unlock, sleep until signalled, lock Monday, February 8, 2010 41
  • 42. The Danger of Locks • Very difficult to get locking right! • All it takes is one poorly behaved client ■ Accessing shared data outside of a lock ■ Deadlocks ■ Priority inversion Monday, February 8, 2010 42
  • 43. Threading Pitfalls • Subtle, nondeterministic bugs may be introduced • Code may become more difficult to maintain • In the worst case, more threads can mean slower code Monday, February 8, 2010 43
  • 44. Alternatives to Threading • Asynchronous (nonblocking) functions ■ Specify target/action or delegate for callback ■ NSURLConnection has synchronous and asynchronous variants • Timers ■ One-shot or recurring ■ Specify a callback method ■ Managed by the run loop • Higher level constructs like operations Monday, February 8, 2010 44
  • 45. NSOperation • Abstract superclass • Manages thread creation and lifecycle • Encapsulate a unit of work in an object • Specify priorities and dependencies Monday, February 8, 2010 45
  • 46. Creating an NSOperation Subclass • Define a custom init method - (id)initWithSomeObject:(id)someObject { self = [super init]; if (self) { self.someObject = someObject; } return self; } • Override -main method to do work - (void)main { [someObject doLotsOfTimeConsumingWork]; } Monday, February 8, 2010 46
  • 47. NSOperationQueue • Operations are typically scheduled by adding to a queue • Choose a maximum number of concurrent operations • Queue runs operations based on priority and dependencies Monday, February 8, 2010 47
  • 48. Using an NSInvocationOperation • Concrete subclass of NSOperation • For lightweight tasks where creating a subclass is overkill - (void)someAction:(id)sender { NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doWork:) object:someObject]; [queue addObject:operation]; [operation release]; } Monday, February 8, 2010 48
  • 49. Demo: Threaded Flickr Loading Monday, February 8, 2010 49
  • 50. More on Concurrent Programming • “Threading Programming Guide” https://developer.apple.com/iphone/library/documentation/ Cocoa/Conceptual/Multithreading Monday, February 8, 2010 50
  • 51. Additional Tips & Tricks Monday, February 8, 2010 51
  • 52. Drawing Performance • Avoid transparency when possible ■ Opaque views are much faster to draw than transparent views ■ Especially important when scrolling • Don’t call -drawRect: yourself • Use -setNeedsDisplayInRect: instead of -setNeedsDisplay • Use CoreAnimation Instrument Monday, February 8, 2010 52
  • 53. Reuse Table View Cells • UITableView provides mechanism for reusing table view cells - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Ask the table view if it has a cell we can reuse UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (!cell) { // If not, create one with our identifier cell = [[UITableViewCell alloc] initWithFrame:CGRectZero identifier:MyIdentifier]; [cell autorelease]; } return cell; } Monday, February 8, 2010 53
  • 54. Get notified • Don’t continuously poll! ■ Unless you must, which is rare • Hurts both responsiveness and battery life • Look in the documentation for a notification, delegate callback or other asynchronous API Monday, February 8, 2010 54
  • 55. Take Samples • Instrument that lets you monitor CPU usage • Backtrace taken every fraction of a second • Higher samples = better candidates for optimization Monday, February 8, 2010 55
  • 56. Recap • Performance is an art and a science ■ Combine tools & concrete data with intuition & best practices • Don’t waste memory • Concurrency is tricky, abstract it if possible • Drawing is expensive, avoid unnecessary work Monday, February 8, 2010 56