Why is -drawRect faster than using CALayers/UIViews for UITableViews?

后端 未结 3 1640
甜味超标
甜味超标 2021-02-06 15:07

I can already hear the wrenching guts of a thousand iOS developers.

No, I am not noob.

Why is -drawRect faster for UITableView performance than having mu

3条回答
  •  清酒与你
    2021-02-06 15:53

    I'm only seeing half the conversation here, so I might have misunderstood. Based on my recent experiences optimizing CALayer rendering, and investigating the ways Apple does(n't) optimize stuff you'd expect to be optimized...

    What's the difference if it all ends up cached and flattened anyway?

    Apple ends up creating a separate GPU element per layer. If you have lots of layers, you have lots of GPU elements. If you have one drawRect, you only have one element. Apple often does NOT flatten those, even where they could (and possibly "should").

    In many cases, "lots of elements" is no issue. But if they get to be large ... or there's enough of them ... or they're bad sizes for OpenGL ... AND (see below) they get stored in CPU instead of on GPU, then things start to get nasty. NB: in my experience:

    • "enough": 40+ in memory
    • "large": 100x100 points (200x200 retina pixels)

    Apple's code for GPU elements / buffers is well optimized in MOST places, but in a few places it's very POORLY optimized. The performance drop is like going off a cliff.

    Also, if you're handling cell reuse properly, you shouldn't need to regenerate views on each call to -cellForRowAtIndexPath

    You say "properly", except ... IIRC Apple's docs tell people not to do it that way, they go for a simpler approach (IMHO: weak docs), and instead re-populate all the subviews on every call. At which point ... how much are you saving?

    FINALLY:

    ...doesn't all this change with iOS 6, where the cost of creating a UIView is greatly reduced? (I haven't profiled it yet, just been hearing about it from other devs)

提交回复
热议问题