Machine code compiling optimization method and device and electronic equipment
By comparing the modified code with the source code and distinguishing between the changed and unchanged intermediate representations, and combining the pre-compilation results of dependencies, the compilation results of the unchanged parts can be directly reused, and only the changed parts are incrementally processed. This solves the problem of low machine code generation efficiency for large applications and achieves efficient machine code generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-03
- Publication Date
- 2026-04-03
AI Technical Summary
The machine code generation process for large applications suffers from low generation efficiency, mainly because large-scale intermediate representations (IRs) require a significant amount of time to process and compile, especially the repetitive static analysis and IR construction of dependent libraries.
By comparing the modified code with the source code, the modified and unchanged intermediate representations are distinguished. The intermediate representations of dependencies are compiled in advance, and the compilation results of the unchanged parts are directly reused. Only the modified parts are incrementally processed to generate machine code.
It significantly shortens the machine code generation cycle and improves generation efficiency, especially for projects with complex dependencies and large code size, avoiding a large number of repetitive and time-consuming steps.
Smart Images

Figure CN121785600A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and more specifically, to a machine code compilation and optimization method, apparatus, and electronic device. Background Technology
[0002] As the core carrier for running applications, the way virtual machines compile applications directly determines the startup efficiency, resource consumption, and deployment adaptability of the program. For example, GraalVM, a high-performance multi-language virtual machine, has achieved a key breakthrough in application compilation. One of its core features is the introduction of Ahead-of-Time (AOT) technology, which can fully compile the bytecode of Java applications into independent native executables (native images) adapted to specific hardware platforms before the program runs. It has irreplaceable advantages in cloud-native scenarios, microservice scenarios, and command-line tool scenarios, perfectly meeting the core needs of modern applications for lightweight and rapid deployment.
[0003] However, the intermediate representation (IR) generated after the large application is built is huge and complex in structure. It not only contains IR nodes corresponding to hundreds of thousands of lines of business code, but also needs to integrate the IR logic of all third-party dependencies, which requires a lot of time to process and compile, affecting the efficiency of machine code generation. Summary of the Invention
[0004] The problem addressed by this invention is how to improve the efficiency of machine code generation.
[0005] To address the above problems, this invention provides a machine code compilation optimization method, apparatus, and electronic device.
[0006] In a first aspect, the present invention provides a machine code compilation optimization method, comprising: Based on the comparison results between the modified code and the source code of the obtained project, intermediate representations with and without changes are obtained. The intermediate representation with changes is obtained by modifying the modified code in the modified code and the project's dependencies, while the intermediate representation without changes is obtained by using the initial intermediate representation pre-generated from the source code. The modified intermediate representation, the unchanged intermediate representation, and the early-compiled intermediate representation of the dependency are converted into machine code corresponding to the modified code through machine instructions, wherein the early-compiled intermediate representation is obtained from the early-compilation result of the dependency.
[0007] Optionally, obtaining the modified intermediate representation and the unmodified intermediate representation based on the comparison results between the modified code and the source code of the obtained project includes: The modified code and the source code are compared to obtain the changed code and the unchanged code in the modified code; Based on the changed code and the dependencies, an intermediate representation of the changes is generated through incremental static analysis. The unmodified intermediate representation in the initial intermediate representation is determined based on the matching result between the unmodified code and the source code, wherein the initial intermediate representation is obtained through the source code and the dependencies.
[0008] Optionally, comparing the modified code with the source code to obtain the changed code and the unchanged code in the modified code includes: Determine the source code hash value based on the source code; Determine the hash value of the modified code based on the modified code; The unchanged code and the changed code are determined based on the matching result between the modified code hash value and the source code hash value.
[0009] Optionally, determining the unchanged code and the changed code based on the matching result of the modified code hash value and the source code hash value includes: The modified code whose hash value matches the source code hash value is identified as the unchanged code. The modified code whose hash value does not match the source code hash value is identified as the changed code.
[0010] Optionally, the step of generating an intermediate representation of the change based on the change code and the dependencies through incremental static analysis includes: Based on the changed code and the dependencies, a corresponding change structure metadata file is generated through incremental static analysis. The intermediate representation of the change is generated by merging the changed structure metadata file and the changed code.
[0011] Optionally, determining the unchanged intermediate representation in the initial intermediate representation based on the matching result between the unchanged code and the source code includes: The initial structure metadata is obtained through static analysis based on the dependencies and the source code. The initial structure metadata and the source code are merged to generate an initial intermediate representation corresponding to the source code, and the initial intermediate representation is cached, wherein the cache key of the initial intermediate representation is the hash value of the corresponding source code; The initial intermediate representation corresponding to the portion of the cache key that matches the hash value of the unchanged code is determined as the unchanged intermediate representation.
[0012] Optionally, the step of converting the modified intermediate representation, the unmodified intermediate representation, and the early-compiled intermediate representation of the dependencies into machine code corresponding to the modified code through machine instructions includes... The modified intermediate representation, the unchanged intermediate representation, and the early-compiled intermediate representation of the dependencies are merged to obtain the complete intermediate representation corresponding to the modified code; The complete intermediate representation is converted into machine code using machine instructions.
[0013] Optionally, the method further includes: The dependencies are pre-compiled to generate intermediate representations of the dependencies, and these intermediate representations are independently cached through the base layer. The cache key of the intermediate representation of the dependencies is generated by the hash value of the triple coordinates of the dependencies.
[0014] In a second aspect, the present invention provides a machine code compilation optimization apparatus, comprising: The comparison module is used to obtain a modified intermediate representation and an unmodified intermediate representation based on the comparison results between the modified code and the source code of the obtained project. The modified intermediate representation is obtained by modifying the modified code in the modified code and the project's dependencies, and the unmodified intermediate representation is obtained by using an initial intermediate representation pre-generated from the source code. A generation module is used to convert the modified intermediate representation, the unchanged intermediate representation, and the early-compiled intermediate representation of the dependencies into machine code corresponding to the modified code through machine instructions, wherein the early-compiled intermediate representation is obtained from the early-compilation result of the dependencies.
[0015] Thirdly, the present invention provides an electronic device, including a memory and a processor; The memory is used to store computer programs; The processor is configured to implement the machine code compilation optimization method as described in the first aspect when executing the computer program.
[0016] Fourthly, the present invention provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the machine code compilation and optimization method as described in the first aspect.
[0017] The beneficial effects of the machine code compilation optimization method, apparatus, and electronic device of the present invention are as follows: By comparing the modified code with the source code, corresponding modified intermediate representations and unchanged intermediate representations are obtained. The unchanged part directly reuses the part corresponding to the unchanged code in the pre-generated initial intermediate representation, eliminating the need to re-perform static analysis, logic verification, and IR construction on the unmodified code in the project, thus saving a large amount of repetitive and costly basic calculations. Furthermore, in the process of generating machine code, the pre-compiled intermediate representation of dependencies directly reuses the pre-compiled results of third-party dependencies, eliminating the need to repeatedly perform compilation, static analysis, and IR construction on the minimally changed dependency libraries every time machine code is generated, completely eliminating this fixed and costly process. By clearly distinguishing between modified and unchanged intermediate representations, the machine instruction conversion stage only needs to process the new logic instruction mapping corresponding to the modified code for modified intermediate representations, while the unchanged intermediate representation can directly reuse the previously generated instruction conversion results, eliminating the need to re-perform full instruction conversion on the unmodified code in the project. By reusing all invariant parts and focusing only on incremental changes, the design minimizes invalid calculations in the machine code generation process, significantly shortening the generation cycle. Especially for projects with complex dependencies and large code size, it can avoid a large number of repetitive and time-consuming steps, and significantly improve the efficiency of machine code generation. Attached Figure Description
[0018] Figure 1 This is a flowchart illustrating a machine code compilation optimization method according to an embodiment of the present invention; Figure 2 This is a schematic diagram of the structure of a machine code compilation optimization device according to an embodiment of the present invention; Figure 3 This is a schematic diagram of the structure of an electronic device according to an embodiment of the present invention. Detailed Implementation
[0019] To make the above-mentioned objects, features, and advantages of the present invention more apparent and understandable, specific embodiments of the present invention will be described in detail below with reference to the accompanying drawings. Although some embodiments of the present invention are shown in the drawings, it should be understood that the present invention can be implemented in various forms and should not be construed as limited to the embodiments set forth herein. Rather, these embodiments are provided to provide a more thorough and complete understanding of the present invention. It should be understood that the accompanying drawings and embodiments of the present invention are for illustrative purposes only and are not intended to limit the scope of protection of the present invention.
[0020] It should be understood that the various steps described in the method embodiments of the present invention may be performed in different orders and / or in parallel. Furthermore, the method embodiments may include additional steps and / or omit the steps shown. The scope of the present invention is not limited in this respect.
[0021] The term "comprising" and its variations as used herein are open-ended, meaning "including but not limited to"; the term "based on" means "at least partially based on"; the term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments"; and the term "optionally" means "optional embodiments". Definitions of other terms will be given in the following description. It should be noted that the concepts of "first," "second," etc., mentioned in this invention are used only to distinguish different devices, modules, or units, and are not intended to limit the order of functions performed by these devices, modules, or units or their interdependencies.
[0022] It should be noted that the terms "a" and "a plurality of" used in this invention are illustrative rather than restrictive. Those skilled in the art should understand that, unless otherwise expressly indicated in the context, they should be understood as "one or more".
[0023] The names of the messages or information exchanged between the multiple devices in the embodiments of the present invention are for illustrative purposes only and are not intended to limit the scope of these messages or information.
[0024] In related technologies, large applications often have hundreds of thousands or even millions of lines of code, and rely on a large number of complex third-party libraries. This makes the complete AOT compilation process often take several minutes or even tens of minutes, which is the result of multiple highly complex and computationally intensive core processes working together. Among them, AOT compilation must perform deep and comprehensive static analysis to ensure the correctness of the compilation results and runtime safety. Pointer analysis needs to track the memory reference relationships of all objects in the application and check for potential null pointer and dangling pointer risks. In large applications, the number of object instances is huge and the cross-module reference chains are long. This step alone requires traversing a massive amount of code logic. Furthermore, AOT compilation follows the "closed world assumption," which requires that all classes, methods, and resources that the application may use at runtime be identified during the compilation phase. Therefore, it is necessary to build a heap snapshot of the entire application. This process requires scanning the bytecode of the application itself and all dependent libraries, recording the initial state and dependency relationships of all necessary objects during compilation. The dependency tree of large applications often contains hundreds of modules, and the data volume of the heap snapshot can reach GB level. The scanning and building process consumes a lot of CPU and memory resources. Finally, the machine code generation stage is also very time-consuming. After static analysis and heap snapshot construction, the intermediate representation (IR) generated by large applications is extremely large. The compilation system needs to convert these IRs into machine instructions adapted to specific hardware platforms (such as Linux / x86_64, ARM). At the same time, it also needs to perform performance optimizations such as instruction reordering, register optimization, and loop unrolling. Each optimization step requires processing a massive number of IR nodes and ensuring the logical consistency of the optimized code. This results in the compilation process of machine code accounting for a very high proportion of time, which seriously affects the efficiency of machine code generation.
[0025] To address the problems existing in the aforementioned related technologies, embodiments of the present invention provide a machine code compilation optimization method, apparatus, and electronic device.
[0026] like Figure 1 As shown, an embodiment of the present invention provides a machine code compilation optimization method, comprising: S110, based on the comparison results between the modified code and the source code of the obtained project, a modified intermediate representation and an unmodified intermediate representation are obtained, wherein the modified intermediate representation is obtained by modifying the modified code in the modified code and the project's dependencies, and the unmodified intermediate representation is obtained by using the initial intermediate representation pre-generated from the source code.
[0027] Specifically, in the process of generating intermediate representations (IRs), incremental analysis tools are first used to precisely compare the modified code and source code line by line and module by module. This clearly distinguishes between the changed parts of the code (such as modified business methods, newly added class definitions, and adjusted configuration logic) and the unchanged parts (such as unmodified interface layer code, utility classes, and historical business modules). This comparison result is the core basis for generating the two intermediate representations subsequently. The generation of the modified intermediate representation does not solely rely on the modified code itself, but also needs to consider the project's third-party dependencies. This is because modified code often has dependencies on third parties, such as calls to core components of Spring Boot (a rapid development scaffolding based on the Spring framework) and data access interfaces of MyBatis (an open-source persistence layer framework). These dependencies must be based on pre-compiled dependency intermediate representations (the dependency IRs used for caching in the base layer) to ensure the logical integrity and syntactic correctness of the modified code. For example, when modifying the order amount calculation method, it is necessary to associate the JSON parsing IR (Indirect Reference Array) that Fastjson (a high-performance JSON parsing and serialization library) depends on to ensure the consistency of data format processing logic, ultimately forming a modified intermediate representation that only contains the changed parts and adapts to the dependency logic. The generation of the unchanged intermediate representation directly reuses the initial intermediate representation pre-generated and cached in the intermediate layer from the source code. This means it uses the original initial intermediate representation corresponding to the parts of the source code that have not been modified (such as the unchanged user query interface IR and log utility class IR). It also associates the intermediate representations of project dependencies. This is because the unchanged code itself relies on third-party libraries to implement core functions (such as user authentication logic relying on Spring Security's IR). There is no need to re-analyze the unchanged code and dependencies; simply associating and integrating the existing cached intermediate representations of both can form a logically consistent unchanged intermediate representation. This ensures the integrity of the intermediate representation and avoids repeated compilation and calculation of the unchanged parts.
[0028] S120, the modified intermediate representation, the unchanged intermediate representation, and the early-compiled intermediate representation of the dependency are converted into machine code corresponding to the modified code through machine instructions, wherein the early-compiled intermediate representation is obtained through the early-compilation result of the dependency.
[0029] Specifically, the early-compiled intermediate representation of dependencies originates from the early-compiled (AOT) results of third-party dependencies (such as the Spring Boot framework and Fastjson parsing library). This early-compiled intermediate representation is cached through the base layer, and can be directly called from the base layer when the early-compiled intermediate representation of a dependency is needed. During this early-compilation process, the dependency library has completed static analysis (such as pointer analysis and escape analysis), dependency verification, and generated an IR that conforms to the "closed-world hypothesis." This early-compiled IR of the dependency does not need to be processed repeatedly and can be directly reused as the compilation base. The unmodified intermediate representation is the cached IR of the unmodified parts of the project, such as the cached IR of the UserController (user module controller) interface layer and OrderMapper (order module data mapper) that have not been modified. That is, the unmodified intermediate representation retains the complete logical structure and static analysis results of the original code and does not need to be regenerated. The modified intermediate representation is a new IR generated for modified code, such as the adjusted OrderService.calcAmount() method (the method for calculating order amount with empty parameters). It only contains the logic updates of the changed parts and its size is much smaller than the full IR.
[0030] Furthermore, during the machine instruction conversion stage, these three types of IRs first need to be logically integrated and linked to ensure that the changed intermediate representation can correctly connect the call relationships of the unchanged intermediate representation (such as the modified business method calling the unchanged utility class logic), while maintaining compatibility with the base library interface of the pre-compiled intermediate representation of the dependent items (such as calling the JSON.parse() method of Fastjson), forming a unified IR without logical conflicts and covering the complete business chain. Subsequently, based on the target hardware platform (such as Linux / x86_64, Windows / ARM), each logical node in the unified IR (such as variable assignment, condition judgment, cross-module method call) is mapped one by one to the native machine instructions of the corresponding platform. For example, the "object serialization logic" in the dependent item IR is converted into the CALL (function call) and MOV (data transfer) instructions of the x86 architecture, the "interface parameter verification logic" in the unchanged IR reuses the previously converted machine instruction results, and only the "dynamic tax rate calculation logic" in the changed IR generates new ADD (operation) and JMP (branch jump) instructions. The entire conversion process reuses the dependencies and unchanged parts of the IR and corresponding machine instruction results, focusing only on the instruction mapping of the changed parts. This ensures the consistency between machine code and business logic, significantly reduces conversion time, and ultimately efficiently generates machine code that is adapted to the target platform and contains only the new instructions corresponding to the modified code.
[0031] In this embodiment, by comparing the modified code with the source code, corresponding modified and unchanged intermediate representations are obtained. The unchanged part directly reuses the part corresponding to the unchanged code in the pre-generated initial intermediate representation, eliminating the need to re-perform static analysis, logic verification, and IR construction on the unchanged code in the project, thus saving a large amount of repetitive and costly basic calculations. Furthermore, in the process of generating machine code, the pre-compiled intermediate representation of dependencies directly reuses the pre-compiled results of third-party dependencies, eliminating the need to repeatedly perform compilation, static analysis, and IR construction on the minimally changed dependency libraries every time machine code is generated, completely eliminating this fixed and costly process. By clearly distinguishing between modified and unchanged intermediate representations, the machine instruction conversion stage only needs to process the new logic instruction mapping corresponding to the modified code for modified intermediate representations, while the unchanged intermediate representation can directly reuse the previously generated instruction conversion results, eliminating the need to re-perform full instruction conversion on the unchanged code in the project. By reusing all invariant parts and focusing only on incremental changes, the design minimizes invalid calculations in the machine code generation process, significantly shortening the generation cycle. Especially for projects with complex dependencies and large code size, it can avoid a large number of repetitive and time-consuming steps, and significantly improve the efficiency of machine code generation.
[0032] Optionally, such as Figure 1 As shown, obtaining the modified intermediate representation and the unmodified intermediate representation based on the comparison results between the modified code and the source code of the acquired project includes: The modified code and the source code are compared to obtain the changed code and the unchanged code in the modified code; Based on the changed code and the dependencies, an intermediate representation of the changes is generated through incremental static analysis. The unmodified intermediate representation in the initial intermediate representation is determined based on the matching result between the unmodified code and the source code, wherein the initial intermediate representation is obtained through the source code and the dependencies.
[0033] In this optional embodiment, before constructing an intermediate representation of the modified code of the project, a code comparison tool is first used to conduct a fine-grained verification between the modified code and the source code. The comparison is performed line by line and module by module from dimensions such as class structure, method signature, logic within function body and resource reference path, so as to accurately separate two types of core parts in the modified code: one type is changed code, which is code segments that have been added (such as adding a product inventory query method), modified (such as adjusting the order discount calculation logic), or deleted (such as removing outdated log printing statements); the other type is unchanged code, which is the part that is completely consistent with the source code and has not been adjusted in any way (such as the user registration interface, general data verification utility class, etc. that have not been modified). Based on this comparison result, an incremental static analysis process is initiated for the changed code. This process does not require analyzing the entire codebase; it focuses only on the changed code itself. Simultaneously, it considers the project's dependencies (such as the Spring Boot framework and database connection pool components). Because the changed code may call dependent library interfaces (e.g., the modified payment logic needs to call methods of a third-party payment SDK), it relies on the class inheritance tree, method parameter definitions, and other structural information of the dependencies to ensure the syntactic correctness and call consistency of the changed logic. Ultimately, a modified intermediate representation containing only the changed logic is generated. For unchanged code, since it completely matches the source code, it uses unique identifiers such as classpath and method signature to accurately locate the corresponding IR fragment in the initial intermediate representation (which is a full IR generated based on the complete source code and all dependencies, already containing the static analysis results of all code). This fragment is identified as the unchanged intermediate representation, directly reusing the previous compilation results. This completely avoids repeated static analysis and IR construction for the unchanged parts, effectively improving the generation efficiency of the intermediate representation for modified code.
[0034] Optionally, comparing the modified code with the source code to obtain the changed code and the unchanged code in the modified code includes: Determine the source code hash value based on the source code; Determine the hash value of the modified code based on the modified code; The unchanged code and the changed code are determined based on the matching result between the modified code hash value and the source code hash value.
[0035] Optionally, determining the unchanged code and the changed code based on the matching result of the modified code hash value and the source code hash value includes: The modified code whose hash value matches the source code hash value is identified as the unchanged code. The modified code whose hash value does not match the source code hash value is identified as the changed code.
[0036] In this optional embodiment, for each independently identifiable code unit in the source code (such as the UserService.java file or the selectOrderById method in the OrderMapper interface), its text content is processed using a cryptographic hash algorithm (such as SHA-256) (excluding formatting, indentation, comments, and other content that does not affect the logic), generating a unique corresponding source code hash value. Each hash value is bound one-to-one with a specific code unit in the source code, forming a one-to-one mapping relationship between "source code unit - hash value". Subsequently, with the exact same granularity and algorithm, hash calculation is performed on the modified code. For the code units in the modified code that correspond to the source code structure (such as the same UserService.java file or the selectOrderById method), their modified hash values are calculated to obtain a one-to-one mapping relationship between "modified code unit - hash value". After calculating the two types of hash values, the comparison logic is initiated. The hash value of each unit in the modified code is matched one-to-one with the hash value of the corresponding unit in the source code. If the two are completely identical (identical hash values mean that no logical or structural changes have occurred in the code content, including adding, deleting, or modifying code statements), then that part of the modified code is determined to be unchanged code. If the two are inconsistent (even a modification of a single character, an adjustment of function parameters, or a minor tweak to the code logic will cause a significant change in the hash value), then that part of the modified code is determined to be changed code. This hash value comparison method avoids the inefficiency and errors of manual line-by-line verification, and ensures accurate identification of code changes through the uniqueness of hash values. This provides a reliable basis for subsequent incremental generation of intermediate representations and reduces redundant compilation calculations.
[0037] Optionally, the step of generating an intermediate representation of the change based on the change code and the dependencies through incremental static analysis includes: Based on the changed code and the dependencies, a corresponding change structure metadata file is generated through incremental static analysis. The intermediate representation of the change is generated by merging the changed structure metadata file and the changed code.
[0038] In this optional embodiment, incremental static analysis does not target the entire codebase, but focuses only on the changed code itself. It also incorporates the structural information (class inheritance tree, method parameter list, interface definition, etc.) of project dependencies (such as the Spring Boot framework, database driver, etc.) to analyze the syntax structure, logical relationships, and call associations with dependencies of the changed code. For example, if the changed code adds logic to call Fastjson (a high-performance JSON parsing and serialization library) for data serialization, incremental static analysis will verify whether the signature of the corresponding serialization method in the Fastjson dependency matches, ensuring the legality of the call. These parsing results (including the changed code's class definition, method signature, variable types, dependency reference chains, control flow and data flow information, etc.) are integrated in a structured form to generate a changed structure metadata file. This file is essentially a structured description of "what the changed code is, what it depends on, and how its logic is organized," providing a clear structural basis for the subsequent generation of intermediate representations. Next, the merging phase begins: the changed structure metadata file and the changed code are deeply integrated. On the one hand, the structured information in the metadata file serves as a framework to ensure the structural standardization of the intermediate representation of the change (such as the hierarchical relationship between classes and methods, and the correctness of dependency references). On the other hand, the specific business logic in the changed code (such as conditional judgments, loop calculations, function calls, etc.) is filled into this framework. At the same time, a verification mechanism ensures the consistency between the metadata description and the actual code logic (for example, the "calling the pay method of the payment SDK" recorded in the metadata completely matches the method actually called in the code). Finally, a changed intermediate representation that contains complete changed logic and conforms to the intermediate representation (IR) specification is generated. The entire process, guided by "incremental analysis + structured metadata-guided merging," not only ensures the correctness of the changed intermediate representation but also avoids invalid analysis of the unchanged code, greatly improving the generation efficiency.
[0039] Optionally, determining the unchanged intermediate representation in the initial intermediate representation based on the matching result between the unchanged code and the source code includes: An initial structure metadata file is obtained through static analysis based on the dependencies and the source code. The initial structure metadata and the source code are merged to generate an initial intermediate representation corresponding to the source code, and the initial intermediate representation is cached, wherein the cache key of the initial intermediate representation is the hash value of the corresponding source code; The initial intermediate representation corresponding to the portion of the cache key that matches the hash value of the unchanged code is determined as the unchanged intermediate representation.
[0040] In this optional embodiment, during the process of generating the initial intermediate representation (IR) corresponding to the source code and determining the unchanged intermediate representation, a full static analysis is first performed based on the project's dependencies (such as the Spring Boot framework, MyBatis data access library, etc.) and the complete source code. This analysis deeply analyzes the source code's syntactic structure (such as class definitions, method signatures, variable types), logical relationships (such as control flow, data flow), and call associations with dependencies (such as calling the interfaces of dependent libraries in the source code and inheriting the implementations of dependent classes). At the same time, it verifies the legality of the code syntax and the validity of dependency references. These parsing results are integrated into an initial structure metadata file in a structured form. This file not only records the core structural information of the source code but also contains the association mapping between dependencies and source code (such as the path of a dependent library function called by a certain method), providing a framework basis for the subsequent generation of a standardized initial intermediate representation. Next, the initial structure metadata file and the source code are deeply merged: using the structured information in the metadata file as the skeleton, the hierarchical structure and syntax of the intermediate representation are ensured to meet the compilation requirements. At the same time, the specific business logic in the source code (such as loop calculations, conditional judgments, data processing, etc.) is filled into the skeleton. After passing the logic consistency check (such as the number of method parameters recorded in the metadata matching the actual parameters in the source code), the initial intermediate representation corresponding to the source code is generated. To avoid the subsequent generation of intermediate representations of unchanged code, the initial intermediate representation is cached through the intermediate layer to form an intermediate layer cache, and the hash value of the corresponding source code is used as the cache key. Since the hash value can uniquely identify the source code content (even a small change will cause the hash value to change), it can be ensured that the cached initial intermediate representation strictly corresponds to the source code content. Finally, when determining the unmodified intermediate representation, the previously calculated hash value of the unmodified code is matched one by one with the cache key (source code hash value) used when caching the initial intermediate representation: if the hash value of the unmodified code is completely consistent with the cache key of a certain part of the initial intermediate representation, it means that the source code content corresponding to that part of the initial intermediate representation has not been modified. It can be directly determined as the unmodified intermediate representation without re-performing static analysis and intermediate representation generation, greatly reducing the cost of repeated calculations.
[0041] Optionally, the step of converting the modified intermediate representation, the unmodified intermediate representation, and the early-compiled intermediate representation of the dependencies into machine code corresponding to the modified code through machine instructions includes... The modified intermediate representation, the unchanged intermediate representation, and the early-compiled intermediate representation of the dependencies are merged to obtain the complete intermediate representation corresponding to the modified code; The complete intermediate representation is converted into machine code using machine instructions.
[0042] In this optional embodiment, during the generation of machine code corresponding to the modified code, the integration and merging of three types of intermediate representations (IRs) are first required to construct a complete logical chain. The early-compiled intermediate representation of dependencies is the foundational support, containing underlying logic such as method implementations and interface definitions of dependent classes, providing the necessary calling basis for the project code. The unmodified intermediate representation is a reused result of the unchanged parts of the project, retaining the complete logic of the unadjusted modules in the source code, requiring no reconstruction. The modified intermediate representation focuses on the new logic of the modified code, recording the added or adjusted business logic. During merging, the early-compiled intermediate representation of dependencies serves as the underlying framework. First, the unmodified intermediate representation is logically associated with the dependent IRs to ensure smooth call paths from unchanged modules to dependent libraries (e.g., unchanged interfaces calling dependent database connection methods). Then, the modified intermediate representation is integrated, and a verification mechanism eliminates logical conflicts (e.g., preventing parameter mismatches when modified and unchanged code call the same dependent method). Finally, a complete intermediate representation covering the entire chain of dependencies, unchanged logic, and modified logic is formed, conforming to the "closed-world assumption" of AOT compilation. Subsequently, based on the target hardware platform (such as Linux / x86_64, ARM architecture), the machine instruction conversion process is initiated. Each logical node in the complete intermediate representation (such as variable assignment, condition judgment, cross-module method call, and dependent interface call) is mapped one by one to the native machine instructions of the corresponding platform. For example, the "object serialization logic" in the dependent IR is converted into the CALL (function call) and MOV (data transfer) instructions of the x86 architecture. The "user login parameter verification logic" in the unchanged IR reuses the previously converted instruction results. The "dynamic discount calculation logic" in the changed IR generates new ADD (operation) and JMP (branch jump) instructions. At the same time, it is ensured that the execution order of all instructions is consistent with the logical flow of the complete intermediate representation. Finally, machine code that can be run directly on the target platform and contains the new logic of the modified code is generated.
[0043] Optionally, the method further includes: The dependencies are pre-compiled to generate intermediate representations of the dependencies, and these intermediate representations are independently cached through the base layer. The cache key of the intermediate representation of the dependencies is generated by the hash value of the triple coordinates of the dependencies.
[0044] Specifically, when preprocessing project dependencies (such as third-party libraries or frameworks like Spring Boot, Fastjson, and MyBatis), a pre-compilation operation is performed on the dependencies during the initial project build, dependency version updates, or dependency additions. This process is similar to processing project code, performing static analysis (including pointer analysis and escape analysis to verify the legality of the dependency's internal logic) and structural parsing (extracting class definitions, method signatures, interface implementation relationships, and the dependency's internal call chain) on the dependency's bytecode. This ultimately generates an intermediate representation of the dependency containing complete logical information that can be directly called by the project code. To avoid repeatedly compiling and generating intermediate representations for unchanged dependencies during subsequent project compilations, this intermediate representation is cached through a separate "base layer." This "base layer" is a dedicated cache layer for storing the compiled results of dependencies, completely isolated from the intermediate representation cache of the project code itself (such as the intermediate layer of the initial intermediate representation cache of the source code). This avoids confusion between different types of caches and facilitates separate management of the dependency cache (such as cleaning up expired dependency caches and updating specific version dependency caches). The logic for generating cache keys for dependency intermediate representations uses the dependency's triple coordinates (usually unique identifiers in package management tools, such as groupId (unique identifier for the project group), artifactId (unique identifier for the project artifact), and version (project version number) in Maven coordinates, or group (package scope), name (package name), and version (version number) in Gradle coordinates) as the core basis. It first obtains the complete triple coordinate information of the dependency, and then calculates a unique hash value using a unified hash algorithm (such as SHA-256) to serve as the cache key. This design ensures that the same version of the same dependency always corresponds to a unique cache key (avoiding duplicate caching), while different dependencies or different versions of the same dependency correspond to different cache keys (avoiding cache conflicts). During subsequent project compilation, only the hash value calculated using the dependency's triple coordinates is needed to quickly locate and retrieve the corresponding dependency intermediate representation from the base layer cache, eliminating the need for repeated compilation and significantly reducing overall compilation time.
[0045] like Figure 2 As shown, an embodiment of the present invention provides a machine code compilation optimization device 200, comprising: The comparison module 210 is used to obtain a modified intermediate representation and an unmodified intermediate representation based on the comparison results between the modified code and the source code of the obtained project. The modified intermediate representation is obtained by modifying the modified code in the modified code and the project's dependencies, and the unmodified intermediate representation is obtained by using an initial intermediate representation pre-generated from the source code. The generation module 220 is used to convert the modified intermediate representation, the unmodified intermediate representation, and the early-compiled intermediate representation of the dependency into machine code corresponding to the modified code through machine instructions, wherein the early-compiled intermediate representation is obtained through the early-compilation result of the dependency.
[0046] The machine code compilation optimization apparatus of this embodiment is used to implement the machine code compilation optimization method as described above. Its advantages over the prior art are the same as the advantages of the machine code compilation optimization method over the prior art, and will not be repeated here.
[0047] Optionally, the comparison module 210 is specifically used to: compare the modified code with the source code to obtain the changed code and the unchanged code in the modified code; Based on the changed code and the dependencies, an intermediate representation of the changes is generated through incremental static analysis. The unmodified intermediate representation in the initial intermediate representation is determined based on the matching result between the unmodified code and the source code, wherein the initial intermediate representation is obtained through the source code and the dependencies.
[0048] Optionally, the comparison module 210 is further configured to: determine the source code hash value based on the source code; Determine the hash value of the modified code based on the modified code; The results of matching the modified code hash value with the source code hash value determine the unchanged code and the modified code.
[0049] Optionally, the comparison module 210 is further configured to: determine the modified code whose modified code hash value matches the source code hash value as the unchanged code; The modified code whose hash value does not match the source code hash value is identified as the changed code.
[0050] Optionally, the comparison module 210 is further configured to: generate a corresponding change structure metadata file based on the change code and the dependencies through incremental static analysis; The intermediate representation of the change is generated by merging the changed structure metadata file and the changed code.
[0051] Optionally, the comparison module 210 is further configured to: the preprocessing module is configured to: obtain an initial structure metadata file through static analysis based on the dependencies and the source code; The initial structure metadata and the source code are merged to generate an initial intermediate representation corresponding to the source code, and the initial intermediate representation is cached, wherein the cache key of the initial intermediate representation is the hash value of the corresponding source code; The initial intermediate representation corresponding to the portion of the cache key that matches the hash value of the unchanged code is determined as the unchanged intermediate representation.
[0052] Optionally, the generation module 220 is further configured to: merge the modified intermediate representation, the unchanged intermediate representation, and the pre-compiled intermediate representation of the dependencies to obtain the complete intermediate representation corresponding to the modified code; The complete intermediate representation is converted into machine code using machine instructions.
[0053] Optionally, the machine code compilation optimization device 200 further includes a basic cache module, which is used to: pre-compile the dependency to generate an intermediate representation of the dependency, and cache the intermediate representation of the dependency independently through the base layer, wherein the cache key of the intermediate representation of the dependency is generated by the hash value of the triple coordinates of the dependency.
[0054] like Figure 3 As shown, an electronic device 300 provided in this embodiment of the invention includes a memory 310 and a processor 320; the memory 310 is used to store a computer program; the processor 320 is used to implement the machine code compilation optimization method as described above when the computer program is executed.
[0055] Alternatively, an electronic device 300 includes a memory 310 and a processor 320 coupled to the memory 310; the memory 310 is configured to store a computer program; and the processor 320 is configured to perform the following operations when the computer program is executed: Based on the comparison results between the modified code and the source code of the obtained project, intermediate representations with and without changes are obtained. The intermediate representation with changes is obtained by modifying the modified code in the modified code and the project's dependencies, while the intermediate representation without changes is obtained by using the initial intermediate representation pre-generated from the source code. The modified intermediate representation, the unchanged intermediate representation, and the early-compiled intermediate representation of the dependency are converted into machine code corresponding to the modified code through machine instructions, wherein the early-compiled intermediate representation is obtained from the early-compilation result of the dependency.
[0056] This invention provides a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, it implements the machine code compilation and optimization method described above.
[0057] Alternatively, a non-volatile computer-readable storage medium storing a computer program that, when executed by a processor, causes the processor to perform the following operations: Based on the comparison results between the modified code and the source code of the obtained project, intermediate representations with and without changes are obtained. The intermediate representation with changes is obtained by modifying the modified code in the modified code and the project's dependencies, while the intermediate representation without changes is obtained by using the initial intermediate representation pre-generated from the source code. The modified intermediate representation, the unchanged intermediate representation, and the early-compiled intermediate representation of the dependency are converted into machine code corresponding to the modified code through machine instructions, wherein the early-compiled intermediate representation is obtained from the early-compilation result of the dependency.
[0058] The present invention will now be described an electronic device 300 that can serve as a server or client of the present invention, which is an example of a hardware device that can be applied to various aspects of the present invention. Electronic device 300 is intended to represent various forms of digital electronic computer devices, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. Electronic device 300 can also represent various forms of mobile devices, such as personal digital processors, cellular phones, smartphones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the invention described and / or claimed herein.
[0059] Electronic device 300 includes a computing unit that can perform various appropriate actions and processes based on a computer program stored in read-only memory (ROM) or a computer program loaded from a storage unit into random access memory (RAM). The RAM may also store various programs and data required for device operation. The computing unit, ROM, and RAM are interconnected via a bus. Input / output (I / O) interfaces are also connected to the bus.
[0060] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. The storage medium can be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc. In this application, the units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of the embodiments of the present invention according to actual needs. Furthermore, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated units can be implemented in hardware or as software functional units.
[0061] While the present invention has been disclosed above, its scope of protection is not limited thereto. Those skilled in the art can make various changes and modifications without departing from the spirit and scope of the present invention, and all such changes and modifications will fall within the scope of protection of the present invention.
Claims
1. A machine code compilation optimization method, characterized in that, include: Based on the comparison results between the modified code and the source code of the obtained project, intermediate representations with and without changes are obtained. The intermediate representation with changes is obtained by comparing the modified code in the modified code with the project's dependencies, and the intermediate representation without changes is obtained by using the initial intermediate representation pre-generated from the source code. The modified intermediate representation, the unchanged intermediate representation, and the early-compiled intermediate representation of the dependency are converted into machine code corresponding to the modified code through machine instructions, wherein the early-compiled intermediate representation is obtained from the early-compilation result of the dependency.
2. The machine code compilation optimization method according to claim 1, characterized in that, The process of obtaining modified intermediate representations and unmodified intermediate representations based on the comparison results between the modified code and the source code of the obtained project includes: The modified code and the source code are compared to obtain the changed code and the unchanged code in the modified code; The intermediate representation of the change is generated through incremental static analysis based on the change code and the dependencies; The unmodified intermediate representation in the initial intermediate representation is determined based on the matching result between the unmodified code and the source code, wherein the initial intermediate representation is obtained through the source code and the dependencies.
3. The machine code compilation optimization method according to claim 2, characterized in that, The step of comparing the modified code with the source code to obtain the changed code and the unchanged code in the modified code includes: Determine the source code hash value based on the source code; Determine the hash value of the modified code based on the modified code; The unchanged code and the changed code are determined based on the matching result between the modified code hash value and the source code hash value.
4. The machine code compilation optimization method according to claim 3, characterized in that, The step of determining the unchanged code and the changed code based on the matching result of the modified code hash value and the source code hash value includes: The modified code whose hash value matches the source code hash value is identified as the unchanged code. The modified code whose hash value does not match the hash value of the source code is identified as the changed code.
5. The machine code compilation optimization method according to claim 2, characterized in that, The step of generating an intermediate representation of the change based on the changed code and the dependencies through incremental static analysis includes: Based on the changed code and the dependencies, a corresponding change structure metadata file is generated through incremental static analysis. The intermediate representation of the change is generated by merging the changed structure metadata file and the changed code.
6. The machine code compilation optimization method according to claim 2, characterized in that, The step of determining the unmodified intermediate representation in the initial intermediate representation based on the matching result between the unmodified code and the source code includes: Initial structure metadata is obtained through static analysis based on the dependencies and the source code; The initial structure metadata and the source code are merged to generate an initial intermediate representation corresponding to the source code, and the initial intermediate representation is cached, wherein the cache key of the initial intermediate representation is the hash value of the corresponding source code; The initial intermediate representation corresponding to the portion of the cache key that matches the hash value of the unchanged code is determined as the unchanged intermediate representation.
7. The machine code compilation optimization method according to claim 1, characterized in that, The step of converting the modified intermediate representation, the unchanged intermediate representation, and the early-compiled intermediate representation of the dependencies into machine code corresponding to the modified code through machine instructions includes: The modified intermediate representation, the unchanged intermediate representation, and the early-compiled intermediate representation of the dependencies are merged to obtain the complete intermediate representation corresponding to the modified code; The complete intermediate representation is converted into machine code using machine instructions.
8. The machine code compilation optimization method according to any one of claims 1 to 7, characterized in that, Also includes: The dependencies are pre-compiled to generate intermediate representations of the dependencies, and these intermediate representations are independently cached through the base layer. The cache key of the intermediate representation of the dependencies is generated by the hash value of the triple coordinates of the dependencies.
9. A machine code compilation and optimization apparatus, characterized in that, include: The comparison module is used to obtain a modified intermediate representation and an unmodified intermediate representation based on the comparison results between the modified code and the source code of the obtained project. The modified intermediate representation is obtained by modifying the modified code in the modified code and the project's dependencies, and the unmodified intermediate representation is obtained by using an initial intermediate representation pre-generated from the source code. A generation module is used to convert the modified intermediate representation, the unchanged intermediate representation, and the early-compiled intermediate representation of the dependencies into machine code corresponding to the modified code through machine instructions, wherein the early-compiled intermediate representation is obtained from the early-compilation result of the dependencies.
10. An electronic device, characterized in that, Including memory and processor; The memory is used to store computer programs; The processor is configured to implement the machine code compilation optimization method as described in any one of claims 1 to 8 when executing the computer program.