Exactly what performance-critical things should I have an eye out for?
I want a list with as many examples as possible. Or a list of best practices.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Offscreen rendering / Rendering on the CPU
The biggest bottlenecks to graphics performance are 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 handle 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.clipsToBoundsbeing trueAny layer with
layer.allowsGroupOpacityset to YES andlayer.opacityis 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.shouldRasterizebeing trueAny layer with
layer.cornerRadius,layer.edgeAntialiasingMask,layer.allowsEdgeAntialiasingAny layer with
layer.borderWithandlayer.borderColor?Missing reference / proof
Text (any kind, including
UILabel,CATextLayer,Core Text, etc).Most of the drawings you do with
CGContextindrawRect:. Even an empty implementation will be rendered offscreen.Blending
resizableImagecan cause blending.Avoiding blended layers when using resizableImages on iOS
Any layer which is not
opaqueand has abackgroundColorwithalphaless than 1.0Any layer with
alphaless than 1.0Any layer with
layer.contentor anyUIImageViewwith aUIImagehaving an alpha channelLayout
The following things will trigger
layoutSubviewsto be called on a UIView:Changing
boundstriggers on the same view and superviewChanging
frametriggers on the same view and superviewChanging
transformorlayer.transformtriggers on superviewNote: I’m referring to real changes where values actually are changing
Contradictory these changes does not trigger
layoutSubviewsto be called:center,layer.position,layer.zPosition,layer.anchorPoint,layer.anchorPointZ.Reference: https://github.com/hfossli/LayoutSubviewsInconsistency
General tips for improving performance
Oftentimes it is better to
blendthan torender offscreen.Consider using
drawRect:instead of having a view with multiple labels and subviews.Draw on a background queue to a
UIImageorCGImageRef.Draw to a
CGLayer(which is cached better onGPUcompared 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
opaqueviews with solid background colorAvoid setting
alphaandlayer.opacityto 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 enableshouldRasterizeon 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