ObjC Optimization Tips


Over the past few weeks, I've been focus on doing performance tuning on a iPad app. 

A little background of the app

It is an business app, doing financial investment. It has lots of moving parts on the GUI, user can drag, slide and flipping, a graph, and lots of numbers and dates. User can play around with different combination of input and see their investment prediction over few years. There were around 50 parameters (or more) that affects actual calculation result. The app has at least 8000 lines of codes just for doing calculation. 

The story
The Product Owner has been complaining to the team that the app has a serious performance issue. It's sluggish and the financial result does not reflect user's interaction immediately (PO generally don't care how complicate you back-end is). The financial graphs took ~5 seconds to redraw on screen, when user is moving the slider, and they are moving it continuously back and forth.

Tools I uses for this task.
I mainly use Xcode's Instrument tools to analyst and tune the performance. It has been proven to be an extremely useful tool set.


My Verdicts
The following are the result of my findings. Some of this are pretty basic, but I find some of our experience iOS developer still does it. So I think it's important to spell it out to remind everyone.

  1. NSDate, NSCalendar and NSDateComponents.
    1. NSDate is often used in-conjunction with NSCalendar and NSDateComponents, converting a date to string for display on view is unavoidable; though they provide more functionality they tends to have high tax on performance. Only use this when your need more then what the standard C type time_t can't provide what you need.
    2. If you do use the C type it, please test it properly because there are cases where the C type can't handle.
          Tips: Use C type time_t for heavy date related calculation. E.g. On a graph.
  1. NSNumberFormatter and NSLocale
    1. I use NSNumberFormatter and NSLocale to format numbers into da_DK locale (Danish locale)
          Tips: Create the object once and reuse especially on moving part of the GUI, e.g Graph
  1. NSDateFormatter
    1. This is same as #2. Create once and store it in memory for reuse.
  2. NSDecimalNumber and NSNumber
    1. Boxing and unboxing NS data type are expensive. Don't do them in a loop. A way to do this is to make all the interrelated method use the same data type signature.
  3. NSString
    1. You can never avoid to perform operations on NSString in any type of programming language. Concatenating, splitting, substring or converting it to different data type is an expensive operation.
  4. NSMutableArray or NSArray 's KVO and NSPredicate or NSSortDescriptor
    1. KVO is convenient for doing summation, finding max or min in a collection; but it’s 10 times slower then a normal for loop.  KVO itself is doing loop internally.
    2. NSSortDescriptor is another things you should be caution when using in a loop. They can be really slow as they are doing loops internally.
Last but not least
These are not all that causes the app be sluggish, but they are the main causes. If you look really closely you will notice deep down under those methods mentioned above, they are mostly "string" manipulation or comparison related functions.

Happy coding.

Comments

Popular posts from this blog

Generating INSERT statement from SELECT using SQuirreL SQL

OneNote: We need the password to sync this notebook (Error code: 0xE4010643)

Fixing the AKE BC398 USB3.0 sluggish performance