Code compiling method and device, electronic equipment and storage medium
By converting and compiling the parameter entry parameters in the JNI call process in the compilation stage, intermediate code in the specified format is generated, so that the second code can be shared with it, solving the problem of inefficiency in the JNI call process and improving system performance.
Patent Information
- Application Number
- CN202311171988.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-11
- Publication Date
- 2025-03-11
AI Technical Summary
Existing JNI calls require additional copying and other processes, resulting in low efficiency and affecting system performance.
In the compilation stage, the first type of code in the first code is obtained, the parameter parameter is obtained, the format is converted to the target format, and the format conversion identifier is added, the first code is compiled into the intermediate code of the specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code.
By reducing memory copying during code access, system performance is improved.
Smart Images

Figure CN119597292A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer data processing technology, and more specifically, to a code compilation method, device, electronic device and storage medium. Background Art
[0002] JNI (Java Native Interface) is a programming interface for Java language to interact with native code. Through this interface, Java can call native code and operate data with each other. However, the current calling process usually requires additional copying and other processes, which is inefficient and affects system performance. Summary of the invention
[0003] The present application proposes a code compilation method, device, electronic device and storage medium to improve the above-mentioned problems.
[0004] In a first aspect, an embodiment of the present application provides a code compilation method that can be applied to an electronic device, the method comprising: in a compilation stage, obtaining a first type of code in a first code; obtaining an input parameter in the first type of code; converting the format of the input parameter into a target format, and adding a format conversion identifier to the input parameter after the format conversion; compiling the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are visible to each other at the intermediate code level.
[0005] In a second aspect, an embodiment of the present application provides a code compilation device that can run on an electronic device, the device comprising: a type code acquisition module, used to obtain a first type code in a first code during a compilation phase; an input parameter acquisition module, used to obtain an input parameter in the first type code; a code analysis module, used to convert the format of the input parameter into a target format, and add a format conversion identifier to the input parameter after the format conversion; a code compilation module, used to compile the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are visible to each other at the intermediate code level.
[0006] In a third aspect, the present application provides an electronic device comprising one or more processors and a memory; one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to execute the method of the first aspect above.
[0007] In a fourth aspect, the present application provides a computer-readable storage medium having program code stored therein, wherein the method of the first aspect is executed when the program code is run.
[0008] The present application provides a code compilation method, device, electronic device and storage medium, which obtains a first type of code in a first code during the compilation stage; obtains an input parameter in the first type of code; converts the format of the input parameter into a target format, and adds a format conversion identifier to the input parameter after format conversion; compiles the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are mutually visible at the intermediate code level. Thus, through the above method, it is achieved by converting the format of the input parameter of the called function, and compiling the format-converted code into an intermediate code with which the second code can share information, so that the accessed code can cooperate with the compilation process of the first code based on the intermediate code, thereby reducing the memory copy process and other processes in the code access process without modifying the code, and improving the system performance to a certain extent. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0010] Figure 1 A flowchart of a code compilation method provided by an embodiment of the present application is shown.
[0011] Figure 2 A flowchart of a code compilation method provided by another embodiment of the present application is shown.
[0012] Figure 3 A flowchart of a code compilation method provided by yet another embodiment of the present application is shown.
[0013] Figure 4 An example flowchart of building a compilation link for information sharing between the first code and the second code provided in an embodiment of the present application is shown.
[0014] Figure 5 A structural block diagram of a code compiling device provided in an embodiment of the present application is shown.
[0015] Figure 6 A structural block diagram of an electronic device provided in an embodiment of the present application is shown.
[0016] Figure 7 A storage unit for storing or carrying program codes for implementing the code compilation method according to an embodiment of the present application is shown. DETAILED DESCRIPTION
[0017] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application.
[0018] In order to facilitate a better understanding of the solutions described in the present application, the terms involved in the description of the embodiments of the present application are briefly described below.
[0019] Android: An open source mobile operating system based on the Linux kernel and other open source software, which is continuously led and developed by the Open Handset Alliance founded by Google.
[0020] JVM (Java virtual machine): represents a virtual machine that can run Java bytecode.
[0021] ART (Android Runtime): Android runtime environment is a virtual machine environment that runs Java code in the Android system.
[0022] JNI (Java Native Interface): A programming interface that represents the interaction between the Java language and native code. Through this interface, Java can call and operate data with native code.
[0023] LLVM (Low Level Virtual Machine): A framework system for building compilers, written in C++, which is used to optimize the compile time, link time, run time, and idle time of programs written in any programming language. It is open to developers and compatible with existing scripts.
[0024] LLVM-IR (LLVM intermediate representation): represents the intermediate representation used to express program logic inside LLVM. It has three recording formats: memory format, disk binary format, and disk text format. The three formats are completely equivalent.
[0025] LLVM bitcode: represents LLVM-IR expressed in binary form.
[0026] Java bytecode: Java bytecode carries the execution logic of high-level language codes such as Java and is the set of instructions that JVM ultimately executes.
[0027] In Android and standard JVM, passing Java strings to native languages must go through the JNI interface. Depending on the type of parameters passed to the native function, there are roughly two main implementation schemes. One of the schemes is to pass an object or jstring to the native function, such as passing it through "public native boolean nativeMethod(String input);". This implementation method requires the use of the JNI interface on the native side to retrieve the jstring in the object (this step is not required if the jstring is passed directly), and then call GetStringUTFChars in JNI to retrieve the UTF-8 version of the string, and then pass it to other native functions for use. After use, it is necessary to call ReleaseStringUTFChars to release resources. This method requires multiple JNI calls and usually goes through an additional allocation-copy-release process, and the performance is poor.
[0028] Another solution is to pass byte[] as a parameter to the native function. This implementation requires using the GetByteArrayElements interface to retrieve the jbytes pointer for use by other native functions, and finally using ReleaseByteArrayElements to release the resource. However, this implementation still requires at least two JNI calls (Get / Release), which affects system performance to a certain extent. In addition, using byte[] can easily bring unnecessary burdens to development and maintenance.
[0029] After long-term research, the inventor found that, during the compilation phase, the first type of code in the first code can be obtained; the input parameter in the first type of code can be obtained; the format of the input parameter is converted to the target format, and a format conversion identifier is added to the input parameter after the format conversion; the first code is compiled into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are mutually visible at the intermediate code level. Thus, through the above method, it is achieved by converting the format of the input parameter of the called function, and compiling the format-converted code into an intermediate code with which the second code can share information, so that the accessed code can cooperate with the compilation process of the first code based on the intermediate code, thereby reducing the memory copy process and other processes in the code access process without modifying the code, and improving the system performance to a certain extent.
[0030] Therefore, in order to improve the above problems, the inventor proposed the code compilation method, device, electronic device and computer-readable storage medium provided in the present application, which can reduce memory copying and other processes in the code access process without modifying the code, thereby improving system performance to a certain extent.
[0031] The embodiments of the present application will be described in detail below with reference to the accompanying drawings.
[0032] See also Figure 1 , shows a flow chart of a code compilation method provided by an embodiment of the present application, and this embodiment provides a code compilation method, which can be applied to an electronic device, wherein the electronic device can be a mobile phone, a computer, a tablet or a wearable electronic device, and the type of the specific electronic device may not be limited. The method includes:
[0033] Step S110: During the compiling phase, a first type of code in the first code is obtained.
[0034] In this implementation, the first code can be understood as the C / C++ code on the native side, and the first type of code can be understood as the entry function that needs to be called by JNI.
[0035] As an implementation method, for the edited native side C / C++ code (i.e., native code), the first code can be parsed to identify the entry function in the first code that needs to be quickly called by JNI. For example, the entry function can be identified by the following features, such as: JNICALL identifier, JNIEnv*env as the first input parameter, the function name is separated by "_" and complies with the naming rules of Java class name + method name, etc.
[0036] In a specific application example, a piece of code containing relevant features of a function called by JNI may be:
[0037] JNIEXPORT jboolean JNICALL
[0038] Java_NativeDemo_nativeMethod(JNIEnv*env,jobject obj,jstring input)
[0039] Step S120: Obtain input parameters in the first type code.
[0040] In this implementation, the input parameter is used to represent the parameter type passed from the Java side to the native side (function). The specific parameter type may not be limited. For example, the input parameter may be a parameter type such as object, jstring, byte[], int array, or float array.
[0041] By acquiring the input parameter in the first type of code, it is possible to analyze the usage process of all the input parameters of this type in real time, thereby facilitating targeted processing of the input parameter to optimize the code compilation process.
[0042] Step S130: convert the format of the input parameter into a target format, and add a format conversion identifier to the input parameter after the format conversion.
[0043] The target format can be understood as the encoding format of the input parameter on the Java side, that is, the encoding format of the second code includes the target format, and different types of input parameters correspond to different target formats.
[0044] For any Java string, the Java side will encode the string before passing it to the native side for use, and then pass it to the native side. The native side needs to call the JNI interface to access the string according to the encoding format of the string on the Java side, which may easily lead to multiple calls to the JNI interface and unnecessary copying processes, affecting system performance.
[0045] In order to optimize the above problem, this implementation can convert the format of the input parameter in the entry function that needs to be called by JNI into the target format during the code compilation stage on the native side, so that when the native side retrieves the input parameter, it no longer needs to call the JNI interface to obtain the parameter according to the encoding format of the parameter on the Java side, but can directly obtain the address of the parameter on the Java side (refer to the subsequent description for the specific implementation basis), thereby reducing the number of calls to the JNI interface. After converting the format of the input parameter in the entry function that needs to be called by JNI into the target format, a format conversion identifier will be added to the input parameter after the format conversion to notify the Java side that the native side has performed compilation optimization.
[0046] In a specific application example, a code snippet is as follows:
[0047]
[0048] Among them, through simple reference analysis, it can be seen that the input jstring will only be used as a string in UTF-8 format.
[0049] Step S140: compile the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are visible to each other at the intermediate code level.
[0050] In this implementation, the specified format may be the LLVM-IR (LLVM intermediate representation) format; LLVM-IR is an intermediate representation used to express program logic inside LLVM (Low Level Virtual Machine). LLVM-IR has three recording formats: memory format, disk binary format, and disk text format, and these three formats are completely equivalent. The intermediate code in the specified format is the code in the LLVM-IR format.
[0051] In the implementation mode of the present application, the first code and the second code are mutually visible at the intermediate code level of the specified format, and some analysis and optimization that could not be performed before can be performed. In order to further optimize the code compilation process on the native side, the first code can be compiled into the intermediate code of the specified format, so that the second code can cooperate with the compilation process of the first code based on the intermediate code.
[0052] As an implementation method, a clang compiler may be used to compile the c / c++ code (first code) and compile the c / c++ code into LLVM-IR format.
[0053] In the above code snippet example, when compiling the code snippet into LLVM-IR format code, a special tag 'from_jstring' (i.e., a format conversion identifier) may be added to the input parameter.
[0054] After the code on the native side is compiled and optimized by the code compilation method of this embodiment, when the Java side subsequently passes a string to the native side, taking the above code snippet as an example, the actual execution process of the optimized code for string access on the native side is as follows:
[0055]
[0056] It can be seen that after optimization, the native side will no longer go to the JNI function to get the string to be accessed, but directly obtain the input parameters of the Java side, which reduces the number of calls to JNI and also saves the process of releasing the string, thus improving the efficiency of string access. It should be noted that the above code is pseudo code.
[0057] In this implementation, after the first code is compiled into an intermediate code of a specified format, the raw data of the input parameter in the first type of code is directly obtained during the code execution phase, that is, in the subsequent string access process, the Java side can directly pass the storage address of the string before compilation to the Java side, without having to obtain the input parameter by calling the JNI interface. This allows one compilation to reduce unnecessary native-level allocation, copying, and release processes in the subsequent string access process, thereby improving system performance.
[0058] The code compilation method provided in this embodiment obtains the first type of code in the first code during the compilation stage; obtains the input parameter in the first type of code; converts the format of the input parameter into the target format, and adds a format conversion identifier to the input parameter after the format conversion; compiles the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are mutually visible at the intermediate code level. Thus, through the above method, it is realized that the format of the input parameter of the called function is converted, and the format-converted code is compiled into an intermediate code with which the second code can share information, so that the accessed code can cooperate with the compilation process of the first code based on the intermediate code, thereby reducing the memory copy process and other processes in the code access process without modifying the code, and improving the system performance to a certain extent.
[0059] See also Figure 2, shows a flow chart of a code compiling method provided by another embodiment of the present application. This embodiment provides a code compiling method which can be applied to an electronic device. The method includes:
[0060] Step S210: compile the first type code corresponding to the call point in the second code.
[0061] In this implementation, in order to make the first code and the second code mutually visible at the intermediate code level of the specified format, it is also necessary to compile the call point in the first type of code corresponding to the second code to establish a communication interface between the two ends, so that the Java end can see the optimization to be done by the native end, for example, the Java end can see that the java_NativeDemo_nativeMethod function called by the native end requires the input parameter to be converted from jstring to jbytes* and other optimization behaviors. The compilation of the call point can be compiled only once, and after compilation, it will become binary code, so that the native end can directly access it.
[0062] When the Java side compiles the call point of the native side, the memory block in the GC (Garbage Collection) algorithm is usually moved, which may cause the string accessed by the native side to be incorrect data.
[0063] In order to solve this problem, as an implementation method, the input parameters included in the calling function in the second code corresponding to the first type of code can be allocated to a specified storage space, which will not be moved during the garbage collection process. By directly allocating the data to be accessed by the native end to the space of the pre-set non-movable memory block, the Java end can achieve the normal direct access to the raw data without copying, saving the time required for copying data, thereby improving the efficiency of data access.
[0064] As another implementation, if the memory block in the GC algorithm is movable, garbage collection can be stopped before the function in the second code is called; and the function called by the first code type in the second code is passed to the first code. By stopping GC in advance, efficient access to Java strings can be achieved on the native side.
[0065] Optionally, after the function called by the first code type in the second code is passed to the first code, if the string access process ends, the GC process can be started again.
[0066] In a specific application example, the code snippet for compiling the call point of the native function on the Java side is as follows:
[0067]
[0068]
[0069] Among them, nativeMethod(input) represents the call point that requires the Java compiler to cooperate. The Java compiler needs to read and understand the results of nativeMethod optimization and implement corresponding cooperation to ensure the normal function logic.
[0070] Step S220: During the compiling phase, a first type of code in the first code is obtained.
[0071] The specific implementation of step S220 may refer to the relevant description of step S110 in the aforementioned embodiment, which will not be repeated here.
[0072] Step S230: Obtain input parameters in the first type code.
[0073] The specific implementation of step S230 may refer to the relevant description of step S120 in the aforementioned embodiment, which will not be repeated here.
[0074] Step S240: convert the format of the input parameter into a target format, and add a format conversion identifier to the input parameter after the format conversion.
[0075] The specific implementation of step S240 may refer to the relevant description of step S130 in the aforementioned embodiment, which will not be repeated here.
[0076] Step S250: compile the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are visible to each other at the intermediate code level.
[0077] The specific implementation of step S250 may refer to the relevant description of step S140 in the aforementioned embodiment, which will not be repeated here.
[0078] The code compilation method provided in this embodiment is to compile the first type of code corresponding to the call point in the second code; in the compilation stage, obtain the first type of code in the first code; obtain the input parameter in the first type of code; convert the format of the input parameter to the target format, and add a format conversion identifier to the input parameter after format conversion; compile the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are mutually visible at the intermediate code level. Thus, through the above method, it is realized that by converting the format of the input parameter of the called function, and compiling the format-converted code into an intermediate code with which the second code can share information, the accessed code can cooperate with the compilation process of the first code based on the intermediate code, thereby reducing the memory copy process and other processes in the code access process without modifying the code, and improving the system performance to a certain extent.
[0079] At the same time, by compiling the call points that require the cooperation of the Java compiler, it can be ensured that the access process of the compiled optimized string on the native side is safe.
[0080] See also Figure 3 , shows a flowchart of a code compiling method provided by another embodiment of the present application. This embodiment provides a code compiling method that can be applied to an electronic device. The method includes:
[0081] Step S310: constructing a compilation link for information sharing between the first code and the second code, wherein the intermediate code is an intermediate expression form used to express program logic within the compilation link.
[0082] In the implementation manner of the present application, in order to make the first code and the second code visible to each other at the intermediate code level of the specified format, it is also necessary to build in advance a compilation link for information sharing between the first code and the second code. The intermediate code is an intermediate expression form used to express program logic within the compilation link.
[0083] The compilation link may be an LLVM compilation link. As an implementation method, Figure 4 As shown, you can pass the Java code (i.e. Figure 2The Dex code in the JNI interface) is compiled into LLVM-IR code 203 using the dex2oat compiler 201, and the C / C++ code (native code) is compiled into LLVM-IR code 203 using the Clang compiler 202 simultaneously, so that the codes on both sides of the JNI interface are aligned to LLVM-IR instead of Java bytecode, and the powerful optimization capability of LLVM is used to ensure high performance in the end, solve the problem of insufficient optimization capability of ART, and then build the LLVM compilation link.
[0084] In addition, annotations / special comments can be used to mark the entry of native code, so that the information can be used to generate the correct ABI (application binary interface) docking instruction sequence when the native code is generated; the input ABI of the native entry function must be aligned with ART, but calls to other native functions do not have to comply with ART's ABI. At the same time, static analysis tools are introduced to use this information to automatically check whether the naitve code meets the requirements, so as to control the scope of conversion and integration and improve system stability. Taking advantage of the fact that JNI has little effect on the performance of functions with long execution time, the optimization scope is constrained to short functions, that is, functions marked by annotations / comments, which greatly reduces the difficulty and workload of technical implementation.
[0085] Furthermore, the correctness verification tool can be used to uniformly send the generated LLVM-IR to the correctness verification process 204 to ensure the correctness and validity of the compiled code. The intermediate code is jointly optimized by the LLVM-opt optimizer 205 to obtain the LLVM-llc code 206, which is then converted into binary code 207. In this way, the performance loss caused by the JNI bottleneck in the Android system is eliminated without changing the development method or modifying the source code, and the comprehensive performance of the system / application is improved.
[0086] It should be noted that the compilation link is an LLVM compilation link only as an example. In some other implementations, the compilation link may also be other types of links. The specific type may not be limited. It only needs to ensure that the compilation link can be used for information sharing between the first code and the second code.
[0087] Step S320: During the compiling phase, a first type of code in the first code is obtained.
[0088] The specific implementation of step S320 may refer to the relevant description of step S110 in the aforementioned embodiment, which will not be repeated here.
[0089] Step S330: Obtain input parameters in the first type code.
[0090] The specific implementation of step S330 may refer to the relevant description of step S120 in the aforementioned embodiment, which will not be repeated here.
[0091] Step S340: convert the format of the input parameter into a target format, and add a format conversion identifier to the input parameter after the format conversion.
[0092] The specific implementation of step S340 may refer to the relevant description of step S130 in the aforementioned embodiment, which will not be repeated here.
[0093] Step S350: compile the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are visible to each other at the intermediate code level.
[0094] The specific implementation of step S350 may refer to the relevant description of step S140 in the aforementioned embodiment, which will not be repeated here.
[0095] The code compilation method provided in this embodiment, by constructing a compilation link for information sharing between the first code and the second code, the intermediate code is an intermediate expression form used to express program logic inside the compilation link; in the compilation stage, obtain the first type of code in the first code; obtain the input parameter in the first type of code; convert the format of the input parameter to the target format, and add a format conversion identifier to the input parameter after format conversion; compile the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are mutually visible at the intermediate code level. Thus, through the above method, it is realized that by converting the format of the input parameter of the called function, and compiling the format-converted code into an intermediate code with which the second code can share information, the accessed code can cooperate with the compilation process of the first code based on the intermediate code, thereby reducing the memory copy process and other processes in the code access process without modifying the code, and improving the system performance to a certain extent.
[0096] At the same time, by constructing a compilation link for information sharing between the first code and the second code in advance before the string access, unnecessary memory allocation, copying, and memory release actions can be avoided, thereby improving system performance.
[0097] See also Figure 5, is a structural block diagram of a code compiling device provided in an embodiment of the present application. This embodiment provides a code compiling device 400, which can be run on an electronic device. The device 400 includes a type code acquisition module 410, an input parameter acquisition module 420, a code analysis module 430 and a code compiling module 440:
[0098] The type code acquisition module 410 is used to acquire a first type code in the first code during the compiling stage.
[0099] As an implementation manner, the apparatus 400 may further include a call point compilation module, which is used to compile the call point corresponding to the first type of code in the second code before obtaining the first type of code in the first code during the compilation phase.
[0100] As an implementation method, compiling the first type code corresponding to the call point in the second code may include: allocating the input parameters included in the calling function in the second code corresponding to the first type code to a designated storage space, and the designated storage space will not be moved during garbage collection.
[0101] As another implementation, compiling the first type of code corresponding to the calling point in the second code may include: stopping garbage collection before the function in the second code is called; and passing the function called by the first code type in the second code to the first code.
[0102] As an implementation mode, the device 400 may further include a compilation link construction module, which is used to construct a compilation link for sharing information between the first code and the second code before obtaining the first type of code in the first code during the compilation phase, wherein the intermediate code is an intermediate expression form used to express program logic within the compilation link.
[0103] The input parameter acquisition module 420 is used to obtain the input parameter in the first type code.
[0104] The code analysis module 430 is used to convert the format of the input parameter into a target format, and add a format conversion identifier to the input parameter after the format conversion.
[0105] The code compiling module 440 is used to compile the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are visible to each other at the intermediate code level.
[0106] As an implementation manner, the encoding format of the second code includes the target format.
[0107] It should be noted that, after the first code is compiled into an intermediate code of a specified format, obtaining the input parameter in the first type code includes: obtaining the raw data of the input parameter in the first type code.
[0108] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described devices and modules can refer to the corresponding processes in the aforementioned method embodiments, and will not be repeated here.
[0109] In several embodiments provided in the present application, the coupling between modules may be electrical, mechanical or other forms of coupling.
[0110] In addition, each functional module in each embodiment of the present application can be integrated into a processing module, or each module can exist physically separately, or two or more modules can be integrated into one module. The above integrated modules can be implemented in the form of hardware or software functional modules.
[0111] See also Figure 6 Based on the above code compilation method and device, the embodiment of the present application also provides an electronic device 100 that can execute the above code compilation method. The electronic device 100 includes a memory 102 and one or more (only one is shown in the figure) processors 104 coupled to each other, and the memory 102 and the processor 104 are connected by a communication line. The memory 102 stores a program that can execute the content of the above embodiment, and the processor 104 can execute the program stored in the memory 102.
[0112] Among them, the processor 104 may include one or more processing cores. The processor 104 uses various interfaces and lines to connect various parts of the entire electronic device 100, and executes various functions and processes data of the electronic device 100 by running or executing instructions, programs, code sets or instruction sets stored in the memory 102, and calling data stored in the memory 102. Optionally, the processor 104 can be implemented in at least one hardware form of digital signal processing (Digital Signal Processing, DSP), field programmable gate array (Field-Programmable Gate Array, FPGA), and programmable logic array (Programmable Logic Array, PLA). The processor 104 can integrate one or a combination of a central processing unit (Central Processing Unit, CPU), a graphics processing unit (Graphics Processing Unit, GPU) and a modem. Among them, the CPU mainly processes the operating system, user interface and application programs; the GPU is responsible for rendering and drawing display content; and the modem is used to process wireless communications. It can be understood that the above-mentioned modem may not be integrated into the processor 104, but may be implemented separately through a communication chip.
[0113] The memory 102 may include a random access memory (RAM) or a read-only memory (ROM). The memory 102 may be used to store instructions, programs, codes, code sets or instruction sets. The memory 102 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the aforementioned embodiments, etc. The data storage area may also store data (such as a phone book, audio and video data, chat record data) created by the electronic device 100 during use.
[0114] Please refer to Figure 7 , which shows a structural block diagram of a computer-readable storage medium provided in an embodiment of the present application. The computer-readable medium 500 stores program codes, which can be called by a processor to execute the method described in the above method embodiment.
[0115] The computer-readable storage medium 500 may be an electronic memory such as a flash memory, an EEPROM (electrically erasable programmable read-only memory), an EPROM, a hard disk, or a ROM. Optionally, the computer-readable storage medium 500 includes a non-transitory computer-readable storage medium. The computer-readable storage medium 500 has storage space for program code 510 that performs any method steps in the above method. These program codes can be read from or written to one or more computer program products. The program code 510 can be compressed, for example, in an appropriate form.
[0116] In summary, the embodiments of the present application provide a code compilation method, device, electronic device and storage medium. The method is applied to electronic devices. The method obtains a first type of code in a first code during the compilation stage; obtains an input parameter in the first type of code; converts the format of the input parameter into a target format, and adds a format conversion identifier to the input parameter after format conversion; compiles the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are mutually visible at the intermediate code level. Thus, through the above method, the format of the input parameter of the called function is converted, and the format-converted code is compiled into an intermediate code with which the second code can share information, so that the accessed code can cooperate with the compilation process of the first code based on the intermediate code, thereby reducing the memory copy process and other processes in the code access process without modifying the code, and improving the system performance to a certain extent.
[0117] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A code compilation method, characterized in that: The method comprises: In the compiling stage, a first type of code in the first code is obtained; Obtaining input parameters in the first type code; Convert the format of the input parameter to the target format, and add a format conversion identifier to the input parameter after the format conversion; The first code is compiled into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are visible to each other at the intermediate code level.
2. The method according to claim 1, characterized in that In the compiling stage, before obtaining the first type of code in the first code, the method further includes: Compile the first type of code corresponding to the call point in the second code.
3. The method according to claim 2, characterized in that The compiling the first type of code corresponding to the call point in the second code includes: The input parameters included in the calling function in the second code corresponding to the first type code are allocated to a designated storage space, and the designated storage space will not be moved during the garbage collection process.
4. The method according to claim 2, characterized in that: The compiling the first type of code corresponding to the call point in the second code includes: Stop garbage collection before the function in the second code is called; A function called by the first code type in the second code is passed to the first code.
5. The method according to claim 1, characterized in that The encoding format of the second code includes the target format.
6. The method according to claim 1, characterized in that After compiling the first code into an intermediate code of a specified format, obtaining input parameters in the first type of code includes: Get the raw data of the input parameter in the first type code.
7. The method according to any one of claims 1 to 6, characterized in that: In the compiling stage, before obtaining the first type of code in the first code, the method further includes: A compilation link for information sharing between the first code and the second code is constructed, wherein the intermediate code is an intermediate expression form used to express program logic within the compilation link.
8. A code compiling device, characterized in that: The device comprises: A type code acquisition module, used to acquire a first type code in a first code during a compiling phase; An input parameter acquisition module, used to obtain input parameter in the first type code; A code analysis module, used for converting the format of the input parameter into a target format, and adding a format conversion identifier to the input parameter after the format conversion; A code compilation module is used to compile the first code into an intermediate code of a specified format, so that the second code cooperates with the compilation process of the first code based on the intermediate code, and the first code and the second code are visible to each other at the intermediate code level.
9. An electronic device, characterized in that: including one or more processors and memory; One or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to execute the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores program code, wherein when the program code is executed by a processor, the method according to any one of claims 1 to 7 is executed.
Citation Information
Patent Citations
Optimizing method and device for program calling
CN107861807A
Code compiling method and device, computer readable medium and electronic equipment
CN114416103A