Code segment optimization sequence determination method and device, equipment, medium and product
By filtering and sorting code segments based on their resource consumption, risk, and efficiency characteristics, and combining this with optimization difficulty and business risk, the optimization order of code segments is determined. This solves the problem of inaccurate code segment optimization in existing technologies and achieves more efficient and accurate code optimization.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- INDUSTRIAL AND COMMERCIAL BANK OF CHINA
- Filing Date
- 2026-01-26
- Publication Date
- 2026-05-15
Smart Images

Figure CN122045016A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the fields of artificial intelligence technology and fintech, and to the application of large models in code optimization scenarios, specifically to a method, apparatus, device, medium, and product for determining the optimization order of code segments. Background Technology
[0002] With the widespread adoption of smart devices, business processes automated through program code are playing an increasingly important role. For example, banking applications have greatly facilitated deposit and withdrawal transactions.
[0003] To adapt to different situations and needs, program code requires corresponding optimization. However, for complex code files and code projects, it is often difficult to accurately identify the code segments that need optimization. This may lead to optimizing code segments that do not need optimization or whose optimization benefits are minimal, resulting in a low accuracy in identifying the code segments that need optimization. Summary of the Invention
[0004] In view of the above problems, embodiments of this application provide a method, apparatus, device, medium, and product for determining the code segment optimization order that can improve the accuracy of code segments to be optimized.
[0005] According to a first aspect of this application, a method for determining the optimization order of code segments is provided, comprising: determining a first weight for different code segments in a target code segment set, and adding code segments with a first weight greater than a first weight threshold to a first code segment set; the first weight is positively correlated with the resource consumption of the code segment; determining a second weight for different code segments in the first code segment set, and adding code segments with a second weight greater than a second weight threshold to a second code segment set; the second weight is positively correlated with the resource consumption risk level of the code segment; determining a third weight for different code segments in the second code segment set, and adding code segments with a third weight greater than a third weight threshold to a third code segment set; the third weight is negatively correlated with the response efficiency of the code segment; determining a fourth weight for different code segments in the third code segment set, and sorting the code segments in the third code segment set according to the fourth weight from largest to smallest, and determining the optimization order of the code segments based on the sorting result; the fourth weight is negatively correlated with the optimization difficulty of the code segment, and the fourth weight is positively correlated with the business risk level of the code segment.
[0006] According to an embodiment of this application, the method for determining the first weight includes: for any code segment, determining the amount of preset resources occupied by the code segment during runtime; the preset resources include at least one of the following: processor resources, storage resources, and communication resources; determining a first weight of the code segment based on the determined amount of preset resources occupied; the determined first weight is positively correlated with the determined amount of preset resources occupied.
[0007] According to an embodiment of this application, the method for determining the second weight includes: for any code segment, determining the amount of preset resources occupied by the code segment during runtime; the preset resources include at least one of the following: processor resources, storage resources, and communication resources; determining the resource occupation risk level based on the determined amount of preset resource occupation and the call frequency of the code segment; the determined resource occupation risk level is positively correlated with the determined amount of preset resource occupation and positively correlated with the call frequency of the code segment; determining the second weight of the code segment based on the determined resource occupation risk level; the determined second weight is positively correlated with the determined resource occupation risk level.
[0008] According to an embodiment of this application, determining the resource occupation risk level based on the determined preset resource occupancy and the call frequency of the targeted code segment includes: predicting and determining the future call frequency of the targeted code segment based on the historical call frequency of the targeted code segment; determining the resource occupation risk level based on the determined preset resource occupancy and the future call frequency of the targeted code segment; the determined resource occupation risk level is positively correlated with the future call frequency of the targeted code segment.
[0009] According to an embodiment of this application, the method for determining the third weight includes: for any code segment, determining the response duration of the targeted code segment; determining the response efficiency of the targeted code segment based on the proportion of the determined response duration in the preset maximum response duration of the targeted code segment; the determined response efficiency is negatively correlated with the proportion of the response duration; determining the third weight of the targeted code segment based on the determined response efficiency; the determined third weight is negatively correlated with the determined response efficiency.
[0010] According to an embodiment of this application, determining the response duration of the targeted code segment includes: predicting and determining the future response duration of the targeted code segment based on the historical response duration of the targeted code segment; determining the response efficiency of the targeted code segment based on the proportion of the determined response duration in the preset maximum response duration of the targeted code segment includes: determining the response efficiency of the targeted code segment based on the proportion of the determined future response duration in the preset maximum response duration of the targeted code segment.
[0011] According to an embodiment of this application, the method for determining the fourth weight includes: for any code segment, determining the optimization difficulty of the code segment based on the code characteristics of the code segment; determining the business risk level of the code segment based on the business operations performed by the code segment; determining the fourth weight of the code segment based on the determined optimization difficulty and the business risk level; the determined fourth weight is negatively correlated with the determined optimization difficulty and positively correlated with the determined business risk level.
[0012] According to an embodiment of this application, the method further includes: automatically optimizing code based on a pre-trained code optimization model, according to a determined code segment optimization order, for different code segments in the third code segment set.
[0013] According to an embodiment of this application, the method further includes: for any code segment in the third code segment set, determining the optimized result code segment obtained by the targeted code segment based on the code optimization model; determining the degree of optimization of the optimized result code segment compared to the targeted code segment; and, if the determined degree of optimization is greater than a preset degree of optimization threshold, determining the optimized result code segment as the basis information for modifying the targeted code segment.
[0014] According to an embodiment of this application, determining the amount of preset resources occupied by the target code segment at runtime includes: determining the amount of preset resources occupied by the target code segment at runtime based on the complexity of the target code segment.
[0015] According to a second aspect of this application, a code segment optimization order determination apparatus is provided, comprising: a first filtering module, configured to determine a first weight for different code segments in a target code segment set, and add code segments with a first weight greater than a first weight threshold to a first code segment set; the first weight is positively correlated with the resource consumption of the code segment; a second filtering module, configured to determine a second weight for different code segments in the first code segment set, and add code segments with a second weight greater than a second weight threshold to a second code segment set; the second weight is positively correlated with the resource consumption risk level of the code segment; a third filtering module, configured to determine a third weight for different code segments in the second code segment set, and add code segments with a third weight greater than a third weight threshold to a third code segment set; the third weight is negatively correlated with the response efficiency of the code segment; and a fourth sorting module, configured to determine a fourth weight for different code segments in the third code segment set, and sort the code segments in the third code segment set in descending order of the fourth weight, and determine the code segment optimization order based on the sorting result; the fourth weight is negatively correlated with the optimization difficulty of the code segment, and the fourth weight is positively correlated with the business risk level of the code segment.
[0016] According to a third aspect of this application, an electronic device is provided, comprising: one or more processors; and a memory for storing one or more computer programs, wherein the one or more processors execute the one or more computer programs to implement the steps of the method described above.
[0017] According to a fourth aspect of this application, a computer-readable storage medium is also provided, on which a computer program or instructions are stored, wherein the computer program or instructions, when executed by a processor, implement the steps of the above-described method.
[0018] According to a fifth aspect of this application, a computer program product is also provided, including a computer program or instructions that, when executed by a processor, implement the steps of the above-described method. Attached Figure Description
[0019] The above-mentioned contents, other objects, features and advantages of this application will become clearer from the following description of embodiments with reference to the accompanying drawings, in which:
[0020] Figure 1 This illustration schematically depicts an application scenario of a code segment optimization order determination method according to an embodiment of this application.
[0021] Figure 2 A flowchart illustrating a method for determining the code segment optimization order according to an embodiment of this application is shown schematically.
[0022] Figure 3 This schematically illustrates a structural block diagram of a code segment optimization sequence determination device according to an embodiment of the present application;
[0023] Figure 4 A block diagram schematically illustrates an electronic device suitable for implementing a code segment optimization order determination method according to an embodiment of this application. Detailed Implementation
[0024] The embodiments of this application will now be described with reference to the accompanying drawings. However, it should be understood that these descriptions are exemplary only and are not intended to limit the scope of this application. In the following detailed description, numerous specific details are set forth to provide a thorough understanding of the embodiments of this application for ease of explanation. However, it will be apparent that one or more embodiments may be implemented without these specific details. Furthermore, descriptions of well-known structures and technologies are omitted in the following description to avoid unnecessarily obscuring the concepts of this application.
[0025] The terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the scope of this application. The terms “comprising,” “including,” etc., as used herein indicate the presence of the stated features, steps, operations, and / or components, but do not exclude the presence or addition of one or more other features, steps, operations, or components.
[0026] All terms used herein (including technical and scientific terms) have the meanings commonly understood by those skilled in the art, unless otherwise defined. It should be noted that the terms used herein are to be interpreted in a manner consistent with the context of this specification, and not in an idealized or overly rigid way.
[0027] When using expressions such as "at least one of A, B and C", they should generally be interpreted in accordance with the meaning that is commonly understood by those skilled in the art (e.g., "a system having at least one of A, B and C" should include, but is not limited to, a system having A alone, a system having B alone, a system having C alone, a system having A and B, a system having A and C, a system having B and C, and / or a system having A, B and C, etc.).
[0028] With the widespread adoption of smart devices, business processes automated through program code are playing an increasingly important role. For example, banking applications provide significant convenience for deposit and withdrawal transactions. To adapt to different situations and needs, program code requires corresponding optimization. However, with complex code files and code projects, it is often difficult to accurately identify the code segments that need optimization. This may lead to optimizing code segments that do not require optimization or whose optimization benefits are minimal, resulting in a low accuracy in identifying the code segments that need optimization.
[0029] To address the aforementioned technical problems, embodiments of this application provide a method for determining the optimal order of code segments.
[0030] This method allows for detailed analysis of multiple code segments, identifying those that need optimization based on characteristics such as resource consumption, call frequency, resource consumption risk, and response efficiency.
[0031] It's understandable that the more resource-intensive, frequently invoked, and risky a code segment is, the lower its response efficiency, and the greater its need for and potential benefits of optimization. This makes it easier to identify and optimize code segments that require optimization. Accordingly, for code segments with high resource consumption, optimization can more easily save resources and increase optimization benefits; for frequently invoked code segments, optimization can increase optimization benefits; for code segments with high resource consumption risk, optimization can reduce the risk and probability of resource consumption events, thus increasing optimization benefits; and for code segments with low response efficiency, optimization can improve response efficiency and increase optimization benefits.
[0032] Therefore, by filtering based on the multiple characteristics of the code segments mentioned above, we can identify code segments with high optimization needs and benefits, thereby improving the accuracy of identifying code segments that need optimization and increasing the precision of code optimization.
[0033] In the above screening process, each code segment can be analyzed directly to extract its features, and then the features can be combined for a single screening. Alternatively, multiple screenings can be performed sequentially based on different features. Each screening can analyze the corresponding features in real time based on the code segments selected in the previous screening and then perform the current screening. Since the number of code segments that need to be analyzed is reduced by screening, the computational load of code segment feature analysis can be reduced, and the efficiency of code segment screening can be improved.
[0034] Furthermore, for the selected code segments requiring optimization, the order or priority of optimization can be further determined. Specifically, this can be done based on other characteristics of the code segments, such as their optimization difficulty and business risk, to further rank them and determine the optimization order or priority. Among these, code segments with lower optimization difficulty and higher business risk can be optimized first, further improving the benefits and efficiency of code optimization and increasing its accuracy.
[0035] It should be noted that the code segment optimization order determination method and apparatus provided in the embodiments of this application can be applied to the fields of artificial intelligence technology and fintech. For example, for code content such as bank applications or financial institution web pages, the code segment optimization order determination method provided in the embodiments of this application can be used to determine the code that needs optimization, facilitating subsequent code optimization. The code segment optimization order determination method and apparatus provided in the embodiments of this application can also be applied to any field other than fintech. For example, for cross-node code or cross-microservice code in a distributed system, the code segment optimization order determination method provided in the embodiments of this application can be used to determine the code that needs optimization; for code with real-time high concurrency requirements, the code segment optimization order determination method provided in the embodiments of this application can also be used to determine the code that needs optimization. The application fields of the code segment optimization order determination method and apparatus provided in the embodiments of this application are not limited.
[0036] As used herein, the term "model" refers to a model that learns the relationship between inputs and outputs from training data, thereby generating a corresponding output for a given input after training. Model generation can be based on machine learning techniques. Deep learning is a machine learning algorithm that processes inputs and provides corresponding outputs using multiple layers of processing units. A neural network model is an example of a deep learning-based model. In this application, "model" may also be referred to as a "machine learning model," "learning model," "machine learning network," or "learning network," and these terms are used interchangeably throughout this application.
[0037] It's important to note that the term "neural network" can refer to a machine learning network based on deep learning. A neural network processes input and provides corresponding output, typically consisting of an input layer, an output layer, and one or more hidden layers between them. Neural networks used in deep learning applications often include many hidden layers, increasing the network's depth. The layers of a neural network are connected sequentially, so that the output of the previous layer serves as the input to the next layer. The input layer receives the input to the neural network, while the output layer's output becomes the final output. Each layer of a neural network includes one or more nodes (also called processing nodes or neurons), each processing the input from the layer above.
[0038] It should be understood that machine learning generally includes three phases: training, testing, and application (also known as inference). In the training phase, a given model is trained using a large amount of training data, iteratively updating parameter values until the model can consistently generate inferences that meet the expected goals from the training data. Through training, the model can be considered to have learned the relationship between inputs and outputs (also known as the input-output mapping) from the training data. The parameter values of the trained model are determined. In the testing phase, test inputs are applied to the trained model to test whether it can provide the correct output, thus determining the model's performance. In the application phase, the model can be used to process actual inputs based on the trained parameter values to determine the corresponding output.
[0039] In one or more embodiments described in this application, the term "large model" can refer to a deep learning model with a large number of model parameters, which can include hundreds of millions, tens of billions, hundreds of billions, trillions, or even tens of trillions of model parameters. A large model can also be called a foundational model / basic model. It is pre-trained using large-scale unlabeled corpora to produce a pre-trained model with hundreds of millions of parameters. Such a model can adapt to a wide range of downstream tasks and has good generalization ability, such as large language models and multimodal pre-trained models. It should be understood that in practical applications, large models only require a small number of samples to fine-tune the pre-trained model before it can be applied to different tasks. Large models can be widely used in natural language processing, computer vision, and other fields. Specifically, they can be applied to computer vision tasks such as visual question answering, image description, and image generation, as well as natural language processing tasks such as text-based sentiment classification, text summarization, and machine translation. Major application scenarios for large models can include digital assistants, intelligent robots, search, online education, office software, e-commerce, and intelligent design. In the embodiments of this application, code optimization can be performed based on large models to improve the accuracy and efficiency of code optimization.
[0040] Figure 1 The illustration schematically depicts an application scenario of a code segment optimization order determination method according to an embodiment of this application. For example... Figure 1 As shown, application scenario 100 according to an embodiment of this application may include a first terminal device 101, a second terminal device 102, a third terminal device 103, a network 104, and a server 105. The network 104 serves as a medium for providing a communication link between the first terminal device 101, the second terminal device 102, the third terminal device 103, and the server 105. The network 104 may include various connection types, such as wired or wireless communication links or fiber optic cables. For example, a user can use the first terminal device 101, the second terminal device 102, or the third terminal device 103 to interact with the server 105 through the network 104 to receive or send information, etc.
[0041] The first terminal device 101, the second terminal device 102, and the third terminal device 103 can be electronic devices such as smartphones, wearable devices, personal computers, intelligent voice interaction devices, smart home appliances, intelligent vehicles, in-vehicle terminals, aircraft, unmanned vending terminals, and extended reality devices. Extended reality devices can include virtual reality devices, augmented reality devices, and mixed reality devices. A client application for the target application can be installed and run on the terminal devices. This target application can include, but is not limited to, financial transaction applications, payment applications, shopping applications, web browser applications, search applications, instant messaging tools, email clients, and social media platform software (these are just examples). Furthermore, this application embodiment does not limit the form of the target application, and it can include, but is not limited to, applications, mini-programs, etc., installed on the terminal devices, and can also be in the form of web pages.
[0042] Server 105 can be a server providing various services, such as a backend management server supporting websites browsed by users using the first terminal device 101, the second terminal device 102, and the third terminal device 103 (this is just an example). The backend management server can analyze and process received user requests and other data, and feed back the processing results (such as web pages, information, or data obtained or generated based on user requests) to the terminal devices. Server 105 can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services. Specifically, it can be a cloud server providing basic cloud computing services such as cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks, and big data. Server 105 can be the backend server for the aforementioned target application, used to provide backend services to the client of the target application.
[0043] It should be noted that the code segment optimization order determination method provided in this application embodiment can generally be executed by server 105 and / or terminal devices 101-103. Accordingly, the code segment optimization order determination device provided in this application embodiment can generally be set in server 105 and / or terminal devices 101-103.
[0044] It should be understood that Figure 1 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0045] Figure 2 A flowchart illustrating a method for determining the code segment optimization order according to an embodiment of this application is shown. Figure 2As shown, a code segment optimization order determination method 200 according to an embodiment of this application may include steps S210 to S240.
[0046] The embodiments of this application do not limit the execution subject of a code segment optimization order determination method. Optionally, a code segment optimization order determination method can be applied to any electronic device or any software application. In a specific example, a user terminal can determine the characteristics of multiple local code segments, filter and sort them to determine the code segment optimization order; a server can also determine the characteristics of code segments from multiple applications, filter and sort them to determine the code segment optimization order.
[0047] In step S210, a first weight is determined for each different code segment in the target code segment set, and code segments with a first weight greater than the first weight threshold are added to the first code segment set; the first weight is positively correlated with the resource consumption of the code segment.
[0048] In step S220, a second weight is determined for each different code segment in the first code segment set, and code segments with a second weight greater than the second weight threshold are added to the second code segment set; the second weight is positively correlated with the resource consumption risk level of the code segment.
[0049] In step S230, a third weight is determined for each different code segment in the second code segment set, and code segments with a third weight greater than the third weight threshold are added to the third code segment set; the third weight is negatively correlated with the response efficiency of the code segment.
[0050] In step S240, a fourth weight is determined for each different code segment in the third code segment set, and the code segments in the third code segment set are sorted in descending order of the fourth weight. The optimization order of the code segments is determined based on the sorting result. The fourth weight is negatively correlated with the optimization difficulty of the code segment and positively correlated with the business risk level of the code segment.
[0051] This method can filter out code segments that need optimization based on their resource consumption, resource consumption risk, and response efficiency, thereby improving the accuracy of identifying code segments that need optimization and increasing the precision of code optimization.
[0052] Among them, code segments with high resource consumption, high resource consumption risk, and low response efficiency have a higher degree of optimization demand and optimization benefits, which can improve the accuracy of identifying code segments that need optimization and improve the precision of code optimization.
[0053] This method can also sort code segments according to their optimization difficulty and business risk level to determine the optimization order. This allows for priority optimization of code segments with lower optimization difficulty and higher business risk, which can further improve the benefits and efficiency of code optimization and increase the accuracy of code optimization.
[0054] Furthermore, in this method, the three sequential screening steps S210 to S230 gradually reduce the number of code segments to be screened, lowering the computational load and improving the overall efficiency of determining the code segments requiring optimization. Specifically, the number of code segments in the first code segment set can be less than the target code segment set, thus reducing the number of code segments requiring determination of the second weight and improving screening efficiency; similarly, the number of code segments in the second code segment set can be less than the first code segment set, thus reducing the number of code segments requiring determination of the third weight and improving screening efficiency.
[0055] The embodiments of this application do not limit the code segments. Optionally, a code segment can be runnable code content used to perform business operations, and the corresponding business operations can be executed by running the code segment. The embodiments of this application do not limit the form of the code segment, which can specifically be a code method, code file, code function, or code project, etc. In a specific example, different code segments can be code methods that implement different functions, and the code methods that need to be optimized can be selected by filtering each code method. The embodiments of this application do not limit the method of determining the code segment. Optionally, different code segments that implement different business functions can be divided according to the implemented business functions; or different code files, code methods, or functional code functions can be directly determined as different code segments.
[0056] The embodiments of this application do not limit the target code segment set. Optionally, multiple determined code segments can be added to the target code segment set as a basis for filtering out the code segments that need to be optimized. In a specific example, multiple code segments can be determined based on business operations or business functions for the entire code of an application and added to the target code segment set.
[0057] Optionally, a preliminary screening can be performed on the identified multiple code segments, and then the selected code segments can be added to the target code segment set. The embodiments of this application do not limit the method of preliminary screening. Optionally, preliminary screening can be performed based on factors such as whether the code segment has been optimized, the amount of code in the code segment, the optimization potential of the code segment, and the importance of the business operations of the code segment.
[0058] For example, unoptimized code segments can be filtered out and added to the target code segment set; code segments with a code size greater than a code size threshold can also be filtered out and added to the target code segment set, as code segments with a large code size usually have greater optimization potential, while code segments with a small code size and simple logic are often difficult to optimize further; code segments with business operation importance higher than a preset importance threshold can also be filtered out and added to the target code segment set, which can remove some code segments with lower business operation importance from the candidates for optimization, reduce the number of code segments in the target code segment set, and improve the efficiency of code segment filtering.
[0059] The embodiments of this application do not limit the first weight, second weight, third weight, and fourth weight.
[0060] Optionally, a first weight can be used to characterize the resource consumption of a code segment. Specifically, resource consumption can include the consumption of processor resources, storage resources, communication resources, etc. Resource consumption can be the resource consumption during the code segment's runtime or the resource consumption of a single call to the code segment. It can be determined statistically by collecting actual runtime resource consumption data or by performing static analysis on the code content within the code segment. Correspondingly, the first weight can be positively correlated with the resource consumption of the code segment. Based on the first weight, code segments with high resource consumption can be identified. Code segments with high resource consumption have a significant impact on device performance and may be consuming additional resources due to code design or errors. Therefore, code segments with high resource consumption have a higher optimization demand and higher optimization benefits, and their resource consumption can be reduced through code optimization.
[0061] Optionally, a second weight can be used to characterize the resource consumption risk level of a code segment. The resource consumption risk level can characterize the pressure or load the code segment places on device performance during runtime. This risk level can be determined by two factors: the resource consumption per call to the code segment and the call frequency. If a code segment with high resource consumption is called frequently, it can easily lead to excessive pressure or load on device performance, potentially causing device crashes or freezes. Therefore, the resource consumption risk level can be determined by combining the resource consumption and call frequency of the code segment. The second weight can be positively correlated with the resource consumption risk level of the code segment. Based on the second weight, code segments with high resource consumption risk can be selected. These high-risk code segments have a higher optimization need and higher optimization benefits, and their resource consumption risk can be reduced through code optimization. Optimized code segments can be adapted to higher call frequencies, reducing resource consumption risk and decreasing the probability of device crashes or freezes.
[0062] In this process, after filtering based on the first weight, the second weight is determined. That is, after determining the resource consumption of the code segment, the resource consumption risk level of the code segment is determined. This allows for the reuse of the determined resource consumption and improves the efficiency of determining the resource consumption risk level and the second weight.
[0063] Optionally, a third weight can be used to characterize the response efficiency of a code segment. Response efficiency represents the efficiency of a code segment from initiating the execution of a business operation to its completion. Understandably, the lower the response efficiency, the longer the time it takes for the code segment to complete the business operation, resulting in longer business latency and thus greater optimization potential and benefits. Low response efficiency may be caused by code design issues, such as complex and redundant steps in the business operation implementation process. Therefore, the third weight can be negatively correlated with the response efficiency of the code segment. This third weight can be used to filter out code segments with low response efficiency. Code segments with low response efficiency have a higher optimization need and greater optimization benefits; code optimization can improve response efficiency, reduce response time, and improve the accuracy of code optimization.
[0064] By using the first, second, and third weights mentioned above, code segments with higher optimization needs and higher optimization benefits can be sequentially selected and added to the third code segment set. This improves the accuracy and precision of identifying the code segments requiring optimization, thus enhancing the precision of code optimization. It's understandable that the code segments in the selected third code segment set, compared to randomly selected code segments, possess more accurate and comprehensive optimization needs and benefits. Subsequent code optimization can then reduce resource consumption, mitigate resource consumption risks, and improve response efficiency, thereby increasing the precision of code optimization.
[0065] Optionally, a fourth weight can be used to comprehensively characterize the optimization difficulty and business risk level of a code segment. The optimization difficulty of a code segment represents the cost of optimizing it; higher optimization costs indicate greater difficulty. The business risk level of a code segment represents the importance and scope of influence of the business to which it belongs; higher business importance and a wider scope of influence indicate higher business risk. The fourth weight can be negatively correlated with the optimization difficulty or positively correlated with the business risk level. Based on the fourth weight, code segments in the third code segment set can be sorted to determine the optimization order. Priority can be given to optimizing code segments with lower optimization difficulty and higher business risk. Lower optimization difficulty results in lower optimization costs and higher optimization efficiency, while higher business risk leads to higher optimization benefits, thus comprehensively improving the efficiency and benefits of code optimization.
[0066] Furthermore, for the method of determining the first to fourth weights, please refer to the explanation of the embodiments below.
[0067] The embodiments of this application do not limit the first weight threshold, the second weight threshold, and the third weight threshold.
[0068] Optionally, the first, second, and third weight thresholds can be static or dynamic thresholds, and can be dynamically set. Specifically, by dynamically setting the thresholds, sorting and filtering based on weights can be achieved. This can involve sorting by weight to filter out the top N code segments, where the dynamically set threshold can be the weight value of the Nth code segment in the weight sort. Here, N can be a positive integer.
[0069] Optionally, code segments with a first weight greater than a first weight threshold are added to the first code segment set. Specifically, this can include: sorting the code segments in the target code segment set according to their first weight in descending order, and adding the top N code segments from the sorting result to the first code segment set; or, sorting the code segments in the target code segment set according to their first weight in descending order, determining the first weight of the Nth code segment in the sorting result as the first weight threshold, and adding code segments with a first weight greater than the first weight threshold to the first code segment set. N can be a positive integer. The value of N can be determined according to actual needs.
[0070] Optionally, code segments with a second weight greater than a second weight threshold are added to the second code segment set. Specifically, this can include: sorting the code segments in the first code segment set in descending order of their second weight, and adding the top M code segments from the sorting result to the second code segment set; or, sorting the code segments in the first code segment set in descending order of their second weight, determining the second weight of the Mth code segment in the sorting result as the second weight threshold, and adding code segments with a second weight greater than the second weight threshold to the second code segment set. M can be a positive integer. The value of M can be determined according to actual needs.
[0071] Optionally, code segments with a third weight greater than the third weight threshold are added to the third code segment set. Specifically, this can include: sorting the code segments in the second code segment set in descending order of their third weight, and adding the top P code segments from the sorting result to the third code segment set; or, sorting the code segments in the second code segment set in descending order of their third weight, determining the third weight of the Pth code segment in the sorting result as the third weight threshold, and adding code segments with a third weight greater than the third weight threshold to the third code segment set. P can be a positive integer. The value of P can be determined according to actual needs.
[0072] Furthermore, the values of N, M, and P mentioned above can be the same or different.
[0073] The above embodiments explain the process of determining the third code segment set. The following will further explain the subsequent steps.
[0074] In an optional embodiment, when a third code segment set is determined, the code segments in the third code segment set can be identified as the code segments that need to be optimized, and corresponding code optimization can be performed. The step of determining the code segment optimization order can be used as a preparatory step for the code optimization operation.
[0075] The embodiments of this application do not limit the specific form of the code segment optimization order, nor do they limit the specific method of determining the code segment optimization order.
[0076] Optionally, the code segment optimization order can include the order between different code segments, and the code segments can be optimized one by one in the order of optimization. Optionally, for any code segment in the code segment optimization order, after the optimization of the targeted code segment is completed, the optimization can begin for the next code segment in the code segment optimization order.
[0077] Accordingly, the code segment optimization order is determined based on the sorting results. Specifically, this may include determining the order of code segments in the sorting results as the code segment optimization order.
[0078] Optionally, the code segment optimization order can also include the order of optimization priorities between different code segments. The optimization priority can be determined based on a fourth weight. It is understood that different code segments with the same optimization priority are not limited in their optimization order; they can be optimized in parallel or sequentially, depending on the actual needs. Furthermore, optimization can begin for code segments of the next optimization priority only after optimization of all code segments of the current priority has been completed.
[0079] Accordingly, the sorting results determine the code segment optimization order, which may include: determining the optimization priority of code segments based on the order of code segments in the sorting results and their corresponding fourth weights; and sorting the code segments in the third code segment set according to the optimization priority from high to low to obtain the code segment optimization order. Note that in the code segment optimization order, different code segments with the same optimization priority may not be assigned a specific order.
[0080] The embodiments of this application do not limit the method for determining the optimization priority. Optionally, the optimization priority can be determined based on the range of sorting numbers in the ranking results of the fourth weight; alternatively, it can be determined based on the value range of the fourth weight. The higher the sorting number of the fourth weight from high to low, the larger the value of the fourth weight, and the higher the corresponding optimization priority. In a specific example, the 1st to 10th positions in the sorting results of the fourth weight from high to low can be determined as the highest optimization priority, the 11th to 30th positions as the second highest optimization priority, and so on.
[0081] In a specific example, the code segment optimization order can include the order between different code segments. The corresponding optimization method could be to use a single process for code optimization, allowing individual code segments to be optimized sequentially, with the next segment being optimized only after the current one is completed. Alternatively, multiple processes or devices could be used for parallel code optimization, allowing the next code segment to be optimized based on the optimization order when any process or device is idle.
[0082] In another specific example, the code segment optimization order can include the order of optimization priorities of different code segments from high to low priority. The same optimization priority can correspond to multiple code segments. Correspondingly, the optimization order of different code segments within the same optimization priority is not limited; it can be optimized in parallel or sequentially depending on the actual situation. For example, multiple processes or devices can be used in parallel to optimize different code segments within the same optimization priority separately. After optimizing all code segments of the current optimization priority is completed, optimization can begin for the code segments of the next optimization priority.
[0083] The above embodiments explain the steps for determining the order of code segment optimization. The following explains the subsequent code optimization steps.
[0084] Understandably, once the code segment optimization order is determined, the code segments in the third code segment set can be optimized sequentially according to the optimization order.
[0085] The embodiments of this application do not limit the specific steps for code optimization. Optionally, code optimization can be performed automatically by an electronic device or software application, or the code segment can be fed back to relevant developers for code optimization. Alternatively, both embodiments can be combined, with automatic code optimization performed first, and the optimization results fed back to relevant developers for review or modification. When feeding back code segments, the feedback can be performed according to the order of code segment optimization.
[0086] Optionally, the above method may further include: automatically optimizing different code segments in the third code segment set according to the determined code segment optimization order, based on a pre-trained code optimization model. This embodiment can automatically optimize code according to the code segment optimization order and based on the code optimization model, which can improve the efficiency and accuracy of code optimization.
[0087] The embodiments of this application are not limited to a code optimization model. Optionally, the code optimization model can be a pre-trained deep learning model or a neural network model, or it can be a large model that has been fine-tuned and trained. Optimizing code segments based on a large model can improve the efficiency and effectiveness of code optimization. Since large models have high computational requirements, by filtering the target code segment set to determine a third code segment set, the number of code segments can be reduced, thereby correspondingly reducing the computational load on the large model.
[0088] Correspondingly, the code optimization results of the code optimization model can also be tested to determine the degree of optimization compared to the original code segment. For example, whether there is optimization in terms of code volume, resource consumption, code complexity, resource consumption risk, and response efficiency.
[0089] Therefore, optionally, the above method flow may further include: for any code segment in the third code segment set, determining the optimized result code segment obtained by the targeted code segment based on the code optimization model; determining the degree of optimization of the optimized result code segment compared to the targeted code segment; and, if the determined degree of optimization is greater than a preset optimization degree threshold, determining the optimized result code segment as the basis information for modifying the targeted code segment. This embodiment can filter out better code optimization results based on the degree of optimization of the code optimization results, using them as the basis information for modifying code segments, thereby improving the efficiency and accuracy of code optimization.
[0090] Understandably, by determining the degree of optimization, the effectiveness of code optimization can be easily and accurately measured. This allows for the selection of code optimizations with better results, enabling appropriate code modifications and improving the efficiency and accuracy of code optimization.
[0091] The embodiments of this application do not limit the degree of optimization. Optionally, the degree of optimization may include optimization in multiple aspects, specifically including optimization in terms of code size, resource consumption, code complexity, resource consumption risk, response efficiency, etc. In a specific example, the weighted sum of the optimization in multiple aspects can be determined as the overall degree of optimization, which is used to compare with a preset optimization threshold.
[0092] The embodiments of this application do not limit the specific operations of code modification. Optionally, if the determined optimization level is greater than a preset optimization level threshold, the targeted code segment may be replaced with the optimized code segment; alternatively, if the determined optimization level is greater than the preset optimization level threshold, the targeted code segment may be modified based on a pre-trained code modification model and the optimized code segment; alternatively, if the determined optimization level is greater than the preset optimization level threshold, the optimized code segment may be fed back to relevant developers for reference when modifying the targeted code segment.
[0093] The above embodiments explain the code optimization operation. The determination methods of the first weight, second weight, third weight and fourth weight are further explained below.
[0094] The embodiments of this application do not limit the method for determining the first weight, nor do they limit the method for determining the resource consumption of the code segment. Optionally, the resource consumption of the code segment can be determined by comprehensively considering the resource consumption of multiple resources; the first weight can be determined based on the resource consumption of the code segment and other information, and the first weight can be positively correlated with the resource consumption of the code segment. Other information may include information such as the business risk level of the code, which can be set according to actual needs, and the embodiments of this application do not limit this. Accordingly, the first weight of the code segment can be determined in real time or in advance.
[0095] Optionally, the determination of the first weight may specifically include: for any code segment, determining the amount of preset resources occupied by the target code segment at runtime; the preset resources include at least one of the following: processor resources, storage resources, and communication resources; determining the first weight of the target code segment based on the determined amount of preset resource occupation; the determined first weight is positively correlated with the determined amount of preset resource occupation. This embodiment can combine the occupation of multiple preset resources to determine the first weight, which can improve the accuracy and comprehensiveness of the first weight, and correspondingly improve the accuracy of the first code segment set obtained by filtering based on the first weight, thereby improving the precision of code optimization.
[0096] The embodiments of this application do not limit the preset resources. Optionally, the preset resources include at least one of the following: processor resources, storage resources, and communication resources, and may also include other resources.
[0097] The embodiments of this application do not limit the specific method for determining resource usage. Optionally, resource usage data can be collected during the actual runtime of the code segment, and the statistical value of the resource usage data can be determined as the resource usage. For example, the processor resource usage, storage resource usage (specifically, the amount of allocated memory), and communication resource usage (specifically, the amount of allocated bandwidth) of the code segment during actual runtime can be collected, and the corresponding statistical values (mean or weighted average, etc.) can be determined, and the statistical values can be determined as the resource usage.
[0098] In addition, resource consumption can be determined by considering the code complexity of the code segment itself. Specifically, storage resource consumption can be determined based on the code space complexity of the code segment, or processor resource consumption can be determined based on the cyclomatic complexity of the code segment.
[0099] Therefore, optionally, determining the amount of preset resources occupied by the targeted code segment at runtime can specifically include: determining the amount of preset resources occupied by the targeted code segment at runtime based on the complexity of the targeted code segment. This embodiment can determine the resource consumption based on code complexity, improving the efficiency and accuracy of resource consumption determination, and correspondingly improving the accuracy of the first code segment set obtained by filtering based on the first weight, thereby improving the precision of code optimization.
[0100] The determined amount of pre-defined resource occupancy may include the amount of occupancy of multiple pre-defined resources, or it may include the weighted sum of the occupancy of multiple pre-defined resources.
[0101] The embodiments of this application are not limited to a specific method for determining the first weight based on a preset resource occupancy. Optionally, the predetermined preset resource occupancy can be directly determined as the first weight, and code segments with preset resource occupancy greater than the first weight threshold can be added to the first code segment set accordingly. Optionally, the first weight can also be determined by combining the predetermined preset resource occupancy with other information.
[0102] The embodiments of this application do not limit the method for determining the second weight, nor do they limit the method for determining the degree of resource occupation risk. Optionally, the degree of resource occupation risk can be determined by comprehensively considering the resource occupation of the code segment and the call frequency of the code segment; of course, it can also be determined by combining other information. Optionally, the second weight can be comprehensively determined based on the degree of resource occupation risk and other information. The embodiments of this application do not limit the other information. Accordingly, the second weight can be determined in real time for the code segment, or it can be determined in advance.
[0103] Optionally, the determination of the second weight includes: for any code segment, determining the amount of preset resources occupied by the code segment during runtime; the preset resources include at least one of the following: processor resources, storage resources, and communication resources; determining the resource occupation risk level based on the determined preset resource occupation and the call frequency of the code segment; the determined resource occupation risk level is positively correlated with the determined preset resource occupation and positively correlated with the call frequency of the code segment; determining the second weight of the code segment based on the determined resource occupation risk level; the determined second weight is positively correlated with the determined resource occupation risk level. This embodiment can combine the occupation of multiple preset resources and the call frequency of the code segment to determine the resource occupation risk level and the second weight, which can improve the accuracy and comprehensiveness of the resource occupation risk level and the second weight, and correspondingly improve the accuracy of the second code segment set obtained by filtering based on the second weight, thereby improving the precision of code optimization.
[0104] For an explanation of the preset resource usage, please refer to the explanations in other embodiments.
[0105] The embodiments of this application do not limit the method of determining the degree of resource occupation risk. Optionally, the degree of resource occupation risk can be determined by multiplying the predetermined amount of pre-defined resource occupation by the call frequency of the targeted code segment; alternatively, the degree of resource occupation risk can be determined by the weighted sum of the predetermined amount of pre-defined resource occupation and the call frequency of the targeted code segment, where the weights of resource occupation and call frequency can both be positive numbers.
[0106] The embodiments of this application do not limit the method for determining the call frequency of the code segment. Optionally, the call frequency can be determined based on the historical call frequency of the code segment, specifically, the statistical value of the historical call frequency can be determined as the call frequency. For example, the total call frequency of the code segment within a business cycle can be determined as the call frequency, or the highest historical call frequency of the code segment can be determined as the call frequency.
[0107] Alternatively, the future call frequency can be predicted for the code segment, thereby determining the future call frequency as the call frequency and using it to determine the level of resource consumption risk.
[0108] Understandably, future call frequency trends can serve as a basis for assessing the level of resource consumption risk. For example, if a gradual increase in future call frequency is predicted, the level of resource consumption risk can be increased accordingly.
[0109] Therefore, optionally, the resource occupation risk level can be determined based on the predetermined resource occupancy and the call frequency of the targeted code segment. Specifically, this can include: predicting the future call frequency of the targeted code segment based on its historical call frequency; determining the resource occupation risk level based on the predetermined resource occupancy and the future call frequency of the targeted code segment; and the determined resource occupation risk level is positively correlated with the future call frequency of the targeted code segment. This embodiment can utilize the future call frequency of the code segment to determine the resource occupation risk level, which can improve the accuracy and comprehensiveness of the resource occupation risk level and the second weight, thereby improving the accuracy of the second code segment set obtained by filtering based on the second weight and improving the precision of code optimization.
[0110] In addition, the degree of resource occupation risk can be determined based on the predetermined amount of resources occupied and the historical and future call frequencies of the targeted code segment, thereby further improving the accuracy and comprehensiveness of the degree of resource occupation risk and the second weight.
[0111] The embodiments of this application do not limit the specific method for predicting future call frequencies. Optionally, the future call frequency of a code segment can be predicted based on its historical call frequency and a pre-trained call frequency prediction model. The call frequency prediction model can be a machine learning model, a neural network model, or a large model.
[0112] The embodiments of this application do not limit the specific method of determining the second weight based on the degree of resource occupation risk. Optionally, the determined degree of resource occupation risk can be directly determined as the second weight, and code segments with a resource occupation risk degree greater than the second weight threshold can be added to the second code segment set accordingly. Optionally, the second weight can also be determined by combining the determined degree of resource occupation risk with other information.
[0113] The embodiments of this application do not limit the method for determining the third weight, nor do they limit the method for determining the response efficiency. Optionally, the response efficiency can be determined comprehensively based on the response time of the code segment and the corresponding maximum response time. Optionally, the third weight can be determined comprehensively based on the response efficiency and other information. The embodiments of this application do not limit the other information. Accordingly, the third weight can be determined in real time for the code segment, or it can be determined in advance.
[0114] Optionally, the method for determining the third weight may specifically include: for any code segment, determining the response time of the targeted code segment; determining the response efficiency of the targeted code segment based on the proportion of the determined response time in the preset maximum response time of the targeted code segment; the determined response efficiency is negatively correlated with the proportion of response time; determining the third weight of the targeted code segment based on the determined response efficiency; the determined third weight is negatively correlated with the determined response efficiency. This embodiment can combine the response time of the code segment and the maximum response time to determine the response efficiency, improving the accuracy of the response efficiency, and correspondingly improving the accuracy of the third code segment set obtained by filtering based on the third weight, thereby improving the precision of code optimization.
[0115] The embodiments of this application do not limit the response time. Optionally, the response time can be the time from the start of the code segment's call to execute the business operation to the completion of the business operation. Accordingly, the embodiments of this application do not limit the method of determining the response time. Specifically, it can be to collect response time data of the code segment during actual operation, and use the statistical value of the response time data (such as the mean, peak value, or weighted mean) as the response time to determine the response efficiency.
[0116] The embodiments of this application do not limit the preset maximum response time of the code segment. Optionally, a maximum response time can be preset for the code segment to serve as a benchmark for measuring response efficiency. For example, the preset maximum response time can be the response time for determining the timeout of the code segment response, or the response time for determining the timeout of the business operation performed by the code segment.
[0117] Therefore, the proportion of a code segment's response time within the preset maximum response time of the same code segment can be used to measure the code segment's response efficiency. The higher the proportion, the lower the code segment's response efficiency.
[0118] The embodiments of this application do not limit the specific method of determining response efficiency based on the response time percentage. Optionally, the reciprocal of the response time percentage can be used to determine the response efficiency of the target code segment; alternatively, (1 - response time percentage) can be used to determine the response efficiency of the target code segment. Of course, the response efficiency of the code segment can also be determined comprehensively based on the response time percentage and other information, and the response time percentage can be negatively correlated with the response efficiency.
[0119] Alternatively, in determining the response time, it can be determined based on the statistical value of historical response times, or the future response time can be predicted for the code segment, so that the future response time can be determined as the response time and used to determine the response efficiency.
[0120] Understandably, future response time trends can serve as information for determining response efficiency. Therefore, optionally, determining the response time of a targeted code segment can specifically include: predicting the future response time of the targeted code segment based on its historical response time; and determining the response efficiency of the targeted code segment based on the proportion of the determined response time within the preset maximum response time of the targeted code segment. Specifically, this can include: determining the response efficiency of the targeted code segment based on the proportion of the determined future response time within the preset maximum response time of the targeted code segment. This embodiment can determine the response efficiency of a code segment based on its future response time, which can improve the accuracy and comprehensiveness of response efficiency and the third weight, thereby improving the accuracy of the third code segment set obtained by filtering based on the third weight, and ultimately improving the precision of code optimization.
[0121] Furthermore, the response efficiency of a target code segment can be comprehensively determined based on the proportion of the future response time within the preset maximum response time of the target code segment, and the proportion of historical response time statistics within the preset maximum response time of the target code segment. Response efficiency can be negatively correlated with the proportion of future response time, and response efficiency can be negatively correlated with the proportion of historical response time. This embodiment can further improve the accuracy and comprehensiveness of response efficiency and the third weight.
[0122] The embodiments of this application do not limit the specific method of predicting future response times. Optionally, the future response time of a code segment can be predicted based on the historical response times of the code segment and a pre-trained response time prediction model. The response time prediction model can be a machine learning model, a neural network model, or a large model.
[0123] The embodiments of this application do not limit the specific method of determining the third weight based on response efficiency. Optionally, the third weight can be determined as the reciprocal of the determined response efficiency; alternatively, the third weight can be determined as the determined response time percentage, where the response time percentage can be positively correlated with the third weight. Accordingly, code segments with a response time percentage greater than the third weight threshold can be added to the third code segment set. Optionally, the third weight can also be determined by combining the determined response efficiency with other information.
[0124] The embodiments of this application do not limit the method of determining the fourth weight, nor do they limit the method of determining the optimization difficulty and the degree of business risk.
[0125] Optionally, the optimization difficulty can be determined comprehensively based on the code characteristics of the code segment, such as cyclomatic complexity, code architecture, programming language, and number of code optimizations. Specifically, the optimization difficulty can be determined in conjunction with a larger model. Alternatively, the optimization difficulty can be determined by combining the code characteristics of the code segment with other relevant information.
[0126] Optionally, the degree of business risk can be determined comprehensively based on information related to the business operations executed by the code segment, such as the business it belongs to and the business functions it implements. The higher the importance of the business operation and the wider its impact, the higher the degree of business risk can be determined. Alternatively, the degree of business risk can be determined by combining information related to the business operations executed by the code segment with other relevant information.
[0127] Optionally, the fourth weight can be determined by comprehensively considering the optimization difficulty, the level of business risk, and other relevant information. For example, the weighted sum of the optimization difficulty and the level of business risk can be used as the fourth weight, where the weight corresponding to the optimization difficulty can be negative and the weight corresponding to the level of business risk can be positive. The embodiments of this application do not limit the use of other relevant information. Accordingly, the fourth weight can be determined in real time for a code segment, or it can be predetermined.
[0128] Optionally, the determination of the fourth weight may specifically include: for any code segment, determining the optimization difficulty of the code segment based on its code characteristics; determining the business risk level of the code segment based on the business operations performed by the code segment; determining the fourth weight of the code segment based on the determined optimization difficulty and business risk level; the determined fourth weight is negatively correlated with the determined optimization difficulty and positively correlated with the determined business risk level. This embodiment can comprehensively determine the fourth weight based on the optimization difficulty and business risk level of the code segment, which can improve the accuracy and comprehensiveness of the fourth weight, correspondingly improving the accuracy of the code segment optimization order determined based on the fourth weight, and thus improving the precision of code optimization.
[0129] The embodiments of this application do not limit the method for determining the optimization difficulty of a code segment, nor do they limit specific code features. Optionally, a pre-set correspondence between code features and optimization schemes can be determined, thereby determining one or more corresponding optimization schemes based on the code features of the code segment, further evaluating the optimization difficulty of the determined optimization schemes, and combining the total optimization difficulty of multiple determined optimization schemes. Code features may specifically include at least one of the following: code architecture, code programming language, code historical optimization count, code complexity, etc. In a specific example, the correspondence between code features and optimization schemes may include: if the code complexity is higher than a first complexity threshold, the corresponding optimization scheme is code refactoring; if the code time complexity is higher than a second complexity threshold, the corresponding optimization scheme is reducing time complexity. In addition, optionally, the optimization difficulty of a code segment can be determined based on a pre-trained deep learning model or a large model, and subsequent code optimization can also be performed on the code segment based on the large model.
[0130] The embodiments of this application do not limit the method for determining the business risk level of a code segment. Optionally, the business risk level of a code segment can be determined based on information related to the business operations performed on the code segment. The embodiments of this application do not limit the business operation information, which may specifically include at least one of the following: the business to which the business operation belongs, the importance of the business operation, the scope of impact of the business operation, the importance of the business to which the business operation belongs, and the scope of impact of the business to which the business operation belongs. In a specific example, the business risk level can be determined by combining multiple business operation information, specifically by weighted summing of multiple business operation information. For example, for some relatively important or widely impactful businesses, including user service businesses, security businesses, kernel processing businesses, etc., a higher business risk level can be determined. In addition, optionally, the business risk level of a code segment can be determined based on a pre-trained deep learning model or a large model.
[0131] The embodiments of this application do not limit the specific method for determining the fourth weight. Optionally, the fourth weight can be determined as a weighted sum of optimization difficulty and business risk level, where the weight corresponding to optimization difficulty can be negative and the weight corresponding to business risk level can be positive. Optionally, the fourth weight can also be determined by comprehensively considering optimization difficulty, business risk level, and other information.
[0132] For ease of understanding, this application also provides a specific application embodiment.
[0133] Many current code optimization methods focus only on optimizing frequently called or highly complex code, which may lead to the neglect of other code that also needs optimization. Furthermore, the investment in code optimization may not match the performance gains; code that has been optimized with significant investment may actually fail to improve performance.
[0134] Therefore, this embodiment discloses a method for code optimization and sorting based on multi-dimensional information such as call frequency, response time, cyclomatic complexity, and business risk level. This method uses the method call frequency and cyclomatic complexity during peak production periods, combined with runtime behavior (call frequency, response time) and static complexity (cyclomatic complexity), to identify high-frequency call and high logical complexity code as the core area of performance bottlenecks. This avoids the defects of "blindly refactoring low-frequency call code" or "ignoring high-complexity low-frequency code" in traditional optimization. It can also easily identify code that may have performance problems in advance for optimization, reducing the probability of performance problems occurring.
[0135] In this embodiment, the code segment that performs the business operation is called a code method, or simply a method.
[0136] The method steps disclosed in this embodiment may include steps 1 to 6.
[0137] Step 1: The first round of sorting and filtering can be based on information such as cyclomatic complexity, number of callers, space complexity, and memory usage. First, obtain the cyclomatic complexity of the methods by scanning the code. Cyclomatic complexity reflects the complexity of loops and conditional statements; the more loops or the more complex the conditional statements, the higher the processor resource utilization will be. Weight the number of callers. Simultaneously, monitor memory allocation to obtain the memory usage of each method. Determine the top 1000 methods in terms of processor resource utilization (ranked from highest to lowest) and the top 1000 methods in terms of memory usage (ranked from highest to lowest) (the specific number can be defined according to the number of methods in the project). Record the sequence number of each method. Then, subtract the rank of each method in the processor resource utilization ranking from 1000 to obtain the weight for processor resource utilization; subtract the rank of each method in the memory usage ranking from 1000 to obtain the weight for memory usage. Add the weights of processor resource utilization and memory usage for the same method to obtain the total weight of each method. Sort the methods according to the total weight from high to low to obtain the priority of the methods that need to be optimized, and get the top 500 methods (the specific number can be defined according to the number of methods in the project).
[0138] Step 2: The second round of sorting and filtering is based on the peak call volume of production dynamic monitoring. For the 500 methods obtained from the first round of sorting and filtering, the method call frequency and response time for completing one method execution during the peak business period in the production environment are collected and recorded in real time. A peak trend chart of the call frequency of each method is constructed. Based on the trend chart, the call frequency of the next peak is predicted. The methods are then sorted again based on the product of the call frequency and the cyclomatic complexity. This sorting determines the level of danger of performance problems. The higher the product of the call frequency and the cyclomatic complexity, the greater the possibility of performance problems as the business volume increases, and the greater the benefit of performance optimization. The top 400 methods are obtained again (the specific number can be defined according to the number of methods in the project).
[0139] Step 3: The third round of sorting and filtering involves sorting the 400 methods obtained in the second round based on their response time as monitored in production. The response time of each method during peak business periods has already been recorded. A trend chart of each method's response time is constructed. Based on the trend chart, the response time during the next peak is predicted. The ratio of the response time to the timeout response threshold for the corresponding service is calculated. Methods are then sorted according to this ratio; the closer the ratio is to or exceeds the threshold, the greater the risk of a problem. The top 300 methods are then obtained again (the specific number can be defined based on the number of methods in the project).
[0140] Step 4: The fourth round of ranking, based on business risk level (involving management, accounting, key customers, and business impact, etc.), assigns weights to the 300 methods selected in the third round of ranking. For example, the weight for methods involving management, accounting, key customers, and business impact is 1. If a method involves management, accounting, key customers, and business impact, its weight is 4, indicating a very important method that requires focused optimization. From the 300 methods selected in the third round of ranking, the weight value of the business risk level for each method is labeled; combined with the following code characteristics and the difficulty of their corresponding optimization strategies, a large model is used to analyze the difficulty of code optimization and assign weighted scores to the difficulty of optimization. If multiple features are met, the weights are added together to obtain the total weight of the optimization difficulty. The larger the weight, the more difficult it is to optimize. Then, normalization is performed (the weight score of a specific method is divided by the total weight) to obtain the optimization difficulty score. Subtracting the optimization difficulty score from 1 gives the easy-to-optimize score. Finally, the easy-to-optimize score is multiplied by the business risk level weight. Based on this product, the 300 methods obtained from the third round of sorting are sorted to obtain the final optimization order. The larger the product, the greater the return on investment in optimization.
[0141] The correspondence between code characteristics and optimization strategies can include: "If the cyclomatic complexity of the code is greater than 10, prioritize splitting the method"; "If the code has external dependencies, consider replacing them with caching / asynchronous methods"; "If the code's test coverage is less than 70%, optimize after supplementing tests"; "If the code involves shared mutable state, remove thread safety risks"; "If the code has non-isolation, mix business / persistence logic and require layered refactoring"; "If the code involves distributed interaction of concurrency / transactions, architectural refactoring and transaction boundary adjustment are required"; "If the algorithm complexity in the code is high, the algorithm complexity can be reduced"; "If the code contains core logic from legacy systems, the code can be rewritten and an anti-corruption layer can be designed"; and "If the code method has been optimized in the past, architectural refactoring can be performed".
[0142] Step 5: Following the optimization order determined in Step 4, perform method-level refactoring optimization on the 300 sorted methods using a pre-trained model, and output refactored code that is functionally equivalent but has reduced complexity. The model here can use a reinforcement learning model, such as a refactoring rule base, to generate low-complexity and equivalent code.
[0143] Step 6: Perform method-level benchmarking tests on the methods before and after the refactoring, and compare the metrics before and after the refactoring (e.g., processor resource utilization and memory allocation). If the optimization goal (reduction in processor resource utilization or memory usage) is achieved, it can be considered for use.
[0144] This embodiment can improve the accuracy and efficiency of code optimization by sorting and filtering code methods and determining the optimization order, identifying the scope of potential performance problems in advance, and automatically optimizing them.
[0145] Based on the above method embodiments, embodiments of this application also provide a code segment optimization order determination apparatus. The following will be combined with... Figure 3 The device is described in detail.
[0146] Figure 3 The diagram illustrates a structural block diagram of a code segment optimization sequence determination device according to an embodiment of this application.
[0147] like Figure 3 As shown, the code segment optimization order determination device 300 provided in this embodiment includes: a first filtering module 310, a second filtering module 320, a third filtering module 330 and a fourth sorting module 340.
[0148] The first filtering module 310 is used to determine a first weight for different code segments in the target code segment set, and add code segments with a first weight greater than a first weight threshold to the first code segment set; the first weight is positively correlated with the resource consumption of the code segment. In one embodiment, the first filtering module 310 can be used to execute the steps S210 and related steps described above, which will not be repeated here.
[0149] The second filtering module 320 is used to determine a second weight for different code segments in the first code segment set, and add code segments with a second weight greater than a second weight threshold to the second code segment set; the second weight is positively correlated with the resource consumption risk level of the code segment. In one embodiment, the second filtering module 320 can be used to execute the steps S220 and related steps described above, which will not be repeated here.
[0150] The third filtering module 330 is used to determine a third weight for different code segments in the second code segment set, and add code segments with a third weight greater than a third weight threshold to the third code segment set; the third weight is negatively correlated with the response efficiency of the code segment. In one embodiment, the third filtering module 330 can be used to execute the steps S230 and related steps described above, which will not be repeated here.
[0151] The fourth sorting module 340 is used to determine a fourth weight for each different code segment in the third code segment set, and sort the code segments in the third code segment set in descending order of the fourth weight. The optimization order of the code segments is determined based on the sorting result. The fourth weight is negatively correlated with the optimization difficulty of the code segment and positively correlated with the business risk level of the code segment. In one embodiment, the fourth sorting module 340 can be used to execute step S240 and related steps described above, which will not be repeated here.
[0152] According to an embodiment of this application, the first screening module 310 can be used to perform a method for determining a first weight. The method for determining the first weight may include: for any code segment, determining the amount of preset resources occupied by the target code segment during runtime; the preset resources include at least one of the following: processor resources, storage resources, and communication resources; determining a first weight of the target code segment based on the determined amount of preset resource occupancy; the determined first weight is positively correlated with the determined amount of preset resource occupancy.
[0153] According to an embodiment of this application, the second screening module 320 can be used to perform a method for determining the second weight. The method for determining the second weight may include: for any code segment, determining the amount of preset resources occupied by the target code segment during runtime; the preset resources include at least one of the following: processor resources, storage resources, and communication resources; determining the resource occupation risk level based on the determined amount of preset resource occupation and the call frequency of the target code segment; the determined resource occupation risk level is positively correlated with the determined amount of preset resource occupation and positively correlated with the call frequency of the target code segment; determining the second weight of the target code segment based on the determined resource occupation risk level; the determined second weight is positively correlated with the determined resource occupation risk level.
[0154] According to an embodiment of this application, in the method of determining the second weight, the resource occupation risk level is determined based on the determined preset resource occupancy and the call frequency of the targeted code segment, including: predicting and determining the future call frequency of the targeted code segment based on the historical call frequency of the targeted code segment; determining the resource occupation risk level based on the determined preset resource occupancy and the future call frequency of the targeted code segment; the determined resource occupation risk level is positively correlated with the future call frequency of the targeted code segment.
[0155] According to an embodiment of this application, the third filtering module 330 can be used to perform a method for determining the third weight. The method for determining the third weight may include: for any code segment, determining the response duration of the targeted code segment; determining the response efficiency of the targeted code segment based on the proportion of the determined response duration to the preset maximum response duration of the targeted code segment; the determined response efficiency being negatively correlated with the proportion of response duration; determining the third weight of the targeted code segment based on the determined response efficiency; and the determined third weight being negatively correlated with the determined response efficiency.
[0156] According to an embodiment of this application, in the method of determining the third weight, determining the response time of the targeted code segment includes: predicting and determining the future response time of the targeted code segment based on the historical response time of the targeted code segment; determining the response efficiency of the targeted code segment based on the proportion of the determined response time in the preset maximum response time of the targeted code segment includes: determining the response efficiency of the targeted code segment based on the proportion of the determined future response time in the preset maximum response time of the targeted code segment.
[0157] According to an embodiment of this application, the fourth sorting module 340 can be used to execute a method for determining the fourth weight. The method for determining the fourth weight may include: for any code segment, determining the optimization difficulty of the target code segment based on its code characteristics; determining the business risk level of the target code segment based on the business operations performed by the target code segment; determining the fourth weight of the target code segment based on the determined optimization difficulty and business risk level; the determined fourth weight is negatively correlated with the determined optimization difficulty and positively correlated with the determined business risk level.
[0158] According to an embodiment of this application, the above-described apparatus may further include an optimization module, used to: automatically optimize code based on a pre-trained code optimization model, according to a determined code segment optimization order, for different code segments in a third code segment set.
[0159] According to an embodiment of this application, the optimization module is further configured to: for any code segment in the third code segment set, determine the optimized result code segment obtained by the targeted code segment based on the code optimization model; determine the degree of optimization of the optimized result code segment compared to the targeted code segment; and, if the determined degree of optimization is greater than a preset degree of optimization threshold, determine the optimized result code segment as the basis information for modifying the targeted code segment.
[0160] According to an embodiment of this application, in the method of determining the first weight and the second weight, determining the amount of preset resources occupied by the target code segment at runtime may include: determining the amount of preset resources occupied by the target code segment at runtime based on the complexity of the target code segment.
[0161] According to embodiments of this application, any multiple modules among the first screening module 310, second screening module 320, third screening module 330, fourth sorting module 340, and optimization module can be combined into one module, or any one of these modules can be split into multiple modules. Alternatively, at least some of the functions of one or more of these modules can be combined with at least some of the functions of other modules and implemented in one module. According to embodiments of this application, at least one of the first screening module 310, second screening module 320, third screening module 330, fourth sorting module 340, and optimization module can be at least partially implemented as hardware circuits, such as field-programmable gate arrays, programmable logic arrays, systems-on-a-chip, systems-on-a-substrate, systems-on-package, application-specific integrated circuits, or implemented in hardware or firmware by any other reasonable means of integrating or packaging circuits, or implemented in software, hardware, and firmware, or in any suitable combination of any of these three implementation methods. Alternatively, at least one of the first filtering module 310, the second filtering module 320, the third filtering module 330, the fourth sorting module 340, and the optimization module can be at least partially implemented as a computer program module, which can perform corresponding functions when the computer program module is run.
[0162] The explanation of the above device embodiments can be found in the explanation of other embodiments, and any step in the above method embodiments can be executed by the corresponding module in the above device embodiments.
[0163] Figure 4 A block diagram schematically illustrates an electronic device suitable for implementing a code segment optimization order determination method according to an embodiment of this application.
[0164] like Figure 4 As shown, an electronic device 1200 according to an embodiment of this application includes a processor 1201, which can perform various appropriate actions and processes according to a program stored in a read-only memory 1202 or a program loaded from a storage portion 1208 into a random access memory 1203. The processor 1201 may include, for example, a general-purpose microprocessor, an instruction set processor and / or an associated chipset and / or a dedicated microprocessor. The processor 1201 may also include onboard memory for caching purposes. The processor 1201 may include a single processing unit or multiple processing units for executing different steps of the method flow according to an embodiment of this application.
[0165] Random access memory 1203 stores various programs and data required for the operation of electronic device 1200. Processor 1201, read-only memory 1202, and random access memory 1203 are interconnected via bus 1204. Processor 1201 executes various steps of the method flow according to embodiments of this application by executing programs in read-only memory 1202 and / or random access memory 1203. It should be noted that the programs may also be stored in one or more memories other than read-only memory 1202 and random access memory 1203. Processor 1201 may also execute various steps of the method flow according to embodiments of this application by executing programs stored in said one or more memories.
[0166] According to embodiments of this application, the electronic device 1200 may further include an input / output interface 1205, which is also connected to the bus 1204. The electronic device 1200 may also include one or more of the following components connected to the input / output interface 1205: an input section 1206 including a keyboard, mouse, etc.; an output section 1207 including a cathode ray tube, liquid crystal display, etc., and a speaker, etc.; a storage section 1208 including a hard disk, etc.; and a communication section 1209 including a network interface card, such as a local area network card, modem, etc. The communication section 1209 performs communication processing via a network such as the Internet. A drive 1210 is also connected to the input / output interface 1205 as needed. A removable medium 1211, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on the drive 1210 as needed so that computer programs read from it can be installed into the storage section 1208 as needed.
[0167] Embodiments of this application also provide a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments; or it may exist independently and not assembled into the device / apparatus / system. The computer-readable storage medium carries one or more programs, which, when executed, implement the method according to the embodiments of this application.
[0168] According to embodiments of this application, the computer-readable storage medium can be a non-volatile computer-readable storage medium, such as including but not limited to: portable computer disks, hard disks, random access memory, read-only memory, erasable programmable read-only memory, portable compact disk read-only memory, optical storage devices, magnetic storage devices, or any suitable combination thereof. In embodiments of this application, the computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. For example, according to embodiments of this application, the computer-readable storage medium may include the read-only memory 1202, and / or random access memory 1203, and / or one or more memories other than read-only memory 1202 and random access memory 1203 described above.
[0169] Embodiments of this application also include a computer program product comprising a computer program containing program code for performing the methods shown in the flowchart. When the computer program product is run on a computer system, the program code is used to cause the computer system to implement the methods provided in the embodiments of this application.
[0170] In one embodiment, the computer program may rely on a tangible storage medium such as an optical storage device or a magnetic storage device. In another embodiment, the computer program may also be transmitted and distributed in the form of signals over a network medium, and may be downloaded and installed via the communication section 1209, and / or installed from the removable medium 1211. The program code contained in the computer program can be transmitted using any suitable network medium, including but not limited to: wireless, wired, etc., or any suitable combination thereof.
[0171] In embodiments of this application, the computer program can be downloaded and installed from a network via communication section 1209, and / or installed from removable medium 1211. When the computer program is executed by processor 1201, it performs the functions defined in the system of this application embodiment. According to embodiments of this application, the systems, devices, apparatuses, modules, units, etc., described above can be implemented by computer program modules.
[0172] According to embodiments of this application, program code for executing the computer programs provided in the embodiments of this application can be written in any combination of one or more programming languages. Specifically, these computational programs can be implemented using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. The program code can be executed entirely on the user's computing device, partially on the user's device, partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).
[0173] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0174] Those skilled in the art will understand that the features described in the various embodiments of this application can be combined and / or combined in various ways, even if such combinations or combinations are not explicitly described in this application. In particular, the features described in the various embodiments of this application can be combined and / or combined in various ways without departing from the spirit and teachings of this application. All such combinations and / or combinations fall within the scope of this application.
Claims
1. A method for determining the optimization order of code segments, characterized in that, The method includes: For different code segments in the target code segment set, a first weight is determined for each segment, and code segments with a first weight greater than the first weight threshold are added to the first code segment set; the first weight is positively correlated with the resource consumption of the code segment; For different code segments in the first code segment set, a second weight is determined for each segment, and code segments with a second weight greater than the second weight threshold are added to the second code segment set; the second weight is positively correlated with the resource consumption risk level of the code segment; For different code segments in the second code segment set, a third weight is determined for each segment, and code segments with a third weight greater than the third weight threshold are added to the third code segment set; the third weight is negatively correlated with the response efficiency of the code segment. For different code segments in the third code segment set, a fourth weight is determined for each segment, and the code segments in the third code segment set are sorted in descending order of the fourth weight. The optimization order of the code segments is determined based on the sorting result. The fourth weight is negatively correlated with the optimization difficulty of the code segment and positively correlated with the business risk level of the code segment.
2. The method according to claim 1, characterized in that, The method for determining the first weight includes: For any given code segment, determine the amount of preset resources that the code segment will consume during runtime; the preset resources include at least one of the following: processor resources, storage resources, and communication resources; Based on the determined preset resource usage, a first weight is determined for the target code segment; the determined first weight is positively correlated with the determined preset resource usage.
3. The method according to claim 1, characterized in that, The method for determining the second weight includes: For any given code segment, determine the amount of preset resources that the code segment will consume during runtime; the preset resources include at least one of the following: processor resources, storage resources, and communication resources; The degree of resource occupancy risk is determined based on the predetermined amount of resources used and the frequency of calls to the targeted code segment. The determined degree of resource occupancy risk is positively correlated with the predetermined amount of resources used and with the frequency of calls to the targeted code segment. Based on the determined level of resource occupation risk, a second weight is determined for the target code segment; the determined second weight is positively correlated with the determined level of resource occupation risk.
4. The method according to claim 3, characterized in that, The determination of resource occupancy risk level based on the predetermined resource occupancy and the call frequency of the targeted code segment includes: Based on the historical call frequency of the targeted code segment, predict and determine the future call frequency of the targeted code segment; The degree of resource occupancy risk is determined based on the predetermined resource occupancy and the future call frequency of the targeted code segment; the determined degree of resource occupancy risk is positively correlated with the future call frequency of the targeted code segment.
5. The method according to claim 1, characterized in that, The method for determining the third weight includes: For any given code segment, determine the response time for that segment. The response efficiency of the targeted code segment is determined based on the proportion of the determined response time within the preset maximum response time of the targeted code segment; the determined response efficiency is negatively correlated with the proportion of the response time. Based on the determined response efficiency, a third weight is determined for the target code segment; the determined third weight is negatively correlated with the determined response efficiency.
6. The method according to claim 5, characterized in that, Determining the response time for the targeted code segment includes: Based on the historical response time of the targeted code segment, the future response time of the targeted code segment is predicted and determined. The step of determining the response efficiency of the target code segment based on the proportion of the determined response time within the preset maximum response time of the target code segment includes: The response efficiency of the target code segment is determined based on the proportion of the future response time to the preset maximum response time of the target code segment.
7. The method according to claim 1, characterized in that, The method for determining the fourth weight includes: For any given code segment, determine the optimization difficulty of the target code segment based on its code characteristics; Determine the level of business risk for the targeted code segment based on the business operations performed on it. Based on the determined optimization difficulty and business risk level, a fourth weight is determined for the targeted code segment; the determined fourth weight is negatively correlated with the determined optimization difficulty and positively correlated with the determined business risk level.
8. The method according to claim 1, characterized in that, The method further includes: Based on a pre-trained code optimization model, code optimization is automatically performed on different code segments in the third code segment set according to the determined code segment optimization order.
9. The method according to claim 8, characterized in that, The method further includes: For any code segment in the third code segment set, determine the optimized code segment obtained by the code optimization model based on the targeted code segment; Determine the degree of optimization of the resulting code segment compared to the targeted code segment; If the determined optimization level is greater than the preset optimization level threshold, the optimized code segment is determined as the basis for modifying the targeted code segment.
10. The method according to claim 2 or 3, characterized in that, Determining the amount of preset resources used by the target code segment at runtime includes: Determine the amount of preset resources that the target code segment will consume at runtime, based on the complexity of the target code segment.
11. A device for determining the optimal order of code segments, characterized in that, The device includes: The first filtering module is used to determine a first weight for different code segments in the target code segment set, and add code segments with a first weight greater than a first weight threshold to the first code segment set; the first weight is positively correlated with the resource consumption of the code segment; The second filtering module is used to determine a second weight for different code segments in the first code segment set, and add code segments with a second weight greater than a second weight threshold to the second code segment set; the second weight is positively correlated with the resource consumption risk level of the code segment; The third filtering module is used to determine a third weight for different code segments in the second code segment set, and add code segments with a third weight greater than the third weight threshold to the third code segment set; the third weight is negatively correlated with the response efficiency of the code segment. The fourth sorting module is used to determine a fourth weight for different code segments in the third code segment set, and sort the code segments in the third code segment set in descending order of the fourth weight, and determine the code segment optimization order based on the sorting result; the fourth weight is negatively correlated with the optimization difficulty of the code segment, and the fourth weight is positively correlated with the business risk level of the code segment.
12. An electronic device, comprising: One or more processors; Memory, used to store one or more computer programs. The characteristic feature is that the one or more processors execute the one or more computer programs to implement the steps of the method according to any one of claims 1 to 10.
13. A computer-readable storage medium having a computer program or instructions stored thereon, characterized in that, When the computer program or instructions are executed by a processor, they implement the steps of the method according to any one of claims 1 to 10.
14. A computer program product comprising a computer program or instructions, characterized in that, When the computer program or instructions are executed by a processor, they implement the steps of the method according to any one of claims 1 to 10.