Binary code processing method, optimizer, electronic device, and storage medium

CN123012632APending Publication Date: 2026-10-30DAMO (SHANGHAI) TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510560497.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-29
Publication Date
2026-10-30

AI Technical Summary

Technical Problem

[0003]然而,相关技术中的优化方法,如采用基于贪心算法的热点代码集中策略时,当程序中存在多个独立的热点区域时,贪心算法倾向于将所有热点代码强制集中到一个区域内,忽略了不同热点代码之间的自然分离,导致程序的局部性优化不足

Benefits of technology

[0013]在本申请实施例中,首先获取二进制代码数据在执行过程中,二进制代码数据中多个代码段的执行频率;接着,根据这些执行频率,识别出多个目标代码段,这些目标代码段的执行频率高于其他代码段;接着,根据这些目标代码段在虚拟地址空间中的位置,对目标代码段进行聚类分析,得到多个聚类集合,不同的聚类集合中目标代码段的密集程度不同;最后,基于多个聚类集合,调整目标代码段在虚拟地址空间中的位置。容易注意到的是,本申请通过二进制代码数据在执行过程中各代码段的执行频率确定出多个目标代码段,也即多个热点代码;通过将多个热点代码聚类到多个独立的聚类集合,也即聚类到多个区域;并在虚拟地址空间中合理分布这些聚类集合,由于热点代码被合理地分散到不同的区域,可以避免热点之间的频繁切换而消耗额外的资源,减少了热点之间的数据访问冲突,可以有效增强代码的局部性,使得缓存和快表能够更高效地预取和存储热点区域的数据和指令,从而显著提高缓存和快表的命中率,提高了程序的执行性能,进而解决了相关技术中当二进制代码数据中存在多个热点代码时,会将热点代码集中在一个区域中,导致程序的局部性优化不足,从而削弱了程序性能的提升的技术问题。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123012632A_ABST
    Figure CN123012632A_ABST
Patent Text Reader

Abstract

The application discloses a binary code processing method, an optimizer, an electronic device and a storage medium. The method comprises the following steps: acquiring the execution frequencies of a plurality of code segments in binary code data during the execution of the binary code data; determining a plurality of target code segments in the plurality of code segments based on the execution frequencies of the plurality of code segments; clustering the plurality of target code segments based on the positions of the plurality of target code segments in a virtual address space to obtain a plurality of clustering sets, wherein the target code segments in different clustering sets are different in density; and adjusting the positions of the plurality of target code segments in the virtual address space based on the plurality of clustering sets. The application solves the technical problem that, in the related art, when there are a plurality of hot code in binary code data, the hot code is concentrated in one region, the local optimization of a program is insufficient, and the improvement of the performance of the program is weakened.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and more specifically, to a binary code processing method, optimizer, electronic device, and storage medium. Background Technology

[0002] In the field of computer technology, especially for high-performance computing and real-time systems, optimizing program execution efficiency is increasingly important. Program optimizers such as the Binary Optimization and Layout Tool (LLVMBOLT) analyze the runtime characteristics of a program and dynamically adjust the binary code, thereby improving program performance. Optimization methods can centrally lay out frequently executed code, i.e., hotspot code, to improve the utilization efficiency of caches and TLBs, reduce data loading latency, and thus speed up program execution.

[0003] However, optimization methods in related technologies, such as the hotspot code concentration strategy based on greedy algorithms, tend to forcibly concentrate all hotspot code into one region when multiple independent hotspot regions exist in the program. This ignores the natural separation between different hotspot codes, resulting in insufficient locality optimization. In summary, related technologies concentrate hotspot code into one region when multiple hotspot codes exist in binary code data, reducing program locality and thus weakening performance improvements.

[0004] There is currently no effective solution to the above problems. Summary of the Invention

[0005] This application provides a binary code processing method, optimizer, electronic device, and storage medium to at least solve the technical problem in the related art where, when there are multiple hot codes in binary code data, the hot codes are concentrated in one area, resulting in insufficient locality optimization of the program and thus weakening the improvement of program performance.

[0006] According to one aspect of the embodiments of this application, a code processing method is provided, comprising: obtaining the execution frequency of multiple code segments in binary code data during execution; determining multiple target code segments among the multiple code segments based on the execution frequency of the multiple code segments, wherein the execution frequency of the target code segments is greater than the execution frequency of other code segments, and the other code segments are any code segments in the binary code data other than the target code segments; clustering the multiple target code segments based on their positions in the virtual address space to obtain multiple cluster sets, wherein the density of target code segments in different cluster sets is different; and adjusting the positions of the multiple target code segments in the virtual address space based on the multiple cluster sets.

[0007] According to another aspect of the embodiments of this application, a binary code optimizer is also provided, comprising: an acquisition module, configured to acquire the execution frequency of multiple code segments in the binary code data during execution; a determination module, configured to determine multiple target code segments among the multiple code segments based on the execution frequency of the multiple code segments, wherein the execution frequency of the target code segments is greater than the execution frequency of other code segments, and the other code segments are any code segments in the binary code data other than the target code segments; a clustering module, configured to cluster the multiple target code segments based on their positions in the virtual address space to obtain multiple cluster sets, wherein the density of the target code segments in different cluster sets is different; and an adjustment module, configured to adjust the positions of the multiple target code segments in the virtual address space based on the multiple cluster sets.

[0008] According to another aspect of the embodiments of this application, an electronic device is also provided, including: a memory storing an executable program; and a processor for running the program, wherein the program executes the methods in various embodiments of this application when it runs.

[0009] According to another aspect of the embodiments of this application, a computer-readable storage medium is also provided, the computer-readable storage medium including a stored executable program, wherein the methods according to various embodiments of this application are implemented when the executable program is executed by a processor.

[0010] According to another aspect of the embodiments of this application, a computer program product is also provided, including computer instructions that, when executed by a processor, implement the methods in various embodiments of this application.

[0011] According to another aspect of the embodiments of this application, a computer program product is also provided, including a non-volatile computer-readable storage medium storing computer instructions that, when executed by a processor, implement the methods in various embodiments of this application.

[0012] According to another aspect of the embodiments of this application, a computer program is also provided, wherein computer instructions, when executed by a processor, implement the methods of the various embodiments of this application.

[0013] In this embodiment, the execution frequency of multiple code segments in the binary code data is first obtained during execution. Then, based on these execution frequencies, multiple target code segments are identified, whose execution frequencies are higher than those of other code segments. Next, based on the location of these target code segments in the virtual address space, cluster analysis is performed on the target code segments to obtain multiple cluster sets, with different densities of target code segments in different cluster sets. Finally, based on the multiple cluster sets, the location of the target code segments in the virtual address space is adjusted. It is noteworthy that this application identifies multiple target code segments, i.e. multiple hot codes, by analyzing the execution frequency of each code segment during the execution of binary code data. By clustering these hot codes into multiple independent cluster sets, i.e., into multiple regions, and rationally distributing these cluster sets in the virtual address space, the frequent switching between hot codes and the resulting resource consumption can be avoided. This also reduces data access conflicts between hot codes, effectively enhancing code locality. This allows caches and TLBs to more efficiently prefetch and store data and instructions in hot regions, significantly improving cache and TLB hit rates and program execution performance. Furthermore, this solves the technical problem in related technologies where, when multiple hot codes exist in binary code data, they are concentrated in one region, leading to insufficient locality optimization and weakening program performance improvement.

[0014] It is worth noting that the general description above and the detailed description that follow are merely for illustrative purposes and do not constitute a limitation on this application. Attached Figure Description

[0015] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:

[0016] Figure 1 This is a hardware structure block diagram of a RISC-V system for implementing a code processing method according to an embodiment of this application;

[0017] Figure 2 This is a schematic diagram of a RISC-V architecture on-chip system according to an embodiment of this application;

[0018] Figure 3 This is a flowchart of a code processing method according to an embodiment of this application;

[0019] Figure 4 This is a schematic diagram of a binary code optimizer according to an embodiment of this application;

[0020] Figure 5 This is a structural block diagram of an electronic device according to an embodiment of this application. Detailed Implementation

[0021] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of the present application.

[0022] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0023] First, some nouns or terms that appear in the description of the embodiments of this application shall be interpreted as follows:

[0024] Clustering, a machine learning technique, can divide objects in a dataset into different groups, such that objects within a group have high similarity and objects between groups have low similarity.

[0025] The Hierarchical Density-Based Spatial Clustering of Applications with Noise (HDBSCAN) algorithm is a density-based clustering algorithm that can automatically identify high-density and low-density regions and divide the dataset into multiple levels.

[0026] An optimizer is an algorithm used to adjust model parameters to improve model performance. In machine learning, optimizers help models find optimal parameter combinations during training, thereby improving the model's accuracy and generalization ability. Optimizers can include stochastic gradient descent (SGD), adaptive moment estimation (Adam), and root mean square propagation (RMSprop), among others.

[0027] Caching, a storage technology, stores frequently accessed data or computation results in memory for quick access when needed. Caching can improve system performance and responsiveness, reducing unnecessary disk accesses and computational overhead. In computer systems, caching can be used in web servers, database servers, and operating systems, among others.

[0028] A Translation Lookaside Buffer (TLB) is a type of cache used to store recently accessed virtual addresses. TLBs can be used for access between the processor and memory, reducing the time and overhead for the processor to read virtual addresses from memory. TLBs can be used in conjunction with virtual memory systems to improve system performance and responsiveness.

[0029] Hotspot code refers to code segments that are frequently accessed in a program. These segments are often time-consuming and resource-intensive; optimizing them can improve program performance and responsiveness. Therefore, optimizing hotspot code should be a key focus in program performance optimization.

[0030] According to an embodiment of this application, a code processing method is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.

[0031] The method embodiment provided in Embodiment 1 of this application can be executed in a mobile terminal, computer terminal, or similar computing device. The computing device can employ a multi-core processor architecture, which may be RISC (Reduced Instruction Set Computing), ARM (Advanced RISC Machine), x86, etc., but is not limited to these. The processor can integrate multiple processing cores, each with its own independent L1 cache, sharing a larger L2 cache, and a unified LLC (Last Level Cache). The multi-level cache adopts a set-associative architecture, where both the L2 cache and LLC are highly configurable to adapt to different types of applications and workloads.

[0032] For example, taking the RISC-V architecture as an example, Figure 1 A hardware block diagram of a RISC-V system for implementing a code processing method is shown. Figure 1 As shown, the RISC-V system can be divided into the following layers from bottom to top: Reduced Instruction Set Architecture 101 (including the basic instruction set 101-1 and extended instruction set 101-2), hardware layer 102 (including processor 102-1, peripheral hardware circuits 102-2, etc.), interface layer 103, operating system layer 104 (supporting multiple operating systems 104-1, 104-2, ..., 104-n, such as Linux, FreeSBD, RT-Tread, etc.), middleware and library layer 105 (including system library 105-1, API 105-2 and middleware services 105-3) and application layer 106 (including multiple user programs and services 106-1, 106-2, ..., 106-n). The RISC-V system also includes a toolchain 107 that runs from the underlying hardware to the application layer. This toolchain may include compilers and assemblers 107-1, linkers 107-2, debuggers 107-3, simulators and emulators 107-4, integrated development environments 107-5, hardware description language tools 107-6, performance analysis tools 107-7, and version control systems 107-8, etc.

[0033] The instruction set architecture 101 defines the basic operations and instruction sets supported by the processor 102-1, including the basic instruction set and the extended instruction set. The basic instruction set represents the basic integer instruction set, such as RV32I and RV64I, while the extended instruction set can be floating-point, atomic operations, compressed instructions, etc.

[0034] Interface layer 103 includes the specific design of the processor, such as pipeline design, cache structure, execution units, branch prediction, etc. This layer is the process of mapping abstract instructions to physical hardware.

[0035] The operating system layer 104 sits above the hardware, providing a hardware abstraction layer and management mechanisms, enabling applications to interact with the hardware through system calls. The operating system is responsible for managing processor resources, memory, device drivers, task scheduling, and more.

[0036] The middleware and library layer 105 provides a rich set of services and interfaces to help applications run more efficiently. For example, the standard library provides functions such as file operations and mathematical calculations, while middleware can provide complex services such as network communication and graphical user interfaces.

[0037] The application layer 106 utilizes the functions and services provided by the lower layer to implement specific application logic. These applications can be command-line tools, graphical interface applications, server-side services, etc.

[0038] Toolchain 107 is a key component connecting the underlying hardware to the upper-level software. The various tools in toolchain 107 play a role at different levels to support the entire process from hardware design to software development, ensuring the coherence and effectiveness of the entire system design.

[0039] It should be noted that the layered design of the RISC-V architecture allows for decoupling between different layers, enabling each layer to be developed and optimized independently.

[0040] In one alternative embodiment, Figure 2 The above demonstrates the use of Figure 1 The diagram shows a System-on-Chip (SOC) based on the RISC-V architecture. Figure 2 As shown, the SOC contains at least one RISC-V core 202 (only one is shown in the figure). The RISC-V core 202 is connected to peripheral devices via bus 204, including but not limited to ROM 206, RAM 208, timer 210, UART (Universal Asynchronous Receiver / Transmitter) 212, GPIO (General Purpose Input / Output) 214, SPI (Serial Peripheral Interface Bus) 216, etc.

[0041] Under the aforementioned operating environment, this application provides the following: Figure 3 The code processing method shown, Figure 3This is a flowchart of a code processing method according to an embodiment of this application. For example... Figure 3 As shown, the specific steps may include the following:

[0042] Step S302: Obtain the execution frequency of multiple code segments in the binary code data during the execution process.

[0043] The aforementioned binary code data can refer to the data consisting of instruction sequences in a compiled binary program. Binary code data can be executed directly by the processor. Binary code data can be data from different application scenarios, such as various server applications such as web servers, database servers, and big data processing applications; animation production or game development such as animation rendering and physics engines; mobile applications or embedded application systems; and fields such as computer vision, natural language processing, social network analysis, and image segmentation. The specific source of binary code data can be determined according to actual needs and is not limited here.

[0044] The aforementioned multiple code segments can refer to multiple code program segments obtained by dividing binary code data. This division can be done manually or automatically by a computer. Binary code data can be divided into multiple code segments according to the different functions performed by the program code in the binary code data. For example, it can be an executable unit in the binary code data, or a part that can be executed independently and is logically complete, such as a function, subroutine, loop body, or basic block. The requirements for dividing multiple code segments and the length of the division can be determined according to actual needs, and are not limited here.

[0045] The execution frequency mentioned above can refer to the number of times each code segment is executed or called by the processor per unit of time, which can measure the importance and activity of each code segment when program data is executed.

[0046] In one alternative embodiment, static analysis or dynamic sampling can be performed on binary code data to obtain the execution frequency of multiple code segments within the binary code data. During static analysis, compiler plugins or preprocessors can be used during the compilation phase to analyze the binary code data and identify different code segments. For example, the plugin mechanism provided by the compiler can be used to analyze and modify intermediate code during compilation. By analyzing the control flow graph, different basic blocks can be identified, thereby determining multiple code segments. During dynamic sampling, runtime analysis tools or performance monitoring modules can be used to sample the execution path of the binary code data during the runtime phase, thereby determining multiple code segments. Dynamic sampling can capture the actual execution path of the binary code data under different inputs and scenarios, thus obtaining multiple code segments. After obtaining each code segment, the execution frequency of each code segment can be calculated. This can be achieved by monitoring the number of calls or execution time of each code segment during program execution. Specifically, hardware performance counters can be used to monitor specific events, such as the number of instruction executions. By configuring hardware performance counters, the execution count of each code segment can be collected during runtime. Alternatively, dynamic analysis tools can be used to insert additional code during program execution to collect the execution frequency of code segments. These tools can provide detailed execution information, including the number of executions, execution time, and call stack, thus allowing the determination of the execution frequency. Software timers and counters can also be used, inserted at the beginning and end of each code segment to record its execution time and calculate the execution frequency.

[0047] In the above process, accurately obtaining the execution frequency of each code segment facilitates the subsequent identification of hot code and the implementation of clustering optimization methods. The obtained execution frequency of each code segment helps to identify hot code, that is, the code segment that is frequently executed during program execution. At the same time, the obtained execution frequency of each code segment provides a basis for subsequent clustering algorithms, which helps to determine which code segments should be clustered together and which code segments should be separated, thereby achieving a more reasonable and efficient layout in the virtual address space. In turn, reasonable layout optimizes program performance, reduces resource consumption, and improves overall operating efficiency.

[0048] Step S304: Based on the execution frequency of multiple code segments, determine multiple target code segments among the multiple code segments.

[0049] Among them, the execution frequency of the target code segment is greater than that of other code segments, which are any code segments in the binary code data other than the target code segment.

[0050] The aforementioned target code segments refer to code segments that are frequently called or executed during the execution of binary code data; these can be called hotspot code. These target code segments perform critical tasks or are the core parts of loops during binary code data execution and are therefore executed frequently by the processor. Identifying and optimizing the location of target code segments can improve the hit rate of cache memory (Cache) and translation lookaside buffer (TLB). The design principle of cache and TLB is based on the principle of locality, meaning that programs tend to repeatedly access data that has been recently accessed or is located in a nearby position. By placing hotspot code in a concentrated manner, cache misses and TLB misses can be reduced. Hotspot code is frequently accessed in a short period of time and has a longer residence time in the cache, thereby improving the access speed of data and instructions. By reducing the access latency of cache and TLB, the throughput and response time of the program can be improved. For applications requiring high interactivity, such as games and financial trading systems, or long-running background services, such as web servers and database servers, there is a significant performance improvement effect.

[0051] In one optional embodiment, the obtained execution frequency data of each code segment can be preprocessed, such as by cleaning the data to remove outliers. The preprocessed data can more accurately reflect the relative importance of the code segment in the execution of binary code data. Alternatively, the preprocessing step of the execution frequency of the multiple code segments can be omitted, and the data can be directly used to determine multiple target code segments among the multiple code segments; this is not limited here. When determining multiple target code segments, hotspot thresholds can be set. The selection of the hotspot threshold can depend on multiple factors, including but not limited to the characteristics and performance goals of the program. A specific hotspot threshold can be set, such as code segments whose execution frequency exceeds a certain value can be used as target code segments. A relative frequency threshold can also be used; for example, the top 1% to 10% of the most frequently executed code segments can be selected as target code segments, thus obtaining multiple target code segments. Flexible threshold settings allow optimization methods to adapt to programs of different sizes and characteristics. Furthermore, considering that binary code data can simultaneously contain multiple hot code segments with different execution frequencies, a multi-level hot code identification approach can be adopted. For example, a batch of code segments with high execution frequency can be identified first, followed by the next highest frequency segments, and so on. This hierarchical approach helps identify hotspots at different levels. In dynamically changing environments, such as cloud servers or internet applications, the program's execution mode changes over time. The identification of target code segments can not be limited to static thresholds but can have a dynamic adjustment mechanism. Specifically, hot code can be identified at multiple stages of program execution, and the threshold can be adjusted according to changes in execution frequency. This ensures that the optimization method can adapt to changes in the execution environment and continuously improve performance.

[0052] In the above process, identifying multiple target code segments facilitates subsequent targeted optimization of these segments, thereby reducing cache misses and TLB misses. Since target code segments constitute a significant portion of the execution of binary code data, optimizing these segments has a more substantial impact on overall performance.

[0053] Step S306: Based on the location of multiple target code segments in the virtual address space, cluster the multiple target code segments to obtain multiple cluster sets.

[0054] The density of the target code segments varies in different cluster sets.

[0055] The aforementioned virtual address space can refer to the private address space provided by the operating system or virtual memory management system for each process. Binary code data does not need to run directly in physical memory addresses, but can be mapped to physical memory through the virtual address mechanism. Virtual addresses can be quickly translated into physical addresses through the TLB, thereby reducing memory access latency. The layout of the virtual address space can directly affect the hit rate of the cache and TLB.

[0056] The aforementioned cluster sets can refer to sets formed by grouping target code segments according to their location in the virtual address space and their relationships with each other using a pre-selected algorithm or method. Clustering can group frequently executed target code segments according to their proximity in the virtual address space, thereby optimizing the layout of target code segments in memory, improving locality, and thus increasing the hit rate of caches and TLBs. The clustering proposed in this application allows target code segments not only to be concentrated but also to be distributed across multiple high-density regions in the virtual address space, thereby improving the locality of memory access and reducing the waiting time and memory bandwidth pressure caused by cross-block access.

[0057] In an optional embodiment, the identified target code segments can be grouped according to their distribution in the virtual address space and their execution correlations to form multiple cluster sets. Specifically, the virtual address information of each target code segment can be obtained, which can be obtained through reverse engineering during the compilation stage or runtime. The virtual address information can be used to determine the location of the code segment in the virtual address space. Next, the distance between each target code segment and other code segments can be calculated. This distance can be a physical address distance or a logical distance based on the execution context. The logical distance can be based on the call relationship between code segments, data dependency, or execution frequency correlation over time. Then, a clustering algorithm can be used to cluster the target code segments. The clustering algorithm can be HDBSCAN, etc. The clustering algorithm can also be determined according to actual needs, which is not limited here. HDBSCAN is a density-based clustering algorithm that determines the cluster boundary by calculating the density of each point and the core distance. In the application scenario of this application, the points in the clustering calculation can be target code segments, and the distance can be the relative position of the target code segments in the virtual address space. Furthermore, since clustering algorithms can dynamically determine the number and size of clusters, HDBSCAN does not require pre-specifying the number of clusters. This characteristic makes HDBSCAN more suitable for handling hotspot code clustering problems, as the distribution of hotspots may change at different stages of program execution. Through clustering algorithms, target code segments can be grouped into multiple different cluster sets. The code segments in each set will have high locality, meaning that the target code segments tend to be densely interconnected during execution. The execution correlation of target code segments between different sets is low, reflecting the relative independence of target code segments in the virtual address space.

[0058] In the above process, by clustering target code segments into multiple independent clusters, the locality of program execution can be significantly improved. The principle of locality dictates that programs tend to repeatedly access recently used data and instructions, or access physically adjacent data and instructions. By improving locality, cache misses and TLB misses can be reduced, thereby reducing processor access to main memory and improving program execution efficiency. The clustered code segments have a more rational layout in the virtual address space, which helps optimize the allocation of memory and cache resources. For example, the processor cache can better serve code segments that frequently interact within the clusters, and the TLB can more efficiently manage the virtual address translation of target code segments. By clustering hot code segments in the virtual address space, not only can the locality of code execution be improved, but resource allocation can also be optimized, program performance improved, and ultimately, a smoother and faster software experience provided to users.

[0059] Step S308: Based on multiple cluster sets, adjust the positions of multiple target code segments in the virtual address space.

[0060] In one optional embodiment, after determining multiple cluster sets, the virtual address space can be reorganized. Contiguous virtual address ranges can be allocated to each cluster set to improve the locality of memory access. Code segments within the same cluster set can be arranged within the same contiguous virtual address range, and the relative positions of different cluster sets in the virtual address space can be adjusted. For example, if a code segment in a certain cluster set has a high interaction frequency with code segments in other cluster sets, these cluster sets can be placed closer together in the address space to reduce access latency across cluster sets. After adjusting the virtual address of the target code segment, the target code segment can be recompiled and linked to ensure efficient execution under the new address layout.

[0061] In the above process, adjusting the location of the target code segment in the virtual address space can significantly improve program execution efficiency and reduce resource consumption. Placing frequently accessed target code segments in a more contiguous address range can reduce branch prediction error rates. The processor's branch predictor relies on code continuity to predict subsequent instruction flows; optimized layout of hot code segments helps reduce prediction errors and improve instruction pipeline efficiency. In multi-core processors and heterogeneous computing environments, optimized layout of target code segments can better utilize the advantages of multi-core and heterogeneous resources, improving parallel execution efficiency by reducing data exchange and cache coherency overhead between cores.

[0062] In this embodiment, the execution frequency of multiple code segments in the binary code data is first obtained during execution. Then, based on these execution frequencies, multiple target code segments are identified, whose execution frequencies are higher than those of other code segments. Next, based on the location of these target code segments in the virtual address space, cluster analysis is performed on the target code segments to obtain multiple cluster sets, with different densities of target code segments in different cluster sets. Finally, based on the multiple cluster sets, the location of the target code segments in the virtual address space is adjusted. It is noteworthy that this application identifies multiple target code segments, i.e. multiple hot codes, by analyzing the execution frequency of each code segment during the execution of binary code data. By clustering these hot codes into multiple independent cluster sets, i.e., into multiple regions, and rationally distributing these cluster sets in the virtual address space, the frequent switching between hot codes and the resulting resource consumption can be avoided. This also reduces data access conflicts between hot codes, effectively enhancing code locality. This allows caches and TLBs to more efficiently prefetch and store data and instructions in hot regions, significantly improving cache and TLB hit rates and program execution performance. Furthermore, this solves the technical problem in related technologies where, when multiple hot codes exist in binary code data, they are concentrated in one region, leading to insufficient locality optimization and weakening program performance improvement.

[0063] In the above embodiments of this application, multiple target code segments are clustered based on their positional relationships in the virtual address space to obtain multiple cluster sets. This includes: determining the reachability distance between different target code segments based on their positions in the virtual address space, wherein the reachability distance is used to characterize the density of other target code segments around any target code segment, and the positional relationships between different target code segments in the virtual address space; constructing a directed code graph based on the reachability distance between different target code segments, wherein the first node in the directed code graph is used to characterize a target code segment, and the first edge in the directed code graph is used to characterize the reachability distance between the target code segments corresponding to the two nodes connected by the first edge; and splitting the directed code graph to obtain multiple cluster sets.

[0064] In one optional embodiment, the reachability distance between different target code segments can be determined based on their positions in the virtual address space. The reachability distance can be calculated based on parameters such as the actual distance between the target code segments, which can be the Euclidean distance, Manhattan distance, or a metric based on execution frequency and contextual relationships between two points in the virtual address space. Next, a directed graph of code can be constructed. In constructing the directed graph, each target code segment can be considered a node, containing its execution frequency information and storing its virtual address location. This allows the graph to visually reflect the spatial relationships between code segments. Edges in the directed graph can be used to connect any two target code segments, and the edge weight can be represented as the reachability distance between the two target code segments. The calculation of the reachability distance considers the execution frequency, location, and spatial density between the target code segments, which helps to capture the dynamic and static relationships between target code segments in the graph. By traversing each target code segment, calculating the reachability distance between any two code segments, and establishing connections, a directed graph of code can be generated. Finally, the directed graph of code can be split. For example, a minimum spanning tree (MST) can be constructed to split the directed graph into multiple cluster sets. Other methods can also be used to split the directed graph, such as depth-first search or breadth-first search, to traverse the graph and achieve splitting. By setting a starting point and performing depth-first or breadth-first traversal, code blocks closely connected to the starting point can be searched, forming cluster sets. Changing the starting point and traversing the graph can yield different sets, resulting in multiple cluster sets. The method for splitting the directed graph to obtain multiple cluster sets can be determined according to actual needs and is not limited here. The MST can be an acyclic subgraph containing all nodes of the directed graph, and the total weight of the edges can be minimized. In the application scenario of this application, using the MST helps to identify shorter paths and lower reachability distances between target code segments, providing a foundation for subsequent clustering and segmentation.

[0065] In the above process, based on the location of different target code segments in the virtual address space and the reachability distance between them, and through the construction and segmentation of the directed code graph, efficient clustering of hot code segments can be achieved, thereby optimizing the layout of different target code segments in the virtual address space. By constructing a directed code graph and clustering according to reachability distance, target code segments with high execution frequency and adjacent or closely interacting positions in the virtual address space can be grouped together to form multiple cluster sets. This approach can significantly improve the locality of target code segments during execution, reduce access across cluster sets, thereby reducing the number of cache misses and TLB misses, improving program running efficiency. The layout of clustered code segments in the virtual address space is more reasonable, which helps to optimize the resource allocation of cache hierarchy and memory paging mechanism.

[0066] In the above embodiments of this application, determining the reachability distance between different target code segments based on their positions in the virtual address space includes: determining the distance between different target code segments based on their address differences in the virtual address space; determining the core distance of any target code segment based on the distance between any target code segment and other target code segments, wherein other target code segments are any target code segments other than any target code segment, and the core distance is used to characterize the density of target code segments around any target code segment; and determining the reachability distance between different target code segments based on the distance between different target code segments and the core distance between different target code segments.

[0067] In one alternative embodiment, the distance between different target code segments can be determined based on the address difference between them in the virtual address space. Specifically, the compiler can record the start and end virtual addresses of each target code segment during the compilation phase, or collect this information in real time at runtime through performance monitoring tools. Then, the virtual address difference between two target code segments can be calculated to determine the distance between them. The distance between different target code segments can reflect the physical proximity of them in virtual memory, that is, it can reflect the performance of the cache and the fast table when accessing these target code segments. For example, code segments with adjacent addresses are more likely to be loaded into the cache together, thereby improving the efficiency of subsequent accesses.

[0068] Next, based on the distances between any target code segment and other target code segments, the core distance of any target code segment can be determined. The core distance helps identify which regions have highly correlated code segments. For each target code segment, a certain number of nearest neighbor code segments can be found; these segments are located relatively close to the target code segment in the virtual address space. Among the identified nearest neighbor code segments, the distances of these segments to the current target code segment can be determined, and the core distance of the target code segment can be determined based on these distances. For example, the average or median of these distances can be used as the core distance; there are no limitations here. The size of the core distance reflects the density of hotspots around the target code segment; a smaller core distance indicates a denser density of surrounding code segments.

[0069] Finally, the reachability distance between different target code segments can be determined based on the distance between them and their core distance. The reachability distance combines the direct distance between two target code segments and their respective core distances. The physical meaning of the reachability distance can be the difference in the address of the first instruction in each of the two target code segments. This metric can more accurately reflect the mutual reachability and potential execution dependencies of two code segments in the virtual address space. Specifically, given two target code segments, the reachability distance between them can be calculated based on their respective core distances and the distance between them. For example, the reachability distance can consider the maximum value of the following three cases: the core distance of the first code segment, the core distance of the second code segment, and the direct distance between the two code segments. This method ensures that even if two code segments are far apart in the virtual address space, if they are each located in a dense region, the reachability distance between them will not be too high. This reflects that the two target code segments have similar execution characteristics and are suitable for clustering together.

[0070] In the above process, clustering hot code segments based on their relative position and density in the virtual address space can significantly improve the locality of memory and cache access. That is, when a code segment is loaded into the cache, other closely related code segments can also be loaded simultaneously, thereby reducing unnecessary cache misses and TLB misses and improving system throughput. This application can automatically select cluster centers and boundaries based on the density of data points, and its dynamic nature allows code optimization to adapt to different program execution modes and hardware configurations.

[0071] In the above embodiments of this application, determining the core distance of any target code segment based on the distance between any target code segment and other target code segments includes: determining at least one adjacent target code segment adjacent to any target code segment based on the distance between any target code segment and other target code segments; when the number of adjacent target code segments is greater than or equal to a preset number, determining a preset number of target code segments from the adjacent target code segments based on the distance between any target code segment and any adjacent target code segment, wherein the distance between the preset number of target code segments and any target code segment is less than the distance between other adjacent target code segments and any target code segment, and the other adjacent target code segments are any target code segments other than the preset number of target code segments among the adjacent target code segments; and determining the core distance of any target code segment based on the distance between any target code segment and the preset number of target code segments.

[0072] The aforementioned preset quantity refers to the pre-defined number used to define whether a region is dense enough to be considered a cluster, which can be called minPts. For example, it can be specified as 2. The preset quantity minPts can be dynamically adjusted to adapt to different program execution modes and performance requirements. In some scenarios, a higher density of hotspot regions is needed to reduce unnecessary cache accesses, in which case the preset quantity can be set smaller; while in other scenarios, more attention is paid to the extensive coverage of hotspot regions to improve resource utilization, in which case the preset quantity can be set larger. This flexibility makes the optimization strategy more adaptable to various execution environments, and there is no limitation on the value of the preset quantity here.

[0073] In one optional embodiment, at least one adjacent target code segment can be determined based on the distance between any target code segment and other target code segments. Specifically, the distance between any target code segment and all other target code segments in the virtual address space can be calculated. This can be done through mathematical calculations based on the start and end virtual addresses of the code segments, and a set of code segments that are close to the current target code segment can be determined. By searching for a group of code segments with small distances, the density around hotspot code segments can be initially estimated. Then, if the number of adjacent target code segments is greater than or equal to a preset number, a preset number of target code segments can be determined from the adjacent target code segments based on the distance between any target code segment and any adjacent target code segment. After determining all code segments adjacent to the current target code segment, the preset number of code segments with small distances to the current code segment can be filtered out to ensure that the focus is on closely connected code segments that belong to the same hotspot region. Finally, the core distance of any target code segment can be determined based on the distance between any target code segment and a preset number of target code segments. The core distance can be the average or median distance between a target code segment and a preset number of other code segments, reflecting the lower density threshold of the area surrounding the target code segment. The calculation of the core distance provides an important basis for subsequent clustering, helping to determine which code segments can be considered core code segments of the same dense region.

[0074] The above process enables more accurate identification of the regions surrounding hot code segments, thus avoiding misidentifying non-hot code as hot or dispersing hot code across multiple unrelated regions. Simultaneously, determining the core distance of code segments helps determine the execution correlation between target code segments, thereby optimizing the layout in the virtual address space and improving locality. Locality optimization can significantly reduce cache and TLB miss rates, improve data and instruction fetching speed, and ultimately enhance program execution efficiency.

[0075] In the above embodiments of this application, determining the core distance of any target code segment based on the distance between any target code segment and a preset number of target code segments includes: obtaining the average value of the distance between any target code segment and the preset number of target code segments to obtain the core distance of any target code segment.

[0076] In an optional embodiment, for any target code segment, the distance between the target code segment and other target code segments in the virtual address space can be calculated. This distance can be understood as the difference in virtual address between two target code segments, reflecting their physical proximity. Since the virtual address space can be considered a one-dimensional linear space, calculating the distance between two target code segments can refer to calculating the absolute value of the difference in their virtual addresses. Next, a preset number of target code segments can be selected, these segments having the shortest distance to the current target code segment. The preset number can be used to define a density threshold for hotspot regions; the selected preset number of code segments can represent a relatively dense set of code segments surrounding the current code segment. Then, the average distance between the current target code segment and this preset number of neighboring target code segments can be calculated. This average distance reflects the average density of the area surrounding the target code segment, i.e., the average proximity of the target code segment to the surrounding hotspot code segments. Finally, the calculated average distance can be used as the core distance of the current target code segment. The core distance quantifies the density of the area surrounding hotspot code segments; a smaller core distance value reflects a denser environment around the target code segment, indicating a higher likelihood of forming a hotspot region.

[0077] In the above process, by calculating the average distance between a code segment and its surrounding code segments, hotspot regions can be identified more accurately. This approach considers not only the execution frequency of the target code segment but also its physical location within the virtual address space, thus enabling a more comprehensive assessment of the segment's importance and its correlation with other code segments. Calculating core distance helps optimize the layout of hotspot code segments in the virtual address space, improving locality. This facilitates placing frequently interacting code segments on contiguous or nearby virtual addresses, reducing cache and TLB miss rates and improving instruction fetch speed and execution efficiency.

[0078] In the above embodiments of this application, the method further includes: when the number of adjacent target code segments is less than a preset number, determining the core distance of any target code segment as a preset distance.

[0079] In one optional embodiment, for each target code segment, other code segments directly adjacent to each target code segment can be identified. In the virtual address space, adjacent can refer to code segments with consecutive physical addresses or close proximity. By traversing all other code segments and calculating the distance to the current target code segment, the set of adjacent code segments of the current code segment can be determined. Then, it can be determined whether the number of code segments in the set of adjacent code segments reaches or exceeds a preset number, as a basis for determining whether a target code segment belongs to a high-density region. If the number of adjacent code segments is less than the preset number, it can be determined that the environment around the current target code segment is not dense enough and does not constitute a hotspot region. In this case, the core distance of the current target code segment can be set to a preset large value, which can be called infinite or near infinite, indicating that there are not enough hotspot code segments around the target code segment to form a cluster.

[0080] In the above process, considering that in code optimization scenarios, some code segments may not have enough surrounding code segments to form hotspot regions due to low execution frequency or special physical layout, setting the core distance of these code segments to a preset distance essentially marks them as noise or outliers, thus avoiding their incorrect inclusion in the cluster of hotspot code. This approach improves the accuracy of hotspot identification and prevents erroneous optimization of non-hotspot code. By setting a preset distance, robustness can be maintained when handling sparse or non-hotspot code segments, that is, maintaining stability and the ability to handle abnormal situations, ensuring good performance not only in dense hotspot code regions but also properly handling cold spots or abnormal regions in the code. When a code segment is marked with a core distance equal to the preset distance, that code segment will not participate in subsequent clustering operations, thereby simplifying the algorithm's calculation process and improving clustering efficiency.

[0081] In the above embodiments of this application, the interoperability distance between different target code segments is determined based on the distance between different target code segments and the core distance between different target code segments, including: obtaining the maximum distance among the distance between different target code segments and the core distance between different target code segments, to obtain the interoperability distance between different target code segments.

[0082] In one alternative embodiment, the distance between any two target code segments can be determined. In the virtual address space, this distance can be the difference in virtual addresses between the two target code segments. This distance can represent the physical proximity of the two target code segments, reflecting the interoperability probability of the two target code segments during execution. Next, the core distance of each target code segment can be determined. The core distance can be calculated based on a preset number of neighboring code segments and can reflect the minimum density of the area surrounding the code segment, i.e., the farthest distance among the preset number of neighboring code segments around the code segment. A low core distance can indicate that the code segment is in a high-density area, while a high core distance can indicate that it is in a low-density area or is considered noise. Finally, a reachability distance can be calculated. For example, let p and q represent any two target code segments. For any two target code segments p and q, the reachability distance can be determined based on the direct distance between the two target code segments and the core distance of each target code segment. Specifically, the reachability distance can be the maximum of the following three values: the distance between target code segment p and q, the core distance of target code segment p, and the core distance of target code segment q. This calculation method ensures that even if two code segments are far apart, they can still be clustered together if they are located in highly dense regions, reflecting the potential execution correlation between the two target code segments.

[0083] The method for calculating the reachability distance between two target code segments p and q described above can be specifically expressed as follows:

[0084] mutual_reachability_distance(p, q)=max(core_distance(p), core_distance(q), distant(p, q));

[0085] Among them, mutual_reachability_distance(p, q) can represent the reachability distance between target code segments p and q, core_distance(p) can represent the core distance of target code segment p, core_distance(q) can represent the core distance of target code segment q, and distance(p, q) can represent the distance between target code segments p and q.

[0086] In the above process, the introduction of reachability distance allows two target code segments, even if they are far apart in virtual address space, to be considered reachable if their surrounding environments are dense. This enables them to be clustered into the same hotspot region, providing a more comprehensive perspective for determining the cluster set and ensuring the consistency of the cluster set's coherence and functionality. The use of reachability distance increases the stability of clustering. Based on the density of the code segment's surrounding environment rather than simply relying on direct distance, the clustering results are less susceptible to changes in the location of individual target code segments, especially in dynamic execution environments, helping to build a more stable hotspot code layout.

[0087] In the above embodiments of this application, splitting the directed graph of code to obtain multiple cluster sets includes: splitting the directed graph of code to obtain multiple initial sets; determining the stability index of the initial sets based on the connectivity within the initial sets and the number of edges between different initial sets; and filtering the initial sets based on the stability index to obtain cluster sets.

[0088] In one alternative embodiment, a directed graph can be constructed based on the calling relationships between target code segments. Nodes in the directed graph represent target code segments, and edges represent the relationship of one target code segment calling another, with the direction of the edges pointing from the caller to the callee. Next, high-density regions in the graph can be identified based on the reachability distance between target code segments, and these are used as initial sets. These initial sets may contain one or more code segments, reflecting potential hotspot regions. Then, a stability index for the initial sets can be determined based on the connectivity within each initial set and the number of edges between different initial sets. Specifically, the connectivity within each initial set can be evaluated, i.e., the tightness of the calling relationships between code segments within the set. A set with high internal connectivity indicates that target code segments frequently call each other, forming a functionally related unit. Considering the connectivity between different initial sets, the number of edges between different initial sets can be evaluated. Fewer cross-set edges indicate good separation between hotspot regions, contributing to the formation of independent and functionally defined hotspot code clusters. Finally, a stability index for each initial set can be calculated. High stability indicates that the set, as a whole, is internally compact and externally isolated, making it a good candidate for hotspot code. Finally, the initial set can be filtered based on stability metrics to obtain clustered sets. Specifically, a stability threshold can be defined as the filtering standard. Initial sets below this threshold can be considered unstable or non-hotspot regions and will not be included in the final clustering result. Unstable initial sets can be removed based on stability metrics. Furthermore, for sets with high stability but ambiguous boundaries, the possibility of merging these sets can be considered to form larger and more complete hotspot code clusters.

[0089] In the above process, segmentation and connectivity evaluation help to accurately identify which code segments constitute functionally relevant hotspot regions, thus improving the accuracy of hotspot code identification. The introduction of stability metrics ensures the quality of clustering results, guaranteeing that code segment sets with good internal connectivity and high external isolation are retained. This helps to build more reliable and stable hotspot code clusters, avoiding performance fluctuations caused by unstable clusters.

[0090] In the above embodiments of this application, the directed graph of code is split to obtain multiple initial sets, including: extracting a minimum spanning tree from the directed graph of code, wherein the second node in the minimum spanning tree is used to represent the target code segment, and the second edge in the minimum spanning tree is used to represent the shortest path between the target code segments corresponding to the two nodes connected by the second edge; sorting the edges in the minimum spanning tree according to the weight of the edges in the minimum spanning tree to obtain a sorted edge sequence; and deleting the edges in the sorted edge sequence in turn to obtain multiple initial sets.

[0091] In one alternative embodiment, a minimum spanning tree can be extracted from the directed code graph. The minimum spanning tree can be a subset of the directed code graph, containing all nodes and having the minimum total edge weight, ensuring the lowest cost of connection paths between code segments. In the minimum spanning tree, each node corresponds to a code segment, representing its presence in the program. Edges with higher weights in the minimum spanning tree indicate higher connection costs between two code segments, reflecting that these two code segments are far apart in the virtual address space or rarely call each other during execution. Next, edges can be sorted and deleted according to their weights. Specifically, the edges in the minimum spanning tree can be sorted in ascending order based on their weights, forming a sorted edge sequence. This ensures that analysis starts with the edge with the lowest connection cost and gradually increases the cost upwards. Starting from the sorted edge sequence, edges can be deleted sequentially. Each deleted edge splits the minimum spanning tree into two or more subgraphs. This process can continue until all edges are deleted or a specific stability metric is met. After each split, the resulting subgraph can represent potential hotspot code segment clusters.

[0092] In the above process, by extracting the minimum spanning tree from the directed graph of the code and analyzing the weight of its edges, strong connections between code segments can be found, thereby more accurately identifying hot code segments and clustering them. This avoids incorrectly classifying code segments that are far apart or called infrequently into the same hotspot, improving the accuracy and effectiveness of clustering.

[0093] In the above embodiments of this application, the positions of multiple target code segments in the virtual address space are adjusted based on multiple cluster sets, including: allocating the target code segments to target cluster sets among the multiple cluster sets; adjusting the positions of the target code segments in the target cluster sets in the virtual address space to target regions, wherein the positions of the target code segments in different cluster sets in the virtual address space are adjusted to different regions.

[0094] In one optional embodiment, target code segments can be clustered and assigned. Code segments are divided into different cluster sets based on their execution frequency and interrelationships. Each cluster set can represent a hotspot region where code segments may frequently interact during execution. Assigning each target code segment to its respective cluster set can be based on the algorithm's output, ensuring that code segments are correctly classified into the corresponding hotspot regions. Next, the virtual address space location of the code segments can be adjusted. Specifically, for each cluster set, a target region can be defined, which has a contiguous address layout in the virtual address space to improve the locality of hotspot code segments. Target code segments belonging to the same cluster set can be relocated to their corresponding target regions in the virtual address space. This can be achieved through code segment relocation, i.e., changing the physical location of the code segment in memory to bring it closer to frequently accessed hotspot regions. Target code segments in different cluster sets are respectively adjusted to different virtual address space regions, and each hotspot region can have its own independent memory layout, reducing interference between hotspot regions and improving the effect of locality optimization.

[0095] In the above process, placing the target code segments in a contiguous virtual address space can significantly reduce cache misses and TLB misses. When the program executes hot code, frequently accessed instructions and data will reside more in the cache and TLB, reducing the number of reads from main memory and improving data access speed. By placing functionally related code segments in contiguous address regions, the locality of hot code segments is improved. During execution, the processor can more efficiently utilize the cache and memory hierarchy when accessing instructions and data, reducing access latency and improving program performance.

[0096] The technical solution proposed in this application is described below with reference to an optional embodiment. This application proposes an LLVM BOLT hotspot code clustering optimization method based on the HDBSCAN algorithm. The HDBSCAN algorithm can be a density-based clustering algorithm that determines the cluster to which each point belongs by calculating the density around it. Using the HDBSCAN algorithm, it is not necessary to specify the number of clusters in advance; instead, the number and size of clusters can be continuously adjusted iteratively until convergence is achieved. The technical solution proposed in this application, by employing the HDBSCAN algorithm in LLVM BOLT, can cluster hotspot code into multiple regions, thereby improving the locality of the program and thus enhancing program performance.

[0097] The specific implementation process of the HDBSCAN algorithm used in this application can be as follows: First, the core distance of the hotspot code is calculated. For each hotspot code p, p∈G, the distance between the nearest preset number (minPts) of points to p can be found, which can be denoted as core_distance(p). The preset number (minPts) can be an adjustable parameter, such as 2, etc., and is not limited here. The core distance can be the average of multiple core_distance(p), etc.; if the number of neighboring points of p is less than the preset number (minPts), then core_distance(p)=∞.

[0098] Next, the reachability distance between hotspot codes can be calculated. For each pair of hotspot codes p, q∈G, the following calculation can be performed:

[0099] mutual_reachability_distance(p, q)=max(core_distance(p), core_distance(q), distant(p, q));

[0100] Among them, mutual_reachability_distance(p, q) can represent the reachability distance between hotspot codes p and q, core_distance(p) can represent the core distance of hotspot code p, core_distance(q) can represent the core distance of hotspot code q, and distance(p, q) can represent the distance between hotspot codes p and q.

[0101] Next, a minimum spanning tree (MST) can be constructed. Specifically, the reachability distances determined above can be used as edge weights to construct a complete graph G'. Then, the Kruskal or Prim algorithm can be used to extract the MST from G'. Edges in the MST represent the shortest paths between two points. Next, a cluster hierarchy can be constructed. Specifically, the MST edge weights can be sorted in ascending order, and the edges of the MST can be traversed. Specifically, the current edge can be deleted, splitting the graph into multiple connected components, each of which can form a cluster. The splitting process can be recorded to form a hierarchical cluster structure. Better clustering can be selected based on stability, such as cluster persistence, removing unstable clusters, such as noisy points. The aforementioned edge deletion increases the number of connected components. Different connected components can represent different hotspot codes; therefore, the edge deletion process can be considered the clustering process. Before the clustering algorithm begins, there is a connected graph, so there is only one connected component. After deletion, three connected components are generated, which are the three hotspot codes, thus completing the clustering. The stability mentioned above can be determined in the following ways, for example, by using some algorithms that evaluate the effect of clustering algorithms, and by calculating the connectivity within clusters and the number of edges between clusters to evaluate stability.

[0102] In addition to using the HDBSCAN algorithm, other clustering algorithms such as K-means can also be considered in the above process of this application. In this case, the preset number (minPts) can be manually specified.

[0103] This application employs the HDBSCAN algorithm to segment hot code segments into multiple regions, improving cache and TLB hit rates, enhancing program locality, and improving code execution performance. This application abstracts hot code segments as a directed graph clustering problem and uses the HDBSCAN algorithm to cluster code segments across multiple hot regions. The HDBSCAN algorithm can dynamically adjust the preset quantity (minPts) parameter to ensure that hot code segments are concentrated in adjacent regions, improving program performance. The efficient execution of the HDBSCAN algorithm is particularly suitable for large-scale data point sets, accelerating the optimization process. The HDBSCAN algorithm also has broad application prospects, such as social network analysis and image segmentation.

[0104] According to another aspect of the present invention, a binary code optimizer is also provided. The optimizer can execute the binary code processing method of the above embodiments. The specific implementation method and preferred application scenarios are the same as those of the above embodiments, and will not be described in detail here.

[0105] Figure 4 This is a schematic diagram of a binary code processing method according to an embodiment of this application, such as... Figure 4As shown, the device includes the following: an acquisition module 402, a determination module 404, a clustering module 406, and an adjustment module 408.

[0106] The system includes: an acquisition module 402, used to acquire the execution frequency of multiple code segments in the binary code data during execution; a determination module 404, used to determine multiple target code segments among the multiple code segments based on their execution frequencies, wherein the execution frequency of the target code segments is greater than that of other code segments, which are any code segments in the binary code data other than the target code segments; a clustering module 406, used to cluster the multiple target code segments based on their positions in the virtual address space, resulting in multiple cluster sets, wherein the density of the target code segments in different cluster sets is different; and an adjustment module 408, used to adjust the positions of the multiple target code segments in the virtual address space based on the multiple cluster sets.

[0107] The clustering module is further used to determine the reachability distance between different target code segments based on their positions in the virtual address space. This reachability distance characterizes the density of other target code segments around any given target code segment, as well as the positional relationship between different target code segments in the virtual address space. Based on the reachability distance between different target code segments, a directed code graph is constructed. The first node in the directed code graph represents a target code segment, and the first edge in the directed code graph represents the reachability distance between the target code segments corresponding to the two nodes connected by the first edge. The directed code graph is then split to obtain multiple cluster sets.

[0108] The clustering module is also used to determine the distance between different target code segments based on the address difference between different target code segments in the virtual address space; to determine the core distance of any target code segment based on the distance between any target code segment and other target code segments, where other target code segments are any target code segments other than the target code segment itself, and the core distance is used to characterize the density of target code segments around any target code segment; and to determine the reachability distance between different target code segments based on the distance between different target code segments and the core distance between different target code segments.

[0109] The clustering module is further configured to: determine at least one adjacent target code segment based on the distance between any target code segment and other target code segments; when the number of adjacent target code segments is greater than or equal to a preset number, determine a preset number of target code segments from the adjacent target code segments based on the distance between any target code segment and any adjacent target code segment, wherein the distance between the preset number of target code segments and any target code segment is less than the distance between other adjacent target code segments and any target code segment, and the other adjacent target code segments are any target code segments other than the preset number of target code segments among the adjacent target code segments; and determine the core distance of any target code segment based on the distance between any target code segment and the preset number of target code segments.

[0110] The clustering module is also used to obtain the average distance between any target code segment and a preset number of target code segments, thus obtaining the core distance of any target code segment.

[0111] The clustering module is also used to determine the core distance of any target code segment as the preset distance when the number of adjacent target code segments is less than the preset number.

[0112] The clustering module is also used to obtain the distance between different target code segments and the maximum distance among the core distances of different target code segments, so as to obtain the interoperability distance between different target code segments.

[0113] The clustering module is also used to split the directed graph of the code to obtain multiple initial sets; determine the stability index of the initial sets based on the connectivity within the initial sets and the number of edges between different initial sets; and filter the initial sets based on the stability index to obtain clustered sets.

[0114] The clustering module is also used to extract the minimum spanning tree from the directed code graph. The second node in the minimum spanning tree is used to represent the target code segment, and the second edge in the minimum spanning tree is used to represent the shortest path between the target code segments corresponding to the two nodes connected by the second edge. The edges in the minimum spanning tree are sorted according to their weights to obtain a sorted edge sequence. The edges in the sorted edge sequence are then deleted sequentially to obtain multiple initial sets.

[0115] The adjustment module is also used to allocate the target code segment to the target cluster set in multiple cluster sets; and to adjust the position of the target code segment in the target cluster set in the virtual address space to the target region, wherein the target code segment in different cluster sets is adjusted to different regions in the virtual address space.

[0116] Optionally, Figure 5 This is a structural block diagram of an electronic device according to an embodiment of this application. Figure 5 As shown, the electronic device A may include one or more (only one is shown in the figure) processors 102, memory 104 and peripheral interfaces 106, wherein the processors 102, memory 104 and peripheral interfaces 106 are interconnected via a bus 108.

[0117] The memory can be used to store software programs and modules, such as the program instructions / modules corresponding to the methods and apparatus in the embodiments of this application. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory, thereby implementing the methods in the above embodiments. The memory may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include memory remotely located relative to the processor, and these remote memories can be connected to terminal A via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.

[0118] The processor can invoke information and applications stored in the memory via a transmission device to execute the steps in each embodiment.

[0119] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, use and processing of the relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and corresponding operation portals are provided for users to choose to authorize or refuse.

[0120] It should be noted that, for the sake of simplicity, the foregoing method embodiments are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, as some steps may be performed in other orders or simultaneously according to this application. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions and modules involved are not necessarily essential to this application.

[0121] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, they can also be implemented by hardware. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause the processing unit to execute the methods in the various embodiments of this application.

[0122] Embodiments of this application also provide a computer-readable storage medium. Optionally, in this embodiment, the computer-readable storage medium can be used to store program code executed by the method provided in the above embodiments.

[0123] Optionally, in this embodiment, the storage medium can be located in any one of the electronic devices in the group of electronic devices.

[0124] Optionally, in this embodiment, the computer-readable storage medium is configured to store program code for executing the following: During the execution of binary code data, the execution frequency of multiple code segments in the binary code data is obtained; based on the execution frequency of the multiple code segments, multiple target code segments are determined, wherein the execution frequency of the target code segments is greater than the execution frequency of other code segments, and the other code segments are any code segment in the binary code data other than the target code segments; based on the positions of the multiple target code segments in the virtual address space, the multiple target code segments are clustered to obtain multiple cluster sets, wherein the density of target code segments in different cluster sets is different; based on the multiple cluster sets, the positions of the multiple target code segments in the virtual address space are adjusted; and the methods in the various embodiments of this application are executed.

[0125] Embodiments of this application also provide a computer program product. Optionally, in this embodiment, the computer program product may include computer instructions that, when executed by a processor, implement the methods provided in the embodiments described above.

[0126] The aforementioned computer program products can refer to software instructions that have been written, tested, and released, and can run on computers or other devices. Computer program products can include application programs, operating systems, utility software, etc., used to achieve specific functions or solve specific problems.

[0127] Embodiments of this application also provide a computer program product. Optionally, the computer program product may include a non-volatile computer-readable storage medium, which may be used to store computer instructions that, when executed by a processor, implement the methods provided in the embodiments described above.

[0128] The aforementioned non-volatile computer-readable storage medium can refer to a medium for storing data. Non-volatile computer-readable storage media can retain data without loss when power is off and can be used to store long-term data, such as operating systems, applications, and user files. Non-volatile storage media can include hard disk drives, solid-state drives, optical disks, and flash memory storage devices, etc.

[0129] Embodiments of this application also provide a computer program. Optionally, in this embodiment, when the above-described computer instructions are executed by a processor, the method provided in the above embodiments is implemented.

[0130] The aforementioned computer instructions can refer to a set of instructions used to tell the computer to perform specific tasks or operations. Computer instructions can be written by programmers using specific programming languages ​​and can include algorithms, data structures, logic, and control flow. Computer instructions can be used for a variety of purposes, including application software, operating systems, etc.

[0131] In the above embodiments of this application, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.

[0132] It should be noted that the preferred embodiments involved in the above embodiments of this application are the same as the solutions, application scenarios and implementation processes provided in the above embodiments, but are not limited to the solutions provided in the above embodiments.

[0133] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual coupling, direct coupling, or communication connection may be through some interfaces; the indirect coupling or communication connection between units or modules may be electrical or other forms.

[0134] 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 this embodiment according to actual needs.

[0135] Furthermore, the functional units in the various embodiments of this application 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 unit can be implemented in hardware or as a software functional unit.

[0136] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard drive, magnetic disk, or optical disk.

[0137] The above description is only a preferred embodiment of this application. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of this application, and these improvements and modifications should also be considered within the scope of protection of this application.

[0138] The above description is only a preferred embodiment of this application. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of this application, and these improvements and modifications should also be considered within the scope of protection of this application.

Claims

1. A binary code processing method, characterized in that, include: During the execution of binary code data, the execution frequency of multiple code segments in the binary code data is obtained; Based on the execution frequency of the multiple code segments, multiple target code segments are determined among the multiple code segments, wherein the execution frequency of the target code segment is greater than the execution frequency of other code segments, and the other code segments are any code segment in the binary code data other than the target code segment; Based on the location of the multiple target code segments in the virtual address space, the multiple target code segments are clustered to obtain multiple cluster sets, wherein the density of target code segments in different cluster sets is different; Based on the multiple cluster sets, the positions of the multiple target code segments in the virtual address space are adjusted.

2. The method according to claim 1, characterized in that, The method involves clustering the multiple target code segments based on their positional relationships in the virtual address space, resulting in multiple cluster sets, including: Based on the location of different target code segments in the virtual address space, the interoperability distance between different target code segments is determined, wherein the interoperability distance is used to characterize the density of other target code segments around any target code segment, as well as the positional relationship of different target code segments in the virtual address space; Based on the reachability distance between different target code segments, a directed code graph is constructed, wherein the first node in the directed code graph is used to represent the target code segment, and the first edge in the directed code graph is used to represent the reachability distance between the target code segments corresponding to the two nodes connected by the first edge. The directed graph of the code is split to obtain the multiple cluster sets.

3. The method according to claim 2, characterized in that, Determining the reachability distance between different target code segments based on their positions in the virtual address space includes: The distance between different target code segments is determined based on the address differences of different target code segments in the virtual address space; Based on the distance between any target code segment and other target code segments, determine the core distance of any target code segment, wherein the other target code segments are any target code segments other than the arbitrary target code segment, and the core distance is used to characterize the density of target code segments around the arbitrary target code segment; Based on the distance between different target code segments and the core distance between different target code segments, the interoperability distance between different target code segments is determined.

4. The method according to claim 3, characterized in that, The determination of the core distance of any target code segment based on the distance between any target code segment and other target code segments includes: Based on the distance between any target code segment and other target code segments, determine at least one adjacent target code segment that is adjacent to any target code segment; When the number of adjacent target code segments is greater than or equal to a preset number, the preset number of target code segments are determined from the adjacent target code segments based on the distance between any target code segment and any adjacent target code segment, wherein the distance between the preset number of target code segments and any target code segment is less than the distance between other adjacent target code segments and any target code segment, and the other adjacent target code segments are any target code segments other than the preset number of target code segments among the adjacent target code segments; The core distance of any target code segment is determined based on the distance between any target code segment and the preset number of target code segments.

5. The method according to claim 4, characterized in that, The determination of the core distance of any target code segment based on the distance between any target code segment and the preset number of target code segments includes: The average distance between any target code segment and the preset number of target code segments is obtained to obtain the core distance of the arbitrary target code segment.

6. The method according to claim 4, characterized in that, The method further includes: If the number of adjacent target code segments is less than the preset number, the core distance of any target code segment is determined as the preset distance.

7. The method according to claim 3, characterized in that, The determination of the interoperability distance between different target code segments based on the distance between different target code segments and the core distance between different target code segments includes: The interoperability distance between different target code segments is obtained by acquiring the distance between different target code segments and the maximum distance among the core distances of different target code segments.

8. The method according to claim 2, characterized in that, The splitting of the directed graph of the code to obtain the multiple cluster sets includes: The directed graph of the code is split to obtain multiple initial sets; The stability index of the initial set is determined based on the connectivity within the initial set and the number of edges between different initial sets. The initial set is filtered based on the stability index to obtain the cluster set.

9. The method according to claim 8, characterized in that, The directed graph of the code is split to obtain multiple initial sets, including: Extract the minimum spanning tree from the directed graph of the code, wherein the second node in the minimum spanning tree is used to represent the target code segment, and the second edge in the minimum spanning tree is used to represent the shortest path between the target code segments corresponding to the two nodes connected by the second edge; The edges in the minimum spanning tree are sorted according to their weights to obtain a sorted edge sequence. The edges of the sorted edge sequence are deleted sequentially to obtain the multiple initial sets.

10. The method according to any one of claims 1 to 9, characterized in that, The adjustment of the positions of the multiple target code segments in the virtual address space based on the multiple cluster sets includes: The target code segment is assigned to the target cluster set in the plurality of cluster sets; The target code segments in the target cluster set are adjusted to target regions in the virtual address space, wherein the target code segments in different cluster sets are adjusted to different regions in the virtual address space.

11. A binary code optimizer, characterized in that, include: The acquisition module is used to acquire the execution frequency of multiple code segments in the binary code data during the execution of the binary code data; The determination module is used to determine multiple target code segments among the multiple code segments based on the execution frequency of the multiple code segments, wherein the execution frequency of the target code segment is greater than the execution frequency of other code segments, and the other code segments are any code segment in the binary code data other than the target code segment; The clustering module is used to cluster the multiple target code segments based on their positions in the virtual address space, resulting in multiple cluster sets, wherein the density of the target code segments in different cluster sets is different; The adjustment module is used to adjust the position of the multiple target code segments in the virtual address space based on the multiple cluster sets.

12. An electronic device, characterized in that, include: Memory, which stores executable programs; A processor for running the program, wherein the program, when running, performs the method according to any one of claims 1 to 10.

13. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored executable program, wherein the executable program, when executed by a processor, implements the method according to any one of claims 1 to 10.

14. A computer program product, characterized in that, Includes computer instructions that, when executed by a processor, implement the method according to any one of claims 1 to 10.