preface

Short step without thousands of miles, not small streams into rivers and seas. Learning is like rowing upstream; not to advance is to drop back. I’m a hauler who drifts from platform to platform. Here are 7 tips for performance optimization. Nonsense not to say, directly to everyone dry goods, I hope to help you, excellent people have been praised.

1. What are the reasons why tableView is stuck?

  • 1. The most commonly used is cell reuse, registering reuse identifiers

    When cells are not reused, a new cellHTML is created each time a cell is displayed on the screen

    When you have a lot of data, you accumulate a lot of cells. ios

    To reuse a cell, create an ID for the cell. Whenever a cell needs to be displayed, the buffer pool will be first searched for a cell that can be recycled. Otherwise, cellc++ will not be created again

  • 2. Avoid rearranging the cell

    It takes time to arrange and fill cells. Interviews are usually arranged when the cell is set up

    If you can place the cell in a custom class by itself, swift is laid out at initialization

  • 3. Calculate and cache cell attributes and contents in advance

    When we create cell data source methods, the compiler does not create the cell first and then determine the height of the cell xcode

    After confirming the height of each cell, the cell to be displayed will be created. When scrolling, the height will be calculated every time the cell enters virtual. The height will be estimated in advance to tell the compiler, and then the compiler will establish the cell after knowing the height. This is a way to waste time calculating the cell cache outside the display

  • 4. Reduce the number of controls in the cell

    Try to make the layout of cells roughly the same, different styles of cells can use different reuse identifiers, add controls during initialization, network

    If not, hide the data structure first

  • 5. Do not use ClearColor, no background color, and do not set opacity to 0

    Rendering takes a long time and is multithreaded

  • 6. Use partial updates

    If only a group is updated, use the reloadSection for local updates

  • 7. Load network data, download images, use asynchronous loading, and cache

  • 8. Use addView less to dynamically add views to cells

  • 9. Load cells as required. If a cell rolls quickly, load only the cells in the range

  • 10. Don’t implement useless proxy methods. TableView only complies with two protocols

  • 11. High cache row: estimatedHeightForRow cannot exist at the same time as layoutIfNeed in HeightForRow. Therefore, my personal advice is: whenever the line height is fixed, write the estimated line height to reduce the number of line height calls and improve performance. Instead of writing predictive methods for dynamic line height, use a line height cache dictionary to reduce the number of times your code is called

  • 12. Don’t do extra painting. When you implement drawRect:, its rect parameter is the region to draw. Anything outside of this region is not drawn. For example, in the above example, CGRectIntersectsRect, CGRectIntersection, or CGRectContainsRect can be used to determine whether image and text need to be drawn, and then call the drawing method.

  • 13. Pre-rendered images. There is still a brief pause when a new image appears. The solution is to draw it in a Bitmap context, export it as a UIImage object, and then draw it to the screen;

  • 14. Use the right data structure to store data.

2. How to improve the smoothness of tableView?

  • In essence, it is to reduce the CPU, GPU work, from these two big aspects to improve performance.

    CPU: object establishment and destruction, object property adjustment, layout calculation, text calculation and typesetting, picture format conversion and decoding, image drawing

    GPU: Texture rendering

  • Caton optimization at the CPU level

    Use lightweight objects whenever possible, such as CALayer instead of UIView for places that don’t handle events

    Don’t make frequent calls to UIView properties such as frame, bounds, transform, etc. Minimize unnecessary changes

    Calculate the layout as far in advance as possible, and adjust the corresponding attributes at once if necessary, rather than repeatedly

    Autolayout consumes more CPU resources than setting the frame directly

    The image size should be exactly the same as the UIImageView size

    Control the maximum number of concurrent threads

    Whenever possible, put time-consuming operations into child threads

    Text processing (size calculation, drawing)

    Image processing (decoding, rendering)

  • Caton optimization at the GPU level

    As far as possible to avoid a large number of pictures in a short period of display, as far as possible to display multiple pictures combined

    The maximum texture size that GPU can process is 4096×4096. Once the texture size exceeds this size, IT will occupy CPU resources for processing, so the texture size should not exceed this size as much as possible

    Minimize the number and level of views

    Reduce the size of the transparent view (alpha<1) and set opaque to YES for the opaque view

    Avoid off-screen rendering whenever possible

  • IOS tips for keeping the interface smooth

    1. Pre-typesetting, calculation in advance

    After receiving the data returned by the server, the results of CoreText typesetting, the height of individual controls, and the height of the cell as a whole are calculated as early as possible and stored in the properties of the model. When needed, it can be taken directly from the model to avoid the calculation process.

    Use UILabel as little as possible and use CALayer instead. Avoid AutoLayout’s automatic layout technology and adopt a pure code approach

    2. Pre-render, draw early

    For example, circular ICONS can be processed in the background thread when receiving network data, stored directly in the model data, and called directly after returning to the main thread

    Avoid using CALayer’s Border, corner, Shadow, mask and other techniques, which trigger off-screen rendering.

    3. Draw asynchronously

    4. Global concurrent threads

    5. Efficient asynchronous loading of images

3. From what aspects should APP startup time be optimized?

App startup times can be measured by xcode’s tools. In Xcode’s Product->Scheme- >Edit Scheme->Run->Auguments, set the environment variable DYLD_PRINT_STATISTICS to YES

  • dylib loading time

    The core idea is to reduce dylibs references

    Merge existing Dylibs (preferably within 6)

    Using static libraries

  • rebase/binding time

    The idea is to reduce the pointer inside the DATA block

    Reduce the amount of Object C metadata, reduce the number of Objc classes, and reduce instance variables and functions (conflict with object-oriented design ideas)

    Reduce c++ virtual functions

    Use more Swift structures (Swift is recommended)

  • ObjC setup time

    The core idea is the same as above. This part will not be too time-consuming after optimization in the previous stage

    initializer time

  • Use initialize instead of the load method

    Reduce attribute((constructor)) using C/C ++; Methods such as dispatch_once() pthread_once() STD :once() are recommended

    Swift is recommended

    Do not call the dlopen() method during initialization. Since the loading process is single-threaded and locks are not available, calling dlopen will become multi-threaded, opening up lock consumption and possibly deadlocks

    Do not create threads during initialization

4. How to reduce the size of the APP package?

There are two ways to reduce package size

  • Executable file

    Compiler optimization: Strip Linked Product, Make Strings read-only, Symbols Hidden by Default set to YES Enable C++ Exceptions, Enable objective-C Exceptions set to NO, Other C Flags add -fno-exceptions to detect unused code with AppCode: Menu bar -> Code -> Inspect Code

    Write the LLVM plug-in to detect duplicate code, uncalled code

  • Resources (pictures, audio, video, etc.)

    An optimized approach can compress resources losslessly

    Remove unused resources

5. How to detect off-screen rendering and optimization

  • Xcode ->View Debugging- >Rendering->Run->Color Offscreen-Rendered Yellow

  • Optimizations, such as shadows, add shadow paths when drawing

6. How do I detect layer blending

The “color blended Layers” area indicates that the layer is blended

Instrument- Select Core Animation- select Color Blended Layers

Avoid layer blending:

  • Ensure that the opaque property of the control is set to true to ensure that the backgroundColor is the same color as the parent view and is opaque
  • Do not set alpha below 1 unless otherwise required
  • Make sure there is no alpha channel for UIImage

UILabel layer blending solution:

IOS8 after setting the background color is not transparent color and set the label. The layer. The masksToBounds = YES let label will only render her actual size of the area, can solve a UILabel layer mixed problem

IOS8 used to just set the background color to be opaque

Why do I set the background color and still have layer blending on iOS8?

UILabel has changed in iOS8, before iOS8, UILabel used CALayer as the bottom layer, and in iOS8, the bottom layer of UILabel has changed to _UILabelLayer, and the text has changed. There is an extra transparent edge around the background color that clearly extends beyond the layer’s rectangular area. Setting the layer masksToBounds to YES will crop the layer along the Bounds

7. How to check for memory leaks?

  • There are several ways I know of so far

    Memory Leaks

    Alloctions

    Analyse

    Debug Memory Graph

    MLeaksFinder

  • The memory leaks are as follows:

    Laek Memory is Memory leaked by forgetting to Release.

    Abandon Memory is a kind of Memory that can’t be freed and is referenced in a loop.

At the end

So that’s it for today. I don’t know if these small skills have been clearly expressed, and I hope that all readers will be able to gain something after reading this article, or be able to inspire others. If you need to discuss, you can leave a comment in the comments section. You can also join the iOS community to discuss iOS and look into the future. The end of this article is a benefit for those who want to interview and those who want to learn FLutter.

At the end of the article benefits: the interview will brush interview

FLutter video tutorial takes you hand in hand from start to mastery.