A method and apparatus for optimizing program execution
By generating and using a bidirectional bridge function set, the difficulty of bidirectional function calls between interpreter code and AOT compiled code in the Unity platform hot update scenario is solved, efficient function calls are achieved, and program execution efficiency is improved.
Patent Information
- Application Number
- CN202210663809.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-13
- Publication Date
- 2025-06-10
- Estimated Expiration
- 2042-06-13
AI Technical Summary
In the hot update scenario of the Unity platform, two-way function calls are required between the interpreter code and the AOT compilation code, which has many difficulties and affects the execution efficiency of the program.
By scanning the relevant class library to obtain all functions, generate a set of bidirectional bridge functions between the interpreter code and the AOT compiled code, and package it with other resources to achieve efficient bidirectional function calls.
It realizes efficient calls between interpreter code and AOT compiled code, reduces dependence on third-party libraries, and improves program execution efficiency.
Smart Images

Figure CN114995923B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of computer program application development, and particularly relates to a method, apparatus, computing device, and computer-readable storage medium for optimizing program execution. Background Art
[0002] In the applicant's prior application CN 202210078151.3, a hot update method and apparatus for the Unity platform were proposed. In this application, by customizing and transforming the IL2CPP tool and adding a custom interpreter to the IL2CPP tool, a new hot update architecture was achieved, loading the hot update assembly and the main program into the same runtime, eliminating the type system incompatibility problem, and enabling the hot update code and the main program code to run seamlessly in the same runtime. In this solution, there is also the following scenario where two-way function calls are required between the interpreter code and the AOT-compiled code. For example, the interpreter needs to call a function in the AOT-compiled code, or there is a callback function in the AOT-compiled code that needs to call a function in the interpreter. There are many difficulties in performing such operations. Summary of the Invention
[0003] In view of this, embodiments of this application provide a method, apparatus, computing device, and computer-readable storage medium for optimizing program execution to solve the technical defects existing in the prior art.
[0004] According to the first aspect of the embodiments of this application, a method for optimizing program execution is provided, including:
[0005] Scanning relevant class libraries to obtain all functions in the class libraries;
[0006] Generating a two-way bridging function set between the interpreter code and the AOT-compiled code based on the all functions;
[0007] Packaging and releasing the bridging function set together with other resources.
[0008] According to the second aspect of the embodiments of this application, a device for optimizing program execution is provided, including:
[0009] A scanning unit for scanning relevant class libraries to obtain all functions in the class libraries;
[0010] A generating unit for generating a two-way bridging function set between the interpreter code and the AOT-compiled code based on the all functions;
[0011] A releasing unit for packaging and releasing the bridging function set together with other resources.
[0012] According to a third aspect of the embodiments of the present application, a computing device is provided, including a memory, a processor, and computer instructions stored on the memory and executable on the processor. When the processor executes the instructions, the steps of the program optimization execution method are implemented.
[0013] According to a fourth aspect of the embodiments of the present application, a computer-readable storage medium is provided, which stores computer instructions. When the instructions are executed by a processor, the steps of the program optimization execution method are implemented.
[0014] Through the methods and devices in the embodiments of the present application, efficient calls between interpreter code and AOT-compiled code are achieved. In the embodiments of the present application, all possible functions are scanned in advance, and bridging functions for two-way calls are generated according to the signatures of the functions. When a call occurs, the bridging functions can be directly used without a third-party library to achieve function calls. To further improve the execution efficiency of the program, using the function signature sharing feature of the system architecture, after obtaining the shared bridging signature according to the shared function signature, the corresponding bridging function is generated, so that it is not necessary to generate a bridging function for each function, reducing the number of generated bridging functions. Description of the Drawings
[0015] Figure 1 is a structural block diagram of the computing device provided by the embodiments of the present application;
[0016] Figure 2 is a schematic flowchart of a program optimization execution method provided by the embodiments of the present application;
[0017] Figure 3 is a schematic structural diagram of a program optimization execution device provided by the embodiments of the present application; Detailed Embodiments
[0018] Many specific details are set forth in the following description in order to provide a thorough understanding of the present application. However, the present application can be implemented in many other ways different from those described herein, and those skilled in the art can make similar generalizations without departing from the connotation of the present application. Therefore, the present application is not limited by the specific embodiments disclosed below.
[0019] The terms used in one or more embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of the present application. The singular forms "a", "the", and "said" used in one or more embodiments of the present application and the appended claims are also intended to include the plural forms, unless the context clearly indicates otherwise. It should also be understood that the term "and / or" used in one or more embodiments of the present application refers to and includes any or all possible combinations of one or more of the associated listed items.
[0020] It should be understood that although the terms first, second, etc. may be used in one or more embodiments of the present application to describe various information, such information should not be limited to these terms. These terms are only used to distinguish information of the same type from each other. For example, without departing from the scope of one or more embodiments of the present application, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein may be interpreted as "in response to determining".
[0021] In the present application, a method and apparatus for optimizing program execution, a computing device, and a computer-readable storage medium are provided, and will be described in detail one by one in the following embodiments.
[0022] Figure 1 A structural block diagram of a computing device 100 according to an embodiment of the present application is shown. The components of the computing device 100 include but are not limited to a memory 110 and a processor 120. The processor 120 is connected to the memory 110 through a bus 130, and a database 150 is used to store data.
[0023] The computing device 100 further includes an access device 140, and the access device 140 enables the computing device 100 to communicate via one or more networks 160. Examples of these networks include the Public Switched Telephone Network (PSTN), Local Area Network (LAN), Wide Area Network (WAN), Personal Area Network (PAN), or a combination of communication networks such as the Internet. The access device 140 may include one or more of any type of wired or wireless network interfaces (e.g., Network Interface Card (NIC)), such as IEEE802.11 Wireless Local Area Network (WLAN) wireless interface, Worldwide Interoperability for Microwave Access (Wi-MAX) interface, Ethernet interface, Universal Serial Bus (USB) interface, cellular network interface, Bluetooth interface, Near Field Communication (NFC) interface, and so on.
[0024] In one embodiment of the present application, the above components of the computing device 100 and Figure 1 other components not shown therein may also be connected to each other, for example, through a bus. It should be understood that Figure 1 the shown structural block diagram of the computing device is only for illustrative purposes and is not a limitation on the scope of the present application. Those skilled in the art can add or replace other components as needed.
[0025] The computing device 100 can be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., tablet computer, personal digital assistant, laptop computer, notebook computer, netbook, etc.), mobile phone (e.g., smartphone), wearable computing device (e.g., smartwatch, smart glasses, etc.) or other types of mobile devices, or a stationary computing device such as a desktop computer or PC. The computing device 100 can also be a mobile or stationary server.
[0026] In the Unity development platform, IL2CPP is a new way of Scripting Backend. It processes the IL (Intermediate Language) output for platform compilation, converts the IL language into static C++ code, and then directly compiles it into executable native assembly code by the C++ compilers of each platform, thus realizing cross-platform development of Unity. In addition to fast execution efficiency, this method can also utilize the C++ compilers of each platform to perform compile-time optimization on the code, which can further reduce the size of the final game and improve the game running speed. Therefore, almost all Unity-based development processes use the IL2CPP tool.
[0027] Among them, IL2CPP mainly consists of two parts:
[0028] · AOT (Ahead Of Time) static compilation compiler (il2cpp.exe);
[0029] · Runtime library (libil2cpp);
[0030] The AOT compiler converts IL into C++ code and then hands it over to the C++ compilers of each platform for compilation. Since AOT static compilation is used, the types that need to be used must be completely determined during compilation; the runtime library will provide services and abstractions such as garbage collection, thread / file acquisition, and native generations that directly modify managed data structures for internal calls.
[0031] In the applicant's prior application CN 202210078151.3, by adding an IL interpreter to the IL2CPP tool, the IL2CPP tool is equipped with the functions of an AOT compiler, a runtime environment, and an IL interpreter, and has a compilation and hot update foundation for multiple different platforms. Among them, the code that does not need to be updated is statically compiled into C++ code using AOT and then compiled into a native executable program by the target platform; for the code in the hot update assembly, it is interpreted and executed through the IL interpreter.
[0032] In this solution, there is also the following scenario. When performing hot update code, two-way function calls are required between the interpreter code and the AOT-compiled code. Among them, the parameter passing and storage methods of the AOT-compiled code and the interpreter code are different. For example, when a function in the interpreter calls a function in the AOT code, all the parameters of the function in the interpreter are on the interpreter stack, and appropriate methods must be used to pass the function parameters of the interpreter to the function in the AOT code. Similarly, the interpreter cannot directly obtain the specified parameters of the AOT callback function. In the prior art, using a library similar to FFI can implement the above functions, but the efficiency of calling a third-party library is relatively low, which affects the execution speed of the program.
[0033] Therefore, in the embodiments of the present application, in order to solve the above problems, a program optimized execution method and device are proposed.
[0034] The description appendix Figure 1 The processor 120 in Figure 2 can execute the steps in the program optimized execution method shown. Figure 2 The flowchart showing the implementation of the program optimized execution method is shown, including steps 202 to 206.
[0035] Step 202: Scan relevant libraries and obtain all functions in the libraries;
[0036] In this step, all libraries required by the AOT-compiled code are scanned to obtain all functions. The libraries required by the AOT-compiled code include, but are not limited to, the.net framework basic library, the core library of the Unity engine, and third-party libraries.
[0037] Furthermore, obtain the hot update library and scan it to obtain all functions in the hot update library.
[0038] Step 204: Generate a two-way bridging function set between the interpreter code and the AOT-compiled code according to all the obtained functions.
[0039] In this step, two-way bridging functions are generated according to all the obtained functions. The two-way bridging functions include the bridging function Managed2NativeCallXXX() required for calling a function in the AOT-compiled code in the interpreter code and the bridging function Native2ManagedCallXXX() required for calling an interpreter function in the AOT-compiled code.
[0040] Further, in the above bridging function Native2ManagedCallXXX(), obtain the parameter values and the address of the interpreter function to be called from the stack of the caller's AOT-compiled code function. Subsequently, create a new stack in the interpreter, push the parameter data from the caller, and then perform interpretive execution in the interpreter based on the function address and the parameters in the newly created stack, thereby achieving the calling of the interpreter function in the AOT-compiled code.
[0041]
[0042] Further, in the above bridging function Managed2NativeCallXXX(), obtain data such as the address of the function in the AOT-compiled code to be called and the parameter addresses from the stack of the caller's interpreter. Subsequently, perform runtime initialization of the function according to the function address in the interpreter, and then obtain the parameter values according to the parameter addresses to call the function.
[0043]
[0044] In another feasible implementation, when generating the bidirectional bridging function, generate the corresponding bridging function according to the different signatures of all the scanned functions. The function signature contains information about a function, including the function name, the types of parameters, the number of parameters, the order of parameters, the return value, etc. The function signature is used to identify different functions.
[0045] Therefore, in this implementation, generate the bidirectional bridging function according to each obtained function signature as follows:
[0046]
[0047]
[0048] Among them, i8 and r8 in the above function names respectively represent the int and double data types, and their quantity and order correspond to the parameters in the function signature. Those skilled in the art should be aware that the above bridging functions are only examples and not exhaustive, and the quantity and types of the bridging functions depend on the function signatures of all the scanned functions.
[0049] Further, in the technical field to which it belongs, in different system architectures such as X64 and ARM64 platforms, there is a function signature sharing mechanism. For example, bool, byte, sbyte, short, char, ushort, int, uint, long, ulong, IntPtr, UintPtr, class types, value types with sizes of 1, 2, 4, 8, and all reference types such as int&, Vector3& share the long type signature.
[0050] Therefore, for example, the following functions Fun1 - Fun4:
[0051] int Fun1(int a, int b);
[0052] int Fun2(object a, long b);
[0053] long Fun3(long a, long b);
[0054] object Fun4(object a, object b);
[0055] Under the function signature sharing mechanism, a long(long, long) signature can be shared. Based on this, a common bridge function can be generated for the above-mentioned multiple functions. Therefore, in this embodiment, after obtaining all the functions, the shared bridge signature of the function signature is obtained according to the characteristics of the system architecture, and then the corresponding bridge function is generated, thereby reducing the number of generated bridge functions.
[0056] Step 206: Package and release the generated bridge function file together with other resources.
[0057] In a feasible embodiment, the generated set of bridge functions is saved as a file and packaged and released together with other resources, such as the Unity core library, the.net framework library, third-party libraries, etc. required for program operation, and then deployed to the client following the installation of the program.
[0058] Furthermore, when performing a hot update on the client program, specifically, when the interpreter executes the hot update code, the bridge functions in the above-mentioned bridge function file are used to implement two-way function calls between the interpreter code and the AOT-compiled code.
[0059] Further, when the bridge function file needs to be updated, the new bridge function file is released to the client together with the hot update assembly.
[0060] In the above embodiments, the hot-updated code and the main program code run seamlessly in the same runtime. Among them, the main program code that does not need to be updated is statically compiled into a native executable program through AOT, and the code in the hot-update assembly is interpreted and executed through an interpreter. To achieve efficient calls between the interpreter code and the AOT-compiled code, all functions that may be used are scanned in advance, and bridging functions for two-way calls are generated according to the signatures of the functions. When a call occurs, no third-party library is required, and the function call can be directly implemented using the bridging functions. Further, by utilizing the function signature sharing feature of the system architecture, after obtaining the shared bridging signature according to the shared function signature, the corresponding bridging functions are generated, so that it is not necessary to generate bridging functions for each function, reducing the number of generated bridging functions and further improving the execution efficiency of the program.
[0061] Corresponding to the above method embodiments, the present application also provides an embodiment of a program optimization execution device. Figure 3 The structural schematic diagram of a program optimization execution device according to an embodiment of the present application is shown. As Figure 3 shown, the device includes:
[0062] A scanning unit for scanning relevant class libraries and obtaining all functions in the class libraries;
[0063] A generating unit for generating a two-way bridging function set between the interpreter code and the AOT-compiled code according to all the functions;
[0064] A publishing unit for packaging and publishing the bridging function set together with other resources.
[0065] The above is a schematic solution of a program optimization execution device according to this embodiment. It should be noted that the technical solution of this program optimization execution device and the technical solution of the above program optimization execution method belong to the same concept. For the details not described in the technical solution of this program optimization execution device, reference can be made to the description of the technical solution of the above program optimization execution method.
[0066] An embodiment of the present application also provides a computing device, including a memory, a processor, and computer instructions stored on the memory and executable on the processor. When the processor executes the instructions, the steps of the above program optimization execution method are implemented.
[0067] The above is a schematic solution of a computing device according to this embodiment. It should be noted that the technical solution of this computing device and the technical solution of the above program optimization execution method belong to the same concept. For the details not described in the technical solution of this computing device, reference can be made to the description of the technical solution of the above program optimization execution method.
[0068] An embodiment of the present application further provides a computer-readable storage medium, which stores computer instructions that, when executed by a processor, implement the steps of the program optimization execution method described above.
[0069] The above is a schematic solution of a computer-readable storage medium of this embodiment. It should be noted that the technical solution of this storage medium and the technical solution of the above program optimization execution method belong to the same concept. For the details not described in the technical solution of the storage medium, reference can be made to the description of the technical solution of the above program optimization execution method.
[0070] The specific embodiments of the present application are described above. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims may be performed in a different order than in the embodiments and still achieve the desired result. Additionally, the processes depicted in the figures do not necessarily require the particular order or sequential order shown to achieve the desired result. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0071] The computer instructions include computer program code, which may be in the form of source code, object code, executable files, or some intermediate form, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disc, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signals, telecommunication signals, and software distribution media, etc. It should be noted that the content included in the computer-readable medium may be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, the computer-readable medium does not include electrical carrier signals and telecommunication signals.
[0072] It should be noted that for the foregoing method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations. However, those skilled in the art should know that the present application is not limited by the described action sequence, because according to the present application, certain steps may be performed in other sequences or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily essential to the present application.
[0073] In the above embodiments, the descriptions of the various embodiments have their own focuses. For the parts not detailed in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0074] The preferred embodiments of the present application disclosed above are only used to help illustrate the present application. The alternative embodiments do not describe all the details in detail, nor do they limit the invention to the specific embodiments described. Obviously, many modifications and variations can be made according to the content of the present application. These embodiments are selected and specifically described in order to better explain the principles and practical applications of the present application, so that those skilled in the art can well understand and utilize the present application. The present application is only limited by the claims and their full scope and equivalents.
Claims
1. A method for optimizing program execution, characterized in that, it includes: Scanning relevant libraries to obtain all functions in the libraries; Generating a two-way bridging function set between interpreter code and AOT compilation code based on the all functions, including: obtaining parameter values and the address of the interpreter function to be called from the stack of the caller's AOT compilation code function; creating a new stack in the interpreter, pushing data from the caller, and then performing interpretive execution according to the interpreter function address and the parameters in the new stack, so as to realize calling the interpreter function in the AOT compilation code; obtaining the address of the function in the AOT compilation code to be called and the parameter address from the stack of the caller's interpreter function, initializing the function at runtime in the interpreter according to the function address; and then obtaining the parameter value according to the parameter address to call the function; Packaging and releasing the bridging function set together with other resources, including: when the bridging function file needs to be updated, releasing the new bridging function file together with the hot update assembly to the client.
2. The method according to claim 1, wherein the generating a two-way bridging function set between interpreter code and AOT compilation code based on the all functions includes: When generating the two-way bridging functions, generating corresponding bridging functions according to the different signatures of all functions obtained by scanning.
3. The method according to claim 2, wherein the generating corresponding bridging functions according to the different signatures of all functions obtained by scanning includes: Obtaining a shared bridging signature of the function signature according to the characteristics of the system architecture, and then generating the corresponding bridging functions.
4. A device for optimizing program execution, characterized in that, it includes: A scanning unit for scanning relevant libraries to obtain all functions in the libraries; A generating unit for generating a two-way bridging function set between interpreter code and AOT compilation code based on the all functions, including: obtaining parameter values and the address of the interpreter function to be called from the stack of the caller's AOT compilation code function; creating a new stack in the interpreter, pushing data from the caller, and then performing interpretive execution according to the interpreter function address and the parameters in the new stack, so as to realize calling the interpreter function in the AOT compilation code; obtaining the address of the function in the AOT compilation code to be called and the parameter address from the stack of the caller's interpreter function, initializing the function at runtime in the interpreter according to the function address; and then obtaining the parameter value according to the parameter address to call the function; A releasing unit for packaging and releasing the bridging function set together with other resources, including: when the bridging function file needs to be updated, releasing the new bridging function file together with the hot update assembly to the client.
5. A computing device, including a memory, a processor, and computer instructions stored on the memory and executable on the processor, characterized in that, when the processor executes the instructions, the steps of the method according to any one of claims 1-3 are implemented.
6. A computer-readable storage medium storing computer instructions, characterized in that, when the instructions are executed by a processor, the steps of the method according to any one of claims 1-3 are implemented.
Citation Information
Patent Citations
Hot update method and device for Unity, computing equipment and computer readable storage medium
CN114115964A
Dynamic link library file virtualization method, storage medium and terminal equipment
CN111782334A
Function calling method, computing device and storage medium
CN114217886A
Cited By
Interpreter instruction optimization execution method and device for inline AOT compiling function
CN115495088A
Interpreter instruction optimization execution method and device for inlining AOT compiled functions
CN115495088B