A cross-language code optimization method and related device
By employing cross-language code optimization methods and utilizing large language models to identify and correct logical vulnerabilities and performance bottlenecks in multilingual projects, this approach solves the problem of efficiently identifying and resolving defects in multilingual projects in existing technologies, achieving efficient and reliable code self-optimization and quality improvement.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- NR ELECTRIC CO LTD
- Filing Date
- 2026-03-23
- Publication Date
- 2026-06-19
Smart Images

Figure CN122241715A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of software security, in particular to a cross-language code optimization method and related equipment. BACKGROUND
[0002] In the software development process, code defect detection and repair is a key element to ensure software quality. With the expansion of software system size and the increase of programming language diversity, traditional manual review or rule-based static analysis tools are difficult to meet the demand of efficiently and accurately identifying and solving code problems.
[0003] Since static analysis tools are often limited to a single language, it is difficult to capture logical vulnerabilities and performance bottlenecks in multi-language mixed projects. Meanwhile, manual code review is inefficient and prone to missing context-related issues. Moreover, existing methods lack a self-optimization mechanism for Large Language Model (LLM) generated suggestions, making it difficult to implement optimization solutions. The present application aims to solve the above problems and achieve efficient and reliable cross-language code self-optimization. SUMMARY
[0004] The present application aims to provide a cross-language code optimization method and related equipment, which can locate defects and correct code for multi-language code, and improve the overall quality and development efficiency of software.
[0005] To achieve the above-mentioned purpose, the present application is implemented by using the following technical solutions:
[0006] In a first aspect, the present application provides a cross-language code optimization method, which comprises:
[0007] Obtaining source code to be optimized;
[0008] Performing slicing processing on the source code to obtain a plurality of code segments and context summary information corresponding to each code segment;
[0009] Inputting the source code into a defect detection module to obtain a preliminary defect detection result;
[0010] Based on the preliminary defect detection result, code segment and context summary information, constructing a first prompt word, inputting the first prompt word into a large language model to perform a first processing task, the first processing task being to identify potential logical vulnerabilities in the code segment, and obtaining a final defect detection result;
[0011] Based on the final defect detection result, constructing a second prompt word, inputting the second prompt word into the large language model to perform a second processing task, the second processing task being to generate code optimization suggestions and correct code segments;
[0012] Based on the code optimization suggestions, the self-optimization module locates the defective location in the source code and replaces the source code at the defective location with the corrected code snippet.
[0013] Further, the step of slicing the source code to obtain multiple code segments and context summary information corresponding to each code segment includes:
[0014] Based on the syntax structure of the source code, the source code is divided into multiple code segments;
[0015] Construct the data flow graph and function call graph of the source code;
[0016] Based on the data flow graph and function call graph, the code fragments are analyzed and identified to obtain context summary information corresponding to each code fragment;
[0017] Each code segment contains no more than the size of a preset large language model context window, and the code elements within each code segment maintain a complete semantic context.
[0018] Furthermore, the defect detection module includes a static analysis unit and a compiler analysis unit;
[0019] The step of inputting the source code into the defect detection module to obtain preliminary defect detection results includes:
[0020] The source code is statically analyzed by the static analysis unit to detect the first type of defect;
[0021] The source code is compiled by a compiler analysis unit, and second-type defects are detected and performance bottlenecks are identified during the compilation process.
[0022] By combining the first type of defect, the second type of defect, and the performance bottleneck, preliminary defect detection results are obtained.
[0023] Furthermore, the second prompt is text in Markdown format, including the code snippet to be detected, defect context information, defect type, and location information.
[0024] Further, before locating the defect location in the source code using the self-optimization module based on the code optimization suggestions and replacing the source code at the defect location with the corrected code snippet, the process includes:
[0025] Copy the source code to the preset working directory according to the original file directory structure, and switch the working path of the self-optimization module to the preset working directory.
[0026] Furthermore, the step of locating the defect location in the source code through the self-optimization module based on the code optimization suggestions and replacing the source code at the defect location with the corrected code fragment further includes:
[0027] The code optimization suggestions are parsed using regular expressions to generate structured optimization instructions, wherein the optimization instructions include the file name to be corrected and the line number to be corrected;
[0028] Based on the filename to be corrected, locate the corresponding source code file in the preset working directory;
[0029] Based on the line number information to be corrected, locate the specific defect location in the source code file;
[0030] Replace the source code at the located defect location with the corrected code snippet.
[0031] Secondly, this application provides a cross-language code optimization system, comprising:
[0032] The data acquisition module is used to acquire the source code to be optimized.
[0033] The data processing module is used to slice the source code to obtain multiple code segments and context summary information corresponding to each code segment;
[0034] The detection module is used to input the source code into the defect detection module to obtain preliminary defect detection results;
[0035] The defect identification module is used to construct a first prompt word based on the preliminary defect detection results, code snippets and context summary information, input the first prompt word into a large language model for a first processing task, the first processing task being to identify potential logical vulnerabilities in the code snippets and obtain the final defect detection results;
[0036] The repair suggestion generation module is used to construct a second prompt word based on the final defect detection result, and input the second prompt word into the large language model for a second processing task, which is to generate code optimization suggestions and correct code snippets.
[0037] The optimization and correction module is used to locate the defect location in the source code based on the code optimization suggestions, through the self-optimization module, and replace the source code at the defect location with the corrected code fragment.
[0038] Thirdly, this application provides an electronic device, the computer device including: a memory, a processor, and instructions stored in the memory, the instructions being loaded and executed by the processor to implement the steps of the cross-language code optimization method as described in the first aspect.
[0039] Fourthly, this application provides a computer storage medium storing instructions that are loaded and executed by a processor to implement the steps of the cross-language code optimization method as described in the first aspect.
[0040] Fifthly, this application provides a computer program product, including a computer program / instructions that, when executed by a processor, implement the steps of the cross-language code optimization method described in the first aspect.
[0041] Compared with the prior art, the beneficial effects achieved by this application are as follows:
[0042] The cross-language code optimization method provided in this application first performs preliminary defect detection through a defect detection module, and then uses a large language model to generate targeted optimization suggestions, realizing a solution for defect localization and code correction, reducing the cost of manual intervention, thereby improving software development efficiency and code quality, and solving the fragmented problem of defect detection in cross-language projects. Furthermore, a self-optimization module is used to replace defective code, improving the transparency of code modifications and review efficiency, and reducing the risk of missed detections. Attached Figure Description
[0043] Figure 1 A flowchart illustrating the steps of the cross-language code optimization method provided in this application embodiment. Detailed Implementation
[0044] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. The following description of at least one exemplary embodiment is merely illustrative and is in no way intended to limit this application or its application or use. Example 1
[0045] like Figure 1 As shown, this embodiment provides a cross-language code optimization method, which specifically includes:
[0046] Step 1: Obtain the source code to be optimized;
[0047] In this embodiment, the source code includes multiple programming languages, such as Python, Java, and C.
[0048] Step 2: Slice the source code to obtain multiple code segments and context summary information corresponding to each code segment;
[0049] In this embodiment, the specific steps of slicing the source code include:
[0050] Based on the syntax structure of the source code, the source code is divided into multiple code segments;
[0051] Construct the data flow graph and function call graph of the source code;
[0052] Based on the data flow graph and function call graph, the code fragments are analyzed and identified to obtain context summary information corresponding to each code fragment;
[0053] Each code segment contains no more than the size of a preset large language model context window, and the code elements within each code segment maintain a complete semantic context.
[0054] Furthermore, based on the data flow graph and function call graph, boundary analysis is performed on each code segment to identify the interface dependencies between each code segment, thereby generating context summary information across code segments, which is used to preserve necessary global semantic information when the large language model performs the first processing task.
[0055] Step 3: Input the source code into the defect detection module to obtain preliminary defect detection results;
[0056] In this embodiment, the defect detection module includes a static analysis unit and a compiler analysis unit, and the steps to obtain preliminary defect detection results include:
[0057] The source code is statically analyzed by the static analysis unit to detect the first type of defect;
[0058] The source code is compiled by a compiler analysis unit, and second-type defects are detected and performance bottlenecks are identified during the compilation process.
[0059] By combining the first type of defect, the second type of defect, and the performance bottleneck, preliminary defect detection results are obtained.
[0060] Specifically, the static analysis unit can use static code analysis tools to perform lexical, syntactic, and semantic static analysis of the source code, identifying Type I defects without executing the program. Type I defects specifically refer to deterministic coding errors that can be clearly identified through analysis of code structure, data flow, and control flow. These include:
[0061] Resource management errors: For example, the risk of memory leaks due to a failure to match a deallocation operation on the function exit path; unreleased pointers;
[0062] Coding style violations: For example, not conforming to the project's agreed naming rules, code format, function length, or comment requirements;
[0063] Logical inconsistency issues: For example, variables are not initialized before use; there are code blocks that can never be executed; or there is logical redundancy in the expressions in conditional statements.
[0064] The compiler analysis unit identifies the second type of defects by executing the code program through the compiler. The second type of defects specifically includes detecting syntax errors and logical loopholes such as undefined variables, uninitialized variables, type mismatches, and null pointer references. At the same time, the compiler analysis unit also has optimization analysis functions, which identify potential performance bottlenecks in the source code, such as redundant calculations and inefficient loops.
[0065] Step 4: Construct a first prompt word based on the preliminary defect detection results, code snippets, and context summary information. Input the first prompt word into the large language model for the first processing task, which is to identify potential logical vulnerabilities in the code snippets and obtain the final defect detection results.
[0066] In this embodiment, the large language model can be the GPT series, Deepseek series, Qwen series, etc. These large language models have powerful language understanding and generation capabilities, and can perform in-depth analysis of the code to be optimized based on the input prompts.
[0067] Specifically, the initial defect detection results, code snippets, and contextual summary information are used to construct the first prompt words, which are then input into the large language model for the first processing task. This first processing task primarily performs defect identification. By receiving the initial defect detection results from the defect detection module and combining them with the contextual semantic analysis technology of the large language model, deep reasoning is performed on the code snippets based on the contextual summary information obtained after slicing the source code. Leveraging the reasoning capabilities of the large language model, complex logical defects hidden in the code snippets are identified, such as concurrency race conditions, resource leaks, and security vulnerabilities. Furthermore, the pattern recognition and performance analysis libraries of the large language model are used to perform deep analysis of the source code, identifying issues such as excessive algorithm time complexity, inefficient memory usage, and insufficient database query optimization. This yields the final defect detection results.
[0068] Step 5: Construct a second prompt word based on the final defect detection result, and input the second prompt word into the large language model for the second processing task, which is to generate code optimization suggestions and correct code snippets;
[0069] In this embodiment, the second prompt is text in Markdown format, including the code snippet to be detected, defect context information, defect type, and location information.
[0070] The second prompt word is input into the large language model for a second processing task, which primarily involves code optimization. Based on the second prompt word, the large language model outputs modification suggestions for each code snippet in Markdown format. Each modification suggestion includes code optimization recommendations and the corrected code snippet. Furthermore, the modification suggestions are saved as an optimization report for easy human review.
[0071] Step Six: Based on the code optimization suggestions, locate the defect location in the source code using the self-optimization module, and replace the source code at the defect location with the corrected code snippet.
[0072] In this embodiment, the source code needs to be copied to the preset working directory according to the original file directory structure, and the working path of the self-optimization module needs to be switched to the preset working directory.
[0073] After receiving code optimization suggestions and corrected code snippets generated from the second processing task based on the large language model, the code optimization suggestions are first parsed using regular expressions to generate structured optimization instructions, wherein the optimization instructions include the file name to be corrected and the line number information to be corrected;
[0074] Based on the filename to be corrected, locate the corresponding source code file in the preset working directory;
[0075] Based on the line number information to be corrected, locate the specific defect location in the source code file;
[0076] Replace the source code at the located defect location with the corrected code snippet.
[0077] In summary, the cross-language code optimization process is complete. To facilitate manual review, comparison tools (such as Beyond Compare) can be used to compare the source code in the original file directory with the corrected code in the preset working directory. This allows for a direct comparison of the differences between the source code and the corrected code during manual review, improving the visibility of code modifications and review efficiency. Furthermore, optimizing the report assists manual review, reducing the risk of omissions. Example 2
[0078] This embodiment provides a cross-language code optimization system, including:
[0079] The data acquisition module is used to acquire the source code to be optimized.
[0080] The data processing module is used to slice the source code to obtain multiple code segments and context summary information corresponding to each code segment;
[0081] The detection module is used to input the source code into the defect detection module to obtain preliminary defect detection results;
[0082] The defect identification module is used to construct a first prompt word based on the preliminary defect detection results, code snippets and context summary information, input the first prompt word into a large language model for a first processing task, the first processing task being to identify potential logical vulnerabilities in the code snippets and obtain the final defect detection results;
[0083] The repair suggestion generation module is used to construct a second prompt word based on the final defect detection result, and input the second prompt word into the large language model for a second processing task, which is to generate code optimization suggestions and correct code snippets.
[0084] The optimization and correction module is used to locate the defect location in the source code based on the code optimization suggestions, through the self-optimization module, and replace the source code at the defect location with the corrected code fragment.
[0085] The specific implementation process of each module function in this embodiment can be found in Embodiment 1, which has the same technical effect as the method provided in Embodiment 1, and will not be described in detail here. Example 3
[0086] This embodiment provides an electronic device, a computer device including: a memory, a processor, and instructions stored in the memory, the instructions being loaded and executed by the processor to implement the steps of the cross-language code optimization method as described in Embodiment 1. Example 4
[0087] This embodiment provides a computer storage medium storing instructions that are loaded and executed by a processor to implement the steps of the cross-language code optimization method as described in Embodiment 1. Example 5
[0088] This embodiment provides a computer program product, including a computer program / instruction, which, when executed by a processor, implements the steps of the cross-language code optimization method described in Embodiment 1.
[0089] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0090] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0091] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0092] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0093] The embodiments of this application have been described above with reference to the accompanying drawings. However, this application is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of this application without departing from the spirit and scope of the claims. All of these forms are within the protection scope of this application.
Claims
1. A cross-language code optimization method, characterized in that, The method includes: Obtain the source code to be optimized; The source code is sliced to obtain multiple code segments and context summary information corresponding to each code segment; The source code is input into the defect detection module to obtain preliminary defect detection results; Based on the preliminary defect detection results, code snippets, and context summary information, a first prompt word is constructed. The first prompt word is then input into a large language model for a first processing task, which is to identify potential logical vulnerabilities in the code snippets and obtain the final defect detection results. Based on the final defect detection result, a second prompt word is constructed, and the second prompt word is input into the large language model for a second processing task, which is to generate code optimization suggestions and correct code snippets. Based on the code optimization suggestions, the self-optimization module locates the defective location in the source code and replaces the source code at the defective location with the corrected code snippet.
2. The cross-language code optimization method according to claim 1, characterized in that, The step of slicing the source code to obtain multiple code segments and context summary information corresponding to each code segment includes: Based on the syntax structure of the source code, the source code is divided into multiple code segments; Construct the data flow graph and function call graph of the source code; Based on the data flow graph and function call graph, the code fragments are analyzed and identified to obtain context summary information corresponding to each code fragment; Each code segment contains no more than the size of a preset large language model context window, and the code elements within each code segment maintain a complete semantic context.
3. The cross-language code optimization method according to claim 1, characterized in that, The defect detection module includes a static analysis unit and a compiler analysis unit; The step of inputting the source code into the defect detection module to obtain preliminary defect detection results includes: The source code is statically analyzed by the static analysis unit to detect the first type of defect; The source code is compiled by a compiler analysis unit, and second-type defects are detected and performance bottlenecks are identified during the compilation process. By combining the first type of defect, the second type of defect, and the performance bottleneck, preliminary defect detection results are obtained.
4. The cross-language code optimization method according to claim 1, characterized in that, The second prompt is text in Markdown format, including the code snippet to be detected, defect context information, defect type, and location information.
5. The cross-language code optimization method according to claim 1, characterized in that, The step of locating the defect location in the source code through the self-optimization module based on the code optimization suggestions, and replacing the source code at the defect location with the corrected code snippet, includes: Copy the source code to the preset working directory according to the original file directory structure, and switch the working path of the self-optimization module to the preset working directory.
6. The cross-language code optimization method according to claim 5, characterized in that, The step of locating the defect location in the source code using a self-optimization module based on the code optimization suggestion, and replacing the source code at the defect location with the corrected code fragment, further includes: The code optimization suggestions are parsed using regular expressions to generate structured optimization instructions, wherein the optimization instructions include the file name to be corrected and the line number to be corrected; Based on the filename to be corrected, locate the corresponding source code file in the preset working directory; Based on the line number information to be corrected, locate the specific defect location in the source code file; Replace the source code at the located defect location with the corrected code snippet.
7. A cross-language code optimization system, characterized in that, include: The data acquisition module is used to acquire the source code to be optimized. The data processing module is used to slice the source code to obtain multiple code segments and context summary information corresponding to each code segment; The detection module is used to input the source code into the defect detection module to obtain preliminary defect detection results; The defect identification module is used to construct a first prompt word based on the preliminary defect detection results, code snippets and context summary information, input the first prompt word into a large language model for a first processing task, the first processing task being to identify potential logical vulnerabilities in the code snippets and obtain the final defect detection results; The repair suggestion generation module is used to construct a second prompt word based on the final defect detection result, and input the second prompt word into the large language model for a second processing task, which is to generate code optimization suggestions and correct code snippets. The optimization and correction module is used to locate the defect location in the source code based on the code optimization suggestions, through the self-optimization module, and replace the source code at the defect location with the corrected code fragment.
8. An electronic device, characterized in that, The computer device includes: a memory, a processor, and instructions stored in the memory, the instructions being loaded and executed by the processor to implement the steps of the cross-language code optimization method as described in any one of claims 1 to 6.
9. A computer storage medium, characterized in that, The storage medium stores instructions that are loaded and executed by a processor to implement the steps of the cross-language code optimization method as described in any one of claims 1 to 6.
10. A computer program product comprising a computer program / instructions, characterized in that, When the computer program / instruction is executed by the processor, it implements the steps of the cross-language code optimization method as described in any one of claims 1 to 6.