Performance optimization for iOS is divided into several parts. Physical aspects: APP slimming, power optimization. Experience: APP startup optimization, interface optimization, memory leakage optimization.

1. The APP

The installation package is made up of executables and resources.

Optimization for resources:

1.1.0 Adopt lossless compression of resources.

1.1.1 Remove unused resources.

Optimization of executable files

1.2.0 compiler optimization to remove some unneeded support. For example, Enable C++ Exceptions, Enable Objective-C Exceptions set to NO, and Other C Flags add -fno-exceptions.

1.2.1 Use AppCode to check unused code

1.2.2 Check for duplicate code

2. Optimization of electric quantity

Minimize CPU and GPU power consumption

Use less timer

Optimize I/O operations

Try not to write data frequently. It is better to write data in batches

Positioning optimization:

If you only want to determine the user’s location, turn off location after confirming the location.

Unless you want real-time navigation, turn off location services early

Do not set the highest accuracy of positioning unless high accuracy is required for the position

When locating in the background, try to set the system to stop updating the location automatically when the user is unlikely to move

Others such as accelerometers, gyroscopes, magnetometers, turn them off if you don’t need them, and turn them off early if you need them to run out to save battery life

3.APP startup optimization

APP startup can be divided into hot startup and cold startup. The startup optimization here is focused on cold startup

The startup time is t1 before main and T2 after main

The main function is preceded by the info.plist loading

1. Load related information, such as the splash screen

2. Create a sandbox and check permissions

Then the mach_O file is loaded

1. If it is a fat binary file, find the part that fits the current CPU

2. Load all dependency mach_O files.

3. Locate internal and external pointer references, such as string functions

4. Load c++ constructors

5. Load the methods in the classification

C++ static variable loading and load function loading

The main function is followed by logical optimizations of the Appdelegate and home page

We can make some optimizations for both T1 and T2 above

T1 :1. Reduce the use of dynamic libraries or accelerate dyLD connection process through static libraries

2. Set the dynamic library to required if you are sure, and optional if you are not sure

3. Compress images

4. Reduce or merge some OC classes

5. Reduce C++ constructors

6. Reduce categories

Reduce the load function or optimize its logic, if possible, by placing it inside the initialize method

T2:1. Does not affect the operation of the user experience, do lazy loading, go all in didFinishLaunchingWithOptions or not

2. The updated version, some of the tripartite initialization, don’t need to didFinishLaunchingWithOptions on initialization, interface are initialized again after a show

3. Some network requests are delayed, some business logic is delayed loading, initialize the third-party SDK, configure the environment required by APP running and initialize some of its own tool classes. If possible, delay loading initialization

4. Interface optimization

1. Interface optimization is mainly for CPU and GPU

The main work of CPU is the creation and destruction of objects, the adjustment of object attributes, layout calculation, text calculation and typesetting, the conversion and decoding of picture formats, the drawing of graphics and other work

GPU’s work is texture rendering synthesis and so on

Try to use lightweight objects, such as Calayer instead of UIView for places that don’t require click time

Do not frequently call UIView properties such as frame, bounds, transform, etc. Minimize unnecessary modifications

Calculate the layout in advance and adjust the corresponding properties at once as needed, not multiple times

Automatic layout consumes more CPU resources than using frame

The image size should be the same as imageView

Control the number of concurrent threads

Try to avoid large numbers of images in a short period of time

Reduce view hierarchies and levels

Reduce transparent views

Try to put time-consuming operations on child threads

Prevent off-screen rendering:

Off-screen rendering refers to creating a new screen buffer beyond the current screen buffer for rendering

Why to prevent off-screen rendering:

It can affect the rendering performance of the GPU: it needs to create a new screen buffer and switch the context several times during rendering. He will first switch from the current context to the off-screen, then, when the rendering is finished, upload the render results to the current screen, and finally switch from the off-screen to the current screen

Those operations affect off-screen rendering

1. The rounded corners

Masktobounds = yes and layer.coradis>0 are both used

2. The shadow

3. The rasterizer

4. The mask

5. Memory leaks

At the code level, we need to have good coding habits. Code should be regular. But sometimes mistakes are possible. We can test it at work, too.

Conventional detection methods:

Static analysis

It mainly analyzes four kinds of problems:

1. Access null Pointers or uninitialized variables

2. Memory management error: Memory leaks

3. Declaration error: Never used variable

4.API call error: library and framework not included

Static analysis will give you a hint

The Leak tool dynamically analyzes memory leaks to pinpoint specific code locations

The allocation tool allows you to view memory allocation

Memory monitors memory usage in real time