Code optimization method and device, electronic equipment and storage medium

By obtaining end-to-end feedback information through multi-stage testing and using a large language model for diagnosis, the problem of low efficiency in code optimization diagnosis in existing technologies is solved, and efficient and accurate code optimization and quality improvement are achieved.

CN121785608APending Publication Date: 2026-04-03INST OF SOFTWARE - CHINESE ACAD OF SCI
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202610263280.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-03-05
Publication Date
2026-04-03

AI Technical Summary

Technical Problem

Existing technologies suffer from low efficiency and incomplete analysis in code optimization diagnosis, especially when generating code from large language models, making it difficult to effectively identify logical errors and performance bottlenecks.

Method used

By sequentially performing multi-stage tests on the code to be optimized, feedback information from the entire chain is obtained, key information from the entire chain is extracted, and semantic understanding and logical reasoning are performed using a large language model to generate diagnostic results and optimization suggestions.

Benefits of technology

It enables efficient and accurate diagnosis and optimization of code, improves code quality and efficiency, and ensures the correctness and performance of code in multiple dimensions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121785608A_ABST
    Figure CN121785608A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of computers, and provides a code optimization method and device, electronic equipment and a storage medium, and the method comprises the steps: sequentially executing a multi-stage test on a to-be-optimized code, and under the condition that a test result of a non-initial stage is that the to-be-optimized code does not pass, carrying out the multi-stage test on the to-be-optimized code based on test feedback information including the non-initial stage and before the non-initial stage; obtaining full-link test feedback information; performing information extraction on the full-link feedback information to obtain full-link key information; obtaining a diagnosis result based on the full-link key information; code optimization is executed on the to-be-optimized code based on the diagnosis result, the islanding effect of traditional single-stage test feedback is broken through, and a complete and accurate context diagnosis basis is provided for code diagnosis by integrating multi-stage full-link test feedback information, so that more accurate, comprehensive and efficient code diagnosis is realized, and the diagnosis efficiency is improved. And thus, the code optimization quality and efficiency are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a code optimization method, apparatus, electronic device, and storage medium. Background Technology

[0002] With the widespread application of Large Language Models (LLMs) in automatic code generation, while the generated code can implement complex software functions, it often suffers from logical errors, interface mismatches, or performance degradation due to limitations in the coverage of training data and biases in contextual understanding. Therefore, optimizing the generated code to ensure its correctness and performance has become a critical need in the current intelligent development of software engineering. Existing technologies typically employ feedback-based optimization mechanisms. Specifically, this includes two main approaches: one is to directly input the complete test feedback information from a single test phase as prompts into the LLM, allowing the model to analyze and regenerate code based on this comprehensive information; the other relies on developers manually analyzing test results, extracting key error segments or performance bottleneck data from the complex logs of a single test phase, and then feeding this data into the LLM for targeted optimization.

[0003] However, whether it is direct input of test feedback information at a single stage or manual screening, existing code optimization methods suffer from low diagnostic efficiency and incomplete analysis. Summary of the Invention

[0004] This invention provides a code optimization method, apparatus, electronic device, and storage medium to address the shortcomings of existing code optimization technologies, such as low diagnostic efficiency and incomplete analysis.

[0005] This invention provides a code optimization method, comprising: Multi-stage tests are performed sequentially on the code to be optimized. If the test result in a non-starting stage fails, full-link feedback information is obtained based on the test feedback information including the non-starting stage and the test feedback information before the non-starting stage. Information is extracted from the end-to-end feedback information to obtain key end-to-end information; Based on the key information across the entire chain, the diagnostic results for the code to be optimized are obtained; Based on the diagnostic results, code optimization is performed on the code to be optimized.

[0006] According to a code optimization method provided by the present invention, obtaining the diagnostic result of the code to be optimized based on the end-to-end key information includes: The key information of the entire link is input into a large language model, and semantic understanding and logical reasoning are performed on the key information of the entire link based on the large language model to obtain the diagnostic results. The diagnostic results include the defect code, the defect type of the defect code, and defect optimization suggestions; The defect code includes error code segments and / or inefficient code segments.

[0007] According to a code optimization method provided by the present invention, the step of extracting information from the end-to-end feedback information to obtain key end-to-end information includes: According to the preset key feedback indicators, the feedback information corresponding to the key feedback indicators is extracted from the full-link feedback information to obtain the full-link key information. The key feedback metrics include at least one of the following: exception description information, error stack information, key performance metrics, and affected code module identifiers.

[0008] According to a code optimization method provided by the present invention, the step of performing code optimization on the code to be optimized based on the diagnostic results includes: Based on the diagnostic results, code optimization is performed on the code to be optimized to obtain the optimized code to be verified in the current round; The code to be optimized in the current round is used as the code to be optimized in the next round. Multi-stage tests are repeatedly performed on the code to be optimized. If the test result in the non-initial stage is unsuccessful, the code to be optimized is optimized based on the full-link feedback information in the next round until the optimized code passes the multi-stage tests.

[0009] According to a code optimization method provided by the present invention, the step of performing code optimization on the code to be optimized based on the diagnostic results to obtain the code to be verified for the current round includes: Based on the diagnostic results, an optimized prompt text is generated; The optimization prompt text and the code to be optimized are input into the code generation model to obtain the code to be optimized for the current round generated by the code generation model; The code generation model is trained based on a general large language model.

[0010] According to a code optimization method provided by the present invention, the multi-stage test includes at least two of compilation tests, functional tests, and performance tests. The execution order of the multi-stage test is as follows: the compilation test, the functional test, and the performance test are executed sequentially. The next stage of testing is performed if the previous stage of testing passes.

[0011] The present invention also provides a code optimization apparatus, comprising: The acquisition unit performs multi-stage tests on the code to be optimized in sequence. If the test result in a non-starting stage is unsuccessful, it obtains full-link feedback information based on the test feedback information including the non-starting stage and the test feedback information before the non-starting stage. The extraction unit extracts information from the end-to-end feedback information to obtain key information about the entire link. The diagnostic unit obtains the diagnostic results of the code to be optimized based on the key information across the entire link. The optimization unit performs code optimization on the code to be optimized based on the diagnostic results.

[0012] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the program, implements any of the code optimization methods described above.

[0013] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the code optimization method as described above.

[0014] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements any of the code optimization methods described above.

[0015] The code optimization method, apparatus, electronic device, and storage medium provided by this invention perform multi-stage tests on the code to be optimized sequentially. If the test results in a non-initial stage fail, end-to-end test feedback information is obtained based on the test feedback information including the non-initial stage and previous stages. Information is extracted from the end-to-end feedback information to obtain key end-to-end information. A diagnostic result is obtained based on the key end-to-end information. Based on the diagnostic result, code optimization is performed on the code to be optimized. This provides a complete and accurate contextual diagnostic basis for code diagnosis, enabling more accurate, comprehensive, and efficient code diagnosis, thereby improving the quality and efficiency of code optimization. Attached Figure Description

[0016] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.

[0017] Figure 1This is one of the flowcharts illustrating the code optimization method provided by this invention; Figure 2 This is a comparative diagram of performance changes in code iteration optimization provided by the present invention; Figure 3 This is the second flowchart illustrating the code optimization method provided by this invention; Figure 4 This is a schematic diagram of the code optimization device provided by the present invention; Figure 5 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation

[0018] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.

[0019] It should be noted that directly inputting complete feedback information into a large model for code optimization can be problematic for complex or large codebases. The sheer volume of information and the excessively long input context make it difficult for the model to effectively analyze error messages, leading to inefficient defect localization or performance bottlenecks. Furthermore, while manually extracting error information before inputting it into a large model reduces the amount of input, it still relies on manual operation, is time-consuming, and prone to missing crucial information, ultimately failing to achieve fast and accurate code optimization.

[0020] To address the aforementioned problems, this invention provides a code optimization method to achieve accurate and efficient code optimization for code generated from large models. Figure 1 This is one of the flowcharts illustrating the code optimization method provided by this invention, such as... Figure 1 As shown, the method includes: Step 110: Perform multi-stage tests on the code to be optimized in sequence. If the test result in the non-starting stage is unsuccessful, obtain the full-link feedback information based on the test feedback information including the non-starting stage and the test feedback information before the non-starting stage.

[0021] Here, "code to be optimized" refers to source code that requires correctness verification or performance optimization. In real-world scenarios, this typically refers to software code generated by a large language model based on natural language descriptions, or complex code snippets written by developers. "Multi-stage testing" refers to a pre-defined series of verification tests with a logical order, which may include compilation testing, functional testing, and performance testing. "Non-starting stage" refers to any testing stage following the first test stage in a multi-stage testing process. For example, if the test order is compilation testing, functional testing, and performance testing, then functional testing and performance testing are both non-starting stages.

[0022] Additionally, the test feedback information here refers to various data and status information generated by the system during test execution, specifically including compiler errors, runtime logs, exception stack traces, functional test results, and performance metrics. Here, end-to-end feedback information refers to test feedback information including the current test failure phase and the preceding test phases.

[0023] Specifically, first, the code to be optimized can be obtained, and multi-stage tests can be started sequentially according to a preset order. These multi-stage tests can include compilation tests, functional tests, and performance tests. It's understandable that different test phases focus on different dimensions of code quality: compilation tests focus on syntax correctness, functional tests focus on logical completeness, and performance tests focus on execution efficiency.

[0024] During test execution, if a test fails outside the initial stage, the system doesn't simply capture the error message from that current stage. Instead, it retrieves feedback information from previous test stages and automatically adds metadata such as test type, module identifier, and timestamp to obtain end-to-end feedback information. For example, in a multi-stage test including compilation testing, functional testing, and performance testing, if the functional test fails, the system combines the feedback information from the functional test and all preceding test stages to form the end-to-end feedback information.

[0025] Understandably, even if the preceding testing phase passes, the warnings or non-fatal logs may contain hidden root causes or clues that could lead to failures in subsequent phases. This breaks the silo effect of traditional single-phase test feedback. By constructing end-to-end feedback information, it can provide the context state of previous logic verifications even when deep-seated logical errors or performance bottlenecks occur in the code. This provides complete data support with temporal correlation and logical dependencies for subsequent accurate diagnosis.

[0026] Step 120: Extract information from the full-link feedback information to obtain key information of the entire link.

[0027] Here, key end-to-end information refers to core feature data that is highly correlated with code defects, extracted from the original, complex end-to-end feedback information, such as specific error stacks, key function names that cause performance bottlenecks, and abnormal boundary value inputs.

[0028] Specifically, since end-to-end feedback information may contain a large amount of redundant information, such as routine system printouts and duplicate success status codes, direct use may lead to noise interference in subsequent processing. Therefore, end-to-end feedback information can be cleaned and filtered using pre-defined rule template matching or semantic analysis techniques to obtain key end-to-end information. For example, key call chains in the error stack can be identified and retained, while generic system environment loading logs can be removed; or specific time consumption data exceeding thresholds in performance testing and their corresponding code module identifiers can be extracted.

[0029] It should be noted that by performing semantic compression and feature extraction on multi-source end-link feedback information, high-value end-link key information can be obtained, thereby improving the accuracy and efficiency of subsequent code optimization in large language models.

[0030] Step 130: Based on the key information of the entire link, obtain the diagnostic results of the code to be optimized.

[0031] Here, the diagnostic results refer to the in-depth conclusions about code defects derived from the analysis of the entire feedback information of the input, which may include the specific location of the defect, the type of the defect, and the root cause analysis of the defect.

[0032] Specifically, code diagnostic analysis tools can be used to perform code diagnosis by combining key information from the entire testing process. These tools can be large language models. More specifically, the key information from the entire testing process can be constructed into prompts that the large language model can understand. These prompts can include task instructions, code snippets to be optimized, and key information from the entire testing process. The task instruction could be something like, "Please analyze the following test feedback and locate the error." Then, these prompts are input into a pre-trained large language model. By leveraging the powerful semantic understanding and logical reasoning capabilities of the large language model, joint analysis of key information across different testing stages can be performed to obtain diagnostic results for non-initial stages where the current test fails.

[0033] For example, suppose the code to be optimized passes the compilation test, but the compiler outputs a warning message about "implicit type conversion of variables may result in precision loss"; subsequently, it fails during functional testing, with feedback indicating that "the actual output of a certain numerical calculation test case has a slight deviation from the expected result." At this point, by analyzing key information throughout the entire process, a causal relationship can be established between the "precision loss warning" in the compilation phase and the "numerical deviation error" in the functional testing phase. This allows us to infer that the root cause of the functional test failure is not simply an algorithmic logic error, but rather a precision loss problem caused by improper data type definition. Therefore, a precise diagnostic result can be output: "Change the relevant variables from single-precision floating-point to double-precision floating-point."

[0034] Step 140: Based on the diagnostic results, perform code optimization on the code to be optimized.

[0035] Specifically, diagnostic results can be translated into code generation guidelines, which can then be requested from the large language model or a specialized code generation tool. For example, the large language model can use the error locations and modification suggestions indicated in the diagnostic results, such as "optimize the sorting algorithm to reduce time complexity" or "correct the array out-of-bounds judgment logic," to rewrite or patch the code to be optimized, thereby achieving code optimization.

[0036] The method provided in this invention involves sequentially performing multi-stage tests on the code to be optimized. If the test results in a non-initial stage fail, end-to-end test feedback information is obtained based on the test feedback information including that from the non-initial stage and earlier stages. Information is extracted from this end-to-end feedback information to obtain key end-to-end information. A diagnostic result is obtained based on this key end-to-end information. Based on the diagnostic result, code optimization is performed on the code to be optimized. This method breaks the silo effect of traditional single-stage test feedback. By integrating end-to-end test feedback information from multiple stages, it provides a complete and accurate contextual diagnostic basis for code diagnosis, achieving more accurate, comprehensive, and efficient code diagnosis, thereby improving the quality and efficiency of code optimization.

[0037] To further improve the efficiency and accuracy of code optimization, based on any of the above embodiments, step 130 includes: The key information of the entire link is input into a large language model, and semantic understanding and logical reasoning are performed on the key information of the entire link based on the large language model to obtain the diagnostic results. The diagnostic results include the defect code, the defect type of the defect code, and defect optimization suggestions; The defect code includes error code segments and / or inefficient code segments.

[0038] Here, "large language model" refers to a deep learning model that has been pre-trained on massive amounts of code data and natural language data. It has the ability to understand complex code logic, associate contextual information, and reason about abstract causal relationships.

[0039] Here, defective code refers to a specific qualitative and locational description of a problem in the code. Defective code includes erroneous code segments and / or inefficient code segments. Erroneous code segments refer to specific lines or blocks of code that cause program execution errors or logical anomalies, such as code that exhibits runtime errors during compilation testing or functional testing. Inefficient code segments refer to code fragments that, while capable of running, are inefficient, consume excessive resources, or do not conform to best practices, such as inefficient operators or unreasonable calculation paths identified during performance testing.

[0040] Additionally, here, "defect type" refers to the classification of defective code, such as syntax errors, logical errors, interface mismatches, missing boundary conditions, operator performance bottlenecks, etc. "Defect optimization suggestions" here refer to specific improvement directions or optimization strategies proposed for the identified problems, which may include optimization ideas, code-level modification suggestions, or design adjustment plans.

[0041] Specifically, firstly, the cleaned and standardized end-to-end key information can be used as input data and fed into a large language model. The large language model then utilizes its attention mechanism to jointly focus on error stacks, performance metrics, and corresponding source code fragments within the end-to-end key information—that is, to perform semantic understanding and logical reasoning—to obtain fine-grained diagnostic results. Semantic understanding refers to the large language model's ability to analyze the discrepancy between the code execution intent and the actual runtime state implied in the end-to-end key information. Logical reasoning refers to the large language model's ability to establish connections between causal relationships in test feedback and specific loop structures or resource call logic in the code, thereby deriving the root cause of the problem.

[0042] Understandably, through this deep semantic understanding and logical reasoning, based on a large language model that transcends the limitations of a single testing phase, the apparent error messages are restored to causal relationships at the code logic level, thereby outputting diagnostic results with high confidence.

[0043] When generating diagnostic results based on a large language model, structured, fine-grained information is output to construct fine-grained diagnostic results. First, the large language model can precisely pinpoint the location of the problem, identifying erroneous and / or inefficient code segments. For example, it might indicate that a conditional statement in line 50 caused functional failure, thus identifying an erroneous code segment. Similarly, it might simultaneously indicate that a nested loop in line 80 caused slow processing speed, thus identifying an inefficient code segment. It's worth noting that by distinguishing between defective and inefficient code segments, not only is the correctness of the code addressed, but also its runtime performance, achieving multi-dimensional quality assurance.

[0044] Furthermore, each identified code segment can be labeled with a specific defect type using a large language model. Examples include syntax errors, logical errors, interface mismatches, missing boundary conditions, and operator performance bottlenecks. Understandably, by outputting diagnostic results with inherent fine-grained defect types, more precise defect localization and root cause analysis can be achieved, quickly identifying boundary condition errors, logical defects, and operator performance bottlenecks. This provides targeted and context-consistent optimization directions for code optimization, thereby effectively improving diagnostic quality and optimization reliability.

[0045] Finally, a targeted optimization suggestion can be generated for each defective code segment using a large language model. For example, for inefficient code segments, it might suggest "using dynamic programming to optimize the algorithm structure," or for logical errors, it might suggest "adding validation for non-empty input parameters." It's worth noting that providing specific optimization suggestions directly offers clear directions and references for subsequent code optimization steps, effectively avoiding blind attempts during the optimization process and significantly improving the targeting and success rate of code optimization.

[0046] It should be noted that, compared to vague error messages, this embodiment of the invention achieves accurate root cause localization by outputting fine-grained diagnostic results that include error code segments, inefficient code segments, and defect types.

[0047] The method provided in this invention leverages the strong semantic understanding capabilities of a large language model to deeply analyze end-to-end test feedback, outputting fine-grained diagnostic results covering defect localization, type classification, and optimization strategies. This accurately identifies deep-seated logical vulnerabilities and performance bottlenecks, providing actionable optimization guidance. Code optimization no longer relies on manual experience but is driven by intelligent root cause analysis. This significantly improves the accuracy of defect diagnosis and accelerates the iterative code optimization process through explicit optimization suggestions, achieving efficient and accurate automatic code optimization and significantly enhancing the overall efficiency of software development.

[0048] To address the information overload problem in massive test logs, based on any of the above embodiments, step 120 includes: According to the preset key feedback indicators, the feedback information corresponding to the key feedback indicators is extracted from the full-link feedback information to obtain the full-link key information. The key feedback metrics include at least one of the following: exception description information, error stack information, key performance metrics, and affected code module identifiers.

[0049] Here, key feedback metrics refer to pre-defined core feature dimensions used to measure whether the code is running normally and whether its performance meets the standards. They serve as the basis for filtering high-value information. Key feedback metrics include at least one of the following: exception description information, error stack information, key performance indicators, and affected code module identifiers.

[0050] The exception description information refers to a natural language summary or standard error code description of the reason for the test failure, such as "array index out of bounds exception," "connection timeout," or "assertion failed: expected value does not match actual value." The error stack trace refers to the function call sequence recorded when the program throws an exception, reflecting the context path and specific line number where the error occurred. Key performance indicators (KPIs) refer to quantitative data collected during the performance testing phase, such as function execution time, peak memory usage, cache hit rate, CPU utilization, or database query latency. The affected code module identifier refers to the name of the specific code unit that exhibits an anomaly during testing, such as a class name, function name, or file name.

[0051] Specifically, since the end-to-end feedback information typically contains a large amount of redundant data such as system debugging logs and environment initialization information, directly inputting it into a large language model would result in invalid context usage. Specifically, the test feedback information corresponding to the compilation and testing phase includes compilation logs, error codes, and syntax check information; the test feedback information corresponding to the functional testing phase includes unit test and integration test results and exception stack information; and the test feedback information corresponding to the performance testing phase includes execution time and cache hit rate.

[0052] Therefore, a pre-defined set of rule templates or regular expression matching logic can be used as a filtering filter. By traversing the entire feedback chain, each log or data record can be checked to determine if it contains or matches key feedback metrics. For example, lines in the log containing keywords such as "Error," "Exception," or "Fail" can be scanned, and the following text can be extracted as exception description information; text blocks conforming to the stack trace format can be identified as error stack information; entries with numerical values ​​exceeding a preset threshold can be extracted from performance monitoring logs as key performance indicators; and based on the stack information or test reports, the specific function names involved can be parsed as identifiers of the affected code modules. Finally, these extracted highly relevant fragments are combined into structured, end-to-end key information.

[0053] It should be noted that by setting key feedback indicators for targeted information extraction, semantic compression and denoising of key information from multiple sources across the entire information chain were achieved. On the one hand, this significantly reduced the interference of unnecessary information on the attention mechanism of the large language model, enabling the large language model to focus on core features directly related to defects, such as specific error stacks and performance bottleneck data, thereby significantly improving the accuracy of defect diagnosis.

[0054] On the other hand, by extracting standardized key indicators, unstructured logs are transformed into structured prompts that are easier for large language models to understand, which effectively reduces the inference difficulty and computational overhead of the model and lays a data foundation for generating high-quality optimization suggestions in the future.

[0055] The method provided in this invention accurately extracts end-to-end feedback information based on key indicators such as exception description, error stack, performance metrics, and code module identifiers. This enables the rapid extraction of the most diagnostically valuable feature information from complex end-to-end data, ensuring that the data input to the large language model has both a complete logical context and removes redundant noise. This improves overall processing efficiency while maintaining diagnostic accuracy, allowing the large language model to more sensitively capture fine-grained defects and performance risks in the code.

[0056] To address the pain point that complex code problems are difficult to optimize in one go, based on any of the above embodiments, step 140 includes: Based on the diagnostic results, code optimization is performed on the code to be optimized to obtain the optimized code to be verified in the current round; The code to be optimized in the current round is used as the code to be optimized in the next round. Multi-stage tests are repeatedly performed on the code to be optimized. If the test result in the non-initial stage is unsuccessful, the code to be optimized is optimized based on the full-link feedback information in the next round until the optimized code passes the multi-stage tests.

[0057] Specifically, firstly, based on the diagnostic results output by the large language model, the code to be optimized is logically corrected or its performance optimized, generating the current round of optimized code to be verified.

[0058] It should be noted that the code to be optimized in the current round refers to the intermediate version of code generated after modifications based on the current diagnostic results during a single iteration. Understandably, while this code resolves the issues identified in the previous round of testing, it still needs to be verified to ensure no new errors have been introduced and that all quality standards are met. The next round here refers to a new testing and optimization cycle within the iterative optimization process, thereby achieving an automated cycle of testing, diagnosis, optimization, and retesting.

[0059] Next, instead of directly outputting the code to be optimized in the current round as the final result, it is treated as new code to be optimized and re-entered into the multi-stage testing process described in step 110. In the next round of testing, if the code still fails in the non-initial stage, or if the modification causes new functional tests to fail, the full-link feedback information of the next round can be collected again, and subsequent extraction and diagnostic steps can be performed to optimize the code to be optimized in that round.

[0060] Understandably, this process will be repeated until the code to be optimized passes all the pre-set multi-stage tests, including compilation tests, functional tests, and performance tests, in a complete testing process. At this point, the code is considered to have met the pre-set verification requirements and can be output as the final optimized code.

[0061] In one embodiment, Figure 2 This is a comparative diagram of performance changes in code iteration optimization provided by the present invention, such as... Figure 2 As shown in the figure, a line graph visually illustrates the evolution trend of code execution performance speedup under multiple rounds of optimization iterations. The horizontal axis represents the iteration rounds, and the vertical axis represents the performance speedup. Two baselines are set in the graph: the orange dashed line representing the optimization level of human experts (Human expert) and the blue dashed line representing the optimization level of the standard compiler (Compiler-O3). Simultaneously, the graph uses green and red solid lines to show the performance change curves of the code optimization method provided in this application, taking OpenAI o3 and Gemini 2.5 Pro as the underlying large language model as examples. As shown in the figure, in the initial iteration stage, the code may experience compilation failures or low performance; the "x" mark in the graph indicates that the test result for this stage is unsuccessful.

[0062] However, thanks to the closed-loop optimization mechanism of testing, diagnosis, optimization, and verification proposed in this application, fine-grained diagnosis and targeted optimization code are generated based on full-link test feedback information through a large language model. As the number of iterations increases, the code performance shows a significant step-like upward trend, successfully surpassing the optimization level of human experts around the 5th and 8th rounds, and finally stabilizing in a high performance range.

[0063] thus, Figure 2 It can be used to verify the multi-stage test feedback and closed-loop iterative optimization method provided in this application, which can effectively solve the defects and omissions in traditional methods and achieve continuous improvement in code quality and running performance.

[0064] It should be noted that, compared to traditional one-time code optimization, which often results in the introduction of new bugs after optimizing one, or performance degradation after optimizing logical errors, the method provided in this invention ensures that every code change undergoes rigorous end-to-end verification through multiple rounds of testing, diagnosis, optimization, and verification. This achieves continuous improvement and convergence in code quality, ultimately delivering highly robust code that has undergone multiple verifications. This avoids potential defects and performance degradation that may occur in the traditional linear "test-optimize-retest" process, achieving a steady improvement in code quality and performance, and significantly shortening the development cycle.

[0065] Based on any of the above embodiments, and based on the diagnostic results, code optimization is performed on the code to be optimized to obtain the code to be verified in the current round, including: Based on the diagnostic results, an optimized prompt text is generated; The optimization prompt text and the code to be optimized are input into the code generation model to obtain the code to be optimized for the current round generated by the code generation model; The code generation model is trained based on a general large language model.

[0066] Here, optimizing the prompt text refers to converting the diagnostic results into natural language or structured instructions that the code generation model can understand. For example, the prompt text could be designed as: "Code snippet A contains a logical error because the pointer was not checked for null. Please refer to the following optimization suggestions: Add a null check before accessing the pointer and regenerate the function's code." Furthermore, the code generation model here refers to a deep learning model with code completion, refactoring, and generation capabilities. This model can be based on a general-purpose language model that has undergone fine-tuning of the code corpus or training using reinforcement learning, making it more adept at handling the syntactic structure and logical implementation of programming languages.

[0067] Specifically, the diagnostic results can first be parsed to extract the defect location, defect type, and specific optimization suggestions. Then, this information is filled into a preset prompt template to construct a clearly targeted optimization prompt text.

[0068] Next, the optimization hint text can be input along with the original code to be optimized into the code generation model. After understanding the optimization intent in the hint text, the code generation model, combined with the context of the code to be optimized, uses its generation capabilities to output the corrected code snippet. This output is the optimized code to be verified in the current round.

[0069] The method provided in this invention leverages a code generation model trained on a general-purpose large language model, fully utilizing the programming standards and best practices learned from massive codebases. This not only ensures the syntactic correctness of the optimized code but also generates stylistically sound and highly readable code. Furthermore, by constructing precise optimization hint text, it effectively guides the model's generation direction, enabling the generated code to accurately solve specific diagnosed problems, thus achieving intelligent code optimization.

[0070] Based on any of the above embodiments, the multi-stage test includes at least two of the following: compilation testing, functional testing, and performance testing; The execution order of the multi-stage test is as follows: the compilation test, the functional test, and the performance test are executed sequentially. The next stage of testing is performed if the previous stage of testing passes.

[0071] Here, compilation testing is used to check for fundamental errors in the code, such as syntax errors, undefined symbols, and type mismatches, that prevent the generation of an executable program. Functional testing refers to running pre-defined test cases, such as unit tests and integration tests, to verify whether the business logic and input / output results of the code meet the expected design. Performance testing refers to monitoring the resource consumption and response time during code execution to determine whether there are efficiency bottlenecks or resource leaks. It is understandable that the phased tests included in the multi-phase testing here can be defined according to actual needs; it can be any combination of multiple defined phased tests to adapt to different verification requirements.

[0072] Specifically, after the code to be optimized enters the testing process, compilation testing is initiated first. If the compiler reports an error and the test fails, the process immediately terminates the subsequent tests in the current round and enters the diagnostic optimization phase, because code that cannot be compiled cannot run functionally. Only when the compilation test returns a successful status will the next stage of functional testing be automatically triggered.

[0073] Similarly, in the functional testing phase, test cases can be run. If logical errors or assertion failures are found, the process stops, and end-to-end feedback information is generated directly based on the test feedback from the current and previous phases. Only when all functional test cases pass, confirming the correctness of the code logic, will the final performance testing phase begin. In the performance testing phase, the code can be subjected to stress or run for extended periods to collect performance metrics. If the metrics fail to meet the standards, the test is considered a failure, and the optimization process begins.

[0074] It should be noted that by establishing a hierarchical, multi-stage testing pipeline with strict dependencies, rapid defect interception and layered filtering are achieved. First, this sequential execution mechanism avoids blindly performing expensive functional or performance tests when low-level syntax errors exist in the code, saving computational resources and testing time. Second, it ensures the logical coherence of feedback information throughout the entire process. That is, when code fails in the performance testing phase, it implicitly carries the prior knowledge that its syntax is correct and its logical functionality has been verified. This provides highly valuable contextual constraints for the large language model, allowing it to diagnose performance problems without questioning the correctness of basic syntax or business logic, thus enabling more focused and precise identification of deep-seated performance bottlenecks.

[0075] It should also be noted that by uniformly collecting and standardizing feedback information from different stages such as compilation testing, functional testing, and performance testing, a multi-source contextual data representation that can be directly parsed by a large language model is constructed, thus obtaining end-to-end feedback information. Compared to traditional single-stage or linear analysis processes, the method provided in this embodiment of the invention can integrate multi-dimensional test results at once, achieving simultaneous capture of compilation errors, functional anomalies, and performance bottlenecks, significantly reducing the workload of manual screening and step-by-step analysis, thereby significantly improving the completeness and accuracy of defect diagnosis.

[0076] The method provided in this invention constructs a testing strategy that conforms to the laws of software development by defining the execution order as sequentially executing compilation tests, functional tests, and performance tests. This not only improves the efficiency of test execution and avoids invalid tests, but also provides a well-structured and clearly defined feedback data chain for subsequent diagnostic steps through phased pass criteria, thereby achieving highly accurate automated code optimization.

[0077] Based on any of the above embodiments Figure 3 This is the second flowchart illustrating the code optimization method provided by this invention, as shown below. Figure 3 As shown, the method begins with the initial step, obtaining the initial source code. This source code, as the code to be optimized, enters a multi-stage testing process.

[0078] First, compilation tests are performed to check the source code for syntax errors or missing dependencies. Then, the compilation tests are evaluated to determine if they pass. If the result is negative, a structured defect report is generated through multi-source feedback semantic extraction and fine-grained diagnosis driven by a large model; this is the diagnostic result. It's understood that the diagnosis here is based solely on test feedback information from the compilation test phase. Next, the process flows to the LLM code regeneration module, where the large language model generates optimized code to be verified based on the structured defect report and feeds it back to the test starting point for recompilation testing. If the result is positive, it indicates that the code syntax is correct, and the process proceeds to the next stage.

[0079] Next, functional tests are performed, which can be run using pre-defined test cases to verify the code logic. Then, the pass / fail status of the functional tests is determined. If the result is negative, the core multi-source feedback semantic extraction and large-model-driven fine-grained diagnosis are executed, generating a structured defect report, i.e., generating the diagnostic results. It should be noted that the generation of the structured defect report at this stage utilizes not only the feedback from the current functional tests but also the test feedback information from the preceding compilation phase. Subsequently, the structured defect report is sent to the LLM code regeneration module to guide the generation of new, optimized code to be verified, triggering a new round of testing. If the functional test result is positive, it indicates that the code logic is correct, and the process continues.

[0080] Subsequently, performance testing is performed to evaluate the code's execution efficiency and resource consumption. The performance test results are then used to determine if the code meets the required standards. If the result is negative, it indicates a performance bottleneck. In this case, a backtracking process is initiated to perform multi-source feedback semantic extraction and fine-grained diagnostics driven by a large model, generating a structured defect report—the diagnostic result. This diagnostic result involves a deep root cause analysis combining performance test feedback with information from previous testing phases, generating optimization suggestions for performance improvement. The structured defect report is then input into the LLM code regeneration module to generate new optimized code for verification, and testing is restarted.

[0081] Finally, only when the source code passes the compilation test, functional test, and performance test in sequence—that is, when the result of each test stage is positive—will the output step be executed, outputting code that is functionally correct and meets performance standards. At this point, the entire code optimization process is successfully completed and enters the final state.

[0082] Based on any of the above embodiments Figure 4 This is a schematic diagram of the code optimization device provided by the present invention, as shown below. Figure 4 As shown, the device includes: The acquisition unit 410 performs multi-stage tests on the code to be optimized in sequence. If the test result in the non-starting stage is unsuccessful, the end-to-end feedback information is obtained based on the test feedback information including the non-starting stage and the test feedback information before the non-starting stage. Extraction unit 420 extracts information from the end-to-end feedback information to obtain key end-to-end information; The diagnostic unit 430 obtains the diagnostic results of the code to be optimized based on the key information of the entire link; The optimization unit 440 performs code optimization on the code to be optimized based on the diagnostic results.

[0083] The apparatus provided in this invention performs multi-stage tests sequentially on the code to be optimized. If the test result in a non-initial stage fails, it obtains end-to-end test feedback information based on test feedback information including that from the non-initial stage and earlier stages. Information is extracted from the end-to-end feedback information to obtain key end-to-end information. A diagnostic result is obtained based on this key end-to-end information. Based on the diagnostic result, code optimization is performed on the code to be optimized. This breaks the silo effect of traditional single-stage test feedback. By integrating end-to-end test feedback information from multiple stages, it provides complete and accurate contextual diagnostic basis for code diagnosis, achieving more accurate, comprehensive, and efficient code diagnosis, thereby improving the quality and efficiency of code optimization.

[0084] Based on any of the above embodiments, the diagnostic unit is specifically used for: The key information of the entire link is input into a large language model, and semantic understanding and logical reasoning are performed on the key information of the entire link based on the large language model to obtain the diagnostic results. The diagnostic results include the defect code, the defect type of the defect code, and defect optimization suggestions; The defect code includes error code segments and / or inefficient code segments.

[0085] Based on any of the above embodiments, the extraction unit is specifically used for: According to the preset key feedback indicators, the feedback information corresponding to the key feedback indicators is extracted from the full-link feedback information to obtain the full-link key information. The key feedback metrics include at least one of the following: exception description information, error stack information, key performance metrics, and affected code module identifiers.

[0086] Based on any of the above embodiments, the optimization unit is specifically used for: Based on the diagnostic results, code optimization is performed on the code to be optimized to obtain the optimized code to be verified in the current round; The code to be optimized in the current round is used as the code to be optimized in the next round. Multi-stage tests are repeatedly performed on the code to be optimized. If the test result in the non-initial stage is unsuccessful, the code to be optimized is optimized based on the full-link feedback information in the next round until the optimized code passes the multi-stage tests.

[0087] Based on any of the above embodiments, the optimization unit is further specifically used for: Based on the diagnostic results, an optimized prompt text is generated; The optimization prompt text and the code to be optimized are input into the code generation model to obtain the code to be optimized for the current round generated by the code generation model; The code generation model is trained based on a general large language model.

[0088] Based on any of the above embodiments, the multi-stage test includes at least two of the following: compilation testing, functional testing, and performance testing; The execution order of the multi-stage test is as follows: the compilation test, the functional test, and the performance test are executed sequentially. The next stage of testing is performed if the previous stage of testing passes.

[0089] Figure 5 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 5 As shown, the electronic device may include a processor 510, a communication interface 520, a memory 530, and a communication bus 540, wherein the processor 510, the communication interface 520, and the memory 530 communicate with each other via the communication bus 540. The processor 510 can call logical instructions in the memory 530 to execute a code optimization method. This method includes: sequentially performing multi-stage tests on the code to be optimized; if the test result in a non-initial stage is a failure, obtaining end-to-end feedback information based on test feedback information including the non-initial stage and previous test results; extracting information from the end-to-end feedback information to obtain end-to-end key information; obtaining a diagnostic result for the code to be optimized based on the end-to-end key information; and performing code optimization on the code to be optimized based on the diagnostic result.

[0090] Furthermore, the logical instructions in the aforementioned memory 530 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or a 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 the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0091] On the other hand, the present invention also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the code optimization method provided by the above methods. The method includes: sequentially performing multi-stage tests on the code to be optimized; if the test result in the non-initial stage is unsuccessful, obtaining end-to-end feedback information based on test feedback information including the non-initial stage and the test before the non-initial stage; extracting information from the end-to-end feedback information to obtain end-to-end key information; obtaining a diagnostic result of the code to be optimized based on the end-to-end key information; and performing code optimization on the code to be optimized based on the diagnostic result.

[0092] In another aspect, the present invention also provides a non-transitory computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the code optimization method provided by the above methods. The method includes: sequentially performing multi-stage tests on the code to be optimized; if the test result in a non-initial stage is a failure, obtaining end-to-end feedback information based on test feedback information including the non-initial stage and preceding test results; extracting information from the end-to-end feedback information to obtain end-to-end key information; obtaining a diagnostic result for the code to be optimized based on the end-to-end key information; and performing code optimization on the code to be optimized based on the diagnostic result.

[0093] The device embodiments described above are merely illustrative. 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 modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.

[0094] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, 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 can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.

[0095] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A code optimization method, characterized in that, include: Multi-stage tests are performed sequentially on the code to be optimized. If the test result in a non-starting stage fails, full-link feedback information is obtained based on the test feedback information including the non-starting stage and the test feedback information before the non-starting stage. Information is extracted from the end-to-end feedback information to obtain key end-to-end information; Based on the key information across the entire chain, the diagnostic results for the code to be optimized are obtained; Based on the diagnostic results, code optimization is performed on the code to be optimized.

2. The code optimization method according to claim 1, characterized in that, The diagnostic results for the code to be optimized, obtained based on the key information across the entire link, include: The key information of the entire link is input into a large language model, and semantic understanding and logical reasoning are performed on the key information of the entire link based on the large language model to obtain the diagnostic results. The diagnostic results include the defect code, the defect type of the defect code, and defect optimization suggestions; The defect code includes error code segments and / or inefficient code segments.

3. The code optimization method according to claim 1, characterized in that, The step of extracting information from the end-to-end feedback information to obtain key end-to-end information includes: According to the preset key feedback indicators, the feedback information corresponding to the key feedback indicators is extracted from the full-link feedback information to obtain the full-link key information. The key feedback metrics include at least one of the following: exception description information, error stack information, key performance metrics, and affected code module identifiers.

4. The code optimization method according to any one of claims 1 to 3, characterized in that, The step of performing code optimization on the code to be optimized based on the diagnostic results includes: Based on the diagnostic results, code optimization is performed on the code to be optimized to obtain the optimized code to be verified in the current round; The code to be optimized in the current round is used as the code to be optimized in the next round. Multi-stage tests are repeatedly performed on the code to be optimized. If the test result in the non-initial stage is unsuccessful, the code to be optimized is optimized based on the full-link feedback information in the next round until the optimized code passes the multi-stage tests.

5. The code optimization method according to claim 4, characterized in that, Based on the diagnostic results, code optimization is performed on the code to be optimized to obtain the code to be verified in the current round, including: Based on the diagnostic results, an optimized prompt text is generated; The optimization prompt text and the code to be optimized are input into the code generation model to obtain the code to be optimized for the current round generated by the code generation model; The code generation model is trained based on a general large language model.

6. The code optimization method according to any one of claims 1 to 3, characterized in that, The multi-stage testing includes at least two of the following: compilation testing, functional testing, and performance testing. The execution order of the multi-stage test is as follows: the compilation test, the functional test, and the performance test are executed sequentially. The next stage of testing is performed if the previous stage of testing passes.

7. A code optimization device, characterized in that, include: The acquisition unit performs multi-stage tests on the code to be optimized in sequence. If the test result in a non-starting stage is unsuccessful, it obtains full-link feedback information based on the test feedback information including the non-starting stage and the test feedback information before the non-starting stage. The extraction unit extracts information from the end-to-end feedback information to obtain key information about the entire link. The diagnostic unit obtains the diagnostic results of the code to be optimized based on the key information across the entire link. The optimization unit performs code optimization on the code to be optimized based on the diagnostic results.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the code optimization method as described in any one of claims 1 to 6.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the code optimization method as described in any one of claims 1 to 6.

10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the code optimization method as described in any one of claims 1 to 6.

Citation Information

Patent Citations

  • Code optimization method and device based on artificial intelligence and storage medium

    CN117492760A

  • Multi-round reasoning and feedback optimization method and system for industrial software codes

    CN119440539A

  • Heterogeneous integration test method based on AI

    CN120631724A

  • Full-process intelligent unit testing method driven by large language model

    CN120687368A

  • Code automatic optimization and Bug solution system

    CN120849246A