A CPU and GPU

  • In the process of screen imaging, CPU and GPU play a crucial role
  • Central Processing Unit (CPU) object creation and destruction, object property adjustment, layout calculation, text calculation and typesetting, image format conversion and decoding, Core Graphics
  • Graphics Processing Unit (GPU) texture rendering

How to display the display view below

In iOS, it is double buffering mechanism, with frame caching before and frame caching after

Two screen imaging principle

Three caton causes, solutions and monitoring

Cause of stutter: It takes too long time for CPU or GPU, resulting in incomplete CPU calculation or GPU rendering when vertical signal comes, resulting in frame drop.

  • The main idea of Catton’s solution

(1) reduce CPU and GPU resource consumption as much as possible. (2) according to the brush frame rate of 60FPS, VSync signal will be sent every 16ms

We start from CPU and GPU two aspects to carry on the caton optimization

3.1 Caton Optimization – CPU

  • Try to use lightweight objects, 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., and minimize unnecessary changes

  • Try to calculate the layout in advance, adjust the corresponding attributes at one time if necessary, do not modify the attributes more than once

  • 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

  • Try to put time-consuming operations into child threads

    • Text processing (size calculation, drawing)
    • Image processing (decoding, rendering)

3.2 Caton Optimization – GPU

  • Try to avoid the display of a large number of pictures in a short period of time, as far as possible to display a composite of multiple pictures
  • 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
  • Minimize the number of views and levels
  • Reduce transparent views (alpha<1) and set Opaque to YES for opaque views
  • Try to avoid off-screen rendering

3.3 Off-screen rendering

  1. In OpenGL, the GPU has 2 rendering methods
  • On-Screen Rendering: Current screen rendering, rendering in the screen buffer currently used for display
  • Off-Screen Rendering: Off-screen rendering. Create a new buffer for rendering outside the current screen buffer
  1. Off-screen rendering is a performance drain
  • A new buffer needs to be created
  • In the whole process of off-screen rendering, the context needs to be changed several times, first from the current Screen (on-screen) to off-screen (off-screen); When the off-screen rendering is finished, the rendering results of the off-screen buffer are displayed on the screen, and the context needs to be switched from off-screen to the current screen
  1. What actions trigger an off-screen rendering?
  • rasterizer, layer shouldRasterize = YES
  • mask, the layer mask
  • Rounded corners, while settinglayer.masksToBounds = YES,layer.cornerRadiusGreater than zero

    Solution: consider using CoreGraphics to draw rounded corners, or ask an artist to provide rounded images
  • shadow, layer. ShadowXXX

    If layer.shadowPath is set, off-screen rendering will not occur
  1. Caton detection
  • The “lag” is usually referred to as a time-consuming operation performed on the main thread
  • You can add an Observer to the main RunLoop to monitor the lag by listening for the time it takes for the RunLoop state to change

Main source of electricity consumption

  • CPU,Processing
  • Network,Networking
  • Positioning,Location
  • Images,Graphics

4.1 Optimization of power consumption

  1. Minimize CPU and GPU power consumption
  2. Use less timer
  3. Optimize I/O operations
  • Do not write small data frequently. It is better to write data in batches at a time
  • Consider dispatch_io for reading and writing large amounts of important data, which provides an API for asynchronously operating file I/O based on GCD. The system optimizes disk access with dispatch_io
  • For large amounts of data, it is recommended to use databases (such as SQLite and CoreData).
  1. Network optimization
  • Reduce and compress network data
  • If the result of multiple requests is the same, try to use caching
  • Use breakpoint continuation, otherwise the same content may be transmitted multiple times when the network is unstable
  • Do not attempt to perform network requests when the network is unavailable
  • Allows users to cancel long-running or slow network operations and set appropriate timeouts
  • For example, when downloading video streams, instead of sending small packets, download the entire file directly or in large chunks. If you download ads, download several at a time and display them slowly. If you download emails, download several at a time, not one by one
  1. Location optimization
  • If you just need to quickly determine the user’s location, it’s best to use the requestLocation method of CLLocationManager. After the locating is complete, the locating hardware is automatically powered off
  • If it’s not a navigation app, try not to update your location in real time and turn off location services when you’re done
  • Try to reduce the positioning accuracy, such as try not to use the highest accuracy kCLLocationAccuracyBest
  • Need background position, try to set up pausesLocationUpdatesAutomatically to YES, if the user is unlikely to move the system will automatically suspended position update
  • Try not to use startMonitoringSignificantLocationChanges, priority startMonitoringForRegion:
  1. Hardware detection optimization
  • When the user moves, shakes or tilts the device, motion events are generated, which are detected by hardware such as accelerometers, gyroscopes and magnetometers. These hardware should be turned off when testing is not required

V. Startup of APP

  1. APP startup can be divided into two types
  • Cold Launch: Start the APP from scratch
  • Warm Launch: APP is already in memory and alive in the background. Click the icon again to start APP
  1. The optimization of APP startup time is mainly for cold startup
  2. The APP startup time analysis can be printed by adding environment variables
  • (Edit scheme -> Run -> Arguments) DYLD_PRINT_STATISTICSSet to 1
  • If more detailed information is needed, pleaseDYLD_PRINT_STATISTICS_DETAILSSet to 1

The results

5.1 Startup of APP

The cold start of APP can be summarized into three stages

  • dyld
  • runtime
  • main

5.2 APP startup – DYLD

  • Dynamic Link EditorApple’s dynamic linker, which can be used to loadMach-OFiles (executable files, dynamic libraries, etc.)
  • When the APP is started, dyLD does <1> load the executable of the APP and recursively load all dependent dynamic libraries. <2> When dyLD has loaded the executable and dynamic libraries, it notifies the Runtime for further processing

5.3 APP startup – Runtime

  1. When you start your APP, the Runtime does the following
  • Map_images is called for parsing and processing of executable file contents
  • Call call_load_methods in load_images and call the +load method for all classes and categories
  • Initialize various objC structures (register objC classes, initialize class objects, and so on)
  • Calls C++ static initializers and functions modified by attribute(((constructor))
  1. Up to this point, all symbols (Class, Protocol, Selector, IMP,…) in executables and dynamic libraries All have been successfully loaded into memory in the format managed by the Runtime

5.4 APP startup – main

To summarize

  • The startup of the APP is dominated by dyLD, which loads the executable into memory, along with all the dependent dynamic libraries
  • The Runtime is responsible for loading the structure defined by objC
  • When all initialization is complete, Dyld calls the main function
  • Next came the UIApplicationMain function, AppDelegate application: didFinishLaunchingWithOptions: method

5.5 APP startup optimization

According to different stages

  1. dyld
  • Reduce dynamic libraries, merge some dynamic libraries (periodically purge unnecessary dynamic libraries)
  • Reduce the number of Objc classes and classes, reduce the number of selectors (periodically clean up unnecessary classes and classes)
  • Reduce the number of C++ virtual functions
  • Swift uses structs as much as possible
  1. runtime
  • Replace all attributes ((constructor), C++ static constructors, and ObjC + loads with the +initialize method and dispatch_once method
  1. main
  • Delay as much as possible without affecting the user experience, and don’t put everything into the finishLaunching method
  • According to the need to load

Six installation package slimming

  1. Installation package (IPA) mainly consists of executable files and resources
  2. Resources (pictures, audio, video, etc.)
  • Take lossless compression
  • Remove unused resources: LSUnusedResources
  1. Thin executable files
  • Compiler optimization
  • Strip Linked Product,Make Strings Read-Only,Symbols Hidden by DefaultAnd set the parameter to YES
  • Remove exception support,Enable C++ Exceptions,Enable Objective-C ExceptionsSet it to NO,Other C Flagsadd-fno-exceptions
  • Detect unused Code with AppCode: menu bar -> Code -> Inspect Code
  • Write the LLVM plug-in to detect duplicate code, uncalled code

Seven LinkMap

  • Generate a LinkMap file to view the specific composition of the executable file

Since the | address