Excactly what performance crtitical things should I have an eye out for?
I want a list with as many examples as possible. Or a list of best practice.
Excactly what performance crtitical things should I have an eye out for?
I want a list with as many examples as possible. Or a list of best practice.
Offscreen rendering / Rendering on the CPU
The biggest bottlenecks to graphics performance is offscreen rendering and blending – they can happen for every frame of the animation and can cause choppy scrolling.
Offscreen rendering (software rendering) happens when it is necessary to do the drawing in software (offscreen) before it can be handed over to the GPU. Hardware does not handles text rendering and advanced compositions with masks and shadows.
The following will trigger offscreen rendering:
Any layer with a mask (
layer.mask
)Any layer with
layer.masksToBounds
/view.clipsToBounds
being trueAny layer with
layer.allowsGroupOpacity
set to YES andlayer.opacity
is less than 1.0When does a view (or layer) require offscreen rendering?
Any layer with a drop shadow (
layer.shadow*
).Tips on how to fix: https://markpospesel.wordpress.com/tag/performance/
Any layer with
layer.shouldRasterize
being trueAny layer with
layer.cornerRadius
,layer.edgeAntialiasingMask
,layer.allowsEdgeAntialiasing
Any layer with
layer.borderWith
andlayer.borderColor
?Missing reference / proof
Text (any kind, including
UILabel
,CATextLayer
,Core Text
, etc).Most of the drawing you do with
CGContext
indrawRect:
. Even an empty implementation will be rendered offscreen.Blending
resizableImage
can cause blending.Avoiding blended layers when using resizableImages on iOS
Any layer which is not
opaque
and has abackgroundColor
withalpha
less than 1.0Any layer with
alpha
less than 1.0Any layer with
layer.content
or anyUIImageView
with aUIImage
having an alpha channelLayout
The following things will trigger
layoutSubviews
to be called on a UIView:Changing
bounds
triggers on same view and superviewChanging
frame
triggers on same view and superviewChanging
transform
orlayer.transform
triggers on superviewNote: I'm referring to real changes where values actually is changing
Contradictory these changes does not trigger
layoutSubviews
to be called:center
,layer.position
,layer.zPosition
,layer.anchorPoint
,layer.anchorPointZ
.Reference: https://github.com/hfossli/LayoutSubviewsInconsistency
General tips for improving performance
Often times it is better to
blend
than torender offscreen
.Consider using
drawRect:
instead of having a view with multiple labels and subviews.Draw on a background queue to a
UIImage
orCGImageRef
.Draw to a
CGLayer
(which is cached better onGPU
compared toUIImage
), and draw whatever you want into it.Update, don't: http://iosptl.com/posts/cglayer-no-longer-recommended/
Flatten your hierarchy
Reuse views – don't create and add new ones while scrolling
Have
opaque
views with solid background colorAvoid setting
alpha
andlayer.opacity
to less than 1.0Enable
layer.shouldRasterize
(use with care). I like to avoid this personally, but it performs faster in some occasions since rasters of the layer will be cached and reused. Remember if you enableshouldRasterize
on layers that changing their content or sublayers contents frequently will cause the performance to drop, since iOS will keep rasterizing the layer on each change.Links
http://iosinjordan.tumblr.com/post/56778173518/help-my-tables-dont-scroll-smoothly
https://developer.apple.com/library/IOS/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/DrawingTips/DrawingTips.html