为什么-drawRect比为UITableViews使用CALayers / UIViews更快? [英] Why is -drawRect faster than using CALayers/UIViews for UITableViews?

查看:111
本文介绍了为什么-drawRect比为UITableViews使用CALayers / UIViews更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经听说过一千个iOS开发者的痛苦。

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

不,我不是菜鸟。

为什么UITableView性能的-drawRect比拥有多个视图更快?

我知道合成操作发生在GPU上。但合成是一次性的操作;一旦层被提交到内存,它与缓存的缓冲区没有区别,从GPU的角度来看,它被转换为视图内外。将此与在drawRect中使用Core Graphics进行比较,后者在CPU上使用未知数量的操作来生成最终在CALayers中缓存的像素。如果它最终都缓存和扁平化有什么区别呢?

I understand that compositing operations take place on the GPU. But compositing is a one-time operation; once the layers are committed to memory, it is no different from a cached buffer that, from the point of view of the GPU, gets translated in and out of view. Compare this to using Core Graphics in drawRect, which employ an unknown amount of operations on the CPU to produce pixels that end up getting cached in CALayers anyway. What's the difference if it all ends up cached and flattened anyway?

另外,如果你正确处理单元重用,你不需要在每次调用时重新生成视图to -cellForRowAtIndexPath。实际上,使用UIView / CALayer对象缓存的状态数据(字体,字体大小,文本颜色,属性等)比在-drawRect期间不断重新创建它们可能有性能优势。

Also, if you're handling cell reuse properly, you shouldn't need to regenerate views on each call to -cellForRowAtIndexPath. In fact, there may be a performance benefit to having the state data (font, font size, text color, attributes, etc) cached by UIView/CALayer objects than having them constantly recreated during -drawRect.

为什么drawRect的热潮?有人可以给我指点吗?

Why the craze for drawRect? Can someone give me pointers?

推荐答案

当你谈到优化时,你需要提供特定的情况和条件和限制。因为优化是关于微观管理的。否则,它毫无意义。

When you talking about optimization, you need to provide specific situations and conditions and limitations. Because optimization is all about micro-management. Otherwise, it's meaningless.

更快的基础是什么?你是怎么测量的?这些数字是多少?

What's the basis of your faster? How did you measured it? What's the numbers?

例如,no-op或非常简单的 -drawRect:可以更快,但它并不意味着它总是这样。

For example, no-op or very simple -drawRect: can be faster, but it doesn't mean it always does.

我不知道CA的内部设计。所以这是我的猜测

I don't know internal design of CA neither. So here are my guesses.

这很奇怪你的绘图代码不断被调用。因为 CALayer 缓存绘制结果,并且在您发送 setNeedsDisplay 消息之前不会再次绘制它。如果不更新单元格的内容,则它与单个位图图层相同。应该比多个合成图层更快,因为它不需要合成成本。如果您只使用足够在池中同时存在的少量单元格,则不需要更新它。随着RAM在最近的模型中变得更大,它更有可能发生在最近的模型中。

It's weird that your drawing code is being called constantly. Because CALayer caches drawing result, and won't draw it again until you send setNeedsDisplay message. If you don't update cell's content, it's just same with single bitmap layer. Should be faster than multiple composited layers because it doesn't need composition cost. If you're using only small number of cells which are enough to be exist all in the pool at same time, it doesn't need to be updated. As RAM becomes larger in recent model, it's more likely to happen in recent models.

如果它不断更新,这意味着你实际上是在自己更新它们。因此,您的图层合成版本也可能会不断更新。这意味着每个帧都会再次合成。它可能会因为复杂而庞大而变慢。如果它复杂而且很大并且有很多重叠区域,那么它可能会更慢。如果无法确定哪些区域可以忽略,我猜CA会严格绘制所有内容。不像你可以选择要绘制的内容。

If it is being updated constantly, it means you're actually updating them yourself. So maybe your layer-composited version would also being updated constantly. It means it is being composited again for every frame. It could be slower by how it is complex and large. If it's complex and large and have a lot of overlapping areas, it could be slower. I guess CA will draw everything strictly if it can't determine what area is fine to ignore. Unlike you can choose what to draw or not.

甚至您将视图配置为多层的纯组合,最终应绘制每个子图层。并且不保证在GPU中绘制他们的内容。例如,我相信 CATextLayer 正在CPU中绘制自己。 (因为在当前移动GPU上使用多边形绘制文本在性能方面没有意义)和一些过滤效果也是如此。在这种情况下,总体成本会相似,而且需要合成成本。

Even you configure your view as pure composition of many layers, each sublayers should be drawn eventually. And drawing of their content is not guaranteed to be done in GPU. For example, I believe CATextLayer is drawing itself in CPU. (because drawing text with polygons on current mobile GPU doesn't make sense in performance perspective) And some filtering effects too. In that case, overall cost would be similar and plus it requires compositing cost.

如果您的GPU非常忙于重负载,因为图层太多或直接使用OpenGL图形,您的CPU可能处于空闲状态。如果您的CG绘图可以在空闲的CPU时间内完成,那么它可能比为GPU提供更多负载更快。

If your GPU is very busy for heavy load because there're too many layers or direct OpenGL drawings, your CPU may be idle. If your CG drawing can be done within the idle CPU time, it could be faster than giving more load to GPU.

如果你的情况不是我上面列出的情况,我真的很想看到并检查CG代码的绘制速度比CA组合快。我希望你附上一些源代码。

If your case is none of situations I listed above, I really want to see and check the CG code draws faster than CA composition. I wish you attach some source code.

这篇关于为什么-drawRect比为UITableViews使用CALayers / UIViews更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆