takeaway
Performance optimization has always been the focus and difficulty in the promotion of various apps, and IQiyi APP is no exception. Before this, iQiyi’s Android App has been leading similar apps in terms of startup speed, but its advantage has been very small. Faced with this bottleneck, iQiyi technical product team set up a special team for the optimization of iQiyi App launch. With the strong support of each team, the startup time was optimized from 1.5s to 0.5s, realizing a huge leap. The optimization practice of iQiyi App startup will be shared below.
The length of this article is 2914 words, and the estimated reading time is 8 minutes. \
Forethought makes sense, unforethought makes waste
As for the startup optimization of App, the first reaction of most technical students is to delay the execution of the task, and then improve the execution efficiency of the code through some optimization of the code implementation. The early optimization strategy of iQiyi App is the same: some tasks that are not necessary to be performed in the startup stage are postponed to be executed after a certain life cycle of the home page, or delayed for a fixed time.
This optimization strategy did work in the early stages, but the benefits later fell short. After years of stable iteration, iQiyi App (Android) has become increasingly rich in functions and large in code volume. The startup phase involves the initialization of many businesses, and there are complex dependencies among businesses. Easily changing the code execution sequence may lead to crashes or some business exceptions. The previous simple delay optimization strategy has been difficult to obtain benefits, and a new optimization scheme is imminent.
To do a good job, he must sharpen his tools
Is there a tool that can directly see the bottleneck of the current task? Those of you who have done Android performance optimization are probably familiar with Systrace and TraceView. But that wasn’t enough. We needed a more intuitive and convenient tool. It can intuitively display the sequence, time interval, thread situation and code location information of the code execution in the start-up stage, so that we can see whether the task scheduling in a certain stage is reasonable at a glance, and then make further targeted optimization according to the actual situation.
But so far, we haven’t found an off-the-shelf tool in the industry that meets this need. However, a year ago, our team developed a Lens development and debugging tool that can access any Android application as an SDK, providing task analysis, network analysis (packet capture), page analysis (view pickup, view hierarchy, etc.), sandbox access, shortcuts, and more. After discussion, our team decided to use Lens to realize the function of task analysis, which could not only complete the startup optimization, but also further improve the function of Lens. Through our team’s continuous improvement and optimization, we have broken through numerous technical difficulties. Finally, the ability to see the performance of a task directly, like a doctor getting a PATIENT’s CT report, is ready for precise optimization.
Startup Time and Thread Information before optimization (Debug)
Start tasks before optimization (Debug)
* * * *
Start task execution timing before optimization
Diagnosis: The startup phase has a large number of threads started, performing a large number of tasks, and the main thread is very intensive.
After full consideration, our team came up with the following optimization plan:
· Task-based: encapsulate the startup code into an independent task according to business logic for easy management and scheduling.
· Concurrency: The tasks in the startup phase should be executed concurrently as much as possible.
· Delay: only necessary tasks for rendering display of the first page will be performed in the startup stage, and the delayed tasks will be reasonably scheduled and triggered after the rendering of the first page is completed.
· Bottom pocket: Design the bottom pocket mechanism to ensure the stable execution of the program.
· Monitoring and optimization: establish a regular monitoring mechanism to monitor task changes and achieve accurate optimization.
Due to the interdependence between tasks in the startup phase, dependent tasks may crash due to incomplete asynchronous execution. To solve this problem, TaskManger, a task manager, was designed to dynamically schedule the execution of tasks based on dependencies.
TaskManager (TM) supports task execution conditions, such as waiting for a task to complete or an event to start. Support for relational and relational or dependencies, support for delayed execution, support for submitting tasks to the main thread or child thread for execution, and so on. In addition, tasks can pass data between tasks, listen for events, and so on.
1. Task-based ****
We reorganized the code in the Application stage, classified and sorted according to the function and business, and extracted multiple task modules. For example, we encapsulated the initialization of the player part in the PlayerInitTask class. Each task class is divided into two parts: task registration and task execution. We set different task execution strategies according to whether the program will start the page display.
PS: Flexible task execution timing setting is also a major advantage of TM.
2. Concurrency and thread convergence ****
After the sorting and reconstruction of starting services are completed, the tasks that can be run in the sub-thread are submitted to the sub-thread for execution, and the time-consuming tasks are divided into multiple sub-tasks to block concurrent execution, so as to make full use of CPU computing power as possible.
In addition, all tasks in the startup stage are connected to TM unified scheduling, which avoids other threads’ preemption of CPU resources and some unnecessary system overhead problems, and completes the convergence of threads in the startup stage. By taking these measures, the number of threads in the startup phase is significantly reduced, and the main thread’s low execution efficiency caused by child threads grabbing CPU resources is eliminated.
Three, delayed
In the startup stage, two main events are set up: advertising display event and home page display event. According to the necessity of business execution, we deferred the tasks that were not necessary to be performed during the startup phase to be performed after these two phases. In addition, dependency conditions are set for each task according to the dependency relationship sorted out before, which solves the crash or service exception caused by the delayed execution of the dependent task. TM also provides some operational monitoring mechanisms that can effectively avoid cyclic dependency problems.
Due to the scheduling management of TM, these tasks will be reasonably scheduled and executed according to the current operating conditions and set task priorities when the running conditions are met, so that a large number of concurrent tasks will not be executed at the same time, resulting in page lag.
Four, out
When the task is delayed, the execution time of the task is uncertain. This can lead to problems where some initialization tasks are not completed when the business needs them — TM provides a task pocket mechanism.
Using the above API ensures that the tasks on which the business depends will be executed, thus eliminating the possibility of business crashes or logical exceptions due to “asynchrony” and “latency”.
5. Monitoring and optimization
1) Blocking wait monitoring ****
Because TM provides a bottom-saving mechanism for tasks, there will theoretically be scenarios that wait for tasks to be completed due to business needs. This problem is more common on low-end devices. To solve this problem, we added the task wait monitoring function to TM, incorporated it into the regular monitoring, and successfully found several blocking wait problems. By notifying the business side for logic optimization, the problem of partially blocking wait is greatly reduced or even avoided, and the user experience is further improved.
2) Start data monitoring ****
We posted the Lens startup data to the back end and set up the startup analysis daily report mechanism. The startup process will be divided, in addition to the startup time, but also the delivery of the program initialization stage, page creation stage, page rendering stage by stage time-consuming data, when a data deterioration is obvious, the server will issue a warning.
3) Comparative analysis of startup tasks of historical versions ****
The latest version of Lens supports comparison and analysis of startup tasks between versions. You can view the difference between two versions of Lens, such as tasks that are added or reduced, and tasks whose running time change exceeds the threshold. By analyzing the changing task information, you can directly “talk” to the relevant business parties and quickly complete the optimization.
At the same time, our team performed other optimizations, such as optimizing code execution efficiency, preloading important presentation data, and so on.
conclusion
Through iterations of several versions, the task scheduling framework, normal monitoring mechanism for starting tasks and task analysis functions are improved.
Optimized startup time and Thread information (Debug)
Now it has been optimized to less than 500ms, and the number of starting tasks and threads at the start stage is also greatly reduced. Lens and TM tools played an important role in this optimization practice. We will share more details about Lens in the future.
end
Maybe you’d like to see more
Effectiveness of article | iQIYI APP key medical technology practice
Open source | iQIYI Qigsaw open-source Android App Bundle based dynamic scheme
Scan the qr code below, more exciting content to accompany you!