Code execution method and apparatus, electronic device, and storage medium

CN117539559BActive Publication Date: 2026-08-11GUANGDONG OPPO MOBILE TELECOMMUNICATIONS CORP LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-11-08
Publication Date
2026-08-11

AI Technical Summary

Technical Problem

当调用类中的方法时,若该方法中存在多个初始化slowPath,则每次调用该方法时执行的检查指令将大幅降低代码的执行效率

Benefits of technology

[0008]本申请实施例提供了一种代码执行方法、装置、电子设备及存储介质。首先响应于方法调用指令,若确定目标方法被第一次调用且目标方法对应的机器码序列中包括有初始化代码段,加载目标方法的机器码入口地址,其中,所述初始化代码段被设置在所述机器码序列的尾部,所述初始化代码段中包括至少一个一次性的初始化代码,然后基于该机器码入口地址跳转至初始化代码段的起始位置,开始执行初始化代码段的指令,当完成初始化代码段的指令的执行后,将机器码入口地址设置为机器码序列的起始地址,得到新的机器码入口地址,最后基于该新的机器码入口地址跳转至机器码序列的起始位置,开始执行机器码序列中的功能代码的指令。通过上述方法,将目标方法对应的所有一次性初始化代码整理成初始化代码段,并在目标方法第一次被调用后动态修改机器码入口地址,从而可以让一次性初始化代码在目标方法被多次调用时不再重复执行,提高了代码的执行效率,降低了运行时的负载。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117539559B_ABST
    Figure CN117539559B_ABST
Patent Text Reader

Abstract

This application discloses a code execution method, apparatus, electronic device, and storage medium. The method includes: in response to a method call instruction, if it is determined that a target method is being called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, loading the machine code entry address of the target method, wherein the initialization code segment is located at the end of the machine code sequence and includes at least one one-time initialization code; jumping to the beginning of the initialization code segment based on the machine code entry address and starting to execute the instructions of the initialization code segment; after the execution of the instructions of the initialization code segment is completed, setting the machine code entry address to the beginning address of the machine code sequence to obtain a new machine code entry address; jumping to the beginning of the machine code sequence based on the new machine code entry address and starting to execute the instructions of the functional code segment in the machine code sequence. This method reduces runtime load.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application belongs to the field of computer technology, and specifically relates to a code execution method, apparatus, electronic device and storage medium. Background Technology

[0002] Java is an object-oriented programming language. Java code is highly interpretable, which contributes to its widespread use. Using Java typically involves defining or declaring classes, which can contain methods. When a method within a class is called, if multiple `slowPath` initializations exist, the check instructions executed each time the method is called will significantly reduce the code's execution efficiency. Summary of the Invention

[0003] In view of the above problems, this application proposes a code execution method, apparatus, electronic device, and storage medium to improve the above problems.

[0004] In a first aspect, embodiments of this application provide a code execution method, the method comprising: responding to a method call instruction, if it is determined that a target method is called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, loading the machine code entry address of the target method, wherein the initialization code segment is set at the end of the machine code sequence, and the initialization code segment includes at least one-time initialization code; jumping to the beginning position of the initialization code segment based on the machine code entry address, and starting to execute the instructions of the initialization code segment; after the execution of the instructions of the initialization code segment is completed, setting the machine code entry address to the beginning address of the machine code sequence to obtain a new machine code entry address; and jumping to the beginning position of the machine code sequence based on the new machine code entry address, and starting to execute the instructions of the functional code segment in the machine code sequence.

[0005] Secondly, embodiments of this application provide a code execution apparatus, the apparatus comprising: a loading unit, configured to, in response to a method call instruction, if it is determined that a target method is called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, load the machine code entry address of the target method, wherein the initialization code segment is located at the end of the machine code sequence, and the initialization code segment includes at least one-time initialization code; a first execution unit, configured to jump to the beginning position of the initialization code segment based on the machine code entry address and begin executing the instructions of the initialization code segment; a setting unit, configured to, after the execution of the instructions of the initialization code segment is completed, set the machine code entry address to the beginning address of the machine code sequence to obtain a new machine code entry address; and a second execution unit, configured to jump to the beginning position of the machine code sequence based on the new machine code entry address and begin executing the instructions of the functional code segment in the machine code sequence.

[0006] Thirdly, embodiments of this application provide an electronic device, including one or more processors and a memory; one or more programs, wherein the 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 perform the methods described above.

[0007] Fourthly, embodiments of this application provide a computer-readable storage medium storing program code, wherein the above-described method is executed when the program code is run.

[0008] This application provides a code execution method, apparatus, electronic device, and storage medium. First, in response to a method call instruction, if it is determined that the target method is being called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, the machine code entry address of the target method is loaded. The initialization code segment is located at the end of the machine code sequence and includes at least one one-time initialization code. Then, based on the machine code entry address, the execution jumps to the beginning of the initialization code segment and begins executing the instructions of the initialization code segment. After the execution of the initialization code segment instructions is completed, the machine code entry address is set to the beginning address of the machine code sequence, resulting in a new machine code entry address. Finally, based on the new machine code entry address, the execution jumps to the beginning of the machine code sequence and begins executing the instructions of the functional code in the machine code sequence. Through this method, all one-time initialization code corresponding to the target method is organized into an initialization code segment, and the machine code entry address is dynamically modified after the target method is called for the first time. This prevents the one-time initialization code from being repeatedly executed when the target method is called multiple times, improving code execution efficiency and reducing runtime load. Attached Figure Description

[0009] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0010] Figure 1 A flowchart illustrating a method call in one embodiment of this application is shown;

[0011] Figure 2 A flowchart of a code execution method according to an embodiment of this application is shown;

[0012] Figure 3 A flowchart of a code execution method according to another embodiment of this application is shown;

[0013] Figure 4 A schematic diagram of the process described in steps S210 and S220 in another embodiment of this application is shown;

[0014] Figure 5 A schematic diagram of the process described in steps S210-S270 is shown in another embodiment of this application;

[0015] Figure 6 A flowchart of a code execution method according to another embodiment of this application is shown;

[0016] Figure 7 A schematic diagram of a machine code sequence in yet another embodiment of this application is shown;

[0017] Figure 8 A schematic diagram of a machine code sequence in yet another embodiment of this application is shown;

[0018] Figure 9 This paper shows a structural block diagram of a code execution device according to an embodiment of this application;

[0019] Figure 10 This paper shows a structural block diagram of a code execution device according to an embodiment of this application;

[0020] Figure 11 A structural block diagram of an electronic device for executing a code execution method according to an embodiment of this application is shown;

[0021] Figure 12 This application illustrates a storage unit for storing or carrying program code that implements the code execution method according to the embodiments of this application. Detailed Implementation

[0022] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.

[0023] In the ART (Android Runtime) virtual machine, a Java method is mapped through an ArtMethod object. One Java method corresponds to one ArtMethod object, and the entry_point_from_quick_compiled_code (hereinafter referred to as machine code entry) function pointer is stored inside the ArtMethod object.

[0024] The memory address of the machine code entry point after compilation. Assume there exists a parent method A calling a child method B, where method A...

[0025] First, the ArtMethod object of method B is obtained, then the machine code entry value (a memory address) of the ArtMethod object is loaded, and then the jump is performed, thus completing the method call from A to B.

[0026] During the execution of method B's machine code, the first execution of method B may require some checks and initialization logic (hereinafter referred to as initialization). There may be multiple initialization actions. For example, each time method B calls a static member method of another class, it may need to check whether the class to which the corresponding static member method belongs is in an initialized state. If not, it needs to jump to a logic that initializes that class. After these initialization actions are completed, the second time method B is called, it will still check whether the initialization actions are complete. Since initialization has already been completed, the check result is true, and the method's functional logic continues to execute without re-entering these initialization logics. However, the checks will still be executed every time the method is called.

[0027] In the industry, the functional logic path of a method is generally referred to as the fast path, while paths unrelated to the functional logic, such as initialization, are called slow paths (which are unlikely to be entered during execution). Both fast and slow paths are internal code within the method, and they may not be consecutive, each consisting of multiple code segments. Generally, the fast path comes first, and the slow path is at the end of the machine code sequence.

[0028] The inventors discovered in their research on the relevant method call flow that the relevant method call flow is as follows: Figure 1 As shown, the specific steps are as follows: The parent method obtains the machine code entry address, jumps to the child method's machine code entry address, obtains a certain initialization flag, and then checks whether the flag has been initialized. If it has been initialized, the fastPath is executed; if it has not been initialized, the method jumps to the slowPath corresponding to the flag and executes it. After the slowPath is executed, the fastPath is executed again. Then, the subsequent method call flow continues according to the aforementioned process. In the aforementioned method call flow, if there are multiple initialization slowPaths in the method, the checking instructions executed each time will significantly reduce the code's execution efficiency and increase the runtime load.

[0029] Therefore, the inventors have proposed the code execution method, apparatus, electronic device, and storage medium of this application. First, in response to a method call instruction, if it is determined that the target method is being called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, the machine code entry address of the target method is loaded. The initialization code segment is located at the end of the machine code sequence and includes at least one-time initialization code. Then, based on the machine code entry address, the execution jumps to the beginning of the initialization code segment and begins executing the instructions of the initialization code segment. After the execution of the initialization code segment instructions is completed, the machine code entry address is set to the beginning address of the machine code sequence, resulting in a new machine code entry address. Finally, based on the new machine code entry address, the execution jumps to the beginning of the machine code sequence and begins executing the instructions of the functional code in the machine code sequence. Through this method, all one-time initialization code corresponding to the target method is organized into an initialization code segment, and the machine code entry address is dynamically modified after the target method is called for the first time. This prevents the one-time initialization code from being repeatedly executed when the target method is called multiple times, improving code execution efficiency and reducing runtime load.

[0030] The embodiments of this application will now be described in detail with reference to the accompanying drawings.

[0031] Please see Figure 2 This application provides a code execution method applied to an ART virtual machine in an electronic device. The method includes:

[0032] Step S110: In response to a method call instruction, if it is determined that the target method is called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, load the machine code entry address of the target method, wherein the initialization code segment is set at the end of the machine code sequence and the initialization code segment includes at least one one-time initialization code.

[0033] In this embodiment, the method invocation instruction is used to instruct the invocation of a target method. This instruction can be an instruction triggered by a preset operation, or an instruction sent by another electronic device that has established a communication connection with the electronic device; no specific limitation is made here. The preset operation can be a method execution operation, or a trigger operation to implement a certain function, etc., and no specific limitation is made here. The method invocation instruction may carry a method identifier of the method to be invoked, so that after responding to the method invocation instruction, the target method can be determined through the method identifier carried in the method invocation instruction.

[0034] The target method refers to the Java method that needs to be called. The initialization code segment is a code segment consisting of at least one one-time initialization logic code corresponding to the target method and the corresponding check code for each initialization logic code. The machine code sequence corresponding to the target method refers to the machine code sequence obtained after the ART virtual machine performs AOT (Ahead-of-Time) compilation or JIT (Just-in-Time) compilation on the target method. This machine code sequence includes functional code segments and initialization code segments. The initialization code segments are placed at the end of the machine code sequence; that is, all one-time initialization logic code corresponding to the target method and the corresponding check code for each initialization logic code are placed at the end of the machine code sequence. One-time initialization code refers to initialization code that is executed only once.

[0035] The machine code entry address refers to the starting address of the target method. In the embodiments of this application, the machine code entry address refers to the value of the machine code entry pointer of the ArtMethod object corresponding to the target method. The value of the machine code entry pointer is the starting address of the initialization code segment at the end of the machine code sequence.

[0036] In this embodiment, a target method is determined in response to a method call instruction. After determining the target method, it can be first determined whether the machine code sequence corresponding to the target method contains an initialization code segment. If it is determined that the machine code sequence corresponding to the target method contains an initialization code segment, it is then determined whether the target method is being called for the first time. If it is determined that the method is being called for the first time, the machine code entry address of the ArtMethod object corresponding to the target method is directly loaded. The determination of whether the target method is being called for the first time can be performed before determining whether the machine code sequence corresponding to the target method contains an initialization code segment; alternatively, the determination of whether the target method is being called for the first time and the determination of whether the machine code sequence corresponding to the target method contains an initialization code segment can be performed simultaneously, without specific limitations.

[0037] One method to determine whether a target method is being called for the first time is by checking its call count flag. Specifically, this call count flag can be a pre-set flag used to record the number of times the target method has been called, and its value indicates the number of times the target method has been invoked. Therefore, in this case, if the call count flag value is 0, it can be determined that the target method is being called for the first time.

[0038] As another approach, when determining whether the machine code sequence corresponding to the target method contains an initialization code segment, one can use the flag bit of the target method or analyze the code sequence to determine whether the machine code sequence corresponding to the target method contains an initialization code segment.

[0039] As one approach, when determining whether the machine code sequence corresponding to the target method contains an initialization code segment by using the flag bit of the target method, if the flag bit of the target method indicates that the target method includes functional code and initialization code, then it can be determined that the machine code sequence corresponding to the target method contains an initialization code segment.

[0040] Another method, when determining whether a target method's machine code sequence contains initialization code by analyzing the code sequence, involves analyzing the number of prologues or epilogues. If the machine code sequence contains two prologues or epilogues, it can be determined that the machine code sequence contains an initialization code segment. In compilation, prologue and epilogue are concepts related to method calls and method returns. A prologue refers to a series of instructions preceding method execution, used to set up the method's execution environment and prepare the resources needed for the method call; it typically includes loading parameters into registers or the stack, saving register states, and allocating space for local variables. The prologue ensures the correct setting of the starting point for method execution and prepares for the method call. An epilogue refers to a series of instructions preceding the method's return, used to clean up and restore the environment before method execution; it typically includes releasing space for local variables, restoring saved register states, and handling the method's return value; the epilogue ensures proper cleanup and return operations after method execution. The purpose of the overture and the coda is to ensure the correctness and consistency of method calls; they perform necessary operations at the beginning and end of method calls to ensure the normal execution and return of the method, and to maintain the method's context.

[0041] Step S120: Jump to the beginning of the initialization code segment based on the machine code entry address, and start executing the instructions of the initialization code segment.

[0042] In this embodiment of the application, since the memory address included in the machine code entry address is the starting address of the initialization code segment at the end of the machine code sequence, after loading the machine code entry instruction, it is possible to jump to the starting position of the initialization code segment, thereby enabling the execution of the instructions of the initialization code segment.

[0043] Step S130: After the execution of the instructions of the initialization code segment is completed, the machine code entry address is set to the starting address of the machine code sequence to obtain a new machine code entry address.

[0044] In this embodiment of the application, the starting address of the machine code sequence refers to the starting memory address of the machine code sequence.

[0045] After the initialization code segment instructions are executed, the machine code entry address is modified to the starting memory address of the machine code sequence, resulting in a new machine code entry address.

[0046] Step S140: Based on the new machine code entry address, jump to the beginning position of the machine code sequence and start executing the instructions of the functional code segment in the machine code sequence.

[0047] In this embodiment of the application, after obtaining the new machine code entry address, the new machine code entry address is loaded, thereby allowing a jump to the beginning of the machine code sequence and the execution of the instructions of the functional code segment in the machine code sequence.

[0048] By using the aforementioned method, when the target method is called for the second time, the machine code entry address loaded points to the head of the machine code sequence corresponding to the target method. This allows the instructions of the functional code segment to be executed directly. Since the functional code segment does not contain any checking code, the goal of optimizing the execution of invalid checking code has been achieved.

[0049] This application provides a code execution method that organizes all one-time initialization code corresponding to the target method into an initialization code segment, and dynamically modifies the machine code entry address after the target method is called for the first time. This allows the one-time initialization code to no longer be executed repeatedly when the target method is called multiple times, thereby improving code execution efficiency and reducing runtime load.

[0050] Please see Figure 3 This application provides a code execution method applied to an ART virtual machine in an electronic device. The method includes:

[0051] Step S210: During the loading process of the class method, create the method object corresponding to the target method and set the machine code entry address of the target method.

[0052] In this embodiment, a class method refers to a Class class in the application loaded by the ClassLinker module of the ART virtual machine during application runtime. A method object refers to the ArtMethod object corresponding to the target method. The ArtMethod object is the internal representation of a Java method within ART.

[0053] During the class loading process after the application starts, the ClassLinker module of the ART virtual machine loads the Class class and creates ArtMethod objects corresponding to the Java methods (i.e., the target methods) in the class. At the same time, when creating the ArtMethod object corresponding to the target method, the starting address of the initialization code segment at the end of the machine code sequence corresponding to the target method is set to the machine code entry point method pointer of the ArtMethod object, that is, the machine code entry address of the target method is set.

[0054] Step S220: When setting the machine code entry address of the target method, if it is determined that the machine code sequence corresponding to the target method includes an initialization code segment, the starting address of the initialization code segment is used as the machine code entry address.

[0055] In this embodiment of the application, when setting the machine code entry address of the target method, it is first determined whether the machine code sequence corresponding to the target method includes an initialization code segment. If so, the starting address of the initialization code segment is used as the machine code entry address.

[0056] There are several ways to determine whether the machine code sequence corresponding to the target method includes an initialization code segment.

[0057] As one approach, when setting the machine code entry address of the target method, the machine code sequence corresponding to the target method is analyzed; if it is determined that the machine code sequence includes two preludes, then it is determined that the machine code sequence includes an initialization code segment; the starting address of the initialization code segment is used as the machine code entry address.

[0058] As another approach, when setting the machine code entry address of the target method, the machine code sequence corresponding to the target method is analyzed; if it is determined that the machine code sequence includes two preludes and an epilogue, then it is determined that the machine code sequence includes an initialization code segment; the starting address of the initialization code segment is used as the machine code entry address.

[0059] Specifically, when it is determined through the aforementioned method that the machine code sequence corresponding to the target method includes an initialization code segment, the starting address of the initialization code segment (i.e., the address of the initialization prelude code) is set to the value of the machine code entry pointer of the ArtMethod object.

[0060] Optionally, when setting the machine code entry address of the target method, if it is determined that the machine code sequence corresponding to the target method does not include an initialization code segment, the starting address of the machine code sequence is used as the machine code entry address.

[0061] Specifically, if it is determined that the machine code sequence only includes a prelude or an epilogue, then it can be determined that the machine code sequence corresponding to the target method does not include an initialization code segment. The starting address of the machine code sequence can be directly used as the machine code entry address, so that when the target method is called, it can directly jump to the beginning of the functional code segment and start executing the instructions of the functional code segment.

[0062] For example, the processes described in steps S210 and S220 can be as follows: Figure 4 As shown, in Figure 4 In the process of class loading, the application process loads classes when it starts. During the class loading process after the application process starts, corresponding ArtMethod objects are created for the methods in the class and the machine code entry address is set. When setting the machine code entry address, it is first determined whether there is an initialization code segment in the corresponding machine code sequence. If it exists, the starting address of the initialization code segment is set as the machine code entry address. Otherwise, if it does not exist, the starting address of the machine code is set as the machine code entry address, thus completing the class method loading.

[0063] Step S230: In response to the method call instruction, if it is determined that the target method is called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, load the machine code entry address of the target method through a mutex lock.

[0064] In this embodiment of the application, when the target method is called for the first time, the calling process of the target method needs to be protected by locking to prevent multiple threads from calling the target method at the same time, which would cause competition in the initialization process.

[0065] When the target method is called for the first time, the thread holding the lock invokes the target method, loads the machine code entry address, jumps to the beginning of the initialization code segment, and begins executing the instructions of the initialization code segment. Here, "holding the lock" refers to holding a mutex lock.

[0066] Step S240: Jump to the beginning of the initialization code segment based on the machine code entry address, and start executing the instructions of the initialization code segment.

[0067] Step S250: After the execution of the instructions of the initialization code segment is completed, the machine code entry address is set to the starting address of the machine code sequence to obtain a new machine code entry address.

[0068] Step S260: Release the mutex lock.

[0069] In this embodiment of the application, after the call to the target method is completed, the thread will release the mutex lock, so that other threads that need to call the target method later can load the initialization code segment and run it.

[0070] Step S270: Based on the new machine code entry address, jump to the beginning position of the machine code sequence and start executing the instructions of the functional code segment in the machine code sequence.

[0071] In this embodiment of the application, after the process described in steps S240-S270 is completed, the machine code entry pointer has been correctly replaced and the target method has been initialized. Subsequent calls to this target method will directly jump to the beginning of the machine code sequence corresponding to the target method and will not execute the initialization check code again.

[0072] For example, the process described in steps S210-S270 can be as follows: Figure 5 As shown, in Figure 5 In this process, when a method needs to be called, it first checks whether the method is being called for the first time and whether an initialization code segment exists in the machine code sequence corresponding to the method. If it exists, the thread holding the machine code entry pointer operates on a mutex lock, loads the machine code entry address, jumps to the starting address of the initialization code segment, and begins executing the initialization code segment instructions. After the initialization code segment instructions are executed, the machine code starting address is set to the machine code entry address to obtain a new machine code entry address. The mutex lock is then released and canceled. Then, the new machine code entry address is loaded, jumps to the starting address of the machine code, and executes the function code segment instructions to complete the method call. Otherwise, if the mutex exists, the machine code entry address is directly loaded, jumps to the starting address of the machine code, and executes the function code segment instructions to complete the method call.

[0073] This application provides a code execution method that organizes all one-time initialization code corresponding to the target method into an initialization code segment. The machine code entry address is dynamically modified after the target method is first called, thus preventing the one-time initialization code from being executed repeatedly when the target method is called multiple times, improving code execution efficiency and reducing runtime load. Furthermore, a mutex lock is set before the execution of the initialization code segment to prevent contention during the initialization process caused by multiple threads simultaneously calling the target method.

[0074] Please see Figure 6 This application provides a code execution method applied to an ART virtual machine in an electronic device. The method includes:

[0075] Step S310: During the compilation of the bytecode of the target method, identify the one-time initialization logic code included in the bytecode.

[0076] In the embodiments of this application, during the compilation of the bytecode of the target method, one-time initialization logic is identified. The implementation function of slowPath can determine whether it will only be executed on the first method call. For example, the initialization logic of static members of a class. After a class is initialized, the class state is changed to the state of "initialized and completed". If the class state is retrieved again and the state of "completed" is returned, the initialization logic of the class will not be executed again.

[0077] The specific implementation process is as follows: All types of slowPath logic can be organized. If a slowPath is set to run only once within a method, an initialization type flag is set for that slowPath. When a method needs to create a slowPath of that type, the check code corresponding to the machine of this created slowPath is moved and organized to the end of the machine code sequence.

[0078] Therefore, in this embodiment of the application, all one-time initialization logic code included in the bytecode of the target method can be identified in the aforementioned manner, and an initialization type flag can be set for all one-time initialization logic code.

[0079] Step S320: After compiling the bytecode of the target method, the one-time initialization logic code and the check code corresponding to the initialization logic code included in the bytecode are moved sequentially to the end of the machine code sequence corresponding to the target method, and the corresponding overture and coda are generated.

[0080] In this embodiment, after compiling the bytecode of the target method, all one-time initialization logic code and the corresponding check code of each one-time initialization logic code included in the bytecode can be moved to the end of the machine code sequence according to the initialization type flag. Specifically, when moving all one-time initialization logic code and the corresponding check code of each one-time initialization logic code to the end of the machine code sequence, they can be moved sequentially according to their order in the compiled machine code sequence.

[0081] Step S330: The one-time initialization logic code moved to the end of the machine code sequence, the check code corresponding to the initialization logic code, the prelude and the coda are taken as the initialization code segment.

[0082] In this embodiment, after moving all the one-time initialization logic code and the corresponding check code of each one-time initialization logic code to the end of the machine code sequence in sequence, since the initialization code segment only runs when the target method is called for the first time, the initialization code segment is equivalent to the implementation of a separate initialization method, which needs an overture and an end. Therefore, an overture and an end (such as stack frame expansion, stack overflow check, return result, etc.) can be added to all the one-time initialization logic code and the corresponding check code of each one-time initialization logic code that are moved to the end of the machine code sequence in sequence, to obtain the initialization code segment.

[0083] As one approach, the initialization code segment is optimized to obtain an optimized initialization code segment.

[0084] In this embodiment, the code in the initialization code segment can be optimized, such as eliminating redundant instructions, eliminating repeated memory read and write instructions, reusing code, and removing unnecessary check code. The optimized initialization code segment forms a whole initialization logic, which is beneficial to the running efficiency of the initialization code segment.

[0085] Step S340: During the loading process of the class method, create the method object corresponding to the target method and set the machine code entry address of the target method.

[0086] Step S350: When setting the machine code entry address of the target method, if it is determined that the machine code sequence corresponding to the target method includes an initialization code segment, the starting address of the initialization code segment is used as the machine code entry address.

[0087] Step S360: In response to a method call instruction, if it is determined that the target method is called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, load the machine code entry address of the target method, wherein the initialization code segment is set at the end of the machine code sequence and the initialization code segment includes at least one one-time initialization code.

[0088] In one manner, in response to a method call instruction, if it is determined that the target method is being called for the first time and the machine code sequence corresponding to the target method includes the optimized initialization code segment, the machine code entry address of the target method is loaded.

[0089] Step S370: Jump to the beginning of the initialization code segment based on the machine code entry address, and start executing the instructions of the initialization code segment.

[0090] Step S380: After the execution of the instructions of the initialization code segment is completed, the machine code entry address is set to the starting address of the machine code sequence to obtain a new machine code entry address.

[0091] Step S390: Based on the new machine code entry address, jump to the beginning of the machine code sequence and begin executing the instructions of the functional code segment in the machine code sequence.

[0092] For example, the comparison of machine code sequences before and after the execution of the process described in steps S310-S390 can be as follows: Figure 7 and Figure 8 As shown, Figure 7 This is a schematic diagram of the machine code sequence before execution. Figure 7 There are two one-time initialization logics, corresponding to slowPath2 and slowPath3. Figure 7 The directional lines on the left and right sides of the box represent the instruction execution jump path. For example, when the code is checked by GC (Garbage Collection), if it is found that slowPath1 needs to be executed, it will jump to and load the first instruction inside slowPath1. After the last jump instruction of slowPath1 is executed, it will jump back to the beginning of the first functional code block and execute the first instruction of the functional code block. This process continues until the method call is completed.

[0093] Figure 8 This is a schematic diagram of the machine code sequence after execution. Figure 8 All one-time initialization logic and check code are sequentially arranged to the end of the machine code sequence, and necessary preludes and codas are constructed and inserted before and after the initialization code. Figure 8 In this approach, upon the first call to the method, the initialization code segment is loaded and all instructions within it are executed. After executing all instructions in the initialization code segment, the system can jump directly to the function code segment and execute all instructions there. Compared to... Figure 7 The calling process shown is as follows: Figure 8 The commission adjustment process shown optimizes all initialization code as a whole, resulting in a code segment with higher execution efficiency.

[0094] This application provides a code execution method that organizes all one-time initialization code corresponding to the target method into an initialization code segment, and dynamically modifies the machine code entry address after the target method is called for the first time. This allows the one-time initialization code to no longer be executed repeatedly when the target method is called multiple times, thereby improving code execution efficiency and reducing runtime load.

[0095] Please see Figure 9This application provides a code execution device 400, which includes:

[0096] The loading unit 410 is configured to, in response to a method call instruction, if it is determined that the target method is being called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, load the machine code entry address of the target method, wherein the initialization code segment is set at the end of the machine code sequence and the initialization code segment includes at least one one-time initialization code.

[0097] In one manner, the loading unit 410 is also used to load the machine code entry address of the target method via a mutex lock.

[0098] Alternatively, the loading unit 410 is also configured to, in response to a method call instruction, load the machine code entry address of the target method if it is determined that the target method is being called for the first time and the machine code sequence corresponding to the target method includes the optimized initialization code segment.

[0099] The first execution unit 420 is used to jump to the beginning of the initialization code segment based on the machine code entry address and start executing the instructions of the initialization code segment.

[0100] The setting unit 430 is used to set the machine code entry address to the starting address of the machine code sequence after the execution of the instructions of the initialization code segment is completed, so as to obtain a new machine code entry address.

[0101] In one manner, the setting unit 430 is also used to release the mutex lock.

[0102] The second execution unit 440 is used to jump to the beginning of the machine code sequence based on the new machine code entry address and start executing the instructions of the functional code segment in the machine code sequence.

[0103] Please see Figure 10 The device 400 further includes:

[0104] The creation unit 450 is used to create a method object corresponding to the target method during the loading process of the class method and set the machine code entry address of the target method; when setting the machine code entry address of the target method, if it is determined that the machine code sequence corresponding to the target method includes an initialization code segment, the starting address of the initialization code segment is used as the machine code entry address.

[0105] In one manner, the creation unit 450 is also used to, when setting the machine code entry address of the target method, if it is determined that the machine code sequence corresponding to the target method does not include an initialization code segment, use the starting address of the machine code sequence as the machine code entry address.

[0106] Alternatively, the creation unit 450 is also used to identify one-time initialization logic code included in the bytecode during the compilation of the bytecode of the target method; after the compilation of the bytecode of the target method is completed, the one-time initialization logic code included in the bytecode and the check code corresponding to the initialization logic code are moved sequentially to the end of the machine code sequence corresponding to the target method, and corresponding prelude and coda are generated; the one-time initialization logic code moved to the end of the machine code sequence, the check code corresponding to the initialization logic code, the prelude and the coda are used as the initialization code segment.

[0107] Optionally, the creation unit 450 is also used to optimize the initialization code segment to obtain an optimized initialization code segment.

[0108] Optionally, the creation unit 450 is further configured to analyze the machine code sequence corresponding to the target method when setting the machine code entry address of the target method; if it is determined that the machine code sequence includes two preludes, then it is determined that the machine code sequence includes an initialization code segment; and the starting address of the initialization code segment is used as the machine code entry address.

[0109] It should be noted that the device embodiments in this application correspond to the aforementioned method embodiments. The specific principles in the device embodiments can be found in the content of the aforementioned method embodiments, and will not be repeated here.

[0110] The following will combine Figure 11 This application describes an electronic device.

[0111] Please see Figure 11 Based on the aforementioned code execution method and apparatus, this application embodiment also provides another electronic device 800 capable of executing the aforementioned code execution method. The electronic device 800 includes one or more (only one shown in the figure) processors 802, a memory 804, and a network module 806 coupled together. The memory 804 stores programs capable of executing the contents of the aforementioned embodiments, and the processor 802 can execute the programs stored in the memory 804.

[0112] The processor 802 may include one or more processing cores. The processor 802 connects to various parts within the electronic device 800 using various interfaces and lines, and performs various functions and processes data by running or executing instructions, programs, code sets, or instruction sets stored in the memory 804, and by calling data stored in the memory 804. Optionally, the processor 802 may be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor 802 may integrate one or a combination of several of the following: Central Processing Unit (CPU), Graphics Processing Unit (GPU), and modem. The CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the displayed content; and the modem handles wireless communication. It is understood that the modem may also not be integrated into the processor 802 and may be implemented separately using a communication chip.

[0113] The memory 804 may include random access memory (RAM) or read-only memory (ROM). The memory 804 can be used to store instructions, programs, code, code sets, or instruction sets. The memory 804 may include a program storage area and a data storage area. The program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as touch functionality, sound playback functionality, image playback functionality, etc.), and instructions for implementing the various method embodiments described below. The data storage area may also store data created by the electronic device 800 during use (such as phonebook data, audio and video data, chat log data, etc.).

[0114] The network module 806 is used to receive and transmit electromagnetic waves, realizing the mutual conversion between electromagnetic waves and electrical signals, thereby communicating with communication networks or other devices, such as electronic devices. The network module 806 may include various existing circuit elements for performing these functions, such as antennas, radio frequency transceivers, digital signal processors, encryption / decryption chips, SIM cards, memory, etc. The network module 806 can communicate with various networks such as the Internet, corporate intranets, and wireless networks, or communicate with other devices through wireless networks. The aforementioned wireless networks may include cellular telephone networks, wireless local area networks, or metropolitan area networks. For example, the network module 806 can interact with base stations.

[0115] Please refer to Figure 12 This diagram illustrates a structural block diagram of a computer-readable storage medium provided in an embodiment of this application. The computer-readable storage medium 900 stores program code that can be called by a processor to execute the methods described in the above method embodiments.

[0116] The computer-readable storage medium 900 may be an electronic memory such as flash memory, EEPROM (Electrically Erasable Programmable Read-Only Memory), EPROM, hard disk, or ROM. Optionally, the computer-readable storage medium 900 includes a non-transitory computer-readable storage medium. The computer-readable storage medium 900 has storage space for program code 910 that performs any of the method steps described above. This program code can be read from or written to one or more computer program products. The program code 910 may be compressed, for example, in a suitable form.

[0117] This application provides a code execution method, apparatus, electronic device, and storage medium. First, in response to a method call instruction, if it is determined that the target method is being called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, the machine code entry address of the target method is loaded. The initialization code segment is located at the end of the machine code sequence and includes at least one-time initialization code. Then, based on the machine code entry address, the execution jumps to the beginning of the initialization code segment and begins executing the instructions of the initialization code segment. After the execution of the initialization code segment instructions is completed, the machine code entry address is set to the beginning address of the machine code sequence, resulting in a new machine code entry address. Finally, based on the new machine code entry address, the execution jumps to the beginning of the machine code sequence and begins executing the instructions of the functional code in the machine code sequence. Through this method, all one-time initialization code corresponding to the target method is organized into an initialization code segment, and the machine code entry address is dynamically modified after the target method is called for the first time. This prevents the one-time initialization code from being repeatedly executed when the target method is called multiple times, improving code execution efficiency and reducing runtime load.

[0118] The embodiments of the present invention have been described above with reference to the accompanying drawings. However, the present invention is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of the present invention without departing from the spirit and scope of the claims, and all of these forms are within the protection scope of the present invention.

Claims

1. A code execution method, characterized in that, The method includes: In response to a method call instruction, if it is determined that the target method is being called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, the machine code entry address of the target method is loaded, wherein the initialization code segment is set at the end of the machine code sequence and the initialization code segment includes at least one one-time initialization code. Based on the machine code entry address, the system jumps to the beginning of the initialization code segment and begins executing the instructions of the initialization code segment; After the execution of the instructions in the initialization code segment is completed, the machine code entry address is set to the starting address of the machine code sequence to obtain a new machine code entry address; Based on the new machine code entry address, the system jumps to the beginning of the machine code sequence and begins executing the instructions of the functional code segment in the machine code sequence.

2. The method according to claim 1, characterized in that, The machine code entry address for loading the target method includes: The machine code entry address of the target method is loaded using a mutex lock; After the execution of the instructions in the initialization code segment is completed, the step of setting the machine code entry address to the starting address of the machine code sequence to obtain the new machine code entry address further includes: Release the mutex.

3. The method according to claim 1, characterized in that, In response to a method call instruction, if it is determined that the target method is being called for the first time and the target method includes an initialization code segment, the method further includes the following steps before loading the machine code entry address of the target method: During the loading process of a class method, a method object corresponding to the target method is created, and the machine code entry address of the target method is set. When setting the machine code entry address of the target method, if it is determined that the machine code sequence corresponding to the target method includes an initialization code segment, the starting address of the initialization code segment is used as the machine code entry address.

4. The method according to claim 3, characterized in that, The method further includes: When setting the machine code entry address of the target method, if it is determined that the machine code sequence corresponding to the target method does not include an initialization code segment, the starting address of the machine code sequence is used as the machine code entry address.

5. The method according to claim 3, characterized in that, Before creating the method object corresponding to the target method and setting the machine code entry address of the target method during the class method loading process, the process further includes: During the compilation of the bytecode of the target method, the one-time initialization logic code included in the bytecode is identified; After the bytecode of the target method is compiled, the one-time initialization logic code and the check code corresponding to the initialization logic code included in the bytecode are moved sequentially to the end of the machine code sequence corresponding to the target method, and the corresponding overture and outro are generated. The one-time initialization logic code moved to the end of the machine code sequence, the check code corresponding to the initialization logic code, the prelude, and the coda are used as the initialization code segment.

6. The method according to claim 5, characterized in that, The method further includes: The initialization code segment is optimized to obtain the optimized initialization code segment; In response to a method call instruction, if it is determined that the target method is being called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, the machine code entry address of the target method is loaded, including: In response to a method call instruction, if it is determined that the target method is being called for the first time and the machine code sequence corresponding to the target method includes the optimized initialization code segment, the machine code entry address of the target method is loaded.

7. The method according to claim 3, characterized in that, When setting the machine code entry address of the target method, if it is determined that the machine code sequence corresponding to the target method includes an initialization code segment, the starting address of the initialization code segment is used as the machine code entry address, including: When setting the machine code entry address of the target method, the machine code sequence corresponding to the target method is analyzed; If it is determined that the machine code sequence includes two preludes, then it is determined that the machine code sequence includes an initialization code segment; The starting address of the initialization code segment is used as the machine code entry address.

8. A code execution device, characterized in that, The device includes: A loading unit is configured to, in response to a method call instruction, if it is determined that the target method is being called for the first time and the machine code sequence corresponding to the target method includes an initialization code segment, load the machine code entry address of the target method, wherein the initialization code segment is set at the end of the machine code sequence and the initialization code segment includes at least one one-time initialization code. The first execution unit is configured to jump to the beginning of the initialization code segment based on the machine code entry address and begin executing the instructions of the initialization code segment; The setting unit is used to set the machine code entry address to the starting address of the machine code sequence after the execution of the instructions of the initialization code segment is completed, so as to obtain a new machine code entry address. The second execution unit is used to jump to the beginning of the machine code sequence based on the new machine code entry address and start executing the instructions of the functional code segment in the machine code sequence.

9. An electronic device, characterized in that, It includes one or more processors; one or more programs are stored in memory and configured to be executed by the one or more processors according to any one of claims 1-7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores program code, wherein the program code, when executed by a processor, performs the method according to any one of claims 1-7.

Citation Information

Patent Citations

  • Hot update method and device for target application, storage medium and electronic equipment

    CN111666096A

  • Code repositioning method and device

    CN114296794A