Learning and time to repeat, not also said

In front of the word

Hello everyone, I am the cup, write this series, one is for study and review. The second is for a better reading experience. Read a lot of similar interview questions of the blog or summary PDF. There are two types of content, one is very concise and comprehensive dry goods, very short, but read to understand, remember, rote words and do not know the principle and logic. One is to write a very long and detailed blog, read the back, forget the front, and finally do not know where the focus. In fact, when we interview, it is impossible to read the article as usual, can describe every detail clearly, the interview can mainly tell the key points, and can further ask questions, the principle of these points in series, maintain logical clarity. Hopefully, this series of articles that I’ve written will help you, it’s not long, it’s not short and you can’t understand short answers.

PS: Now use the question-and-answer mode to imitate the interview, put the relevant questions and knowledge points together. This improves the reading experience and helps you remember more.

Insert a picture description here

Begin by

I was waiting in the conference room when a paunchy, middle-aged man in a plaid shirt approached me with his resume. I looked at his hair and thought he must be a great iOS developer. Also have a good look at the cup to write “iOS together into Dachang” series, think now is full of knowledge, just nervous to the throat of the heart, and press down, calm calm, not empty good log, is so confident calm.


Young man, you mentioned in your resume that you have some knowledge of view performance optimization, can you first explain the principles related to image display

Hello, handsome interviewer. CPU, GPU and monitor work together in iOS system in the way shown below. The CPU and GPU are connected by bus. The CPU calculates the display content and submits it to the GPU. After the GPU completes rendering, the rendering result is put into the frame buffer.Here is the flow of the image:

About the division of labor between CPU and GPU:

  • The CPU is responsible for:
    • Object creation and destruction
    • Object property adjustment
    • Layout calculation, text calculation
    • Typesetting, image format conversion and decoding
    • Image drawing
  • GPU is responsible for:
    • Texture rendering
    • Mixing of views
    • Graph generation

Now that you know how graphics display works, do you know why IOS views lag and drop frames?

Caton frame drop

In the standard case, the page slides smoothly at 60 FPS, which means 60 frames refresh per second, and one frame every 16.7ms(1/60 of a second). The time between the two vsyncs above is 16.7ms. If the combined PROCESSING time of the CPU and GPU exceeds 16.7ms, it will cause frame drops and even stutter. When the FPs is below 30, the human eye can perceive a noticeable lag.


The interviewer blew down his hair curtain, thinking, good boy, all answers to the point, then talk a little deeper, then do you know how to monitor the interface lag

Now that we know what’s causing Cadon, we have a way to monitor him.

  • Idea 1: monitor whether the number of frames per second is often below or well below 60FPs.
  • Idea 2: Monitor whether the duration of each frame times out.

Idea one implementation method: CADisplayLinker to count

CADisplayLink can call a selector at the rate that the screen refreshes. In iOS, the normal screen refresh rate is 60 times per second. If you count the number of times the method is executed per second, the refresh rate of the current screen can be determined by the number of times/time.

The second implementation method: monitor the RunLoop of the main thread through the child thread, and judge whether the time between the two state areas of the RunLoop reaches a certain threshold.

Start the child thread and calculate whether the time between the two states reaches a certain threshold in real time. These performance killers can be identified. Assume that six consecutive 50ms timeouts are considered to be slow (including, of course, a single 300ms timeout).

The old interviewer showed a satisfied smile, but to do the play to do a complete set, then you finally say how to optimize the frame jam

The work of image display is jointly completed by CPU and GPU, so the direction and idea of optimization is to minimize their processing time.

Optimization for CPU processing:

  • Save CPU time by creating, adjusting, and destroying objects in child threads
  • Pretypesetting (layout calculations, text calculations) in child threads gives the main thread more time to respond to user interactions
  • Prerendering and pretypesetting for asynchronous text rendering, image codec and other content

Optimization of GPU processing

  • Avoid using CALayer’s Border, corner, shadow, mask and other techniques as much as possible to avoid triggering off-screen rendering
  • Combine as many pictures as possible into a single display to reduce the view hierarchy

The end of the interview

The young man answered well, and will continue with the rest of the interview tomorrow. Oh, my God. There’s more tomorrow. (Forced introduction for next article lol)

conclusion

This paper is mainly aimed at the principle of the image, the cause of the stuck frame, optimization scheme.

  • Principle: CPU is responsible for calculation, GPU is responsible for rendering, rendering results into the frame buffer and then display on the display.
  • Cause: When the processing time of CPU and GPU exceeds 1/60 seconds, there will be frame drop or even lag.
  • Monitoring: There are two ways to monitor frame lag: whether the time between the number of frames and the RunLoop status exceeds the threshold in the monitoring time.

In fact, there are many aspects of view optimization, such as the use of UIView asynchronous rendering, how to avoid off-screen rendering, in the use of TableView how specific optimization is not mentioned, these need another article to describe clearly. You don’t have to put it all together.

Insist to see the students here, you are all talented, I like it.


Beg point attention, add collect, beg share

Creation is not easy, absolutely white whao, hello everyone I am empty cup, the article continues to update every week, if this article is good, think “cup” I have written something to you a little bit with the words of praise 👍 for attention ❤ for share

If there are any mistakes in this blog, please comment, thank you very much!

Search for “empty cup One” in wechat official account or scan the qr code below to read and urge the first time (One or two articles earlier than the blog), you can also get the most organized iOS interview questions, answers and iOS learning materials. This article is available at GitHub github.com/schelling/i… Have included, there is a big factory pilot mind map, also sorted out a lot of my blog documents, welcome Star and perfect, everyone interview can refer to the examination points review, learn and learn together, not to mention.