Code optimization method and device based on multi-agent collaboration and electronic equipment
By employing a multi-agent collaborative approach, utilizing performance analysis components to filter runtime data, and combining suggestion-generating agents and optimization knowledge bases, this method simulates the optimization thinking of human experts, solving the problem of insignificant code optimization effects in existing technologies, and achieving efficient code performance bottleneck localization and optimization.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HANGZHOU DIANZI UNIV
- Filing Date
- 2026-01-08
- Publication Date
- 2026-04-21
AI Technical Summary
Existing code optimization methods based on large language models struggle to accurately identify runtime performance bottlenecks, resulting in insignificant optimization effects or even negative optimization. Furthermore, the large volume of runtime performance data, coupled with abundant noise, makes it difficult to utilize effectively.
A multi-agent collaborative approach is adopted, which filters runtime data through a performance analysis component to generate performance description information. Combined with suggestion generation agents, optimization knowledge bases, and code generation agents, it simulates the optimization thinking of human experts and provides high-level optimization directions and specific implementation references.
It enables precise identification and efficient optimization of code performance bottlenecks, reduces the risk of performance regression caused by over-reliance on model generation, and improves the predictability and feasibility of optimization results.
Smart Images

Figure CN121900770A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer software technology, and more specifically to a code optimization method, apparatus, and electronic device based on multi-agent collaboration. Background Technology
[0002] As software systems become increasingly complex, code performance optimization has become a crucial aspect of ensuring system efficiency and reducing resource consumption. In traditional development models, performance optimization heavily relies on the programmer's personal experience, typically following a "observation-analysis-retrieval-implementation-correction" path: developers first use performance profiling tools to locate hotspots in the code and analyze the bottlenecks based on the code logic; then, they rely on their own experience or consult resources to obtain feasible optimization strategies; subsequently, they manually implement code improvements and repeatedly test and correct errors until the code functions correctly and meets performance standards.
[0003] In recent years, code generation technology based on Large Language Models (LLMs) has offered new possibilities for automated code optimization, and related research has gradually increased. However, most existing optimization methods based on LLM models are still limited to analyzing static code text, which is essentially similar to automated code scanning. Since the actual performance of a program is closely related to dynamic factors such as runtime input and system state, static analysis alone often fails to accurately identify the real performance bottlenecks, leading to optimization suggestions that may deviate from the critical path, resulting in insignificant optimization effects or even negative optimization.
[0004] To address the limitations of static analysis, a common approach is to provide the model with runtime performance data, such as complete performance profiling stack logs. However, these logs are typically massive, easily exceeding the context length limit of LLM models, and contain a large amount of noisy information, such as memory addresses, that is useless for model inference. Summary of the Invention
[0005] In view of this, this application provides a code optimization method, apparatus and electronic device based on multi-agent collaboration to achieve accurate location and efficient optimization of code performance bottlenecks.
[0006] Specifically, this application is implemented through the following technical solution:
[0007] According to a first aspect of the embodiments of this specification, a code optimization method based on multi-agent collaboration is provided, comprising: acquiring runtime performance data of code to be optimized, filtering the runtime performance data to obtain performance description information for characterizing performance bottlenecks; inputting the code to be optimized and the performance description information into a suggestion-generating agent to obtain code optimization suggestions; based on the code optimization suggestions, retrieving semantically matching target optimization examples from a pre-built optimization knowledge base, wherein the optimization knowledge base stores multiple optimization examples, and the optimization examples at least include a transition relationship from long-running code to short-running code; and inputting the code to be optimized, the code optimization suggestions, and the target optimization examples into a code-generating agent to obtain optimized code.
[0008] According to a second aspect of the embodiments of this specification, a code optimization apparatus based on multi-agent collaboration is provided, comprising: a data filtering unit, configured to acquire runtime performance data of code to be optimized, and filter the runtime performance data to obtain performance description information characterizing performance bottlenecks; a suggestion generation unit, configured to input the code to be optimized and the performance description information into a suggestion generation agent to obtain code optimization suggestions; an example query unit, configured to retrieve semantically matching target optimization examples from a pre-built optimization knowledge base based on the code optimization suggestions, wherein the optimization knowledge base stores multiple optimization examples, and the optimization examples at least include the transition relationship from long-running code to optimized code; and a code optimization unit, configured to input the code to be optimized, the code optimization suggestions, and the target optimization examples into a code generation agent to obtain optimized code.
[0009] According to a third aspect of the embodiments of this specification, an electronic device is provided, including a processor; and a computer-readable storage medium storing computer program instructions that, when executed by the processor, cause the processor to perform the method described in the first aspect.
[0010] According to a fourth aspect of the embodiments of this specification, a computer-readable storage medium is provided having a computer program stored thereon, the computer program being executed by a processor of the method described in the first aspect.
[0011] In this embodiment, runtime performance data of the code to be optimized is filtered to generate performance description information characterizing performance bottlenecks, enabling subsequent analysis to focus on real performance hotspots. A multi-stage collaborative process involving a suggestion generation agent, an optimization knowledge base, and a code generation agent simulates the optimization thought process of human experts. The suggestion generation agent provides high-level optimization directions based on performance description information, the optimization knowledge base provides verifiable optimization examples, and the code generation agent is responsible for implementation. This forms a hierarchical and traceable optimization decision-making process, avoiding the limitations of a single model in complex optimization tasks. Furthermore, this embodiment retrieves semantically matched optimization examples from the optimization knowledge base, providing validated optimization patterns and specific implementation references for code optimization. This ensures the technical feasibility of the optimization scheme and improves the predictability of optimization results by reusing successful optimization experience, reducing the risk of performance regression due to over-reliance on freely generated models. Attached Figure Description
[0012] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the accompanying drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Some specific embodiments of the present invention will be described in detail below with reference to the accompanying drawings in an exemplary and non-limiting manner. The same reference numerals in the drawings indicate the same or similar parts or components. Those skilled in the art should understand that these drawings are not necessarily drawn to scale. In the drawings:
[0013] Figure 1 This is a flowchart illustrating an exemplary embodiment of the code optimization method based on multi-agent collaboration.
[0014] Figure 2 This is a schematic diagram illustrating the runtime performance data filtering process in an exemplary embodiment of this application;
[0015] Figure 3 This is a schematic diagram illustrating the fine-tuning process of the suggestion generation agent in an exemplary embodiment of this application;
[0016] Figure 4 This is a schematic diagram illustrating the modification process of the optimized code in an exemplary embodiment of this application;
[0017] Figure 5 This is a schematic diagram of the architecture of a code optimization system illustrated in an exemplary embodiment of this application;
[0018] Figure 6 This is a schematic diagram of the code optimization process shown in an exemplary embodiment of this application;
[0019] Figure 7 This is a block diagram illustrating an electronic device according to an exemplary embodiment of this application;
[0020] Figure 8 This is a block diagram illustrating a code optimization device based on multi-agent collaboration, as shown in an exemplary embodiment of this application. Detailed Implementation
[0021] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0022] The terminology used in this application is for the purpose of describing particular embodiments only and is not intended to be limiting of the application. The singular forms “a,” “the,” and “the” used in this application and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used herein refers to and includes any or all possible combinations of one or more of the associated listed items.
[0023] Code optimization methods based on the LLM model have at least the following drawbacks:
[0024] (1) Lack of runtime context awareness: Most methods analyze only static code text, similar to static code scanning tools. However, the performance bottleneck of the code often depends on the actual input distribution and system state at runtime. It is difficult to accurately locate the real time-consuming operation based on static code alone, which may cause the optimized code generated by the model to target non-bottleneck segments, resulting in insignificant optimization or even negative optimization.
[0025] (2) Lack of specific optimization direction: General LLM lacks "intuition" for specific performance problems. Without specific optimization direction guidance, the model can often only be fine-tuned at the syntax level, and it is difficult to carry out deep algorithm-level or architecture-level optimization.
[0026] (3) Context length limit: If runtime performance data (such as complete profiling stack logs) is directly introduced, the amount of data is usually extremely large, which can easily exceed the context window limit of LLM, and contains a lot of noise information such as memory addresses that are useless to the model understanding.
[0027] Therefore, this application provides a code optimization scheme based on multi-agent collaboration to more accurately simulate the optimization thinking of human experts, efficiently integrate runtime performance data into the multi-agent collaboration process, and guide the model to generate accurate code optimization schemes.
[0028] To facilitate understanding of the following embodiments of this application, the relevant concepts involved in the embodiments of this application will be introduced first.
[0029] Tag Agent: Primarily used for system cold start and optimizing knowledge base construction. Its function is to analyze the logical differences between short-running and long-running code pairs, and reverse-engineer optimization suggestions in natural language form from the long-running code to the short-running code. Examples of optimization suggestions include "optimize bubble sort to quicksort" and "use a Hash Map instead of a List for lookup." These natural language optimization suggestions are used to subsequently build the index of the optimization knowledge base.
[0030] The performance analysis component (Profiler) is responsible for dynamic code analysis. This component runs the code and uses tools such as Callgrind and gperftools to collect runtime performance data. In some embodiments, this component can also be configured with filtering functions to process the collected runtime performance data. For example, it can extract function call chains and the time-consuming weights of each function, set a weight threshold n for nodes and a weight threshold e for edges, filter out function nodes with a time consumption percentage less than n% and edges with a call contribution percentage less than e%, and convert the remaining critical path data into Dot graph description language format to generate performance description information containing only function names, call relationships, and weights.
[0031] The Suggest Agent takes as input the source code to be optimized and performance descriptions generated by the performance analysis component, and generates abstract optimization suggestions for the current specific scenario. This agent undergoes specific supervised fine-tuning (SFT) to improve its understanding of the Dot data and the accuracy of its suggestion generation.
[0032] The Retrieval Tool connects to a vector database. It receives optimization suggestions generated by the suggestion-generating agent, vectorizes them, retrieves them from the database, and returns several semantically most similar optimization examples (including the original code, optimized code, and optimization suggestions) as contextual references for the code-generating agent.
[0033] The code generation agent is responsible for the actual code implementation. It has two functions: first, generation, which generates optimized code based on the code to be optimized, optimization suggestions, and a small number of retrieved examples; second, repair, which receives error messages or input / output differences and self-corrects the code when compilation fails or test cases fail.
[0034] The Evaluator module is the core of the system's evaluation. It is responsible for recording the runtime and memory usage of the code before and after optimization, calculating the speedup ratio, and controlling the termination of the entire iteration process.
[0035] The embodiments described in this specification will now be described in detail.
[0036] This application provides a code optimization method based on multi-agent collaboration. Figure 1 This is a flowchart illustrating an exemplary embodiment of a code optimization method based on multi-agent cooperation, as shown in this application. Figure 1 As shown, the code optimization method 100 includes at least the following steps S110 and S140:
[0037] Step S110: Obtain runtime performance data of the code to be optimized, and filter the runtime performance data to obtain performance description information used to characterize performance bottlenecks.
[0038] The code optimization method 100 provided in this application can be executed by a hardware or software entity with data processing capabilities, including but not limited to servers, processors, computer equipment, chips, system-on-a-chip (SoC), application-specific integrated circuits (ASIC), programmable logic devices (FPGA), distributed computing nodes, cloud computing instances, edge computing devices, or functional modules / systems integrating corresponding processing logic. For clarity, the following embodiments will use a code optimization system as the execution subject for illustrative purposes.
[0039] The system can run the code to be optimized and collect runtime data through the performance analysis component, such as CPU stack data. Since the runtime data is massive and contains a lot of noise information such as memory addresses that is useless to the agent's understanding, this embodiment can filter the runtime data through the performance analysis component to remove the noise information and retain the performance description information that can characterize the performance bottleneck, thereby providing high-quality and highly relevant input for subsequent intelligent analysis.
[0040] The performance description information is generated by filtering, aggregating, and reorganizing the runtime performance data. It is structured information that characterizes key performance hotspots, major function call relationships, and resource consumption weights during code execution. Its typical form can be a text-based graphical description file, such as text conforming to the DOT language specification. This performance description information strips away irrelevant details such as memory addresses and timestamps from the original runtime performance data, focusing on the topological structure and quantitative characteristics of performance bottlenecks, providing high-quality foundational data for subsequent agent analysis.
[0041] Step S120: Input the code to be optimized and the performance description information into the suggestion to generate an intelligent agent to obtain code optimization suggestions.
[0042] The suggestion-generating agent refers to a specially supervised intelligent model capable of integrating code semantics and runtime performance characteristics to generate targeted optimization suggestions. Its role is to simulate the analytical reasoning process of human experts after receiving code and its performance profiling reports. The suggestion-generating agent can be a module based on a large language model.
[0043] The code optimization suggestions can be in the form of natural language text or structured vector data. If the code optimization suggestions generated by the suggestion-generating agent are in the form of natural language text, they also need to be vectorized to facilitate semantic matching with optimization examples in the optimization knowledge base.
[0044] Step S130: Based on the code optimization suggestions, retrieve semantically matching target optimization examples from the pre-built optimization knowledge base. The optimization knowledge base stores multiple optimization examples, and each optimization example contains at least the transition relationship from long-time-consuming code to short-time-consuming code.
[0045] In this embodiment, long-running code refers to code versions that, under the same functional requirements and test input conditions, have relatively long execution times or exhibit clear performance bottlenecks. It is typically used as a starting point for optimization, and its long-running characteristics can be quantified using performance analysis components.
[0046] In this embodiment, short-running code refers to a code version that performs the same function as the aforementioned long-running code, but whose runtime is significantly shortened under the same conditions through effective optimization methods, such as algorithm improvement and logic refactoring. Short-running code represents a target state with better performance.
[0047] The transition relationship can be understood as follows: in the optimization knowledge base, an optimization example includes the transformation logic and logical modification intention from long-running code to short-running code. Each optimization example stored in the optimization knowledge base includes long-running code as the starting point of optimization, short-running code as the ending point of optimization, and optimization suggestion tags. The optimization suggestion tags are high-frequency vector representations of natural language descriptions. These natural language descriptions explain the core optimization strategy, modification intentions, or key transformation logic used to transform long-running code into short-running code. For example, changing nested loops to using dictionary preprocessing reduces the time complexity from O(n²) to O(n).
[0048] Step S140: Input the code to be optimized, the code optimization suggestions, and the target optimization example into the code generation agent to obtain the optimized code.
[0049] The code generation agent refers to an intelligent model that receives optimization instructions and reference examples, and generates target code that meets functional requirements and has better performance. Its role is to simulate the process of human experts combining optimization strategies and reference cases to implement specific code. The code generation agent can be a module based on a large language model.
[0050] like Figure 1 As shown in the code optimization method 100, this embodiment filters runtime performance data of the code to be optimized to generate performance description information representing performance bottlenecks, enabling subsequent analysis to focus on real performance hotspots. A multi-stage collaborative process involving a suggestion generation agent, an optimization knowledge base, and a code generation agent simulates the optimization thought process of human experts. The suggestion generation agent provides high-level optimization directions based on performance description information, the optimization knowledge base provides verifiable optimization examples, and the code generation agent is responsible for implementation, forming a hierarchical and traceable optimization decision-making process that avoids the limitations of a single model in complex optimization tasks. Furthermore, this embodiment retrieves semantically matched optimization examples from the optimization knowledge base, providing validated optimization patterns and specific implementation references for code optimization. This ensures the technical feasibility of the optimization scheme and improves the predictability of optimization results by reusing successful optimization experience, reducing the risk of performance regression due to over-reliance on freely generated models.
[0051] In some embodiments, filtering the runtime performance data in step S110 to obtain performance description information for characterizing performance bottlenecks includes: constructing a function call graph based on the runtime performance data, wherein nodes in the function call graph represent called functions, edges represent call relationships between functions, and both nodes and edges have weights statistically derived from the runtime performance data; filtering the nodes or edges in the function call graph according to the weights; and obtaining the performance description information based on the subgraph formed by the filtered nodes and edges.
[0052] like Figure 2As shown, the performance analysis component collects runtime data of the code to be optimized. This runtime data includes a large amount of memory addresses, stack traces, noise, etc. In this embodiment, the runtime data is parsed to construct a function call graph. The function call graph includes nodes V and edges E. Based on the runtime data, the runtime consumption percentage of each node V and the call contribution of each edge E are statistically analyzed. The call contribution refers to the call frequency or the percentage of runtime contribution. The calculation scheme for the runtime consumption percentage and call contribution can be found in relevant technologies, which will not be elaborated here. This embodiment also sets a node weight threshold of n% and an edge weight threshold of e%; function nodes with a runtime consumption percentage lower than n% and call edges with a call contribution lower than e% are removed from the function call graph. The remaining nodes V and edges E after filtering are converted into a text-based graphical description file. This graphical description file can be a Dot format file. The Dot format file retains the function name, call topology, and corresponding weight values, but does not contain noise information such as memory addresses.
[0053] In some embodiments, the optimization knowledge base is constructed through the following steps: extracting submission records and test cases corresponding to multiple questions from the code evaluation dataset; for each question, filtering out code pairs that meet the condition of difference in execution time based on the execution time information in the submission records, wherein the code with shorter execution time in the code pair is designated as short-time code and the code with longer execution time is designated as long-time code; performing logical difference analysis on each code pair using a tagging agent to generate natural language suggestions describing their performance optimization logic; and vectorizing the natural language suggestions and associating them with the corresponding code pairs for storage to construct the optimization knowledge base based on semantic retrieval.
[0054] The code evaluation dataset is the foundational data source for training and building the optimized knowledge base. This dataset can be a publicly available open-source dataset, such as CodeNet, or a proprietary dataset built for a specific purpose. Essentially, it is a structured collection of data, whose metadata includes, but is not limited to, problem information, submission records, and test cases. The problem information indicates the corresponding programming problem or task identifier, such as the problem ID, and possible problem descriptions. The submission records refer to multiple code submissions for each problem. Each record typically includes a submission ID, source code, execution time, memory usage, evaluation result (usually a "pass" or "fail" status to ensure the correctness of the selected code), and test cases.
[0055] By analyzing this code evaluation dataset, a large number of code pairs that implement the same function but have significant differences in performance (based on execution time) can be automatically and in batches, providing real optimization case materials for building a knowledge base.
[0056] The labeled agent is an intelligent model or program specifically designed to automate the analysis of code differences and generate natural language optimization suggestions. Its function is to automatically generate a natural language suggestion describing the core optimization logic for each code pair, replacing manual intervention for the longer and shorter code segments.
[0057] In one example, the natural language suggestions are converted into high-dimensional vectors based on an embedding model, such as the OpenAI Embedding API or BERT. These high-dimensional vector labels are used as index keys, and the longer and shorter code segments in the code pair are used as values, stored in a database such as Milvus or Faiss, thus optimizing the construction of the knowledge base.
[0058] In some embodiments, the suggestion-generating agent is obtained through the following steps: acquiring performance description information corresponding to long-running code in the code pair; using the long-running code and its corresponding performance description information as samples, and the natural language suggestions as sample labels, constructing a fine-tuning training dataset; and using the fine-tuning training dataset to perform supervised instruction fine-tuning on the suggestion-generating agent.
[0059] To enable the suggestion-generating agent to understand performance description information and generate accurate performance optimization suggestions, this embodiment fine-tunes it. For example... Figure 3 As shown, the extracted test cases drive the execution of long-running code in the code pair. During execution, runtime data is collected using a Profiler, and double threshold filtering is performed to generate denoised performance description information in Dot format. The long-running code and its corresponding performance description information are used as sample data, and the high-dimensional vectors generated by the label agent are used as labels for the sample data, thus constructing a fine-tuning training dataset. This fine-tuning training dataset is then used to fine-tune a general-purpose large language model using supervised instructions, resulting in a dedicated suggestion generation agent.
[0060] In some embodiments, the code optimization method 100 further includes verifying the optimized code by at least one of the following methods:
[0061] Method 1: Compiler-level verification.
[0062] In some embodiments, if the optimized code fails to compile, the optimized code is corrected based on the compiler error message.
[0063] Method 2: Functional level verification.
[0064] In some embodiments, if the optimized code fails the preset test cases, the optimized code is corrected based on the difference information between the expected output corresponding to the test cases and the actual output of the optimized code.
[0065] Method 3: Verification based on the exit mechanism.
[0066] In some embodiments, if the verification fails after the number of consecutive corrections to the same code segment reaches a preset limit, the verification process ends.
[0067] The above verification methods can be used individually or in combination, and those skilled in the art can choose flexibly according to their needs.
[0068] like Figure 4 As shown, after the code generation agent generates the initial optimized code, a self-correction process is triggered: If the optimized code fails to compile, the code optimization system extracts the compiler error information and feeds it back to the code generation agent for syntax repair. If the optimized code compiles successfully but fails the preset test cases, the code optimization system encapsulates the differences between the input data, expected output, and actual output of the failed test cases into prompt words and feeds them back to the code generation agent for logic repair. If the number of consecutive repairs for the same code segment reaches a preset limit, such as failing the verification after 3 attempts, the current optimization branch is forcibly terminated to avoid wasting resources.
[0069] In some embodiments, the code optimization method 100 further includes: determining whether the optimized code meets a preset iteration exit condition; if the iteration exit condition is not met, then the optimized code is used as a new code to be optimized, and steps S110 to S140 are repeated; if the iteration exit condition is met, then the code with the best overall performance is selected from all code versions generated during the iteration process as the final optimized code.
[0070] In one example, the iteration exit condition includes the performance gain of the optimized code relative to the code to be optimized being lower than a preset gain threshold, or the number of iterations reaching a preset upper limit. The next iteration continues only when the performance gain of the optimized code relative to the code to be optimized is not lower than the preset gain threshold and the number of iterations has not reached the preset upper limit.
[0071] In one example, the execution time and memory usage of the optimized code can be obtained to calculate the speedup ratio, for example, the ratio of the execution time of the code to be optimized to the execution time of the optimized code can be used as the performance gain.
[0072] In one example, when outputting the final result, the code optimization system prioritizes the code with the lowest execution time among all candidate codes that pass the verification; if the execution times are the same, it further selects the code with lower memory usage. Of course, in other embodiments, those skilled in the art can select the code with the best overall performance as needed, and this embodiment does not particularly limit this.
[0073] To illustrate the code optimization process in this embodiment in detail, the following embodiments will be used as a reference.
[0074] by Figure 5 Taking the code optimization system shown as an example, the system includes a preprocessing section (DataPreprocessing), a main processing section (Core Processing), and an evaluation section. The following section will combine... Figure 5 and Figure 6 Provide a detailed explanation of the code optimization process for this system.
[0075] (1) Preprocessing section:
[0076] This embodiment first performs cold start preparation for the code optimization system, with data sourced from large-scale code datasets such as Project CodeNet.
[0077] First, build the basic dataset.
[0078] Metadata is extracted from datasets such as CodeNet. The metadata files of the CodeNet dataset are traversed, extracting key fields including Problem ID, Submission ID, source code path, execution time (CPU Time), memory usage, and judging status. Simultaneously, test cases corresponding to the problems are extracted, including standard input and output files, as benchmarks for subsequent code execution. The input and output files for each problem are obtained as benchmarks for subsequent code execution and verification. Code pairs are constructed, consisting of long-running and short-running code. For the same problem ID, by comparing execution times, two source codes with significantly different execution times (e.g., more than a factor of 1) are selected and paired as the code pair. <slow code,fast code)。
[0079] Next, we will build and optimize the knowledge base.
[0080] Use Tag Agent for each pair constructed above<slow code, fast code> The process involves TagAgent reading slow and fast code, analyzing their logical differences, and constructing natural language optimization suggestions from slow to fast code. Then, using an embedding model (such as the OpenAI Embedding API or BERT), these optimization suggestions are converted into high-dimensional vectors. The vector of optimization suggestions is used as the index key, and the corresponding original slow-code-fast-code data pair is used as the value, stored in a vector database (such as Milvus or Faiss). This process constructs a RAG knowledge base indexed based on the optimization suggestions.
[0081] Finally, supervise the fine-tuning of data preparation and agent training.
[0082] To enable the suggestion-generating agent to understand the performance description information in Dot format and generate accurate code optimization suggestions, fine-tuning is required. Specifically, extracted test cases are used to drive the execution of long-running code in the code pair. During execution, a Profiler is used to collect runtime data, including CPU stack data, and double threshold filtering is performed to generate denoised performance description information. Training samples are constructed based on the long-running code, its corresponding performance description information, and optimization suggestion labels generated by the labeled agent. These training samples are then used to fine-tune a general-purpose large language model under supervised instruction to obtain a dedicated suggestion-generating agent.
[0083] (2) Optimize the code iteration optimization process.
[0084] After completing the data preprocessing and model building, the code optimization iteration process begins. The system gradually improves code performance through multiple rounds of closed-loop iteration. The specific process is as follows:
[0085] In step S1, the system receives the code to be optimized submitted by the user, drives the performance analysis component to run the code using preset test cases, and records the running time. Simultaneously, CPU stack data is collected during code execution. Double threshold filtering is performed on the raw stack data; for example, functions with a weight <5% and edges with a weight <1% are removed, generating performance description information in Dot format describing the critical path.
[0086] Step S2: Input the code to be optimized and the performance description information in Dot format generated in step S1 into the fine-tuned suggestion generation agent. The suggestion generation agent combines the static logic of the code and the dynamic bottlenecks displayed by the performance description information to generate K specific code optimization suggestions.
[0087] Step S3: After receiving the code optimization suggestions and vectorizing them, the retrieval tool performs a semantic search in the optimization knowledge base to obtain the N optimization examples most relevant to the current code optimization suggestions. These optimization examples demonstrate how time-consuming code can be transformed from inefficient to efficient under similar optimization approaches, and are passed as sample hints to subsequent steps.
[0088] In step S4, the code generation agent receives the code to be optimized, code optimization suggestions, and optimization examples, and generates optimized code.
[0089] In some embodiments, step S4 includes a code verification process, which is as follows:
[0090] Compilation verification: If the system fails to compile the optimized code, the error message is sent back to the code generation agent, requesting it to fix the syntax errors.
[0091] Functionality verification involves running test cases. If the code fails all test cases, the system encapsulates the inputs, expected outputs, and actual outputs of the failed test cases into prompts and sends them back to the code generation agent, requiring it to fix the logical errors.
[0092] Based on the exit mechanism, if the number of consecutive error corrections by the code generation agent reaches the preset limit, for example, if the code still fails to pass the verification after three consecutive error corrections, it indicates that there may be a problem with the current optimization direction. At this time, the generation of this round is terminated, the code is rolled back to the code to be optimized at the beginning of this iteration, and step S1 is entered to re-iterate and optimize.
[0093] (3) Code evaluation process.
[0094] After completing the iterative optimization process, the optimized code that has passed code verification is run to obtain its execution time and memory usage, and the speedup ratio is calculated as: Speedup Ratio = Execution time of code before optimization / Execution time of code after optimization. Based on the speedup ratio, the system performs an iteration termination determination, checking whether any of the following exit conditions are met:
[0095] (1) The number of global iterations reaches the preset limit, reaching the limit of 5 iterations. This condition prevents the system from falling into an infinite loop or timeout.
[0096] (2) The speedup ratio of a single iteration is lower than the preset threshold, such as 1.1 times. This condition indicates that the performance improvement brought by this optimization is lower than the expected minimum gain, indicating that there is no significant room for optimization in the code.
[0097] If any of the above conditions are met, the system will immediately terminate the iteration process and output the version code with the lowest running time and the lowest memory usage as the final result; if neither of the above conditions is met, the system will feed the current optimized code back to step S1 as a new benchmark to be optimized and restart a new round of code optimization process.
[0098] Based on the above embodiments of this application, the code optimization method based on multi-agent cooperation of this application has at least the following advantages:
[0099] First, it simulates the thinking of human experts: by replicating the optimization path of human programmers through the process of "Profiler locating bottlenecks - Suggest Agent proposing ideas - Retrieval Tool retrieving reference cases - Code Agent implementing coding and correction".
[0100] Second, a runtime stack dual filtering mechanism is introduced: runtime code data is innovatively introduced, and through dual threshold filtering and Dot format conversion, while retaining key performance bottleneck paths (call relationships and weights), noise such as memory addresses is effectively removed, significantly reducing token consumption, enabling LLM to understand the dynamic behavior of the program.
[0101] Third, high-precision optimization suggestions: The suggestion generation agent has been specially supervised and fine-tuned, enabling it to combine static and dynamic runtime features of the code to generate accurate optimization directions, thereby significantly improving the quality of the code generated by the code generation agent.
[0102] Fourth, closed-loop iterative optimization: It possesses a complete self-correction and evaluation mechanism. In terms of performance optimization, it adopts a restricted incremental benefit-based convergence strategy: the system calculates the speedup ratio of a single iteration in real time, and continues to optimize only when the speedup ratio exceeds a threshold and has not reached the maximum number of iterations limit; once diminishing marginal benefits occur (low speedup ratio) or the preset cycle limit is reached, the optimization process is determined to terminate, thereby ensuring the ultimate performance of the code while preventing the system from falling into an infinite loop or invalid calculation.
[0103] Figure 7 This is a schematic diagram of an electronic device illustrated in this specification according to an exemplary embodiment. Please refer to... Figure 7At the hardware level, the device includes a processor 702, an internal bus 704, a network interface 706, memory 708, a hardware acceleration device 710, and non-volatile memory 712, and may also include other hardware required for its functions. One or more embodiments of this application can be implemented in software, for example, the processor 702 reads the corresponding computer program from the non-volatile memory 712 into the memory 708 and then runs it. Of course, in addition to software implementation, one or more embodiments of this application do not exclude other implementation methods, such as logic devices or a combination of hardware and software, etc. That is to say, the execution subject of the above processing flow is not limited to each logic unit, but can also be hardware or logic devices.
[0104] Figure 8 This is a block diagram illustrating an exemplary embodiment of a code optimization device based on multi-agent cooperation, which can be applied to, for example... Figure 7 The electronic device shown implements the technical solution of this application. The code optimization device based on multi-agent collaboration includes: a data filtering unit 810, a suggestion generation unit 820, an example query unit 830, and a code optimization unit 840, wherein:
[0105] The data filtering unit 810 is used to obtain runtime performance data of the code to be optimized, and to filter the runtime performance data to obtain performance description information that characterizes performance bottlenecks.
[0106] The suggestion generation unit 820 is used to input the code to be optimized and the performance description information into the suggestion generation agent to obtain code optimization suggestions;
[0107] Example query unit 830 is used to retrieve semantically matching target optimization examples from a pre-built optimization knowledge base based on the code optimization suggestions. The optimization knowledge base stores multiple optimization examples, and each optimization example contains at least the transition relationship from long-running code to optimized code.
[0108] The code optimization unit 840 is used to input the code to be optimized, the code optimization suggestions, and the target optimization example into the code generation agent to obtain optimized code.
[0109] In some embodiments, the data filtering unit 810 is configured to construct a function call graph based on the runtime performance data, wherein nodes in the function call graph represent called functions, edges represent call relationships between functions, and both nodes and edges have weights based on statistics from the runtime performance data; the nodes or edges in the function call graph are filtered according to the weights, and the performance description information is obtained based on the subgraph formed by the nodes and edges retained after filtering.
[0110] In some embodiments, the code optimization device based on multi-agent collaboration further includes a knowledge base construction unit, used to extract submission records and test cases corresponding to multiple questions from the code evaluation dataset; for each question, based on the runtime information in the submission records, filter out code pairs that meet the runtime difference condition, where the code pair with longer runtime is designated as long-running code and the code with shorter runtime is designated as short-running code; use a label agent to perform logical difference analysis on each code pair, and generate natural language suggestions describing their performance optimization logic; vectorize the natural language suggestions and associate them with the corresponding code pair for storage, so as to construct the optimization knowledge base based on semantic retrieval.
[0111] In some embodiments, the code optimization device based on multi-agent collaboration further includes an agent fine-tuning unit, used to obtain performance description information corresponding to long-time-consuming code in the code pair; construct a fine-tuning training dataset using the long-time-consuming code and its corresponding performance description information as samples and the natural language suggestions as sample labels; and use the fine-tuning training dataset to perform supervised instruction fine-tuning on the suggestion-generating agent.
[0112] In some embodiments, the code optimization device based on multi-agent collaboration further includes a code verification unit, configured to verify the optimized code through the following steps: if the optimized code fails to compile, the optimized code is corrected according to the compiler error information; and / or, if the optimized code fails to pass preset test cases, the optimized code is corrected according to the difference information between the expected output corresponding to the test cases and the actual output of the optimized code; and / or, if the verification still fails after the number of consecutive corrections for the same code segment reaches a preset upper limit, the verification is terminated.
[0113] In some embodiments, the code optimization device based on multi-agent collaboration further includes an iterative optimization unit, used to determine whether the optimized code meets a preset iteration exit condition; if the iteration exit condition is not met, the optimized code is used as a new code to be optimized, and the steps are repeated; if the iteration exit condition is met, the code with the best overall performance is selected from all code versions generated during the iteration process as the final optimized code.
[0114] In some embodiments, the iteration exit condition includes the performance gain of the optimized code relative to the code to be optimized being lower than a preset gain threshold, or the number of iteration rounds reaching a preset upper limit.
[0115] For the device embodiments, since they basically correspond to the method embodiments, the relevant parts can be referred to in the description of the method embodiments. The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and 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 modules can be selected to achieve the purpose of this application according to actual needs. Those skilled in the art can understand and implement this without creative effort.
[0116] Accordingly, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the methods described in any of the above embodiments.
[0117] Accordingly, embodiments of this application also provide a computer program product configured to perform the methods described in any of the above embodiments.
[0118] The systems, devices, modules, or units described in the above embodiments can be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer, which can take the form of a personal computer, laptop computer, cellular phone, camera phone, smartphone, personal digital assistant, media player, navigation device, email sending and receiving device, game console, tablet computer, wearable device, or any combination of these devices.
[0119] In a typical configuration, a computer includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0120] Memory may include non-persistent storage in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0121] Computer-readable media, including both permanent and non-permanent, removable and non-removable media, can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, disk storage, quantum memory, graphene-based storage media or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0122] While this specification contains numerous specific implementation details, these should not be construed as limiting the scope of any invention or the scope of the claims, but rather are primarily intended to describe features of specific embodiments of a particular invention. Certain features described in the various embodiments herein may also be implemented in combination in a single embodiment. Conversely, various features described in a single embodiment may also be implemented separately in various embodiments or in any suitable sub-combination. Furthermore, while features may function in certain combinations as described above and even initially claimed in this way, one or more features from a claimed combination may be removed from that combination in some cases, and a claimed combination may refer to a sub-combination or a variation thereof.
[0123] Similarly, although the operations are depicted in a specific order in the accompanying drawings, this should not be construed as requiring these operations to be performed in the specific order shown or sequentially, or requiring all illustrated operations to be performed to achieve the desired result. In some cases, multitasking and parallel processing may be advantageous. Furthermore, the separation of various system modules and components in the above embodiments should not be construed as requiring such separation in all embodiments, and it should be understood that the described program components and systems can generally be integrated together in a single software product or packaged into multiple software products.
[0124] Thus, specific embodiments of the subject matter have been described. Other embodiments are within the scope of the appended claims. In some cases, the actions recited in the claims may be performed in a different order and still achieve the desired result. Furthermore, the processes depicted in the drawings are not necessarily shown in a specific order or sequence to achieve the desired result. In some implementations, multitasking and parallel processing may be advantageous.
[0125] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0126] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of protection of this application.
Claims
1. A code optimization method based on multi-agent cooperation, characterized in that, Includes the following steps: Obtain runtime performance data of the code to be optimized, and filter the runtime performance data to obtain performance description information used to characterize performance bottlenecks; The code to be optimized and the performance description information are input into a suggestion to generate an intelligent agent, and code optimization suggestions are obtained. Based on the code optimization suggestions, semantically matching target optimization examples are retrieved from a pre-built optimization knowledge base. The optimization knowledge base stores multiple optimization examples, and each optimization example contains at least the transition relationship from long-running code to short-running code. The code to be optimized, the code optimization suggestions, and the target optimization example are input into the code generation agent to obtain the optimized code.
2. The method according to claim 1, characterized in that, The filtering of the runtime performance data to obtain performance description information for characterizing performance bottlenecks includes: A function call graph is constructed based on the runtime performance data. Nodes in the function call graph represent called functions, and edges represent the calling relationships between functions. Both nodes and edges have weights based on statistics from the runtime performance data. The nodes or edges in the function call graph are filtered according to the weights, and the performance description information is obtained based on the subgraph formed by the nodes and edges retained after filtering.
3. The method according to claim 1, characterized in that, The optimized knowledge base is constructed through the following steps: Extract the submission records and test cases corresponding to multiple questions from the code evaluation dataset; For each question, based on the runtime information in the submission record, code pairs that meet the runtime difference criteria are selected. The code with shorter runtime in the code pair is designated as short-running code, and the code with longer runtime is designated as long-running code. The label-based intelligent agent performs logical difference analysis on each code pair and generates natural language suggestions describing its performance optimization logic. The natural language suggestions are vectorized and associated with the corresponding code pairs for storage, in order to construct the optimized knowledge base based on semantic retrieval.
4. The method according to claim 3, characterized in that, The proposed agent is generated through the following steps: Obtain the performance description information corresponding to the long-running code in the code pair; Using the long-running code and its corresponding performance description information as samples, and the natural language suggestions as sample labels, a fine-tuning training dataset is constructed. The proposed agent is fine-tuned using the fine-tuning training dataset under supervised instruction.
5. The method according to claim 1, characterized in that, The method further includes verifying the optimized code using at least one of the verification methods: If the optimized code fails to compile, the optimized code will be corrected according to the compiler error message; If the optimized code fails the preset test cases, the optimized code is corrected based on the difference between the expected output of the test cases and the actual output of the optimized code. If the verification fails after the number of consecutive corrections to the same code snippet reaches the preset limit, the verification process ends.
6. The method according to claim 1, characterized in that, The method further includes: Determine whether the optimized code meets the preset iteration exit conditions; If the iteration exit condition is not met, the optimized code is treated as new code to be optimized, and the steps are repeated. If the iteration exit condition is met, the code with the best overall performance is selected from all code versions generated during the iteration process as the final optimized code.
7. The method according to claim 6, characterized in that, The iteration exit conditions include the performance gain of the optimized code relative to the code to be optimized being lower than a preset gain threshold, or the number of iteration rounds reaching a preset upper limit.
8. A code optimization device based on multi-agent cooperation, characterized in that, The device includes: The data filtering unit is used to obtain runtime performance data of the code to be optimized, and to filter the runtime performance data to obtain performance description information that characterizes performance bottlenecks. The suggestion generation unit is used to input the code to be optimized and the performance description information into the suggestion generation agent to obtain code optimization suggestions; Example query unit, used to retrieve semantically matching target optimization examples from a pre-built optimization knowledge base based on the code optimization suggestions. The optimization knowledge base stores multiple optimization examples, and each optimization example contains at least the transition relationship from long-time-consuming code to short-time-consuming code. The code optimization unit is used to input the code to be optimized, the code optimization suggestions, and the target optimization example into the code generation agent to obtain optimized code.
9. An electronic device, characterized in that, include: processor; And a computer-readable storage medium storing computer program instructions that, when executed by the processor, cause the processor to perform the method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, which is executed by a processor according to any one of claims 1 to 7.
Citation Information
Cited By
Code data migration method, system and electronic device
CN122285077A