Symptom
When an atomic service is opened for the first time or after a long period of inactivity, the duration from tapping the icon until the home page is fully loaded is excessive. During this period, a blank screen or loading animation may be displayed.
Context
Cold start: When an atomic service is started, no process for the atomic service exists in the background of the device. The system must create a new process and complete initialization.
The Atomic Service Cross-Platform Framework (ASCF) is a cross-platform runtime framework for atomic services, implemented based on the JSVM engine and atomic service technology stack. The cold start process of an ASCF atomic service includes framework-specific steps such as the AbilityStage lifecycle, JSVM engine initialization, loading and execution of the app.js file, and creation and rendering of the home page.
The following table lists the phases in the cold start process of an ASCF atomic service.
| Phase | Description |
|---|---|
| Runtime environment preparation | The runtime environment of an atomic service includes the atomic service process, HarmonyOS system components, UI elements (such as the navigation bar and tab bar), the WebView container for page rendering, and the execution environment for executing JavaScript code. |
| Logic-layer code injection | When an atomic service is started, the atomic service configuration and code need to be read from the code package and injected into the JavaScript engine. During the injection of the main package code, the App.onLaunch and App.onShow lifecycles of the atomic service are triggered. |
| View-layer code injection | After the WebView container is ready, the page structure and style resources required for rendering the page accessed by the user are loaded. Logic-layer code injection and view-layer code injection are performed concurrently. |
| Page rendering | After the logic-layer code injection is complete, ASCF initializes the page component tree for the page accessed by the user, generates the initial data for first screen rendering, and sends the data to the view layer. Based on the initial data for first screen rendering and the page structure and style resources at the view layer, the home page is rendered and displayed. During this process, the Page.onShow, Page.onLoad, and Page.onReady lifecycles are triggered. |
ASCF embeds trace points in key phases of cold start. You can use the Launch analysis capability of DevEco Profiler to capture the time consumption data during cold start and locate the performance bottlenecks by searching for trace keywords.
Trace points for ASCF cold start
Use DevEco Profiler to capture the trace data during the cold start of the ASCF atomic service, and check the time consumption in each phase based on the ASCF trace keywords listed in the following table.
The table below lists all trace points embedded by ASCF in the cold start process, ordered by execution sequence.
| Trace Keyword | Phase | Description |
|---|---|---|
| H:StageCreate | Runtime environment preparation | Total time taken to start the AscfAbilityStage of the atomic service, including configuration parsing and JSVM engine initialization. |
| H:UIAbilityCreate | Runtime environment preparation | Total time taken to start the AscfUIAbility of the atomic service, including window information initialization and startup parameter loading. |
| H:MainPageAppear | Runtime environment preparation | Total time taken to create AscfMainPage, including UI context initialization, API registration, and system component creation. |
| H:LaunchMiniApp | Logic-layer code injection | Total time taken to load and execute the app.js file of the atomic service, from the start of loading to the completion of loading. |
| H:AscfRenderPageAppear | View-layer code injection | Time taken for AscfRenderPage, including subpackage loading and page configuration information reading. |
| H:NewPageRender | View-layer code injection | Rendering duration when navigating to a new page, including page generation and NavDestination rendering. |
| H:WebPageLoad | Page rendering | Total duration from view-layer code injection to home page rendering. |
| H:JSVM_Exe_Code | Runtime | Duration for executing JS code at the logic layer, including all service logic execution, API callbacks, and page lifecycle callbacks. |
Troubleshooting
Use the Launch analysis capability of DevEco Profiler to capture trace data during the cold start of the ASCF atomic service.
Search for the trace keywords listed in the preceding table in the trace data to view the time consumed in each phase.
Locate the specific causes by referencing the detailed descriptions for trace points with long duration:
Logic-layer code injection duration: Check the duration of H:LaunchMiniApp to analyze the time consumed in logic-layer code injection and execution.
View-layer code injection duration: Check the overall duration from the end of H:AscfRenderPageAppear to the start of H:WebPageLoad to analyze the time consumed in loading the page structure and style resources at the view layer.
Page rendering duration: Check the duration of H:WebPageLoad to analyze the time consumed in rendering the page based on the page structure and page data.
Runtime logic duration: Check the duration of H:JSVM_Exe_Code to analyze whether the JS execution at the logic layer blocks data update.
Conclusion
Common causes of slow cold start of an ASCF atomic service are as follows:
The app.js file for initializing the logic layer of the atomic service (app, page, and component registration) is too large, resulting in a long loading time.
The initialization logic of onLaunch and onShow in app.js is too complex, resulting in long execution time.
The page structure and style resources at the view layer are complex. Code injection and loading at the view layer take a long time.
The synchronous API calls at the logic layer takes a long time, affecting timely data update to the view layer and blocking page data rendering.
Home page data is obtained through network requests in onLoad, which takes a long time. As a result, the overall cold start duration is long.
Recommendation
Package splitting optimization: Properly use the package splitting strategy. Place non-home-page pages in subpackages for on-demand loading to reduce the size of the main package and the loading time of the home page.
Reducing initialization overhead in app.js: Change the unnecessary initialization logic of onLaunch and onShow in app.js to asynchronous execution to avoid blocking the startup process. Defer time-consuming operations until the home page rendering is complete.
Lower home page component complexity: Simplify the component structure of the home page and avoid excessive page structure and style resources. Enable the render-time injection feature for components to reduce the number of components that need to be loaded during startup.
Logic-layer code optimization: Avoid complex calculation logic and minimize synchronous API calls in the atomic service initialization code and startup-related lifecycles.
Home page data loading optimization: Use the data prefetch capability to load data in advance, reducing the waiting time incurred in onLoad of the home page.