Friday May 31, 2013

Remember How Pre-rendering With Core Graphics Was Better?

Excellent analysis by Florian Kugler about the tradeoffs of compositing with Core Graphics vs. animating CALayer trees:

While the time it took to render the views with subviews and sublayers improved with each device generation, we see a huge drop in drawing performance with the iPhone 4. The retina screen results in four times the number of pixels to be drawn. And since drawing with Core Graphics is a CPU task, the improvement in CPU power is not enough to offset the increased demands of this new display generation. The iPhone 5 is the first device which is able beat the iPhone 3GS (!) in terms of Core Graphics drawing performance in this example.

Of course, once you have the Core Graphics layer rendered, moving it around as an opaque texture is lightning fast on modern devices. Still, it’s good to recognize that if your view or layer heirarchy is simple and you are only moving it briefly across the screen, then you might get far better performance on a modern iOS device by moving all the layers with Core Animation rather than spending the time up front to flatten it all.

I noticed this myself with my app, ReadMore. The table view list of books uses textured backgrounds that are drawn in a separate background thread and then plopped into the table view when they are ready. That way the user can flick to scroll through quickly but when they come to rest, the textures eventually show up if they weren’t already cached. When the iPhone 4 came out, there was a noticably longer delay for the textures to first show up.1 I like Florian’s benchmarking technique using CADisplayLink and I’ll use that next time I run into issues like this.

(via iOS Dev Weekly)

  1. I ended up just leaving it. It didn’t reduce the experience enough in my opinion. Now that I’ve dropped support for the 3GS, I think I’ll revisit this to see if animating the layers as-is gives a better experience.