1# JSVM Tuning Practices 2 3## JSVM Call Invocation 4 5The process of executing JavaScript (JS) code on a JavaScript virtual machine (JSVM) can be divided into the following layers: 6 7- Native: logic layer for the application to run JS code. In this layer, the APIs provided by the JSVM are used to compile and run JS code and create a code cache. 8- JSVM-API: intermediate layer between C/C++ and V8 JS engine. This layer ensures compatibility with JS engines of different versions and provides standardized practices of JS engines. 9- JSVM: JS engine layer, in which JS code is compiled and executed. 10 11If the application startup is slowed down due to unnecessary overheads generated by using of the JSVM, you can analyze the cause and make optimization in the three layers. 12 13## Accelerating Startup 14 15For the applications that use JSVM, optimization can be made in the cold startup and hot startup. 16In the cold startup, generally, the initial startup, there is no profile or cache that can be used for optimization. 17In the hot startup, the code cache can be used for optimization. 18 19### Reducing Overheads of the JSVM Layer 20 21Some of the overheads in the JSVM layer are generated from compilation. You can adjust the options passed in when JSVM-API is called to reduce the compilation overhead of the JS engine in the main thread. 22In the following example, the **eagerCompile** parameter specifies the compilation behavior. You can enable this option in the startup scenarios to optimize the compilation effect. 23 24```cpp 25/** 26 * ... 27 * @param eagerCompile: Whether to compile the script eagerly. 28 * ... 29 */ 30JSVM_EXTERN JSVM_Status OH_JSVM_CompileScript(JSVM_Env env, 31 JSVM_Value script, 32 const uint8_t* cachedData, 33 size_t cacheDataLength, 34 bool eagerCompile, // Set it to true to enable full compilation. 35 bool* cacheRejected, 36 JSVM_Script* result); 37``` 38 39Using a code cache also helps speed up compilation. For details, see [Accelerating Compilation Using a Code Cache](use-jsvm-about-code-cache.md). 40 41#### Hot Startup: Creating a Code Cache with Adequate Code 42 43To speed up hot startup, create a code cache with adequate code to reduce the compilation overhead. The code coverage of the created code cache determines the optimization effect of the hot startup. 44 45To create a code cache with adequate code, set **eagerCompile** to **true** for the compilation before the code cache is created. In this way, full compilation is performed in V8, which ensures high code coverage of the cache. 46 47This approach, however, causes extra compilation time, which may increase the cold startup time used. Read on to learn the ways to optimize the cold startup in the native layer. 48 49#### Cold Startup: Disabling eagerCompile 50 51Enabling **eagerCompile** increases the compilation time for cold startup. To reduce the compilation time, you can use V8 lazy compile, which delays the compilation of JS code until it is executed. 52 53By disabling **eagerCompile** for the code that may block the main thread, you can speed up the code startup. 54 55### Reducing Time Overheads in the Native Layer 56#### Reducing the Code Cache Impact in Cold Startup 57 58It seems contradictory since you are advised to enable **eagerCompile** for higher hot startup performance and to disable **eagerCompile** for higher cold startup performance. To avoid the trade-off between cold startup and hot startup performance, let's focus on the code cache itself. 59 60Code compilation is required before a code cache is created, and the creation of the code cache generates overheads. 61 62To use a code cache without compromising the cold startup speed in the native layer, you can start another thread to create the code cache. 63 64You can use either of the following methods to achieve this purpose(No API calling is involved as they are used to demonstrate the logic process.): 65 66- Start another thread to complete the code compilation and create a code cache. In this way, you can enable **eagerCompile** for code cache creation and disable it for cold startup. This approach decouples the code cache creation and the application running, and you do not need to consider the time when the code cache is created. However, the peak resource usage during the application running may increase. The pseudo-code of this process is as follows: 67 68``` 69async_create_code_cache() { 70 compile_with_eager_compile(); 71 create_code_cache(); 72 save_code_cache(); 73} 74 75... 76 77if (has_code_cache) { 78 evaluate_script_with_code_cache(); 79} else { 80 start_thread(async_create_code_cache()); 81 evaluate_script_without_code_cache(); 82} 83``` 84 85 86- After all the paths are executed in the startup, start a new thread to create a code cache. In this way, you can create a cache with sufficient code without enabling **eagerCompile** while maintaining the hot startup performance. This approach will not increase the peak resource usage or affect the I/O. However, the time for generating the code cache is restricted. The pseudo-code of this process is as follows: 87 88``` 89async_create_code_cache() { 90 compile_with_out_eager_compile(); 91 create_code_cache(); 92 save_code_cache(); 93} 94 95... 96 97if (has_code_cache) { 98 evaluate_script_with_code_cache(); 99} else { 100 evaluate_script_without_code_cache(); 101} 102 103... 104 105if (script_run_completed) { 106 start_thread(async_create_code_cache()); 107} 108``` 109 110 111### Using Performant JSVM-API 112 113You can use more performant APIs provided by JSVM-API to improve performance. 114 115#### Using IsXXX Instead of TypeOf 116 117To determine the native type of an object, it is inefficient to 118 119use **OH_JSVM_TypeOf** to obtain the object type and check whether the object type is the same as a specific type. 120 121Instead, you can use the **Is**XXX APIs. For details about the JSVM-API used in the following example, see [JSVM-API Data Types and APIs](./jsvm-data-types-interfaces.md). The following example only demonstrates the calling procedure. 122 123- Example (not recommended): 124 125 126```cpp 127bool Test::IsFunction(JSVM_Env env, JSVM_Value jsvmValue) const { 128 // type judement 129 JSVM_ValueType valueType; 130 OH_JSVM_TypeOf(*env, jsvmValue, &valueType); 131 return valueType == JSVM_FUNCTION; 132} 133``` 134 135- Example (recommended): 136 137 138```cpp 139bool Test::IsFunction(JSVM_Env env, JSVM_Value jsvmValue) const { 140 // type judement 141 bool result = false; 142 OH_JSVM_IsFunction(*env, jsvmValue, &result); // Check whether the object type is function. 143 return result; 144} 145``` 146 147The following optimization decreases the code execution time by 150 ms, accounting for about 5% of the performance benefits of an applet. 148 149#### Using OH_JSVM_CreateReference to Avoid Creating Redundant Objects 150 151Generally, the procedure for creating a reference to an object is as follows: 152 153Create an object -> Set a value of the object -> Create the reference to the object 154 155If the object already has the value, you can directly create a reference to the value. 156 157For details about the JSVM-API used in the following example, see [JSVM-API Data Types and APIs](./jsvm-data-types-interfaces.md). The following example only demonstrates the calling procedure. 158 159 160- Example (not recommended): 161 162```cpp 163// (1) Open a handle scope. 164JSVM_HandleScope scope; 165OH_JSVM_OpenHandleScope(*env, &scope); 166// (2) Obtain JSVM_Value. 167JSVM_Value jsvmValue; 168OH_JSVM_GetNull(*env, &jsvmValue); 169// (3) Create a Reference for JSVM_Value and save it. 170JSVM_Value wrappingObject; 171OH_JSVM_CreateObject(*env, &wrappingObject); 172OH_JSVM_SetElement(*env, wrappingObject, 1, jsvmValue); 173OH_JSVM_CreateReference(*env, wrappingObject, 1, &result->p_member->jsvmRef); 174// (4) Close the handle scope. 175OH_JSVM_CloseHandleScope(*env, scope); 176``` 177 178- Example (recommended): 179 180```cpp 181// (1) Open a handle scope. 182JSVM_HandleScope scope; 183OH_JSVM_OpenHandleScope(*env, &scope); 184// (2) Obtain JSVM_Value. 185JSVM_Value jsvmValue; 186OH_JSVM_GetNull(*env, &jsvmValue); 187// (3) Create a Reference for JSVM_Value and save it. 188OH_JSVM_CreateReference(*env, jsvmValue, 1, &result->p_member->jsvmRef); // Create a reference to an object of any type, making your code simpler and more performant. 189// (4) Close the handle scope. 190OH_JSVM_CloseHandleScope(*env, scope); 191``` 192 193This optimization also helps reduce the number of API calls for another applet and decreases the code execution time by 100+ ms, accounting for about 3% of the performance benefits. 194