Code optimization method and computer equipment
By filtering effective historical edit and build function sequences using a large language model and combining internal and external code libraries to optimize functions, the problem of low accuracy in code performance optimization in existing technologies is solved, achieving highly efficient code optimization results.
Patent Information
- Application Number
- CN202510795689.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-13
- Publication Date
- 2025-10-17
AI Technical Summary
In the existing technology, functions after code performance optimization are difficult to achieve optimization goals, the optimization accuracy is low, and LLM cannot effectively consider the complex dependencies between functions and cross-file code optimization requirements during the code generation process.
Through the large language model (LLM), effective historical edits related to code optimization tasks are screened, a sequence of functions to be optimized is constructed, the abstract syntax tree and neural network model are combined to determine the similarity between functions, the function call graph is optimized, and internal and external code libraries are used for code optimization.
It improves the accuracy and correctness of code optimization, ensures that the optimized code is relevant to the code optimization task, and enhances code execution efficiency and correctness.
Smart Images

Figure CN120803519A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] One or more embodiments of the present specification relate to the technical field of computer application, and particularly relate to a code optimization method and a computer device. BACKGROUND
[0002] In the field of software development, code performance optimization is a key link to ensure efficient operation of the system and improve user experience. Code performance optimization is a scheme for optimizing code performance through automated programs and the like.
[0003] In the related art, there are problems that the function after code performance optimization is difficult to achieve optimization goals and the optimization accuracy is low. SUMMARY
[0004] Therefore, one or more embodiments of the present specification provide a code optimization method and a computer device.
[0005] According to a first aspect of one or more embodiments of the present specification, a code optimization method is provided, comprising:
[0006] According to the description information of the code optimization task, the effective historical editing related to the code optimization task is filtered from the historical editing corresponding to the function to be optimized by a large language model LLM; the historical editing is an editing record of the function;
[0007] According to the effective historical editing, the first editing corresponding to the code optimization task is generated for the function to be optimized by the LLM.
[0008] According to a second aspect of one or more embodiments of the present specification, a code optimization device is provided, comprising:
[0009] An effective historical editing identification module is configured to filter, by a large language model LLM, effective historical editing related to a code optimization task from historical editing corresponding to a function to be optimized according to description information of the code optimization task; the historical editing is an editing record of the function;
[0010] A function optimization module is configured to generate, by the LLM, a first editing corresponding to the code optimization task for the function to be optimized according to the effective historical editing.
[0011] According to a third aspect of an embodiment of the present specification, a computer readable storage medium is provided, which stores computer instructions, and the instructions are executed by a processor to implement the code optimization method according to the first aspect of the embodiment of the present specification.
[0012] According to a fourth aspect of an embodiment of the present specification, a computer device is provided, comprising:
[0013] A processor;
[0014] A memory for storing processor-executable instructions;
[0015] The processor implements the code optimization method according to the first aspect of the embodiments of the present specification by running the executable instructions.
[0016] According to a fifth aspect of the embodiments of the present specification, a computer program product is provided, which, when executed by a processor, implements the code optimization method according to the first aspect of the embodiments of the present specification.
[0017] The present specification provides a code optimization method. First, input the code optimization task into the LLM, and the LLM screens out effective historical edits useful for the code optimization task from the historical edits of the function to be optimized. And input the screened effective historical edits into the LLM, so that the LLM optimizes the function to be optimized according to the effective historical edits, and obtains the first edit.
[0018] The method provided by the present specification screens out historical edits related to the code optimization task according to the code optimization task. Thus, it is ensured that the historical edits used by the LLM for optimization are related to the code optimization task, and other irrelevant historical edits are prevented from interfering with the code optimization task, ensuring the correctness of the syntax and function of the optimized code, and improving the relevance of the optimized code to the code optimization task.
[0019] It should be understood that the above general description and the following detailed description are only exemplary and explanatory, and cannot limit the present specification. BRIEF DESCRIPTION OF DRAWINGS
[0020] The drawings herein are incorporated into the specification and form a part of the specification, show embodiments consistent with the present specification, and together with the specification serve to explain the principles of the present specification.
[0021] Figure 1 is an architectural diagram of a code optimization method.
[0022] Figure 2 is a function call graph.
[0023] Figure 3 is a flowchart of a code optimization method.
[0024] Figure 4 is a block diagram of a code optimization device.
[0025] Figure 5 is a hardware structure diagram of a computer device. DETAILED DESCRIPTION
[0026] The exemplary embodiments will be described in detail herein with reference to the attached drawings. The following description is presented in connection with the drawings and describes embodiments of the application. However, the application should not be considered limited to the embodiments described herein. Rather, the present application is widely applicable to various embodiments as long as they are consistent with the principles of the present application as detailed in the appended claims.
[0027] It should be noted that the steps of the methods in other embodiments are not necessarily performed in the order shown and described in the specification. In some other embodiments, the steps of the methods can be more or less than described in the specification. In addition, a single step described in the specification can be broken down into multiple steps in other embodiments, and multiple steps described in the specification can be combined into a single step in other embodiments.
[0028] Next, the code optimization method in the related art will be described.
[0029] With the increasing size and complexity of software, the requirements for code performance are also increasing. Large language models (LLMs) have good applications in code generation. For example, by inputting a natural language description, an LLM can generate corresponding correct and functional code.
[0030] Although LLMs have good applications in code generation, code performance optimization is still a challenge for LLMs. Among them, code performance optimization is used to optimize code performance while ensuring code functional correctness, hereinafter referred to as code optimization. The reason is that code optimization through LLMs can usually only optimize a single function. Project-level code includes multiple files, and each file has multiple functions. LLMs cannot effectively consider the complex dependency relationships between functions and the cross-file code optimization requirements, and are difficult to adapt to the diversity and complexity of the actual development environment.
[0031] In addition, although the code editing technology in the related art is used for project-level code optimization, the optimized functions generated by the current code editing technology still have problems in terms of correctness.
[0032] Based on this, the present specification provides a code optimization method. First, input the code optimization task into the LLM, and the LLM filters out effective historical edits useful for the code optimization task from the historical edits of the function to be optimized. And input the filtered effective historical edits into the LLM, so that the LLM optimizes the function to be optimized according to the effective historical edits, and obtains a first edit.
[0033] In the code editing technology of the related art, full historical edits are generally used for code optimization, or the historical edits are screened only according to the similarity between each historical edit and the function to be optimized. However, there may be some historical edits that have a high similarity with the function to be optimized but do not meet the code optimization task. These historical edits will interfere with the code optimization task, and the optimized function may have problems such as incorrect syntax, incorrect function, or inconsistency with the code optimization task.
[0034] The method provided in the specification screens historical edits related to the code optimization task according to the code optimization task. Thus, the historical edits used by the LLM when optimizing are related to the code optimization task, irrelevant historical edits are prevented from interfering with the code optimization task, the correctness of the syntax and function of the optimized code is ensured, and the relevance of the optimized code to the code optimization task is improved.
[0035] Next, the code optimization method provided in the specification will be described in detail with reference to the accompanying drawings. Figure 1 The overall architecture of the code optimization method provided in the specification will be described. As shown in FIG. 1, three parts are involved, namely, the construction of the sequence of functions to be optimized, the identification of effective historical edits, and the optimization of functions. Figure 1
[0036] In the code optimization process, the code optimization task input by the user needs to be obtained first. The code optimization task is generally used to optimize the target function. In other words, in the process of code optimization, the optimization is performed around the specific target function. However, since the target function may have a calling or called relationship with other functions in the project, only optimizing the target function without optimizing other functions may introduce problems. Therefore, in order to complete the code optimization task, not only the target function needs to be optimized, but also the functions that have a direct or indirect calling relationship with the target function need to be optimized.
[0037] The construction of the sequence of functions to be optimized is used to identify all functions that need to be optimized. The function to be optimized is the function that needs to be optimized. The function to be optimized includes the target function and the functions that have a direct or indirect calling relationship with the target function. In addition, not only the function to be optimized needs to be identified, but also the optimization order of the function to be optimized needs to be constructed according to the importance of the function, that is, the sequence of functions to be optimized needs to be constructed. This can achieve better optimization effect.
[0038] After obtaining the sequence of functions to be optimized, the corresponding effective historical edit needs to be screened for each function to be optimized. This step corresponds to the identification of effective historical edits. Through this part, the correctness of the function code after optimization can be improved.
[0039] Finally, the function to be optimized needs to be optimized using the effective historical edit. This step corresponds to the function optimization part.
[0040] Next, the three parts shown will be described in detail. Figure 1
[0041] First, a sequence of functions to be optimized is constructed.
[0042] First, a first function called by the target function and a second function calling the target function can be obtained according to the calling relationship between each function in the project to which the target function belongs.
[0043] The target function is the function to be optimized by the code optimization task. The project mentioned above is the project to which the target function belongs. The project involves multiple files or multiple functions. The first function is a function called (callees) by the target function directly or indirectly, and the second function is a function calling (callers) the target function directly or indirectly. The first function and the second function refer to the type of function, not the number of functions. Multiple functions called by the target function can all be the first function, and similarly, multiple functions calling the target function can all be the second function.
[0044] For the method of obtaining the first function and the second function, data representing the calling relationship between each function in the project can be obtained, and the first function and the second function can be determined therefrom. In an optional embodiment, an abstract syntax tree (AST) can be constructed for the entire project. The AST is a tree-like representation of the abstract syntax structure of the source code, which can indicate the calling relationship between each function. Optionally, the AST can be automatically constructed by the tree-sitter tool.
[0045] Further, the first function and the second function can be found from the AST starting from the target function by identifying function call nodes, and the function call graph of the target function can be constructed, as shown in Figure 2 The target function is ft, and fa and fb in the graph are the second functions calling the target function, and fc and fd are the second functions called by the target function.
[0046] Secondly, after determining the first function and the second function, the similarity between each two functions in the target function, each first function, and each second function (this similarity is also referred to as the second similarity) can be determined, and the similarity score of each first function and each second function can be determined based thereon.
[0047] Specifically, three types of functions have been obtained, which are the target function, the first function, and the second function, and the three types of functions can form a function pool. Then the similarity between each two functions in the function pool can be determined. Finally, for each function in the function pool, the similarity score of the function can be determined according to the similarity of the function to each function in the function pool.
[0048] By determining the similarity between each two functions in the function pool, rather than only determining the similarity between the target function and each first function / second function, the influence of each function on other functions in the function pool can be better evaluated. Although the goal of the final code optimization task is to optimize the target function, the performance of the first function and the second function will also affect the target function, and the first function and the second function will also affect each other. By determining the similarity between each two functions in the function pool, and for each function, the similarity score of the function is determined according to the similarity between the function and each function, the influence of the function on the whole of each function in the function pool can be determined.
[0049] The second similarity can be determined based on semantic similarity and structural similarity. The structural similarity is based on the structural features of the function call graph, and measures the degree of dependence between functions. The semantic similarity filters out functions that are related in syntax but irrelevant in semantics by means of natural language processing technology and code semantic understanding.
[0050] The above-mentioned structural similarity and semantic similarity can be obtained based on CoEdPilot, or can also be implemented based on a pre-trained neural network model. The neural network model can take two functions as input and output the structural similarity and semantic similarity of the two functions.
[0051] In addition, for each of the first function and the second function, after determining the similarity between the function and each function, the similarity scores can be obtained by synthesizing each similarity based on the average, weighted average or other statistical methods of each similarity. In the case of determining the similarity score by using the weighted average, the weight corresponding to the target function can be set to be higher than the weights corresponding to other functions, so that the influence of the function on the target function is considered as a larger proportion, so that the function is better evaluated for the usefulness of the code optimization task.
[0052] Finally, after determining the similarity scores of each first function and second function, the first function and / or second function with a similarity score greater than a second threshold can be determined as the associated function of the target function. Then, any one function in the associated function and the target function can be determined as the function to be optimized, and the function to be optimized is optimized.
[0053] Specifically, functions with a similarity score greater than a second threshold can be selected based on the similarity score. Functions with a similarity score not exceeding the second threshold can be considered to have a low degree of association with other functions in the function pool, and have a small effect on the code optimization task. By removing these functions, invalid associations between functions can be avoided, and unnecessary calculations and processing can be reduced.
[0054] In addition, considering that the optimization order can affect the final optimization result, there is an optimization order between the functions to be optimized, which is determined based on the calling relationship between the functions to be optimized and the similarity score.
[0055] Specifically, first, the first function in the function to be optimized can be arranged in descending order according to the similarity score to obtain a first optimization sequence. And the second function in the function to be optimized is arranged in descending order according to the similarity score to obtain a second optimization sequence. Then the function optimization sequence is composed in the order of the first optimization sequence, the target function, and the second optimization sequence, and then optimized in the sequence from bottom to top. In the function optimization sequence, the optimization priority of the first function is after the target function, and the optimization priority of the second function is before the target function. The function optimization priority between the first functions is determined according to the similarity score, and the optimization priority of the function with a higher similarity score is higher. The second function is similar to the first function and will not be repeated.
[0056] In this way, it can be ensured that the function that has a greater impact on the function pool is optimized earlier, and since the optimization result of the function affects the optimization result of the function after the optimization priority (the optimization result of the function will be used as the historical edit of the function after the optimization priority), it can have a better optimization effect. In addition, for the first function and the second function, the parent function has a wider range of influence, and its optimization priority is also higher.
[0057] Second, effective historical edit identification.
[0058] Next, the identification process of the effective historical edit of the function to be optimized will be described in conjunction with Figure 3 the flowchart shown in the figure.
[0059] Figure 3 A flowchart of a code optimization method is shown, which includes the following steps:
[0060] Step 301, through the LLM, according to the description information of the code optimization task, the effective historical edit related to the code optimization task is screened from the historical edit corresponding to the function to be optimized.
[0061] Among them, the historical edit is the editing record of the function.
[0062] Specifically, the scheme provided in the present specification can identify the historical edit useful for the code optimization task according to the input description information of the code optimization task. Compared with the method in the related art which does not screen the historical edit or only screens the historical edit according to the similarity between the historical edit and the function to be optimized, the method of the present specification can avoid the influence of the historical edit useless for the code optimization task on the code optimization result by inputting the description information of the code optimization task to the LLM.
[0063] wherein the to-be-optimized function is as described above, which can be any one of the correlation function and the target function. Figure 3 The method shown can be executed multiple times, each time for a to-be-optimized function Figure 3 The processing of the steps shown. The history editing is the record of the past editing for the function, such as the first editing or the second editing generated this time, which will be used as the history editing when the next to-be-optimized function is optimized.
[0064] The history editing corresponding to the to-be-optimized function can include the history editing for the to-be-optimized function. In another optional implementation, the history editing corresponding to the to-be-optimized function can include not only the history editing for the to-be-optimized function, but also the history editing for the correlation function and the target function mentioned. In another optional implementation, the history editing corresponding to the to-be-optimized function can include the history editing for the to-be-optimized function, and the history editing for the correlation function or the target function with a similarity score greater than a preset threshold.
[0065] In an optional implementation, before step 301 is implemented, in order to reduce the amount of calculation of the LLM, the history editing can be filtered first. Specifically, from the historical editing records, the editing records with a similarity greater than a first threshold with the to-be-optimized function can be filtered out and used as the history editing in step 301. In other words, the history editing is the editing record with a similarity greater than the first threshold with the to-be-optimized function.
[0066] In this way, the amount of data that the LLM needs to process can be reduced, the search range of the LLM can be narrowed, and the identification efficiency of the effective history editing can be improved.
[0067] wherein the way of determining the similarity between the to-be-optimized function and each editing record can be similar to the way of determining the similarity between each two functions in the function pool as described above, and can be determined based on the structural similarity and the semantic similarity, which will not be described herein again.
[0068] In the case where the similarity between the to-be-optimized function and each history editing is obtained according to the above method, in step 301, in addition to inputting the code optimization task to the LLM for screening, the similarity between the to-be-optimized function and each history editing can also be inputted, which can help the LLM to preferentially identify the history editing with a higher similarity, so as to more quickly identify the effective history editing.
[0069] In other words, step 301 specifically comprises: filtering, by the LLM, the effective historical edits related to the code optimization task from the historical edits corresponding to the to-be-optimized function according to the code optimization task and the similarity (which can be referred to as a first similarity) corresponding to each historical edit.
[0070] Hereinafter, the method of inputting each first similarity and the code optimization task to the LLM will be further described through a specific embodiment.
[0071] First, an effective historical edit definition is obtained; the effective historical edit definition is generated according to the code optimization task.
[0072] Specifically, the definition of the corresponding effective historical edit can be determined according to the code optimization task first. The effective historical edit is the historical edit useful for the code optimization task, which can help the LLM to identify the effective historical edit. Its definition corresponds to the code optimization task. For example, the code optimization task is to optimize the performance of function A. The definition of the effective historical edit can be, for example: the historical edit that directly or indirectly affects the input, output or performance of function A, or the historical edit that needs to make necessary adjustments to the function of function A.
[0073] In addition, in addition to the effective historical edit definition, the content input to the LLM can also include: task definition, and function call and output format restriction.
[0074] The task definition is used to require the LLM to identify the effective historical edit from the input historical edits sorted according to the similarity. For example, the task definition can specifically include: you are a programmer proficient in programming language B and good at optimizing function performance. Your main task is to identify the effective historical edit useful for optimizing function A from the historical edits.
[0075] The function call is used to call a pre-written function that can obtain the historical edit ranked between i and j from the input sorted historical edits, i and j being parameters of the function. For example, the LLM can be input: you can retrieve the historical edit through function B (showing the call format of function B), and you have at most MAX opportunities to call this function. MAX can be defined according to actual needs, for example, it can be 10.
[0076] The output format restriction is used to limit the format of the effective historical edit output by the LLM, so as to facilitate the use of the effective historical edit in the subsequent steps. For example, the LLM can be input: please output in format C.
[0077] Secondly, after receiving the input content, the LLM can obtain target historical edits with a similarity ranking within a preset range according to the similarity of each historical edit; and the LLM can filter out effective historical edits related to the code optimization task from the target historical edits according to the effective historical edits and the target historical edits.
[0078] Specifically, the LLM can obtain the similarity of each historical edit, and determine the historical edits with a similarity ranking within a preset range based on the similarity, and determine the effective historical edits based on this. In the processing process, the LLM can obtain the top N historical edits at a time, and determine which historical edits are effective historical edits. The LLM can also iteratively call the above function, and retrieve in order according to the ranking of the historical edits until enough historical edits are obtained (whether to stop iteration is determined by the LLM), or the maximum number of iterations is reached (which can be MAX times as described above). Finally, the LLM integrates the retrieved historical edits, and comprehensively analyzes and summarizes the filtered historical edits, and outputs the effective historical edits.
[0079] Third, function optimization.
[0080] In step 303, the LLM generates a first edit corresponding to the code optimization task for the function to be optimized based on the effective historical edits.
[0081] Specifically, the LLM can be input with the effective historical edits obtained in step 301 to generate a first edit for the effective historical edits. Since the effective historical edits are filtered according to the code optimization task, the first edit output in step 303 based on the filtered effective historical edits can be more accurate and meet the requirements of the code optimization task.
[0082] In step 303, in addition to inputting the effective historical edits into the LLM, the LLM can also be input with the code optimization task, so that the LLM can generate a first edit that better meets the code optimization task.
[0083] In addition, not only the filtering process of the historical edits is improved in the present specification, but also the function optimization process is improved.
[0084] The first edit is a code generated by the LLM according to the effective history edit, representing the preliminary optimization direction for the code optimization task. In an optional embodiment, after obtaining the first edit, further optimization can be performed on the first edit. Specifically, first, code segments with similarity that meets a similarity condition can be queried from a preset internal code library and an external code library. The internal code library includes the code contained in the project to which the function to be optimized belongs, and the external library includes the code obtained from outside the project to which the function to be optimized belongs.
[0085] Specifically, the first edit is an edit generated by the LLM according to the requirements of the code optimization task, and its performance may not be optimal. Therefore, code segments similar to the first edit can be retrieved from the internal code library and the external code library to broaden the optimization ideas and resources, so that a more optimal second edit can be obtained.
[0086] Among them, the internal code library refers to the code included in the project to which the target function belongs. The external code library refers to the code obtained from outside the project, which can be high-performance functions contained in online repositories, such as functions in Leetcode and Numba. The code included in the external code library can be high-performance code.
[0087] In related technologies, code optimization is generally performed only according to internal code and context, but the optimization resources provided by internal code are limited, and even may lead the LLM to generate incorrect edits. The method of the present specification not only retrieves from the internal code library, but also retrieves from the external code library, which can provide more abundant optimization resources and guide the generation of a second edit that is more in line with the requirements of the code optimization task and has high performance.
[0088] Among them, the similarity between the first edit and the code segment can be calculated by using a code embedding model (such as TransformCode) to obtain embedding vectors corresponding to the first edit and each code segment, and then calculating the similarity between each vector and the first edit according to a similarity calculation method (such as cosine similarity) between vectors. The similarity condition can be that the similarity is greater than a preset threshold, or the similarity ranking is within a preset range, etc., which is not limited in the present specification.
[0089] Secondly, after obtaining the code segment, the first edit can be optimized according to the queried code segment to obtain a second edit; and the second edit is embedded into the function to be optimized.
[0090] Specifically, the first edit can be performance-optimized based on the code segment to obtain a second edit with better performance, and the second edit is embedded into the function to be optimized.
[0091] In an optional implementation, the second edit can be obtained directly through the LLM.
[0092] In another optional implementation, the pre-trained language model can be fine-tuned to generate the second edit according to the language model. Compared with the LLM that is not fine-tuned, the fine-tuned LLM can achieve better code optimization effect.
[0093] In other words, the process of obtaining the second edit can be specifically as follows: the first edit is optimized according to the code snippet through the fine-tuned language model to obtain the second edit; the language model is obtained by fine-tuning based on a plurality of code pairs and labels corresponding to each code pair; and any code pair includes an unoptimized function and a corresponding optimized function.
[0094] In another optional implementation, considering that a small language model can achieve the expected demand after fine-tuning, and fine-tuning of the small language model is simpler than fine-tuning of the LLM, the language model can also be a small language model, so that the expected task can be completed more efficiently.
[0095] In the fine-tuning of the language model, the plurality of code pairs are the unoptimized function and the corresponding optimized function, and the label is used to indicate which function in the code pair is the optimized function, that is, which function has better performance. The code pair can be obtained from various data sets. The present specification does not limit the way of obtaining training data.
[0096] For the fine-tuning method of the language model, in an optional implementation, the Low-Rank Adaptation (LoRA) method can be used for fine-tuning. In the training process, the original function and its semantically similar function with better performance are displayed to the model to guide the model to learn how to generate a function version with better performance.
[0097] After the second edit is generated, the code snippet corresponding to the second edit can be embedded into the function to be optimized. The embedding process can be directly placing the code snippet in the function to be optimized. In another optional implementation, the second edit can also be embedded into the function to be optimized through the LLM. Since the second edit generated by the language model can have problems such as incoherence with the context or inconsistency of variable names, the LLM can be used to jointly correct the second edit according to the context to embed the function to be optimized.
[0098] The method provided in this specification focuses only on function-level issues in LLMs-based code optimization methods. By constructing a sequence of functions to be optimized, starting from the project level, using an abstract syntax tree to build a function call graph, comprehensively considering the structural and semantic relevance between functions, and determining the optimized function sequence, it achieves cross-function and cross-file code optimization, and fully considers the overall structure of the project and the dependencies between functions.
[0099] To address the problem of inaccurate output optimization functions caused by invalid history edits in code editing technology, the method provided in this specification adopts an LLM-based agent. Through clear task allocation, interactive function calls and summary steps, it can more accurately screen out valid history edits related to the target function, reduce the interference of invalid history edits, and improve the accuracy and effectiveness of optimization.
[0100] Regarding function optimization, we iterate through performance editing, build specific datasets to fine-tune small language models, and combine various technologies such as LLM and code function embedding models to continuously optimize function versions, tap into the performance potential of internal functions, and improve the performance of internal functions, thereby improving code execution efficiency and correctness.
[0101] Corresponding to the aforementioned method embodiments, this specification also provides embodiments of an apparatus and a computer device to which it is applied.
[0102] like Figure 4 As shown, Figure 4 1 is a block diagram of a code optimization device according to an exemplary embodiment of the present specification, the device comprising:
[0103] The valid historical edit identification module 410 is configured to filter valid historical edits related to the code optimization task from the historical edits corresponding to the function to be optimized based on the description information of the code optimization task using the large language model (LLM); the historical edits are function edit records;
[0104] The function optimization module 420 is configured to generate, by LLM, a first edit corresponding to the code optimization task for the function to be optimized according to the valid historical edits.
[0105] In an optional implementation manner, the historical edits are edit records whose similarity with the function to be optimized is greater than a first threshold.
[0106] In an optional embodiment, the apparatus further includes a similarity acquisition module (not shown) configured to acquire a first similarity between the function to be optimized and each historical edit. Function optimization module 420 is specifically configured to, using the LLM, filter valid historical edits related to the code optimization task from the historical edits corresponding to the function to be optimized based on each first similarity.
[0107] In an optional implementation, the function optimization module 420 is specifically configured to obtain an effective historical editing definition, where the effective historical editing definition is generated according to description information of the code optimization task; obtain a target historical editing whose ranking of similarity is within a preset range according to similarity of each historical editing; and filter, through an LLM, an effective historical editing related to the code optimization task from the target historical editing according to the effective historical editing definition and the target historical editing.
[0108] In an optional implementation, the function optimization module 420 is further configured to query, from a preset internal code library and an external code library, a code segment that is similar to the first editing and meets a similarity condition, where the internal code library includes code contained in a project to which the function to be optimized belongs, and the external code library includes code obtained from outside the project to which the function to be optimized belongs; optimize the first editing according to the queried code segment to obtain a second editing; and embed the second editing into the function to be optimized.
[0109] In an optional implementation, the function optimization module 420 is specifically configured to optimize, through a fine-tuned language model, the first editing according to the code segment to obtain a second editing, where the language model is obtained by fine-tuning a plurality of code pairs and a label corresponding to each code pair, and any code pair includes an unoptimized function and a corresponding optimized function.
[0110] In an optional implementation, the function optimization module 420 is specifically configured to embed, through an LLM, the second editing into the function to be optimized.
[0111] In an optional implementation, the code optimization task is used to optimize a target function, and the apparatus further includes a function sequence to be optimized construction module (not shown in the figure), configured to obtain a first function called by the target function and a second function called by the target function according to a calling relationship between each function in a project to which the target function belongs; determine a second similarity between each two functions of the target function, each first function, and each second function, and determine a similarity score corresponding to each first function and each second function based on the second similarity; and take a first function and / or a second function with a similarity score greater than a second threshold value as an associated function of the target function; and the function to be optimized is any one of the associated functions or the target function.
[0112] In an optional implementation, the second similarity is determined based on a semantic similarity and a structural similarity.
[0113] In an optional implementation, there is an optimization order between each function to be optimized, and the optimization order is determined based on a calling relationship between each function to be optimized and the similarity score.
[0114] The implementation process of the functions and effects of each module in the above-mentioned device is specifically described in the implementation process of the corresponding steps in the above-mentioned method, and will not be repeated here.
[0115] For the device embodiments, since they basically correspond to the method embodiments, the relevant parts can be referred to the partial description of the method embodiments. The device embodiments described above are merely illustrative, wherein the modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network modules. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this specification. A person of ordinary skill in the art can understand and implement it without paying any creative work.
[0116] like Figure 5 As shown, Figure 5 The figure shows a hardware structure diagram of a computer device in which the code optimization apparatus is located. The device may include: a processor 1010, a memory 1020, an input / output interface 1030, a communication interface 1040, and a bus 1050. The processor 1010, the memory 1020, the input / output interface 1030, and the communication interface 1040 are connected to each other within the device via the bus 1050.
[0117] The processor 1010 can be implemented using a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, and is configured to execute relevant programs to implement the technical solutions provided in the embodiments of this specification. The processor implements the above-described method by running executable instructions.
[0118] The memory 1020 for storing processor-executable instructions can be implemented in the form of ROM (Read Only Memory), RAM (Random Access Memory), static storage devices, dynamic storage devices, etc. The memory 1020 can store an operating system and other application programs. When the technical solutions provided in the embodiments of this specification are implemented through software or firmware, the relevant program code is stored in the memory 1020.
[0119] The input / output interface 1030 is used to connect input / output modules to implement information input and output. The input / output modules can be configured as components within the device (not shown in the figure) or can be externally connected to the device to provide corresponding functions. Input devices may include a keyboard, mouse, touch screen, microphone, various sensors, etc., and output devices may include a display, speaker, vibrator, indicator light, etc.
[0120] The communication interface 1040 is used to connect to a communication module (not shown) to enable communication between the device and other devices. The communication module can communicate via a wired method (such as USB, network cable, etc.) or a wireless method (such as mobile network, WiFi, Bluetooth, etc.).
[0121] The bus 1050 comprises a path for transmitting information between the various components of the device (eg, the processor 1010 , the memory 1020 , the input / output interface 1030 , and the communication interface 1040 ).
[0122] It should be noted that although the above device only shows the processor 1010, the memory 1020, the input / output interface 1030, the communication interface 1040, and the bus 1050, in a specific implementation, the device may also include other components necessary for normal operation. In addition, it will be understood by those skilled in the art that the above device may only include the components necessary to implement the embodiments of this specification, and does not necessarily include all the components shown in the figure.
[0123] The embodiments of this specification also provide a computer program product, which implements the above-mentioned code optimization method when executed by a processor.
[0124] The embodiments of this specification also provide a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the above-mentioned code optimization method is implemented.
[0125] Computer-readable media includes permanent and non-permanent, removable and non-removable media implemented in any method or technology for information storage. Information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information accessible to a computing device. According to the definition herein, computer-readable media does not include transitory media such as modulated data signals and carriers.
[0126] It should also be noted that the terms "comprising", "containing", or any other variant thereof are intended to cover non-exclusive inclusions, so that a process, method, article or apparatus that includes a list of elements not only includes those elements, but also includes other elements not explicitly listed or inherent to such process, method, article or apparatus. Without more limitations, the element defined by the statement "comprising a" does not exclude the presence of additional identical elements in the process, method, article or apparatus that includes the element.
[0127] The above describes specific embodiments of the present specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that in the embodiments and still achieve the desired result. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or sequential order shown to achieve the desired result. In some embodiments, multi-task processing and parallel processing are possible or can be advantageous.
[0128] The user information (including but not limited to user equipment information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in the present application are information and data authorized by the user or authorized by all parties, and the collection, use and processing of related data need to comply with relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation portal for user to choose authorization or refusal.
Claims
1. A code optimization method, comprising: Using the Large Language Model (LLM), valid historical edits related to the code optimization task are filtered from the historical edits corresponding to the function to be optimized based on the description information of the code optimization task; the historical edits are edit records of the function; By using the LLM, a first edit corresponding to the code optimization task is generated for the function to be optimized according to the valid historical edits. 2 . The method according to claim 1 , wherein the historical edits are edit records whose similarity with the function to be optimized is greater than a first threshold.
3. The method according to claim 1, further comprising: Obtaining a first similarity between the function to be optimized and each historical edit; The method of filtering valid historical edits related to the code optimization task from historical edits corresponding to the function to be optimized using the large language model (LLM) according to the code optimization task includes: By using the LLM, valid historical edits related to the code optimization task are screened out from the historical edits corresponding to the to-be-optimized function according to the respective first similarities.
4. The method according to claim 3, wherein the LLM is used to filter out valid historical edits related to the code optimization task from the historical edits corresponding to the function to be optimized based on the similarity between the code optimization task and each historical edit, including: Obtaining a valid history edit definition; the valid history edit definition is generated according to the description information of the code optimization task; According to the similarity of each historical edit, obtain the target historical edit whose similarity ranking is within a preset range; Through the LLM, according to the valid history edit definition and the target history edit, the valid history edit related to the code optimization task is screened out from the target history edit.
5. The method according to claim 1, further comprising: Searching for code snippets whose similarity with the first edit meets similarity conditions from a preset internal code library and an external code library; The internal code library includes the code included in the project to which the function to be optimized belongs, and the external library includes the code obtained from outside the project to which the function to be optimized belongs; Optimizing the first edit according to the retrieved code snippet to obtain a second edit; The second edit is embedded into the function to be optimized.
6. The method according to claim 5, wherein optimizing the first edit according to the queried code snippet to obtain a second edit comprises: The first edit is optimized according to the code snippet by using a fine-tuned language model to obtain a second edit, wherein the language model is obtained by fine-tuning based on multiple code pairs and labels corresponding to each code pair, and any code pair includes an unoptimized function and its corresponding optimized function.
7. The method according to claim 5, wherein embedding the second edit into the function to be optimized comprises: The second edit is embedded into the function to be optimized through LLM.
8. The method according to claim 1, wherein the code optimization task is used to optimize an objective function; The method further comprises: According to the calling relationship between functions in the project to which the target function belongs, obtaining a first function called by the target function and a second function called by the target function; Determining a second similarity between each two of the target function, each first function, and each second function, and determining a similarity score corresponding to each first function and each second function based on the second similarity; The first function and / or the second function whose similarity score is greater than the second threshold is used as the associated function of the objective function; the function to be optimized is any one of the associated functions or the objective function. The method according to claim 8 , wherein the second similarity is determined based on semantic similarity and structural similarity.
10. The method according to claim 8, wherein there is an optimization order among the functions to be optimized, and the optimization order is determined based on the calling relationship between the functions to be optimized and the similarity score.
11. A computer device comprising: processor; a memory for storing processor-executable instructions; The processor implements the method according to any one of claims 1 to 9 by running the executable instructions.