1# Accelerating Compilation Using a Code Cache 2 3## Introduction to Code Cache 4 5JSVM-API provides APIs for creating a code cache and using the code cache to store and manage compiled code to accelerate compilation. The code cache stores the code that has been compiled into native code by the compiler. This helps speed up the execution by avoiding repeated compilation of the same code. The procedure for creating and using a code cache is as follows: 6 7- Use the **compile** APIs to obtain **JSVM_Script**. 8- Call **OH_JSVM_CreateCodeCache** with the passed-in **JSVM_Script** to create a code cache. 9- Save the created code cache. During the next compilation, pass the code cache as a parameter to the **compile** APIs. 10 11The compilation using the code cache greatly reduces the compilation time because the serialized script in the code cache only needs to be deserialized, eliminating the need for parsing and compiling the code. In this way, the compilation process is simplified as a process for reading data. 12 13## Example 14 15The following pseudocode demonstrates a typical use case. If the value of **cacheRejected** is not **true** in the second compilation, the code cache is successfully used. 16 17For details about how to use the JSVM-API, see [JSVM-API Data Types and APIs](./jsvm-data-types-interfaces.md). The following example only demonstrates the call procedure. 18For details about the cross-language interaction, see [JSVM-API Development Process](./use-jsvm-process.md). 19 20```c++ 21#include "napi/native_api.h" 22#include "ark_runtime/jsvm.h" 23#include <hilog/log.h> 24 25void UseCodeCache(JSVM_Env env, JSVM_CallbackInfo info) { 26 // Set compilation parameters. 27 JSVM_Value jsSrc; 28 JSVM_Script script; 29 size_t length = 0; 30 const uint8_t* dataPtr = nullptr; 31 bool cacheRejected = true; 32 static std::string src = R"JS( 33 a = 65536; 34 b = 32768; 35 c = a + b; 36 )JS"; 37 38 // Create a code cache. 39 { 40 JSVM_HandleScope handleScope; 41 OH_JSVM_OpenHandleScope(env, &handleScope); 42 43 // Convert the source code string into a JS string. 44 OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &jsSrc); 45 46 // Compile the JS code. 47 OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script); 48 49 // Run the JS code. 50 JSVM_Value result; 51 OH_JSVM_RunScript(env, script, &result); 52 int value = 0; 53 OH_JSVM_GetValueInt32(env, result, &value); 54 OH_LOG_INFO(LOG_APP, "first run result: %{public}d\n", value); 55 56 if (dataPtr && lengthPtr && *dataPtr == nullptr) { 57 // Save the script compiled from the JS source code to the cache to prevent repeated compilation and improve performance. 58 OH_JSVM_CreateCodeCache(env, script, &dataPtr, &length); 59 } 60 61 OH_JSVM_CloseHandleScope(env, handleScope); 62 } 63 64 // Use the code cache. 65 { 66 JSVM_HandleScope handleScope; 67 OH_JSVM_OpenHandleScope(env, &handleScope); 68 69 // Convert the source code string into a JS string. 70 OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &jsSrc); 71 72 // Use the code cache to compile the JS code. 73 OH_JSVM_CompileScript(env, jsSrc, dataPtr, length, true, &cacheRejected, &script); 74 75 // Run the JS code. 76 JSVM_Value result; 77 OH_JSVM_RunScript(env, script, &result); 78 int value = 0; 79 OH_JSVM_GetValueInt32(env, result, &value); 80 OH_LOG_INFO(LOG_APP, "second run result: %{public}d\n", value); 81 82 OH_JSVM_CloseHandleScope(env, handleScope); 83 } 84 OH_LOG_INFO(LOG_APP, "cache rejected: %{public}d\n", cacheRejected); 85} 86 87// Register the WasmDemo callback. 88static JSVM_CallbackStruct param[] = { 89 {.data = nullptr, .callback = UseCodeCache} 90}; 91static JSVM_CallbackStruct *method = param; 92// Register the C++ WasmDemo callback as a JSVM globalThis.UseCodeCache property for the JS to call. 93static JSVM_PropertyDescriptor descriptor[] = { 94 {"UseCodeCache", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 95}; 96``` 97 98**Expected Result** 99``` 100first run result: 98304 101second run result: 98304 102cache rejected: 0 103``` 104 105## Precautions 106 107In the preceding code, a code cache is used for compilation. In **OH_JSVM_CompileScript(env, jsSrc, dataPtr, length, true, &cacheRejected, &script)**, the **cacheRejected** parameter is passed in to obtain whether the code cache is rejected in the compilation process. This status includes several situations: 108 109- Code cache verification failed 110- Code cache verification successful 111- The code cache is not verified because there is a compilation cache in the memory 112 113**cacheRejected** will be set to **true** in the first case and to **false** in the latter two cases. Therefore, it is important to note that event if **cacheRejected** is **false**, it does not necessarily mean that the code cache is used. 114