Dependency module loading method, device and computer program product
By determining whether the dependent module has been loaded in JavaScript development projects and writing to the storage structure when it is not loaded, the problem of multiple loading of dependent modules is solved and the loading performance and responsiveness of script files are improved.
Patent Information
- Application Number
- CN202210153119.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-18
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2042-02-18
AI Technical Summary
In JavaScript development projects, due to the multi-layer nesting relationship of dependent modules, dependent modules are loaded multiple times, resulting in a waste of resources and low running efficiency of script files.
By determining whether the dependent module has been loaded when loading it, if it has not been loaded, it is loaded in the usual way and the object information is written into the preset storage structure. Subsequently, it is obtained directly from the storage structure to avoid repeated loading.
It reduces the number of loading times of dependent modules, optimizes the loading performance of script files, saves script execution time, and improves the responsiveness of script applications.
Smart Images

Figure CN114637548B
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a method and device for loading a dependency module, and a computer program product. Background Art
[0002] JavaScript is a literal scripting language, which is currently widely used in the development of Web applications. In a JavaScript development project, there are often complex dependencies between modules, which can be realized by calling each other between the current module and the modules on which the current module depends. When executing the current module, the dependent module of the current module can be loaded first. After loading, the object corresponding to the dependent module will be generated, and the object information will be returned to the current module. The current module can call the dependent module based on the object information.
[0003] However, in the process of loading dependent modules, dependent modules may be loaded multiple times. For example, some modules may be loaded multiple times when there are multiple layers of nested dependencies. For example, module A depends on modules B and C, and modules B and C both depend on module D. At this time, when executing module A, modules B and C will be loaded first, and when loading modules B and C, module D will be loaded once respectively, that is, module D will be loaded twice, which is easy to cause resource waste and ultimately lead to low running efficiency of script files. Summary of the invention
[0004] Embodiments of the present disclosure provide a method, device, and computer program product for loading a dependency module.
[0005] In a first aspect, an embodiment of the present disclosure provides a method for loading a dependent module, including:
[0006] Execute the script file;
[0007] When loading a dependent module, determining whether the dependent module has been loaded;
[0008] When the dependent module has not been loaded, loading the dependent module;
[0009] The object information of the loaded dependent module is written into a preset storage structure, so that when the dependent module is subsequently loaded, the object information of the dependent module is directly obtained from the preset storage structure.
[0010] Further, when loading a dependent module, determining whether the dependent module has been loaded includes:
[0011] Determine the path information of the dependent module to be loaded;
[0012] Matching the path information with the key information written into the preset storage structure;
[0013] When the path information does not match any key information in the preset storage structure, it is determined that the dependent module has not been loaded.
[0014] Further, writing the object information of the loaded dependent module into a preset storage structure includes:
[0015] Using the path information of the dependent module to be loaded as key information and the object information of the dependent module as the value corresponding to the key information to generate a key-value pair;
[0016] The key-value pair is written into a preset storage structure.
[0017] Furthermore, the method further comprises:
[0018] When the path information matches one of the key information in the preset storage structure, it is determined that the loading module has been loaded, and the value corresponding to the matched key information is used as the object information of the dependent module.
[0019] Furthermore, the method further comprises:
[0020] Get the script file to be executed;
[0021] Compiling the script file;
[0022] During the compilation process, determining the location of the original loading code of the dependent module in the script file;
[0023] Based on the position, the original loading code of the dependent module is replaced with a custom loading code; the custom loading code includes a first code and a second code, the first code is used to determine whether the dependent module has been loaded, the second code is used to execute the original loading code when the dependent module has not been loaded, and after the execution of the original loading code is completed, write the object information of the loaded dependent module into a preset storage structure.
[0024] Furthermore, the customized loading code also includes a third code, and the third code is used to directly obtain the object information of the dependent module from the preset storage structure when the dependent module has been loaded.
[0025] Furthermore, the first code is implemented by matching the path information of the dependent module specified in the original loading code with the key information in the preset storage structure; the second code is implemented by calling the original recording code when the path information does not match any key information in the preset storage result, and after the original loading code is executed, using the path information of the dependent module as the key information and writing the object information of the dependent module returned by the original loading code as the value corresponding to the key information into the preset storage structure; the third code is implemented by using the value corresponding to the matched key information as the object information of the dependent module and returning it when the path information matches one of the key information in the preset storage result.
[0026] Furthermore, the script file is a JavaScript file.
[0027] In a second aspect, an embodiment of the present invention provides a device for loading a dependent module, which includes:
[0028] An execution module, configured to execute a script file;
[0029] A first determination module is configured to determine whether the dependent module has been loaded when loading the dependent module;
[0030] A first acquisition module is configured to acquire the object information of the dependent module from the preset storage structure when the dependent module is loaded;
[0031] A loading module, configured to load the dependent module when the dependent module has not been loaded;
[0032] The writing module is configured to write the object information of the loaded dependent module into a preset storage structure, so that when the dependent module is subsequently loaded, the object information of the dependent module can be directly obtained from the preset storage structure.
[0033] The functions can be implemented by hardware or by hardware executing corresponding software. The hardware or software includes one or more modules corresponding to the functions.
[0034] In one possible design, the structure of the above-mentioned device includes a memory and a processor, the memory is used to store one or more computer instructions that support the above-mentioned device to execute the above-mentioned corresponding method, and the processor is configured to execute the computer instructions stored in the memory. The above-mentioned device may also include a communication interface for the above-mentioned device to communicate with other devices or communication networks.
[0035] In a third aspect, an embodiment of the present disclosure provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the method described in any one of the above aspects.
[0036] In a fourth aspect, an embodiment of the present disclosure provides a computer-readable storage medium for storing computer instructions used by any of the above-mentioned devices, and when the computer instructions are executed by a processor, they are used to implement the method described in any of the above-mentioned aspects.
[0037] In a fifth aspect, an embodiment of the present disclosure provides a computer program product, which includes computer instructions, and when the computer instructions are executed by a processor, they are used to implement the method described in any of the above aspects.
[0038] The technical solution provided by the embodiments of the present disclosure may have the following beneficial effects:
[0039] In the process of executing a script file, if a loading operation of a dependent module is encountered in the disclosed embodiment, it is possible to first determine whether the dependent module has been loaded. If it has not been loaded, the dependent module can be loaded in a conventional manner, and after the loading is successful, the object information of the dependent module generated during the loading process is written into a preset storage structure, so that the object information of the dependent module can be directly obtained from the preset storage structure when the dependent module is loaded next time, without having to repeat the loading process of the dependent module. In this way, the number of times the dependent module is loaded can be reduced, the loading performance of the script file can be optimized, the script execution time can be saved, and the responsiveness of the script application can be improved.
[0040] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] Other features, objectives and advantages of the present disclosure will become more apparent through the following detailed description of non-limiting embodiments in conjunction with the accompanying drawings. In the accompanying drawings:
[0042] Figure 1 A flowchart showing a method for loading a dependent module according to an embodiment of the present disclosure is shown;
[0043] Figure 2 A schematic diagram showing an application scenario of a method for loading a dependent module according to an embodiment of the present disclosure;
[0044] Figure 3 A structural block diagram of a loading device for a dependent module according to an embodiment of the present disclosure is shown;
[0045] Figure 4It is a structural schematic diagram of an electronic device suitable for implementing a method for loading a dependent module according to an embodiment of the present disclosure. DETAILED DESCRIPTION
[0046] Hereinafter, exemplary embodiments of the present disclosure will be described in detail with reference to the accompanying drawings so that those skilled in the art can easily implement them. In addition, for the sake of clarity, parts not related to the description of the exemplary embodiments are omitted in the accompanying drawings.
[0047] In the present disclosure, it should be understood that terms such as "include" or "have" are intended to indicate the presence of features, numbers, steps, behaviors, components, parts, or a combination thereof disclosed in the specification, and do not exclude the possibility that one or more other features, numbers, steps, behaviors, components, parts, or a combination thereof exist or are added.
[0048] It should also be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present disclosure may be combined with each other. The present disclosure will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.
[0049] The details of the embodiments of the present disclosure are described in detail below through specific examples.
[0050] Figure 1 FIG. 1 is a flow chart showing a method for loading a dependent module according to an embodiment of the present disclosure. Figure 1 As shown, the method for loading the dependent module includes the following steps:
[0051] In step S101, the script file is executed;
[0052] In step S102, when loading a dependent module, it is determined whether the dependent module has been loaded;
[0053] In step S103, when the dependent module is loaded, the object information of the dependent module is obtained from the preset storage structure;
[0054] In step S104, when the dependent module has not been loaded, the dependent module is loaded;
[0055] In step S105, the object information of the loaded dependent module is written into a preset storage structure, so that when the dependent module is subsequently loaded, the object information of the dependent module is directly obtained from the preset storage structure.
[0056] In this embodiment, the script file can be a JavaScript file. The dependent module can be other modules that the current module needs to use during execution. A module can be understood as a section of code that implements a certain function or functions, in which objects, properties of objects, variables, functions, etc. can be defined. Assuming that in a JavaScript project, module A needs to use the functions in module B and module C when implementing its functions, it can be understood that module A depends on module B and module C. In the process of executing module A, the code files of module B and module C need to be loaded into the memory first, and the code files of module B and module C are executed, so as to generate information about the objects in module B and module C, and the information of the objects can be provided to module A so as to use the functions of module B and module C in module A. It can be understood that if module B or module C depends on other modules, the modules on which module B and module C depend need to be loaded successfully first, and then the object information of module B or module C is returned to module A.
[0057] In JavaScript, you can use the loader Require.JS to load a module's code file into memory and execute the code file. Require.JS generates the module's object information by executing the code file.
[0058] It is understandable that the module loader in JavaScript is not limited to Require.JS, and Import / Export etc. can also be used. In addition, the script file in the embodiment of the present disclosure is not limited to JavaScript scripts, and can also be a script in other languages. As long as it involves the loading of dependent modules, the above-mentioned dependent module loading method proposed in the embodiment of the present disclosure can be applied.
[0059] In order to more clearly describe the technology of the present disclosure, the following embodiments are all described using JavaScript scripts as examples.
[0060] After the script executor receives the script file, it can interpret and execute the script statements therein one by one. After executing the loading entry function of the dependent module to be loaded, it can first determine whether the dependent module to be loaded has been loaded before. The dependent module has been loaded before can be understood as before executing the loading entry function of the dependent module to be loaded, when the code in the code file that has been executed has been executed, the code for loading the dependent module has been executed. At this time, it can be determined that the code file of the dependent module has been loaded into the memory, and the object information of the dependent module has also been generated, so there is no need to load the dependent file again.
[0061] If the dependent module has not been loaded before, it can be loaded in a conventional loading manner, which may include but is not limited to loading the code file of the dependent module into memory by a loader, and generating object information of the dependent module by executing the code file. The object information can be provided to the dependent module for calling the object.
[0062] After the dependent module is loaded successfully, the disclosed embodiment can also write the object information of the dependent module into a preset storage structure, such as a map storage structure in JavaScript. The map storage structure is a key-value pair structure, and the map storage structure can be stored in a cache. That is, when the disclosed embodiment executes a script file, if a dependent module is loaded for the first time, the dependent module can be loaded in a conventional manner, and after the loading is successful, the object information of the dependent module generated during the loading process can be cached; and when the dependent module is not loaded for the first time, the object information of the dependent module can be directly found from the cache without reloading the dependent module. In this way, the module loading performance of the script file during the execution of the script file can be optimized.
[0063] It should be noted that in the conventional loading method of the dependent module, the loading process is directly executed for the dependent module specified in the loading entry function, and the judgment process of whether it has been loaded is not performed, and the object information of the loaded dependent module is not written into the preset storage structure. The embodiment of the present disclosure improves the conventional loading method. Before loading the dependent module, it is first judged whether the dependent module has been loaded. If it has not been loaded, it is loaded using the conventional loading method. After the loading is completed, the object information of the dependent module is written into the preset storage structure, so that the next time the same dependent module is loaded, the object information of the dependent module can be directly obtained from the preset storage structure without repeating the loading process.
[0064] In the process of executing a script file, if a loading operation of a dependent module is encountered in the disclosed embodiment, it is possible to first determine whether the dependent module has been loaded. If it has not been loaded, the dependent module can be loaded in a conventional manner, and after the loading is successful, the object information of the dependent module generated during the loading process is written into a preset storage structure, so that the object information of the dependent module can be directly obtained from the preset storage structure when the dependent module is loaded next time, without having to repeat the loading process of the dependent module. In this way, the number of times the dependent module is loaded can be reduced, the loading performance of the script file can be optimized, the script execution time can be saved, and the responsiveness of the script application can be improved.
[0065] In an optional implementation of this embodiment, step S102, i.e., the step of determining whether the dependent module has been loaded when loading the dependent module, further includes the following steps:
[0066] Determine the path information of the dependent module to be loaded;
[0067] Matching the path information with the key information written into the preset storage structure;
[0068] When the path information does not match any key information in the preset storage structure, it is determined that the dependent module has not been loaded.
[0069] In this optional implementation, in the script file, the path information of the dependent module to be loaded is specified in the parameters of the loading entry function (such as the require function) of the dependent module, and the path information may include the storage path of the code file of the dependent module. The path information of the same dependent module is the same, so when judging whether the dependent module to be loaded currently has been loaded before, the path information of the dependent module can be determined from the parameters of the loading entry function, and the path information is matched with the preset storage structure, which can be a storage structure in the form of a key-value pair, including key information and the value corresponding to the key information; the key information can store the path information of the dependent module, and the value corresponding to the key information can store the object information generated after the dependent module is successfully loaded. When matching the path information with the preset storage structure, the path information can be matched with the key information in the preset storage structure. If the match is successful, it means that the object information of the dependent module is stored in the preset storage structure, that is, the dependent module has been loaded before, and if the match is unsuccessful, it means that the object information of the dependent module is not stored in the preset storage structure, that is, the dependent module has not been loaded before. It can be seen that using the preset storage structure to store the object information of the dependent module after successful loading can be used to determine whether the dependent module to be loaded has been loaded, and when it has been loaded, the object information of the dependent module can be directly obtained from the preset storage structure without reloading it.
[0070] In an optional implementation of this embodiment, step S104, i.e., the step of writing the object information of the loaded dependent module into a preset storage structure, further includes the following steps:
[0071] Using the path information of the dependent module to be loaded as key information and the object information of the dependent module as the value corresponding to the key information to generate a key-value pair;
[0072] The key-value pair is written into a preset storage structure.
[0073] In this optional implementation, if the dependent module has not been loaded, the dependent module can be loaded into the memory, and object information corresponding to the dependent module can be generated. The understanding information of the dependent module can be used as key information, and its object information can be used as the value of the key information to generate a key-value pair. The key-value pair can be stored in a preset storage structure, so that when the dependent module needs to be loaded later, the dependent module can be called directly after obtaining the object information from the preset storage structure without reloading the dependent module.
[0074] In an optional implementation of this embodiment, the method further includes the following steps:
[0075] When the path information matches one of the key information in the preset storage structure, it is determined that the loading module has been loaded, and the value corresponding to the matched key information is used as the object information of the dependent module.
[0076] In this optional implementation, as described above, if the object information of the dependent module currently to be loaded is stored in the key information of the preset storage structure, it means that the dependent module has been loaded before, and the object information of the dependent module has been stored in the preset storage structure. Therefore, the object information of the dependent module can be directly obtained based on the value corresponding to the matching key information.
[0077] In an optional implementation of this embodiment, the method further includes the following steps:
[0078] Get the script file to be executed;
[0079] Compiling the script file;
[0080] During the compilation process, determining the location of the original loading code of the dependent module in the script file;
[0081] Based on the position, the original loading code of the dependent module is replaced with a custom loading code; the custom loading code includes a first code and a second code, the first code is used to determine whether the dependent module has been loaded, the second code is used to execute the original loading code when the dependent module has not been loaded, and after the execution of the original loading code is completed, write the object information of the loaded dependent module into a preset storage structure.
[0082] In this optional implementation, after receiving the script file to be executed, the script file may be scanned and detected first, and the scanning and detecting process may be, for example, a detection process for sensitive codes in the script file. Of course, it is understandable that the scanning and detecting process may also be a common detection process for replacing the original loading code of the dependent module with the customized loading code.
[0083] During the scanning and detection process of the script file, the script file can be statically compiled, and then the location of the original loading code of the dependent module can be determined by using the syntax analysis tree obtained by the compilation. In the JavaScript script file, the original loading code can be the code including the loading entry function require(). After the syntax analysis tree is generated during the compilation process, the location of the original loading code in the script file can be determined by matching whether the require function is included.
[0084] Based on the position of the original loading code, the original loading code in the script file can be replaced with a custom loading code. For example, the loading code can be defined as an X_require() function, and the require() function in the script file can be replaced with the X_require() function.
[0085] The custom loading code may include but is not limited to a first code and a second code; the first code is used to determine whether the dependent module has been loaded before; the second code is used to execute the original loading code when it is determined that the dependent module has not been loaded. It should be noted that the original loading code is also the conventional loading code of the dependent module, which is implemented in JavaScript using the require() function, and during the script execution process, the require() function can load the dependent module in a conventional manner.
[0086] In the static compilation process of the script file, the embodiment of the present disclosure replaces the original loading code with the customized loading code that can realize the above functions, so that when the loading code of the dependent module is executed during the execution process, it can first determine whether the dependent module has been loaded, and perform regular loading if it has not been loaded, and write the loaded object information into the preset storage structure. In this way, the original loading code can be replaced during the static compilation process of the script file without having to be replaced during the execution process of the script file, which can further save the execution time of the script file and optimize the performance of the script execution system.
[0087] In an optional implementation of this embodiment, the customized loading code further includes a third code, and the third code is used to directly obtain the object information of the dependent module from the preset storage structure when the dependent module has been loaded.
[0088] In this optional implementation, the custom loading code also includes a third code, which is used to directly obtain the object information of the dependent module from the preset storage structure when the dependent module has been loaded. As described above, the original loading code will re-execute the loading process regardless of whether the dependent module to be loaded has been loaded before. In order to reduce the number of times the same dependent module is loaded, the embodiment of the present disclosure replaces the original loading code with the custom loading code. In the custom loading code, it is first determined whether the dependent module has been loaded. If it has not been loaded, the original loading code is still used for loading, and after the loading is successful, the object information of the dependent module is written into the preset storage structure. When the dependent module has been loaded, the object information of the dependent module is directly obtained from the preset storage structure without executing the loading process. Therefore, the above steps can be implemented in the custom loading code, and after the original loading code is replaced with the custom loading code, the above steps are performed when the script file is executed.
[0089] In an optional implementation of the present embodiment, the first code is implemented by matching the path information of the dependent module specified in the original loading code with the key information in the preset storage structure; the second code is implemented by calling the original recording code when the path information does not match any key information in the preset storage result, and after the original loading code is executed, using the path information of the dependent module as the key information and writing the object information of the dependent module returned by the original loading code as the value corresponding to the key information into the preset storage structure; the third code is implemented by using the value corresponding to the matched key information as the object information of the dependent module and returning it when the path information matches one of the key information in the preset storage result.
[0090] In this optional implementation, as described above, three parts of code can be included in the custom loading code, namely the first code, the second code and the third code, and the original loading code can be replaced with the custom loading code, so that during the execution of the script file, when the custom loading code is executed, the following functions can be implemented, namely, first determine whether the dependent module to be loaded has been loaded, and when it has not been loaded, call the original loading code to execute the loading process, and after the loading is successful, write the object information of the dependent module into the preset storage structure, and when it has been loaded, directly obtain the object information of the dependent module from the preset storage structure without executing the loading process.
[0091] In some embodiments, the preset storage structure can store data in the form of key-value pairs, for example, in Javascript, a map structure can be used for storage. The key information in the preset storage structure can store the path information of the dependent module, and the value corresponding to the key information can store the object information generated after the dependent module is successfully loaded. After a dependent module is successfully loaded for the first time, the key-value pair corresponding to the dependent module can be stored in the preset storage structure, and in the subsequent loading process, the object information can be directly obtained from the preset storage structure based on the path information of the dependent module.
[0092] Therefore, in the disclosed embodiment, the first code can be implemented as matching the path information of the dependent module specified in the original loading code with the key information in the preset storage structure; the second code can be implemented as calling the original recording code when the path information does not match any key information in the preset storage result, and after the original loading code is executed, the path information of the dependent module is used as the key information, and the object information of the dependent module returned by the original loading code is written into the preset storage structure as the value corresponding to the key information; the third code can be implemented as when the path information matches one of the key information in the preset storage result, the value corresponding to the matched key information is used as the object information of the dependent module and returned. It should be noted that the path information of the same dependent module is the same, so the same path information corresponds to only one key-value pair.
[0093] Figure 2 A schematic diagram of an application scenario of a method for loading a dependent module according to an embodiment of the present disclosure is shown. Figure 2As described, the client pages in the service platform can be written in advance by programmers using JavaScript script files. The programmers upload all the script files that implement the client pages in the service platform to the server. The server scans and detects these script files in advance. During the scanning and detection process, these script files can be statically compiled, and the positions of the loading entry functions require() of the dependent modules in these script files can be analyzed based on the syntax tree in the compilation results, and then the loading entry function require() is replaced with a custom X_require() function based on the position. The custom X_require() function first checks whether the path information of the dependent module to be loaded in the loading entry function require() is stored in the preset map storage structure. When the path information is not stored in the preset map storage structure, it means that the dependent module has not been loaded. Therefore, the code in the replaced loading entry function require() is executed, and then the object information generated in the execution result is written into the preset map storage structure. When the path information is stored in the preset map storage structure, it means that the dependent module has been loaded. At this time, the value corresponding to the path information is directly read from the preset map storage structure, and the value information is used as the object information obtained after the dependent module is loaded.
[0094] After all script files have been checked in the above manner, they can be stored in the server, and these script files can be associated with the client page. In certain embodiments, these script files can also be pushed to the client and stored in the client local area.
[0095] When a user opens a page through a client, the script file associated with the page is obtained from the server or the client locally, and the script file is executed. In the process of executing the script file, once the loading entry function of the dependent module (that is, the custom X_require function) is encountered, the following functions are implemented by executing the code in the X_require function: when the dependent module has been loaded, the object information of the dependent module is directly obtained from the map storage structure, and when the dependent module has not been loaded, the loading process is executed through the loading function require, and the path information of the dependent module and the object information generated after loading are stored in the map storage structure in the form of key-value pairs.
[0096] During the execution of the script file, the page can be presented on the client interface, and corresponding responses can also be made based on the user's operations.
[0097] For example, when a user accesses a bus details page through a client of a map navigation application, the client sends a request to the server to browse the bus details page. After receiving the request, the server executes the JS file associated with the bus details page. During the execution of the JS file, the custom X_require function in the embodiment of the present disclosure is used to load the dependent module. When the dependent module has been loaded, the object information of the dependent module is directly obtained from the map storage structure of the memory. When the dependent module has not been loaded, the loading process is executed through the JS loading function require, and the path information of the dependent module and the object information generated after loading are stored in the map storage structure in the form of key-value pairs. In this way, the server can quickly return the bus details page to the client for display. Experiments show that when accessing the bus details page in this way based on the embodiment of the present disclosure, 60-70ms of response time can be saved each time.
[0098] The following are embodiments of the apparatus of the present disclosure, which can be used to execute embodiments of the method of the present disclosure.
[0099] Figure 3 FIG. 1 is a block diagram of a device for loading a dependency module according to an embodiment of the present disclosure. The device can be implemented as part or all of an electronic device through software, hardware, or a combination of both. Figure 3 As shown, the loading device of the dependent module includes:
[0100] An execution module 301 is configured to execute a script file;
[0101] A first determination module 302 is configured to determine whether the dependent module has been loaded when loading the dependent module;
[0102] A first acquisition module 303 is configured to acquire object information of the dependent module from the preset storage structure when the dependent module is loaded;
[0103] A loading module 304, configured to load the dependent module when the dependent module has not been loaded;
[0104] The writing module 305 is configured to write the object information of the loaded dependent module into a preset storage structure, so that when the dependent module is subsequently loaded, the object information of the dependent module is directly obtained from the preset storage structure.
[0105] In this embodiment, the script file can be a JavaScript file. The dependent module can be other modules that the current module needs to use during execution. A module can be understood as a section of code that implements a certain function or functions, in which objects, properties of objects, variables, functions, etc. can be defined. Assuming that in a JavaScript project, module A needs to use the functions in module B and module C when implementing its functions, it can be understood that module A depends on module B and module C. In the process of executing module A, the code files of module B and module C need to be loaded into the memory first, and the code files of module B and module C are executed, so as to generate information about the objects in module B and module C, and the information of the objects can be provided to module A so as to use the functions of module B and module C in module A. It can be understood that if module B or module C depends on other modules, the modules on which module B and module C depend need to be loaded successfully first, and then the object information of module B or module C is returned to module A.
[0106] In JavaScript, you can use the loader Require.JS to load a module's code file into memory and execute the code file. Require.JS generates the module's object information by executing the code file.
[0107] It is understandable that the module loader in JavaScript is not limited to Require.JS, and Import / Export etc. can also be used. In addition, the script file in the embodiment of the present disclosure is not limited to JavaScript scripts, and can also be a script in other languages. As long as it involves the loading of dependent modules, the above-mentioned dependent module loading device proposed in the embodiment of the present disclosure can be applied.
[0108] In order to more clearly describe the technology of the present disclosure, the following embodiments are all described using JavaScript scripts as examples.
[0109] After the script executor receives the script file, it can interpret and execute the script statements therein one by one. After executing the loading entry function of the dependent module to be loaded, it can first determine whether the dependent module to be loaded has been loaded before. The dependent module has been loaded before can be understood as before executing the loading entry function of the dependent module to be loaded, when the code in the code file that has been executed has been executed, the code for loading the dependent module has been executed. At this time, it can be determined that the code file of the dependent module has been loaded into the memory, and the object information of the dependent module has also been generated, so there is no need to load the dependent file again.
[0110] If the dependent module has not been loaded before, it can be loaded in a conventional loading manner, which may include but is not limited to loading the code file of the dependent module into memory by a loader, and generating object information of the dependent module by executing the code file. The object information can be provided to the dependent module for calling the object.
[0111] After the dependent module is loaded successfully, the disclosed embodiment can also write the object information of the dependent module into a preset storage structure, such as a map storage structure in JavaScript. The map storage structure is a key-value pair structure, and the map storage structure can be stored in a cache. That is, when the disclosed embodiment executes a script file, if a dependent module is loaded for the first time, the dependent module can be loaded in a conventional manner, and after the loading is successful, the object information of the dependent module generated during the loading process can be cached; and when the dependent module is not loaded for the first time, the object information of the dependent module can be directly found from the cache without reloading the dependent module. In this way, the module loading performance of the script file during the execution of the script file can be optimized.
[0112] It should be noted that in the conventional loading method of the dependent module, the loading process is directly executed for the dependent module specified in the loading entry function, and the judgment process of whether it has been loaded is not performed, and the object information of the loaded dependent module is not written into the preset storage structure. The embodiment of the present disclosure improves the conventional loading method. Before loading the dependent module, it is first judged whether the dependent module has been loaded. If it has not been loaded, it is loaded using the conventional loading method. After the loading is completed, the object information of the dependent module is written into the preset storage structure, so that the next time the same dependent module is loaded, the object information of the dependent module can be directly obtained from the preset storage structure without repeating the loading process.
[0113] In the process of executing a script file, if a loading operation of a dependent module is encountered in the disclosed embodiment, it is possible to first determine whether the dependent module has been loaded. If it has not been loaded, the dependent module can be loaded in a conventional manner, and after the loading is successful, the object information of the dependent module generated during the loading process is written into a preset storage structure, so that the object information of the dependent module can be directly obtained from the preset storage structure when the dependent module is loaded next time, without having to repeat the loading process of the dependent module. In this way, the number of times the dependent module is loaded can be reduced, the loading performance of the script file can be optimized, the script execution time can be saved, and the responsiveness of the script application can be improved.
[0114] In an optional implementation of this embodiment, the first determining module includes:
[0115] A first determination submodule is configured to determine path information of the dependent module to be loaded;
[0116] a matching submodule, configured to match the path information with the key information written into the preset storage structure;
[0117] The second determining submodule is configured to determine that the dependent module has not been loaded when the path information does not match any key information in the preset storage structure.
[0118] In this optional implementation, in the script file, the path information of the dependent module to be loaded is specified in the parameters of the loading entry function (such as the require function) of the dependent module, and the path information may include the storage path of the code file of the dependent module. The path information of the same dependent module is the same, so when judging whether the dependent module to be loaded currently has been loaded before, the path information of the dependent module can be determined from the parameters of the loading entry function, and the path information is matched with the preset storage structure, which can be a storage structure in the form of a key-value pair, including key information and the value corresponding to the key information; the key information can store the path information of the dependent module, and the value corresponding to the key information can store the object information generated after the dependent module is successfully loaded. When matching the path information with the preset storage structure, the path information can be matched with the key information in the preset storage structure. If the match is successful, it means that the object information of the dependent module is stored in the preset storage structure, that is, the dependent module has been loaded before, and if the match is unsuccessful, it means that the object information of the dependent module is not stored in the preset storage structure, that is, the dependent module has not been loaded before. It can be seen that using the preset storage structure to store the object information of the dependent module after successful loading can be used to determine whether the dependent module to be loaded has been loaded, and when it has been loaded, the object information of the dependent module can be directly obtained from the preset storage structure without reloading it.
[0119] In an optional implementation of this embodiment, the writing module includes:
[0120] A key-value pair generation submodule is configured to use the path information of the dependent module to be loaded as key information and the object information of the dependent module as the value corresponding to the key information to generate a key-value pair;
[0121] The key-value pair writing submodule is configured to write the key-value pair into a preset storage structure.
[0122] In an optional implementation of this embodiment, the device further includes:
[0123] The second determination module is configured to determine that the loading module has been loaded when the path information matches one of the key information in the preset storage structure, and to use the value corresponding to the matching key information as the object information of the dependent module.
[0124] In this optional implementation, as described above, if the object information of the dependent module currently to be loaded is stored in the key information of the preset storage structure, it means that the dependent module has been loaded before, and the object information of the dependent module has been stored in the preset storage structure. Therefore, the object information of the dependent module can be directly obtained based on the value corresponding to the matching key information.
[0125] In an optional implementation of this embodiment, the device further includes:
[0126] A second acquisition module is configured to acquire a script file to be executed;
[0127] A compiling module, configured to compile the script file;
[0128] A third determination module is configured to determine the location of the original loading code of the dependent module in the script file during the compilation process;
[0129] A replacement module is configured to replace the original loading code of the dependent module with a custom loading code based on the position; the custom loading code includes a first code and a second code, the first code is used to determine whether the dependent module has been loaded, and the second code is used to execute the original loading code when the dependent module has not been loaded and write the object information of the loaded dependent module into a preset storage structure after the execution of the original loading code is completed.
[0130] In this optional implementation, after receiving the script file to be executed, the script file may be scanned and detected first, and the scanning and detecting process may be, for example, a detection process for sensitive codes in the script file. Of course, it is understandable that the scanning and detecting process may also be a common detection process for replacing the original loading code of the dependent module with the customized loading code.
[0131] During the scanning and detection process of the script file, the script file can be statically compiled, and then the location of the original loading code of the dependent module can be determined by using the syntax analysis tree obtained by the compilation. In the JavaScript script file, the original loading code can be the code including the loading entry function require(). After the syntax analysis tree is generated during the compilation process, the location of the original loading code in the script file can be determined by matching whether the require function is included.
[0132] Based on the position of the original loading code, the original loading code in the script file can be replaced with a custom loading code. For example, the loading code can be defined as an X_require() function, and the require() function in the script file can be replaced with the X_require() function.
[0133] The custom loading code may include but is not limited to a first code and a second code; the first code is used to determine whether the dependent module has been loaded before; the second code is used to execute the original loading code when it is determined that the dependent module has not been loaded. It should be noted that the original loading code is also the conventional loading code of the dependent module, which is implemented in JavaScript using the require() function, and during the script execution process, the require() function can load the dependent module in a conventional manner.
[0134] In the static compilation process of the script file, the embodiment of the present disclosure replaces the original loading code with the customized loading code that can realize the above functions, so that when the loading code of the dependent module is executed during the execution process, it can first determine whether the dependent module has been loaded, and perform regular loading if it has not been loaded, and write the loaded object information into the preset storage structure. In this way, the original loading code can be replaced during the static compilation process of the script file without having to be replaced during the execution process of the script file, which can further save the execution time of the script file and optimize the performance of the script execution system.
[0135] In an optional implementation of this embodiment, the customized loading code further includes a third code, and the third code is used to directly obtain the object information of the dependent module from the preset storage structure when the dependent module has been loaded.
[0136] In this optional implementation, the custom loading code also includes a third code, which is used to directly obtain the object information of the dependent module from the preset storage structure when the dependent module has been loaded. As described above, the original loading code will re-execute the loading process regardless of whether the dependent module to be loaded has been loaded before. In order to reduce the number of times the same dependent module is loaded, the embodiment of the present disclosure replaces the original loading code with the custom loading code. In the custom loading code, it is first determined whether the dependent module has been loaded. If it has not been loaded, the original loading code is still used for loading, and after the loading is successful, the object information of the dependent module is written into the preset storage structure. When the dependent module has been loaded, the object information of the dependent module is directly obtained from the preset storage structure without executing the loading process. Therefore, the above steps can be implemented in the custom loading code, and after the original loading code is replaced with the custom loading code, the above steps are performed when the script file is executed.
[0137] In an optional implementation of the present embodiment, the first code is implemented by matching the path information of the dependent module specified in the original loading code with the key information in the preset storage structure; the second code is implemented by calling the original recording code when the path information does not match any key information in the preset storage result, and after the original loading code is executed, using the path information of the dependent module as the key information and writing the object information of the dependent module returned by the original loading code as the value corresponding to the key information into the preset storage structure; the third code is implemented by using the value corresponding to the matched key information as the object information of the dependent module and returning it when the path information matches one of the key information in the preset storage result.
[0138] In this optional implementation, as described above, three parts of code can be included in the custom loading code, namely the first code, the second code and the third code, and the original loading code can be replaced with the custom loading code, so that during the execution of the script file, when the custom loading code is executed, the following functions can be implemented, namely, first determine whether the dependent module to be loaded has been loaded, and when it has not been loaded, call the original loading code to execute the loading process, and after the loading is successful, write the object information of the dependent module into the preset storage structure, and when it has been loaded, directly obtain the object information of the dependent module from the preset storage structure without executing the loading process.
[0139] In some embodiments, the preset storage structure can store data in the form of key-value pairs, for example, in Javascript, a map structure can be used for storage. The key information in the preset storage structure can store the path information of the dependent module, and the value corresponding to the key information can store the object information generated after the dependent module is successfully loaded. After a dependent module is successfully loaded for the first time, the key-value pair corresponding to the dependent module can be stored in the preset storage structure, and in the subsequent loading process, the object information can be directly obtained from the preset storage structure based on the path information of the dependent module.
[0140] Therefore, in the disclosed embodiment, the first code can be implemented as matching the path information of the dependent module specified in the original loading code with the key information in the preset storage structure; the second code can be implemented as calling the original recording code when the path information does not match any key information in the preset storage result, and after the original loading code is executed, the path information of the dependent module is used as the key information, and the object information of the dependent module returned by the original loading code is written into the preset storage structure as the value corresponding to the key information; the third code can be implemented as when the path information matches one of the key information in the preset storage result, the value corresponding to the matched key information is used as the object information of the dependent module and returned. It should be noted that the path information of the same dependent module is the same, so the same path information corresponds to only one key-value pair.
[0141] Figure 4 It is a structural schematic diagram of an electronic device suitable for implementing the method for loading a dependent module according to an embodiment of the present disclosure.
[0142] like Figure 4 As shown, the electronic device 400 includes a processing unit 401, which can be implemented as a processing unit such as a CPU, a GPU, an FPGA, an NPU, etc. The processing unit 401 can perform various processes in the embodiments of any of the above methods of the present disclosure according to a program stored in a read-only memory (ROM) 402 or a program loaded from a storage part 408 to a random access memory (RAM) 403. In RAM403, various programs and data required for the operation of the electronic device 400 are also stored. The processing unit 401, ROM402, and RAM403 are connected to each other via a bus 404. An input / output (I / O) interface 405 is also connected to the bus 404.
[0143] The following components are connected to the I / O interface 405: an input section 406 including a keyboard, a mouse, etc.; an output section 407 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 408 including a hard disk, etc.; and a communication section 409 including a network interface card such as a LAN card, a modem, etc. The communication section 409 performs communication processing via a network such as the Internet. A drive 410 is also connected to the I / O interface 405 as needed. A removable medium 411, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 410 as needed, so that a computer program read therefrom is installed into the storage section 408 as needed.
[0144] In particular, according to an embodiment of the present disclosure, any method in the above referenced embodiment of the present disclosure may be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program tangibly contained on a machine-readable medium, and the computer program includes a program code for executing any method in the embodiment of the present disclosure. In such an embodiment, the computer program can be downloaded and installed from a network through the communication part 409, and / or installed from a removable medium 411.
[0145] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, functions and operations of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the road map or block diagram can represent a part of a module, program segment or code, and the part of the module, program segment or code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a different order from the order marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0146] The units or modules involved in the embodiments described in the present disclosure may be implemented by software or hardware. The units or modules described may also be set in a processor, and the names of these units or modules do not constitute limitations on the units or modules themselves in some cases.
[0147] As another aspect, the present disclosure further provides a computer-readable storage medium, which may be a computer-readable storage medium included in the device described in the above embodiment; or a computer-readable storage medium that exists independently and is not assembled into the device. The computer-readable storage medium stores one or more programs, and the programs are used by one or more processors to execute the method described in the present disclosure.
[0148] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of the invention involved in the present disclosure is not limited to the technical solution formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the inventive concept. For example, the above features are replaced with the technical features with similar functions disclosed in the present disclosure (but not limited to) by each other.
Claims
1. A method for loading a dependent module, wherein: include: Execute the script statements of the script file; When executing a script statement for loading a dependent module, determining whether the dependent module has been loaded; When the dependent module is loaded, obtaining object information of the dependent module from a preset storage structure; When the dependent module has not been loaded, loading the dependent module; The object information of the loaded dependent module is written into a preset storage structure, so that when the dependent module is subsequently loaded, the object information of the dependent module is directly obtained from the preset storage structure.
2. The method according to claim 1, wherein: When executing a script statement for loading a dependent module, determining whether the dependent module has been loaded includes: Determine the path information of the dependent module to be loaded; Matching the path information with the key information written into the preset storage structure; When the path information does not match any key information in the preset storage structure, it is determined that the dependent module has not been loaded.
3. The method according to claim 2, wherein: Writing the loaded object information of the dependent module into a preset storage structure includes: Using the path information of the dependent module to be loaded as key information and the object information of the dependent module as the value corresponding to the key information to generate a key-value pair; The key-value pair is written into a preset storage structure.
4. The method according to claim 3, wherein: The method further comprises: When the path information matches one of the key information in the preset storage structure, it is determined that the dependent module has been loaded, and the value corresponding to the matched key information is used as the object information of the dependent module.
5. The method according to claim 1, wherein: Before executing the script statement of the script file, the method further includes: Get the script file to be executed; Compiling the script file; During the compilation process, determining the location of the original loading code of the dependent module in the script file; Based on the position, replacing the original loading code of the dependent module with the customized loading code; The customized loading code includes a first code and a second code, wherein the first code is used to determine whether the dependent module has been loaded, and the second code is used to execute the original loading code when the dependent module has not been loaded, and after the execution of the original loading code is completed, write the object information of the loaded dependent module into a preset storage structure.
6. The method according to claim 5, wherein: The customized loading code also includes a third code, and the third code is used to directly obtain the object information of the dependent module from the preset storage structure when the dependent module has been loaded.
7. The method according to claim 6, wherein: The first code is implemented to match the path information of the dependent module specified in the original loading code with the key information in the preset storage structure; The second code is implemented to call the original loading code when the path information does not match any key information in the preset storage structure, and after the original loading code is executed, use the path information of the dependent module as key information and write the object information of the dependent module returned by the original loading code as the value corresponding to the key information into the preset storage structure; The third code is implemented as follows: when the path information matches one of the key information in the preset storage structure, the value corresponding to the matched key information is used as the object information of the dependent module and returned.
8. The method according to any one of claims 1 to 7, wherein: The script file is a JavaScript file.
9. A loading device for a dependent module, wherein: include: An execution module, configured to execute script statements of a script file; A first determination module is configured to determine whether the dependent module has been loaded when executing a script statement for loading a dependent module; A first acquisition module is configured to acquire object information of the dependent module from a preset storage structure when the dependent module is loaded; A loading module, configured to load the dependent module when the dependent module has not been loaded; The writing module is configured to write the object information of the loaded dependent module into a preset storage structure, so that when the dependent module is subsequently loaded, the object information of the dependent module can be directly obtained from the preset storage structure.
10. A computer program product comprising computer instructions, wherein: When the computer instructions are executed by a processor, the method according to any one of claims 1 to 8 is implemented.
