Tag: performance

  • Some Things I’ve Learned About Performance on iOS

    Some Things I’ve Learned About Performance on iOS

    Filed under “things I didn’t realise anyone else would find useful”.

    Screen Shot 2015-01-26 at 3.31.36 pm

    The Hybrid App

    The second iOS app I worked on was a hybrid app, and I became a master of the UIWebView. It looked native. But it didn’t quite feel native because… performance.

    We had a bridge that connected the iOS to the Javascript and vice versa. A fairly horrible solution, but this was back when people believed native apps were just a stop gap until the web won, and that this product didn’t really matter on mobile, anyway.

    Eventually we discovered that passing data across when calling Obj-C from Javascript was really slow but that calling a Javascript getter from Obj-C was just fine. Crisis averted.

    The Location Tracker

    This one had a myriad of issues. In theory, the API says you can just register for location updates with accuracy. In practise, if the accuracy is… accurate (<150m, if I recall correctly) the GPS turns on, stays on, and the battery rapidly runs down.

    Even if the phone is sat on your desk, not moving (one would have thought the accelerometer would be useful here, but no).

    So the answer was to operate with the GPS off most of the time, and then after a notification turn it on again for a limited period.

    Then there is the sheer volume of updates you get. If you just send each one with a network call, then… that’s a lot of network calls.

    So instead at each point you have to decide, should I send the update now, or wait for better accuracy. Meanwhile, cache the unsent ones.

    Now you have an app that is running in the background, and being awoken when there’s an update. But if the app gets kicked out of memory, all your unsent locations are lost.

    Fun story – we had it just about working and then I went to Kangaroo island which has terrible cell-phone service, and was taking loads of pictures. So even though I was intermittently on the network, hardly any locations were being sent because the camera was causing the app to be kicked out of memory.

    Basically: we had to implement persistent storage for location tracking to work.

    The Image Processor

    This app was handling a quantity of data (pixels) and had time and space problems.

    To reduce the memory usage, I got rid of as many Objective-C objects as I could, and replaced them with C. So the NSArray of NSNumbers became a CGFloat[]. Stopped using UIColor and started working with raw RGB values.

    Memory footprint reduced by around 66%, and the app became much, much faster.

    (I wrote all the tests on slower and easier to read code, which helped a lot with this process).

    Major problem here was how little information there was on the internet. I’m not an expert C coder, and I would look for how to do something in C within Objective-C and the answer was typically “there is almost certainly no need for you to do this“.

    The other performance improvement I made was to break up the processing work and do some pre-processing in the background thread, so that the wait is split into two very short waits, as opposed to one, slightly longer, wait.

    Things I’ve Learned

    Find the One Thing

    There’s not, in my experience (assuming you’ve been sensible around the basics of ARC, table views etc) any need to go through your code looking for “things to optimise”. iOS is mostly pretty good. The problems I described above all occurred in the most non-standard places of the app. Getting a deep understanding of how that thing worked was how it was fixed (aside from the first one, which I still think is just weird).

    The Profiler is Not That Scary

    Memory allocations is the one I’ve been working with most recently, but there is also a way to profile which parts of your code most time is spent in. I can see this being really useful. I used to be really intimidated by the profiler, but having spent more time with it, it’s OK [helpful tutorial].

    Performance Tests are Easy, but Slow

    I started adding performance tests, typically at stress sizes (10x the data I expect to process), using the (void)measureBlock:(void (^)(void))block function in XCTestCase [more info]. Each performance test runs 10x to give variance, and you can save a per-device baseline. Because these are my most time consuming tests I don’t want to run them when I’m developing against my tests so I moved them out into a separate target (that takes a few minutes to run) so that my core test target runs faster (<30 seconds).

  • Public Speaking as Performance

    Public Speaking as Performance

    cute bunny
    Credit: Flickr / Sarah Embaby

    I’ve written before about how I prepare mentally for a talk. Most recently, I’ve started to view it as a performance and be more and more (as the fall conference season is now underway) I’ve got more comfortable with the things I need to give a good performance. This change is mental, viewing it as a performance (rather than, commonly, a terrifying obligation past-me committed to), so differences are subtle, but important. I felt really good giving my last talk, which I think is a sign it’s time to prep a new one!

    Because, it is a performance. I stand up in front of people, not my natural habitat, and try to be intensively witty and insightful.

    I hope I’m usually witty and insightful, but in conversations, you take turns. On stage, it’s all on me.

    One of my pet peeves as an audience member is when speakers are unprepared (even, maybe especially when they apologise for it!) Not preparing is disrespectful to the audience who have given up their time, and often significant amounts of money to be there.

    If I’m speaking, then everything I do is around showing up prepared and in a good place mentally. This makes the conference experience very different. I feel OK about missing talks prior to mine. Although, pro-tip, for small conferences it’s worth letting them know you are hiding prior to your talk, and when to expect you as they may worry if they don’t see you!

    Now, I always ask for travel costs (most conferences give speakers a free ticket) in part because it means I don’t feel any obligation to make the cost of attending worthwhile. Any value I got (which has typically been high) is gravy. Everything comes second to the performance.

    Decompression time afterwards is also important. I usually use some of this time to make a storify of tweets during my talk.

    Following day – a good night’s sleep and a good breakfast!

    The other thing I’ve realised is that as a speaker, you can ask for things. Like water. Or to avoid specific slots. You can also ask for specific slots, but that is much harder for the organisers. It is incredibly hard organising a conference, so I try to go along with as much as possible and only ask for the things that will genuinely make an impact on my talk.

    • Prepare.
    • Hide (mental prep / power poses).
    • Setup equipment, test sound etc.
    • Perform.
    • Hide.
    • Socialise (this is when people say nice things! Don’t want to miss that!)
    • Relax (sleep in, have a nice breakfast).