A method and device for dynamic specialization of IL instructions during runtime
By dynamically specializing the IL instruction when it is running, the actual address of the dynamic instruction parameter is obtained and saved, which solves the inefficiency problem caused by the IL instruction's dependence on runtime query in the existing technology, improves the speed and efficiency of hot update, and realizes cross-platform implementation. Efficient hot update of the platform.
Patent Information
- Application Number
- CN202210079062.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-24
- Publication Date
- 2024-09-10
- Estimated Expiration
- 2042-01-24
AI Technical Summary
In the existing technology, the interpreter of the IL instruction in the C#-based hot update solution has low operating efficiency, resulting in a slow hot update speed. This is mainly because the IL instruction relies on runtime queries, resulting in low execution efficiency of the interpreter.
Through the dynamic specialization method when the IL instruction is running, the actual address of the dynamic instruction parameter is obtained and saved, the instruction parameter is replaced, and the instruction is modified to the special instance instruction. The interpreter directly calls the actual address the next time it is executed, avoiding query operations and improving Execution speed.
It improves the execution speed of the interpreter, improves the efficiency of hot update, and realizes a cross-platform efficient hot update solution.
Smart Images

Figure CN114443052B
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer compilation technology, and in particular to a method, apparatus, computing device and computer-readable storage medium for dynamic specialization of IL instructions at runtime. Background Art
[0002] In the field of mobile Internet, hot update is usually used to implement functional updates or bug fixes of APP. Users can update when they open the APP without re-downloading the entire APP, thus achieving a good user experience. In the prior art, a popular solution is a hot update solution based on C#, which uses an interpreter to execute IL instructions in the hot update part of the dynamic library to implement hot update. Since a large number of instructions in the IL instructions rely on the query steps in the runtime stage, it is relatively time-consuming, resulting in low operating efficiency of the interpreter and slow hot update speed. Summary of the invention
[0003] In view of this, the present application example provides a method, apparatus, computing device and computer-readable storage medium for dynamic specialization of IL instructions at runtime to solve the technical defects existing in the prior art.
[0004] According to a first aspect of an embodiment of the present application, a method for dynamic specialization of an IL instruction at runtime is provided, comprising:
[0005] Use the interpreter to interpret and execute the IL language in the dynamic library;
[0006] When a dynamic instruction in the IL instruction set is executed for the first time, the actual address p corresponding to the dynamic instruction parameter is obtained and saved;
[0007] Replacing the parameters of the dynamic instruction with the actual address p;
[0008] The dynamic instruction is modified into a specialized instruction, and the actual address p is directly called when the interpreter executes the instruction next time.
[0009] According to a second aspect of an embodiment of the present application, a dynamic specialization device for an IL instruction at runtime is provided, comprising:
[0010] The instruction execution unit is used to interpret and execute the IL language in the dynamic library;
[0011] A storage unit, used for obtaining and storing the actual address p corresponding to the dynamic instruction parameter when the dynamic instruction in the IL instruction set is executed for the first time;
[0012] A replacement unit, used for replacing the parameters of the dynamic instruction with the actual address p;
[0013] An editing unit modifies the dynamic instructions into special instructions;
[0014] When the instruction execution unit executes the dynamic instruction not for the first time, the actual address p is directly called.
[0015] According to a third aspect of an embodiment of the present application, a computing device is provided, comprising a memory, a processor, and computer instructions stored in the memory and executable on the processor, wherein when the processor executes the instructions, the steps of the dynamic specialization method of the IL instruction at runtime are implemented.
[0016] According to a fourth aspect of an embodiment of the present application, a computer-readable storage medium is provided, which stores computer instructions, and when the instructions are executed by a processor, the steps of the dynamic specialization method during the runtime of the IL instruction are implemented.
[0017] Through the method and device in the embodiment of the present application, the structure of IL2CPP is improved, and a runtime that only supports AOT static compilation is transformed into a runtime that supports AOT+interpreter, which realizes the basis of efficient cross-platform hot update solution and optimizes the operation mode of the interpreter. When the IL dynamic instruction is executed for the first time, the actual address required for the execution of the instruction is saved, and the ID of the instruction is modified to a specialized instruction that the interpreter can recognize. When the interpreter executes the instruction next time, the actual address is directly called according to the specialized instruction without the need for querying, thereby improving the execution speed of the interpreter and the efficiency of hot update. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] Figure 1 is a structural block diagram of a computing device provided in an embodiment of the present application;
[0019] Figure 2 It is a flowchart of a dynamic specialization method for IL instruction runtime provided by an embodiment of the present application;
[0020] Figure 3 It is a schematic diagram of replacing instruction parameters in the dynamic specialization method of IL instruction runtime provided by an embodiment of the present application;
[0021] Figure 4 It is a schematic diagram of modifying the instruction ID in the dynamic specialization method of IL instruction runtime provided in an embodiment of the present application.
[0022] Figure 5 It is a structural diagram of a dynamic specialization device for IL instruction runtime provided in an embodiment of the present application. DETAILED DESCRIPTION
[0023] Many specific details are described in the following description to facilitate a full understanding of the present application. However, the present application can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present application, so the present application is not limited by the specific implementation disclosed below.
[0024] 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 of "a", "said" and "the" used in one or more embodiments of the present application and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. 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 associated listed items.
[0025] It should be understood that, although the terms first, second, etc. may be used to describe various information in one or more embodiments of the present application, these information should not be limited to these terms. These terms are only used to distinguish the same type of information 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 determination".
[0026] In the present application, a method and apparatus for dynamic specialization of IL instructions at runtime, a computing device and a computer-readable storage medium are provided, which are used in a hot update solution of C# to improve the speed of hot update, and are described in detail one by one in the following embodiments.
[0027] Figure 1 The structure 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 via a bus 130, and the database 150 is used to store data.
[0028] The computing device 100 also includes an access device 140 that enables the computing device 100 to communicate via one or more networks 160. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a 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 network interface (e.g., a network interface card (NIC)) whether wired or wireless, such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a World Wide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a universal serial bus (USB) interface, a cellular network interface, a Bluetooth interface, a near field communication (NFC) interface, and the like.
[0029] In one embodiment of the present application, the above components of the computing device 100 and Figure 1 Other components not shown in the figure may also be connected to each other, for example, via a bus. It should be understood that Figure 1 The computing device structure block diagram shown is only for the purpose of illustration, and is not intended to limit the scope of the present application. Those skilled in the art may add or replace other components as needed.
[0030] The computing device 100 may be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smart phone), a wearable computing device (e.g., a smart watch, 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 may also be a mobile or stationary server.
[0031] In the prior art, various Lua-based frameworks such as Xlua or C#-based runtime ILRuntime are usually used to implement hot updates of APPs.
[0032] Although the Lua-based solution can achieve hot update, it also has multiple defects, such as:
[0033] In a C# project, if Lua is used to implement hot update logic, team members need to be familiar with both Lua and C#, which increases the cost of development.
[0034] Since you need to switch between C# and Lua frequently during operation, and the speed of Lua calling C# code is slow, the game operation efficiency will be reduced;
[0035] It is impossible to use the integrated development and debugging environment, and there is a lack of good tool support for single-step debugging of code and performance analysis, which makes it difficult to meet the requirements of large-scale projects.
[0036] Due to the above defects, a C#-based ILRuntime tool has been proposed in the prior art to implement a hot update solution in the same language. ILRuntime provides a pure C# implementation for C#-based platforms such as Unity, a fast, convenient and reliable IL runtime, which enables hot updates of code in hardware environments that do not support JIT (such as iOS). The implementation principle is that ILRuntime uses the third-party Mono.Cecil library to read the PE information of the hot update DLL and all the information of the types therein, and finally obtains the IL assembly code, and then interprets the IL instructions in the DLL through the interpreter, translating and executing them one by one. This method compiles the hot update code written in different types of development languages into IL bytecode, which is compiled into the DLL file through the established IL assembler. When the client loads the DLL file, the IL bytecode is executed, and then the ILRuntime is initialized to load the hot update code.
[0037] Although ILRuntime overcomes some of the shortcomings of Lua in the hot update scenario, it also has many shortcomings of its own. First, because it uses an external interpreter, the type system used by the hot update script is not unified with the type system of the CLR, resulting in a large number of incompatibility issues. It requires many additional operations such as cross-domain inheritance and auxiliary reflection, and the development efficiency is very low. Secondly, when using a combination of advanced language features, such as reflection and generics, errors are prone to occur and the stability is poor. The pure computing performance is weaker than Lua, and computationally intensive code is not suitable for use.
[0038] Therefore, in the prior art, there is no convenient, efficient, and low-cost hot update solution for applications developed using a C# platform, such as Unity.
[0039] In the Unity development platform, IL2CPP is a new scripting backend method that processes the IL (intermediate language) compiled by the platform, converts the IL language into static C++ code, and then directly compiles it into executable native assembly code by the C++ compiler of each platform, thereby realizing Unity's cross-platform development. In addition to its high execution efficiency, this method can also use the C++ compiler of each platform to perform compile-time optimization on the code, which can further reduce the size of the final game and increase the running speed of the game. Therefore, almost all Unity-based development processes use the IL2CPP tool.
[0040] Among them, IL2CPP mainly consists of two parts:
[0041] AOT (Ahead Of Time) static compilation compiler (il2cpp.exe);
[0042] Runtime library (libil2cpp);
[0043] The AOT compiler converts IL into C++ code, and then gives it to the C++ compiler 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 internal calls to directly modify the native generation of managed data structures.
[0044] Both Lua and ILRuntime use the principle of interpreter to interpret and execute the hot update code without generating local machine code, thus realizing hot update on various platforms. Therefore, the hot update function can also be realized by adding an interpreter to the IL2CPP tool.
[0045] By adding an IL interpreter to the IL2CPP tool, the IL2CPP tool has the functions of an AOT compiler, a runtime environment, and an IL interpreter, and has the basis for compilation and hot update of 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 local executable program by the target platform; the code in the hot update assembly is interpreted and executed by the IL interpreter.
[0046] However, in existing interpreters, a large number of instructions in the standard IL instruction set rely on querying metadata or other context information at runtime, so it takes a lot of time to execute related instructions, affecting the execution efficiency of the interpreter. For example, the function call Callvirt instruction contains a token parameter; when executing this instruction, it is necessary to look up the table based on the token, which is a relatively time-consuming operation. Even if the common caching technology in the prior art is adopted, it will involve multi-threaded concurrent query or hashmap query operations, and there is still a large computational overhead.
[0047] In an embodiment of the present application, in order to solve the above-mentioned problem, a method and apparatus for dynamic specialization of IL instructions at runtime, a computing device and a computer-readable storage medium are proposed.
[0048] Instructions attached Figure 1 The processor 120 in the embodiment may execute Figure 2 The steps in the dynamic specialization method of IL instruction runtime are shown. Figure 2 , a flowchart of a method for implementing dynamic specialization of IL instructions at runtime is shown, including steps 202 to 208.
[0049] Step 202: Use an interpreter to interpret and execute the IL language in the dynamic library.
[0050] After adding the IL interpreter to the IL2CPP tool, the tool has the functions of AOT compiler, CLR runtime environment and IL interpreter, and has the basis for compilation and hot update of multiple different platforms.
[0051] Furthermore, the hot updated assembly is loaded into the assembly list of the runtime through the System.Reflection.Assembly.Load method, and the IL language in the assembly is interpreted and executed through the interpreter.
[0052] Step 204: When the dynamic instruction is executed for the first time, the actual address p corresponding to the parameter of the dynamic instruction is obtained and saved.
[0053] In a specific implementation, the instruction CallVirt in the IL instruction set calls a late-bound method in an object, that is, the method is selected based on the runtime type of obj, rather than based on a method pointer at compile time, that is, the instruction CallVirt is a dynamic instruction.
[0054] When calling a function, the instruction CallVirt calls a method metadata token, which provides the name, class, and signature of the method to be called. The instruction CallVirt queries the instance of the virtual table v-table based on the token, and finds the address MethodInfo* of the function to be called, and then completes the function call.
[0055] It can be seen that in standard IL instructions, such as CallVirt, calling a function requires table lookup, which consumes a lot of time.
[0056] Therefore, in this embodiment, when the CallVirt instruction is executed for the first time, the address MethodInfo* of the function queried according to the token is recorded.
[0057] In another specific implementation, the instruction Ldfld in the IL instruction set is used to push the field value in the specified object into the stack. The field of the object is specified by a metadata token that must point to a field member. The field can be an instance field or a static field. It can be seen that the instruction also needs to query the metadata table of the field according to the token.
[0058] Therefore, in this embodiment, after the Ldfld instruction is executed for the first time, the type of the field and the offset of the field in the Baseclass can be obtained, and the offset and size of the field can be recorded.
[0059] Those skilled in the art should know that there are multiple dynamic instructions in the IL instruction set, that is, instructions whose specific execution content can only be determined at runtime. In the above embodiment, the dynamic instructions listed are only examples and not exhaustive. Those skilled in the art can perform the operations in this embodiment on the relevant IL dynamic instructions according to actual needs, and will not be repeated here.
[0060] Step 206: Replace the parameters of the dynamic instruction with the actual addresses corresponding to the parameters.
[0061] In this step, in order to speed up the execution of the interpreter, the actual address corresponding to the parameter of the dynamic instruction obtained in step 204 is replaced with the original parameter.
[0062] In another specific implementation, a space of several bytes is reserved after the dynamic instruction, and the actual address corresponding to the parameter of the dynamic instruction is written into the space to complete the replacement.
[0063] like Figure 3 As shown, for example, in the command CallVirt <token>After that, multiple bytes are reserved to write the function address MethodInfo* corresponding to the token into it; for example, in the instruction Ldfld <token>Several bytes are reserved after the token to represent the field address actually required by the instruction corresponding to the token. <offset>and <size>Write into it.
[0064] Step 208: Modify the dynamic instruction into a specialized instruction. The next time the interpreter executes the instruction, it directly calls the actual address p according to the specialized instruction.
[0065] In this step, the ID of the original dynamic instruction is modified so that the original token parameter is skipped the next time it is executed by the interpreter, and the actual address corresponding to the parameter is directly executed.
[0066] When the interpreter executes the CallVirt instruction for the first time and obtains and saves the actual address corresponding to the parameters of the dynamic instruction, the ID of the CallVirt instruction needs to be replaced; otherwise, the interpreter will still use the token to query the instruction the next time it executes the instruction, which will not improve the operating efficiency of the interpreter.
[0067] In a specific implementation, after the dynamic instruction CallVirt is executed for the first time, the instruction is modified into a special instruction, such as CallVirtJIT. Figure 4 As shown;
[0068] When the interpreter executes the instruction next time, it directly obtains the required actual address p according to the special instruction CallVirtJIT to execute the instruction, thus eliminating the need for a query step, greatly improving the execution speed of the interpreter.
[0069] In another specific implementation, after the dynamic instruction CallVirt is executed for the first time, the instruction code of the dynamic instruction is obtained, and the instruction code is modified to be a special instruction code that can be recognized by the interpreter. When the interpreter executes the instruction next time, the required actual address p is directly obtained according to the special instruction code to execute the instruction, and there is no need to perform a query step.
[0070] In the above embodiment, the structure of IL2CPP is improved, and a runtime that only supports AOT static compilation is transformed into a runtime that supports AOT+interpreter, which realizes the basis of cross-platform efficient hot update solution; further, the operation mode of the interpreter is optimized, and when the IL dynamic instruction is executed for the first time, the actual address required for the instruction execution is saved, and the instruction ID is modified to a special instruction that the interpreter can recognize. When the interpreter executes the instruction next time, the actual address required for the instruction execution is directly called according to the special instruction, without the need to query according to the parameters, thereby improving the execution speed of the interpreter and the efficiency of hot update.
[0071] Corresponding to the above method embodiment, the present application also provides an embodiment of a dynamic specialization device when an IL instruction is running, Figure 5 FIG. 1 is a schematic diagram showing a structure of a dynamic specialization device for IL instruction execution according to an embodiment of the present application. Figure 5 As shown, the device comprises:
[0072] The instruction execution unit is used to interpret and execute the IL language in the dynamic library;
[0073] A storage unit, used for obtaining and storing the actual address p corresponding to the dynamic instruction parameter when the dynamic instruction in the IL instruction set is executed for the first time;
[0074] A replacement unit, used for replacing the parameters of the dynamic instruction with the actual address p;
[0075] An editing unit modifies the dynamic instructions into special instructions;
[0076] When the instruction execution unit executes the dynamic instruction not for the first time, the actual address p is directly called.
[0077] The above is a schematic scheme of a dynamic specialization device when an IL instruction is running in this embodiment. It should be noted that the technical scheme of the dynamic specialization device when the IL instruction is running and the technical scheme of the dynamic specialization method when the IL instruction is running belong to the same concept, and the details of the technical scheme of the dynamic specialization device when the IL instruction is running that are not described in detail can all be referred to the description of the technical scheme of the dynamic specialization method when the IL instruction is running.
[0078] In one embodiment of the present application, a computing device is also provided, including a memory, a processor, and computer instructions stored in the memory and executable on the processor, wherein when the processor executes the instructions, the steps of the dynamic specialization method of the IL instruction at runtime are implemented.
[0079] The above is a schematic scheme of a computing device of this embodiment. It should be noted that the technical scheme of the computing device and the technical scheme of the dynamic specialization method when the IL instruction is running belong to the same concept, and the details not described in detail in the technical scheme of the computing device can be referred to the description of the technical scheme of the dynamic specialization method when the IL instruction is running.
[0080] An embodiment of the present application further provides a computer-readable storage medium storing computer instructions, which, when executed by a processor, implement the steps of the dynamic specialization method during the execution of the IL instruction as described above.
[0081] The above is a schematic scheme of a computer-readable storage medium of this embodiment. It should be noted that the technical scheme of the storage medium and the technical scheme of the dynamic specialization method during the operation of the IL instruction described above belong to the same concept, and the details not described in detail in the technical scheme of the storage medium can be referred to the description of the technical scheme of the dynamic specialization method during the operation of the IL instruction described above.
[0082] The above describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0083] The computer instructions include computer program codes, which may be in source code form, object code form, executable files or some intermediate forms, 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 disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium, etc. It should be noted that the content contained 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, computer-readable media do not include electric carrier signals and telecommunication signals.
[0084] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described action sequence, because according to the present application, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0085] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0086] The preferred embodiments of the present application disclosed above are only used to help explain the present application. The optional embodiments do not describe all the details in detail, nor do they limit the invention to the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of the present application. The present application selects and specifically describes these embodiments in order to better explain the principles and practical applications of the present application, so that those skilled in the art can understand and use the present application well. The present application is only limited by the claims and their full scope and equivalents.< / size> < / offset> < / token> < / token>
Claims
1. A method for dynamic specialization of IL instructions at runtime, characterized in that: include: Add an interpreter to the IL2CPP tool, the interpreter runs in the IL2CPP runtime; use the interpreter to interpret and execute the IL language in the dynamic library; When a dynamic instruction in the IL instruction set is executed for the first time, the actual address p corresponding to the dynamic instruction parameter is obtained and saved; Replacing the parameters of the dynamic instruction with the actual address p; The dynamic instruction is modified into a specialized instruction, and the actual address p is directly called when the interpreter executes the instruction next time.
2. The method according to claim 1, wherein: The dynamic instruction is an instruction whose specific execution content is determined at runtime.
3. The method according to claim 1, wherein: The step of replacing the parameters of the dynamic instruction with the actual address p further comprises: A space of several bytes is reserved after the dynamic instruction, and the actual address p corresponding to the parameter of the dynamic instruction is written into the space.
4. The method according to claim 1, wherein: The step of modifying the dynamic instruction to a special instruction further includes: The instruction code of the dynamic instruction is modified into a special instruction code that can be recognized by the interpreter.
5. The method according to claim 1, wherein: This method is executed in the CLR runtime environment.
6. A dynamic specialization device for IL instruction execution, characterized in that: include: IL2CPP runtime environment; An interpreter, which runs in the IL2CPP runtime and is used to interpret and execute the IL language in the dynamic library; The interpreter includes: A storage unit, used for obtaining and storing the actual address p corresponding to the dynamic instruction parameter when the dynamic instruction in the IL instruction set is executed for the first time; A replacement unit, used for replacing the parameters of the dynamic instruction with the actual address p; An editing unit modifies the dynamic instructions into special instructions; When the instruction execution unit executes the dynamic instruction not for the first time, the actual address p is directly called.
7. A computing device comprising a memory, a processor, and computer instructions stored in 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 to 5 are implemented.
8. A computer-readable storage medium storing computer instructions, characterized in that: When the instruction is executed by a processor, the steps of the method described in any one of claims 1 to 5 are implemented.
