Source code reconstruction method and device, equipment and medium
By identifying target code snippets, generating calls, and refactoring input-output relationships, the problem of low efficiency in source code upgrades was solved, and automated performance optimization was achieved.
Patent Information
- Application Number
- CN202511626225.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-07
- Publication Date
- 2026-01-30
AI Technical Summary
Current technologies are inefficient at upgrading the source code of existing systems and cannot achieve automated performance modifications.
By acquiring the source code to be processed and the refactoring performance description, the target code segment is determined, the call relationship and input-output relationship are generated, and the target code segment is refactored based on these relationships to generate a refactored code segment, which replaces the original code to achieve performance optimization.
It improves the efficiency and automation of source code upgrades, and can optimize internal performance to meet system performance requirements without changing external functions and dependencies.
Smart Images

Figure CN121433715A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of information technology, and in particular to a source code reconstruction method, apparatus, device, and medium. Background Technology
[0002] As enterprise applications grow in scale and concurrency, their performance requirements for systems become increasingly demanding. The performance bottlenecks of the original system's source code (uncompiled code) directly affect the system's interface response, resource consumption, and stability. Developers typically need to refactor the original system's source code within the constraints of established performance standards (such as CPU (Central Processing Unit) performance, memory performance, and concurrency performance) to meet the system's performance requirements.
[0003] Currently, existing technologies involve developers manually modifying the original code to redevelop the functions implemented by the original system.
[0004] However, in the existing technology, upgrading the source code of the original system is inefficient and cannot automatically improve the performance of the source code. Summary of the Invention
[0005] This invention provides a source code refactoring method, apparatus, device, and medium. The embodiments of this invention can improve the efficiency and automation of upgrading the source code of existing systems.
[0006] In a first aspect, embodiments of the present invention provide a source code refactoring method, the method comprising:
[0007] Obtain the source code to be processed and the refactoring performance description;
[0008] Based on code refactoring standards, identify at least one target code segment in the source code to be processed;
[0009] Based on the source code to be processed, generate the corresponding call relationships and input / output relationships for the source code to be processed;
[0010] For each target code segment, the target code segment is reconstructed based on the call relationship and input-output relationship to obtain the reconstructed code segment;
[0011] The target code segment in the source code to be processed is replaced by each refactored code segment. The source code to be processed before and after the replacement performs the same function.
[0012] Secondly, embodiments of the present invention also provide a source code refactoring apparatus, the apparatus comprising:
[0013] The data acquisition module is used to acquire the source code to be processed and the refactoring performance description content;
[0014] The target code snippet determination module is used to determine at least one target code snippet in the source code to be processed based on code refactoring criteria.
[0015] The relationship generation module is used to generate the call relationships and input / output relationships corresponding to the source code to be processed.
[0016] The code snippet refactoring module is used to refactor each target code snippet based on the call relationship and input-output relationship to obtain the refactored code snippet.
[0017] The code replacement module is used to replace the target code segment in the source code to be processed with various refactored code segments. The source code to be processed before and after the replacement performs the same function.
[0018] Thirdly, embodiments of the present invention also provide a source code refactoring device, the source code refactoring device comprising:
[0019] At least one processor; and
[0020] A memory that is communicatively connected to at least one processor; wherein,
[0021] The memory stores a computer program that can be executed by at least one processor, such that the at least one processor is able to execute the source code refactoring method of any embodiment of the present invention.
[0022] Fourthly, embodiments of the present invention also provide a computer-readable storage medium storing computer instructions for causing a processor to execute and implement the source code refactoring method of any embodiment of the present invention.
[0023] The technical solution of this invention, by acquiring the source code to be processed and the refactoring performance description, allows subsequent refactoring activities to be carried out with reference to preset performance requirements; by identifying at least one target code segment in the source code to be processed according to code refactoring standards, the refactoring scope can be limited to specific implementation units related to performance requirements; by generating the call relationship corresponding to the source code to be processed, the dependency chain between the target code segment and other functions or modules can be clarified; by generating the input-output relationship corresponding to the source code to be processed, the external interaction boundary of the target code segment can be defined; by refactoring the target code segment under the constraints of the call relationship and input-output relationship, the internal implementation can be adjusted without changing the existing dependencies and data boundaries; and by targeting... Each target code segment generates a refactored code segment, enabling separate handling and step-by-step replacement of different implementation units. By replacing the original target code segment with a refactored code segment, refactoring changes can be confined to a controlled location. By organizing the refactoring and replacement sequence based on call relationships and input / output relationships, the processing of multiple target code segments can be carried out in an orderly manner. By keeping the external functionality unchanged after the replacement is completed, it is possible to update and optimize the internal implementation without affecting external behavior. This solves the technical problem that existing technologies for upgrading the source code of the original system are inefficient and cannot automatically modify the performance of the source code of the original system, thereby improving the efficiency and automation of upgrading the source code of the original system.
[0024] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of the present invention, nor is it intended to limit the scope of the invention. Other features of the invention will become readily apparent from the following description. Attached Figure Description
[0025] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0026] Figure 1 A flowchart of a source code refactoring method provided in an embodiment of the present invention;
[0027] Figure 2 A flowchart of a source code refactoring method provided in an embodiment of the present invention;
[0028] Figure 3 This is a schematic diagram of the structure of a source code refactoring device provided in an embodiment of the present invention;
[0029] Figure 4This is a schematic diagram of the structure of a source code refactoring device provided in an embodiment of the present invention. Detailed Implementation
[0030] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.
[0031] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0032] In the technical solutions of this invention, the acquisition, storage, and application of the source code to be processed and the reconstructed performance description content, etc., all comply with the provisions of relevant laws and regulations and do not violate public order and good morals.
[0033] Figure 1 This is a flowchart illustrating a source code refactoring method provided in an embodiment of the present invention. This embodiment is applicable to source code refactoring and can be executed by a source code refactoring device, which can be implemented in hardware and / or software.
[0034] See Figure 1 The source code refactoring methods shown include:
[0035] S101. Obtain the source code to be processed and the refactoring performance description.
[0036] The source code to be processed refers to a collection of software program source files selected for refactoring, which are text-based program entities used to describe program logic, algorithm flow, and functional implementation. As a static data carrier, the source code to be processed can generate a syntax tree and dependency structure through syntax parsing, thereby supporting subsequent analysis of call relationships and input / output relationships. The source code to be processed serves as input for refactoring analysis and performance evaluation, providing a data foundation for identifying target code snippets, generating call relationships, and performing refactoring and replacement operations.
[0037] The refactoring performance description refers to the descriptive information used to constrain the performance goals that the refactored code must achieve, manifested as performance metrics and their corresponding target values. The refactoring performance description includes at least one quantifiable performance metric, such as processor utilization, memory usage, concurrent response time, throughput, and latency. The refactoring performance description provides criteria for selecting target code segments and determining the refactoring type, thus giving the refactoring process quantifiable constraints.
[0038] S102. Based on the code refactoring standards, identify at least one target code segment in the source code to be processed.
[0039] In this context, the target code segment refers to the code segment in the source code to be processed that needs to be modified. The target code segment is identified as the specific implementation unit in the source code to be processed that needs to be refactored. It is the direct object of performance improvement and is used to determine the specific location that needs to be replaced or optimized. It is the starting point for the refactoring operation.
[0040] S103. Based on the source code to be processed, generate the corresponding call relationship and input / output relationship.
[0041] In this context, the call relationship refers to the call dependency structure between different functions or methods in the source code, used to describe the execution path and logical hierarchy of the program. The call relationship can include a call graph, which is a dependency graph composed of functions or methods as nodes and call operations as directed edges. It can contain static call, dynamic call, and recursive call information. For example, by identifying function call points through semantic analysis of the syntax tree and constructing a call dependency graph, the actual call path can be verified by combining dynamic runtime information.
[0042] The input-output relationship refers to the data flow in the code. It can include the final output location of each input parameter and the output parameters themselves.
[0043] S104. For each target code segment, reconstruct the target code segment according to the calling relationship and input-output relationship to obtain the reconstructed code segment.
[0044] In this context, a refactored code snippet refers to a replacement implementation unit obtained by adjusting or optimizing a target code snippet. It is functionally equivalent but offers improved performance. While maintaining consistency in external interfaces and input / output behavior, the internal structure is optimized or rewritten. External interactions are constrained by call relationships and input / output relationships, and a suitable pattern from the refactoring type library is selected for replacement based on the refactoring performance description. The refactored code snippet replaces the original target code snippet, enabling the overall source code to achieve performance optimization goals while maintaining functional consistency. For example, sequential I / O operations can be changed to asynchronous I / O calls, or inefficient linear searches can be replaced with hash lookups.
[0045] S105. Replace the target code segment in the source code to be processed with each refactored code segment. The source code to be processed before and after the replacement have the same function.
[0046] As can be seen, in this embodiment, by obtaining the source code to be processed and the refactoring performance description, subsequent refactoring activities can be carried out with reference to preset performance requirements; by determining at least one target code segment in the source code to be processed according to code refactoring standards, the scope of refactoring can be limited to specific implementation units related to performance requirements; by generating the call relationship corresponding to the source code to be processed, the dependency chain between the target code segment and other functions or modules can be clarified; by generating the input-output relationship corresponding to the source code to be processed, the external interaction boundary of the target code segment can be defined; by refactoring the target code segment under the constraints of the call relationship and input-output relationship, the internal implementation can be adjusted without changing the existing dependencies and data boundaries; by targeting Each target code segment generates a refactored code segment, enabling separate handling and step-by-step replacement of different implementation units. By replacing the original target code segment with a refactored code segment, refactoring changes can be confined to a controlled location. By organizing the refactoring and replacement sequence based on call relationships and input / output relationships, the processing of multiple target code segments can be carried out in an orderly manner. By keeping the external functionality unchanged after the replacement is completed, it is possible to update and optimize the internal implementation without affecting external behavior. This solves the technical problem that existing technologies for upgrading the source code of the original system are inefficient and cannot automatically modify the performance of the source code of the original system, thereby improving the efficiency and automation of upgrading the source code of the original system.
[0047] In an optional embodiment, Figure 2The flowchart of a source code refactoring method provided in this embodiment of the invention refines the step of "determining at least one target code segment in the source code to be processed according to the code refactoring standard" into "determining the current performance data of the source code to be processed according to the source code to be processed; determining the target performance data corresponding to the code refactoring standard according to the code refactoring standard; and determining at least one target code segment in the source code to be processed according to the difference between the current performance data and the target performance data", thereby improving the source code refactoring operation.
[0048] It should be noted that for parts not described in detail in the embodiments of the present invention, please refer to the descriptions in other embodiments.
[0049] See Figure 2 The source code refactoring methods shown include:
[0050] S201. Obtain the source code to be processed and the refactoring performance description.
[0051] S202. Determine the current performance data of the source code to be processed based on the source code to be processed.
[0052] The current performance data can refer to the set of performance indicator values obtained by running or analyzing the existing implementation state of the source code to be processed, which is used to reflect the actual performance of the code.
[0053] S203. Based on the code refactoring standards, determine the target performance data corresponding to the code refactoring standards. The current performance data is used to establish a performance benchmark for the current state of the source code, providing a basis for subsequent comparison with the target performance data, thereby determining the target code segment that needs to be refactored.
[0054] Among them, target performance data can refer to the set of expected performance indicators set according to the refactoring performance description or code refactoring standards, which are used to represent the performance goals that the code should achieve after refactoring.
[0055] S204. Based on the difference between the current performance data and the target performance data, identify at least one target code segment in the source code to be processed. The target performance data provides a benchmark for determining which source code segments do not meet the performance target, thereby guiding the identification of target code segments and the selection of refactoring directions.
[0056] Here, "difference" refers to the quantitative deviation between current performance data and target performance data under the same performance metric dimension, reflecting the gap between actual and expected performance. Based on the specific performance data showing this difference, the relevant code causing the performance difference is identified as the target code segment.
[0057] S205. Based on the source code to be processed, generate the corresponding call relationship and input / output relationship.
[0058] S206. For each target code segment, reconstruct the target code segment according to the calling relationship and input-output relationship to obtain the reconstructed code segment.
[0059] S207. Replace the target code segment in the source code to be processed with each refactored code segment. The source code to be processed before and after the replacement have the same function.
[0060] As can be seen, in this embodiment, by determining the current performance data based on the source code to be processed, the true performance of the source code under the current implementation state can be obtained, providing an objective basis for subsequent analysis; by determining the target performance data based on the code refactoring standard, an expected performance target can be established, which can be used as a reference standard for performance improvement; by comparing the current performance data with the target performance data, the gap between the source code performance and the target performance can be quantitatively reflected; by determining at least one target code segment in the source code to be processed based on the performance difference, the refactoring scope can be focused on the specific implementation location where the performance is substandard.
[0061] In some embodiments, at least one target code segment is identified in the source code to be processed based on the difference between current performance data and target performance data, including:
[0062] Based on the difference between the current performance data and the target performance data, identify at least one difference in the source code to be processed that corresponds to the difference.
[0063] Based on the differences in the code, determine the refactoring code segment of the source code to be processed.
[0064] In this context, a difference code refers to a code segment in the source code to be processed where there is a significant deviation between the current performance data and the target performance data. It is used to characterize the location of the source code that is not performing as expected. Each difference code has a unique source location identifier (file path, line number range) and corresponds to a performance metric category (such as CPU, memory, concurrency). The difference code is the basis for locating performance problems and determining the target code segment, guiding the precise scope of the refactoring operation.
[0065] As can be seen, in this embodiment, by identifying at least one difference code corresponding to the difference, the performance problem can be accurately mapped to a specific source code implementation unit. By determining the target code segment based on the difference code, the automatic association from performance deviation to refactoring target can be achieved. This difference-based localization method can reduce the interference of manual analysis and improve the accuracy of target code segment identification.
[0066] In some embodiments, the performance data includes processor performance data, memory performance data, and concurrency performance data.
[0067] Processor performance data refers to a set of data reflecting the runtime characteristics of the source code being processed at the processor level. Processor performance data includes metrics such as instruction execution time, CPU utilization, context switch count, cache hit rate, branch prediction hit rate, and thread switching overhead, and can be sampled at the function, thread, and process levels. Processor performance data is used to analyze the distribution of computational load, providing a basis for identifying dissimilar code and determining refactoring optimization directions.
[0068] Memory performance data refers to a set of performance metrics that reflect the source code's memory resource usage and access efficiency. Memory performance data includes parameters such as memory allocation rate, peak memory usage, garbage collection (GC) counts and pause times, cache utilization, and page fault count. Memory performance data is used to identify code performance degradation caused by duplicate allocation, cache misses, or memory leaks.
[0069] Concurrency performance data refers to a collection of data describing the performance of source code during multi-threaded or multi-task concurrent execution. Concurrency performance data includes parameters such as the number of threads, lock wait time, number of contention attempts, context switching frequency, blocking wait duration, and task scheduling latency. Concurrency performance data is used to identify code segments with performance degradation caused by lock contention, synchronization blocking, or uneven task scheduling.
[0070] As can be seen, in this embodiment, by combining three types of performance data—processor, memory, and concurrency—the overall performance status of the source code can be comprehensively characterized from different dimensions. By setting up multi-dimensional performance data, a more comprehensive reference basis can be provided for subsequent performance difference analysis. By using the three types of performance data in combination, the accuracy of target code segment location and the coverage of performance problem identification can be improved.
[0071] In some embodiments, based on the source code to be processed, the call relationship and input / output relationship corresponding to the source code to be processed are generated, including:
[0072] Generate a syntax tree corresponding to the source code to be processed;
[0073] Based on the syntax tree, generate the call relationships and input / output relationships corresponding to the source code to be processed.
[0074] In this context, a syntax tree (SBR) can refer to an abstract syntactic representation of the source code structure, used to express the semantics of the code in a hierarchical manner. A syntax tree is a tree-like structure composed of nodes and edges. Nodes represent language elements (such as expressions, statements, and function definitions), edges represent syntactic hierarchical relationships, and each node carries type information and source location identifiers.
[0075] As can be seen, in this embodiment, by generating a syntax tree based on the source code to be processed, the syntax structure of the source code can be transformed into a parsable hierarchical representation, thereby providing a structural foundation for subsequent relation generation. By generating call relationships based on the syntax tree, the call dependencies between different functions or methods can be identified, making the execution path of the program logic clearly expressed. By establishing the above relation structure, dependency constraints and boundary conditions can be provided for subsequent refactoring operations, ensuring that the refactored source code remains consistent with the original implementation at the logical and data levels.
[0076] In some embodiments, for each target code segment, the target code segment is reconstructed according to the call relationship and input-output relationship to obtain the reconstructed code segment, including:
[0077] For each target code fragment pair, the refactoring type is determined in the refactoring code pattern library based on the pre-built refactoring code type library. The performance data of the code corresponding to the refactored source code to be processed are different for each refactoring type.
[0078] Based on the call relationship, input-output relationship, and refactoring type, the refactored code snippet is refactored to obtain the code corresponding to the source code to be processed after refactoring.
[0079] In this context, a refactoring code type library refers to a pre-established set of code refactoring patterns used to guide the selection of refactoring methods for target code snippets. The refactoring code type library provides knowledge support for automatically selecting refactoring strategies, achieving different types of performance optimization goals.
[0080] In this context, the refactoring type refers to the type of code structure optimization method or pattern selected to address a specific performance issue. The refactoring type directly determines the specific refactoring method, such as data structure replacement, concurrency mechanism adjustment, and algorithm rearrangement.
[0081] As can be seen, in this embodiment, by determining the refactoring type from the pre-built refactoring code type library for each target code segment, different refactoring strategies can be applied to different types of performance problems, thereby improving the targeting of optimization. By matching the refactoring type in the refactoring code type library, automated refactoring type selection can be achieved based on existing rules or patterns, reducing manual intervention.
[0082] In some embodiments, it also includes:
[0083] Retrieve comments from the source code to be processed;
[0084] Based on the source code to be processed, generate the corresponding call relationships and input / output relationships for the source code to be processed;
[0085] Based on the call relationship, input-output relationship and comments, generate a functional description document for the source code to be processed. The functional description document includes a description of the functions implemented by the source code to be processed, as well as the location identifier of the code segment corresponding to each functional description.
[0086] Use the functional description document as the functional description document for the code corresponding to the source code to be processed after refactoring.
[0087] Location identifiers can refer to information used to uniquely locate the position of source code or refactored code snippets within the source file. Location identifiers are used to establish a mapping between functional description documents and source code, enabling traceability from functionality to code.
[0088] As can be seen, in this embodiment, by obtaining the comments of the source code to be processed, the semantic information retained during the development stage can be used to provide a textual basis for subsequent function identification. By generating call relationships and input-output relationships based on the source code to be processed, static semantic information of code structure and data interaction can be obtained, providing structural support for function mapping. By combining call relationships, input-output relationships and comments to generate a function description document, the functional implementation of the source code and structural information can be integrated at the logical level, thereby forming a function-oriented documented expression. By using the function description document as the function description document of the reconstructed source code, the consistency of the document before and after the reconstruction can be guaranteed, and the traceability of the reconstruction process can be achieved.
[0089] In an optional embodiment, the source code input from the legacy system is first received via an AI reverse engineering module. A lexical analyzer converts the source code into a token sequence, a syntax analyzer constructs an abstract syntax tree, and a control flow analyzer generates function call graphs and control flow graphs. Simultaneously, a dynamic analysis component is activated, inserting probes at key locations using bytecode instrumentation technology. The code is executed in a test environment, runtime behavior is monitored, and function call sequences and data transmission paths are recorded. Subsequently, an information fusion component correlates and aligns the structural information obtained from static analysis with the runtime data acquired from dynamic analysis, and generates a complete set of business logic rules and a data flow graph through a multi-source data fusion algorithm.
[0090] Next, the automatic document generation module receives the parsing results, retrieves matching document templates from the template database, maps business logic rules into structured text descriptions through a natural language generation engine, identifies the corresponding positions of code elements in the document using semantic role labeling technology, and generates system documents containing business function descriptions, data processing flows, and interface definitions.
[0091] The AI-powered refactoring analysis module receives system documents and enterprise input requirements specifications. It converts textual requirements into formal logical assertions through a rule-based reasoning engine, analyzes the differences between existing code and the target state using a machine learning model, identifies code sniffing issues through a code smell detection algorithm, selects appropriate refactoring strategies from a refactoring pattern library based on a multi-objective optimization algorithm, and generates a refactoring scheme containing specific modification instructions.
[0092] Finally, the code refactoring execution module loads the original source code and performs code refactoring according to the modification instructions provided in the refactoring plan. It employs application transformation techniques, uses syntax tree modification operations to extract and parameterize functions, optimizes the program structure through control flow transformation, and uses semantic preservation transformation to ensure functional invariance. After completing the modifications, it verifies the correctness of the refactored code through symbolic execution and test cases, ultimately outputting a refactored system that meets enterprise needs and performance metrics. The entire process coordinates the execution order of each module through a workflow engine, employs exception handling mechanisms to ensure the reliability of the processing, and records the entire process operation trace through a logging system.
[0093] Figure 3 This invention provides a schematic diagram of a source code refactoring device. This invention is applicable to source code refactoring and can execute source code refactoring methods. The device can be implemented in hardware and / or software.
[0094] See Figure 3 The source code refactoring apparatus shown includes: a data acquisition module 301, a target code fragment determination module 302, a relation generation module 303, a refactored code fragment determination module 304, and a code replacement module 305, wherein...
[0095] Data acquisition module 301 is used to acquire the source code to be processed and the refactoring performance description content;
[0096] The target code fragment determination module 302 is used to determine at least one target code fragment in the source code to be processed according to code refactoring criteria.
[0097] The relationship generation module 303 is used to generate the call relationship and input / output relationship corresponding to the source code to be processed based on the source code to be processed.
[0098] The code fragment refactoring module 304 is used to refactor each target code fragment according to the calling relationship and the input-output relationship to obtain the refactored code fragment.
[0099] The code replacement module 305 is used to replace the target code segment in the source code to be processed with each refactored code segment. The source code to be processed before replacement and the source code to be processed after replacement have the same function.
[0100] The technical solution of this invention, by acquiring the source code to be processed and the refactoring performance description, allows subsequent refactoring activities to be carried out with reference to preset performance requirements; by identifying at least one target code segment in the source code to be processed according to code refactoring standards, the refactoring scope can be limited to specific implementation units related to performance requirements; by generating the call relationship corresponding to the source code to be processed, the dependency chain between the target code segment and other functions or modules can be clarified; by generating the input-output relationship corresponding to the source code to be processed, the external interaction boundary of the target code segment can be defined; by refactoring the target code segment under the constraints of the call relationship and input-output relationship, the internal implementation can be adjusted without changing the existing dependencies and data boundaries; and by targeting... Each target code segment generates a refactored code segment, enabling separate handling and step-by-step replacement of different implementation units. By replacing the original target code segment with a refactored code segment, refactoring changes can be confined to a controlled location. By organizing the refactoring and replacement sequence based on call relationships and input / output relationships, the processing of multiple target code segments can be carried out in an orderly manner. By keeping the external functionality unchanged after the replacement is completed, it is possible to update and optimize the internal implementation without affecting external behavior. This solves the technical problem that existing technologies for upgrading the source code of the original system are inefficient and cannot automatically modify the performance of the source code of the original system, thereby improving the efficiency and automation of upgrading the source code of the original system.
[0101] In some embodiments, in determining at least one target code fragment in the source code to be processed according to code refactoring criteria, the target code fragment determining module 302 is specifically configured to:
[0102] Based on the source code to be processed, determine the current performance data of the source code to be processed;
[0103] Based on the code refactoring standards, determine the target performance data corresponding to the code refactoring standards;
[0104] Based on the difference between the current performance data and the target performance data, identify at least one target code segment in the source code to be processed.
[0105] In some embodiments, in determining at least one target code fragment in the source code to be processed based on the difference between current performance data and target performance data, the target code fragment determining module 302 is specifically configured to:
[0106] Based on the difference between the current performance data and the target performance data, identify at least one difference in the source code to be processed that corresponds to the difference.
[0107] Based on the differences in the code, determine the refactoring code segment of the source code to be processed.
[0108] In some embodiments, the performance data includes processor performance data, memory performance data, and concurrency performance data.
[0109] In some embodiments, in generating the call relationships and input / output relationships corresponding to the source code to be processed, based on the source code to be processed, the relationship generation module 303 is specifically used for:
[0110] Generate a syntax tree corresponding to the source code to be processed;
[0111] Based on the syntax tree, generate the call relationships and input / output relationships corresponding to the source code to be processed.
[0112] In some embodiments, in reconstructing the code fragment according to the call relationship and input-output relationship to obtain the code corresponding to the reconstructed source code to be processed, the reconstructed code fragment determination module 304 is specifically used for:
[0113] For each target code fragment pair, the refactoring type is determined in the refactoring code pattern library based on the pre-built refactoring code type library. The performance data of the code corresponding to the refactored source code to be processed are different for each refactoring type.
[0114] Based on the call relationship, input-output relationship, and refactoring type, the refactored code snippet is refactored to obtain the code corresponding to the refactored source code to be processed.
[0115] In some embodiments, the source code refactoring apparatus further includes:
[0116] The annotation retrieval module is used to retrieve the annotations from the source code to be processed.
[0117] The relationship generation module is used to generate the call relationships and input / output relationships corresponding to the source code to be processed.
[0118] The document generation module is used to generate a functional description document for the source code to be processed based on the call relationship, input-output relationship and comments. The functional description document includes a description of the functions implemented by the source code to be processed, as well as the location identifier of the code segment corresponding to each functional description.
[0119] The document replacement module is used to replace the functional description document with the functional description document of the code to be processed after refactoring.
[0120] The source code refactoring apparatus provided in this embodiment of the invention can execute the source code refactoring method provided in any embodiment of the invention, and has the corresponding functional modules and beneficial effects for executing the source code refactoring method.
[0121] Figure 4This is a schematic diagram of the structure of a source code refactoring device provided in an embodiment of the present invention.
[0122] like Figure 4 As shown, the source code refactoring device 400 includes at least one processor 401 and a memory, such as a read-only memory (ROM) 402 and a random access memory (RAM) 403, communicatively connected to the at least one processor 401. The memory stores computer programs executable by the at least one processor. The processor 401 can perform various appropriate actions and processes based on the computer program stored in the ROM 402 or loaded into the RAM 403 from storage unit 408. The RAM 403 may also store various programs and data required for the operation of the source code refactoring device 400. The processor 401, ROM 402, and RAM 403 are interconnected via a bus 404. An input / output (I / O) interface 408 is also connected to the bus 404.
[0123] Multiple components in the source code refactoring device 400 are connected to the I / O interface 405, including: an input unit 406, such as a keyboard, mouse, etc.; an output unit 407, such as various types of displays, speakers, etc.; a storage unit 408, such as a disk, optical disk, etc.; and a communication unit 409, such as a network card, modem, wireless transceiver, etc. The communication unit 409 allows the source code refactoring device 400 to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.
[0124] Processor 401 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of processor 401 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. Processor 401 performs the various methods and processes described above, such as source code refactoring methods.
[0125] In some embodiments, the source code refactoring method may be implemented as a computer program tangibly contained in a computer-readable storage medium, such as storage unit 408. In some embodiments, part or all of the computer program may be loaded and / or installed onto the source code refactoring device 400 via ROM 402 and / or communication unit 409. When the computer program is loaded into RAM 403 and executed by processor 401, one or more steps of the source code refactoring method described above may be performed. Alternatively, in other embodiments, processor 401 may be configured to perform the source code refactoring method by any other suitable means (e.g., by means of firmware).
[0126] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations may include: implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.
[0127] Computer programs used to implement the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, such that when executed by the processor, the computer programs cause the functions / operations specified in the flowcharts and / or block diagrams to be performed. The computer programs may be executed entirely on a machine, partially on a machine, or as a standalone software package, partially on a machine and partially on a remote machine, or entirely on a remote machine or server.
[0128] In the context of this invention, a computer-readable storage medium can be a tangible medium that may contain or store a computer program for use by or in conjunction with an instruction execution system, apparatus, or device. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination thereof. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0129] To provide interaction with the user, the systems and techniques described herein can be implemented on an operational detection device. This source code reconstruction device includes: a display device for displaying information to the user (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor); and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the source code reconstruction device. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).
[0130] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as data servers), or middleware components (e.g., application servers), or frontend components (e.g., user computers with graphical user interfaces or web browsers through which users can interact with implementations of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., communication networks). Examples of communication networks include local area networks (LANs), wide area networks (WANs), blockchain networks, and the Internet.
[0131] A computing system can include clients and servers. Clients and servers are generally located far apart and typically interact through communication networks. The client-server relationship is created by computer programs running on the respective computers and having a client-server relationship with each other. The server can be a cloud server, also known as a cloud computing server or cloud host, which is a hosting product within the cloud computing service system. It addresses the shortcomings of traditional physical hosts and Virtual Private Server (VPS) services, such as high management difficulty and weak business scalability.
[0132] It should be understood that the various forms of processes shown above can be used, with steps reordered, added, or deleted. For example, the steps described in this invention can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution of this invention can be achieved, and this is not limited herein.
[0133] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.
Claims
1. A source code refactoring method characterized by, The method comprises: acquiring a source code to be processed and a performance description content to be reconstructed; determining at least one target code segment in the source code to be processed according to the code reconstruction standard; generating a call relationship and an input-output relationship corresponding to the source code to be processed according to the source code to be processed; reconstructing the target code segment according to the call relationship and the input-output relationship to obtain a reconstructed code segment; replacing the target code segment in the source code to be processed with the reconstructed code segment, and the source code to be processed before replacement and the source code to be processed after replacement have the same function.
2. The method of claim 1, wherein, The method comprises: determining current performance data of the source code to be processed according to the source code to be processed; determining target performance data corresponding to the code reconstruction standard according to the code reconstruction standard; determining at least one target code segment in the source code to be processed according to the difference between the current performance data and the target performance data.
3. The method of claim 2, wherein, The method comprises: determining at least one difference code in the source code to be processed corresponding to the difference between the current performance data and the target performance data according to the difference between the current performance data and the target performance data; determining a reconstruction code segment of the source code to be processed according to each difference code.
4. The method of claim 2, wherein, The performance data comprises processor performance data, memory performance data and concurrency performance data.
5. The method of claim 1, wherein, The method comprises: generating a syntax tree corresponding to the source code to be processed according to the source code to be processed; generating a call relationship and an input-output relationship corresponding to the source code to be processed according to the syntax tree.
6. The method of claim 1, wherein, The method comprises: determining a reconstruction type in the reconstruction code mode library according to a pre-constructed reconstruction code type library for each target code segment, and the performance data between the reconstructed source code corresponding to different reconstruction types is different; reconstructing the reconstructed code segment according to the call relationship, the input-output relationship and the reconstruction type to obtain the code corresponding to the reconstructed source code.
7. The method of claim 1, wherein, The method further comprises: acquiring an annotation of the source code to be processed; generating a call relationship and an input-output relationship corresponding to the source code to be processed according to the source code to be processed; generating a function description document of the source code to be processed according to the call relationship, the input-output relationship and the annotation, the function description document comprising function descriptions of the source code to be processed and position identifiers of code segments corresponding to each function description; The function description document is used as a function description document of the code corresponding to the to-be-processed source code after reconstruction.
8. A source code refactoring apparatus characterized by comprising: The method comprises the following steps: a data acquisition module is configured to acquire to-be-processed source code and reconstruction performance description content; a target code segment determination module is configured to determine at least one target code segment in the to-be-processed source code according to the code reconstruction standard; a relationship generation module is configured to generate a calling relationship and an input-output relationship corresponding to the to-be-processed source code according to the to-be-processed source code; a reconstructed code segment determination module is configured to reconstruct each target code segment according to the calling relationship and the input-output relationship, to obtain a reconstructed code segment; a code replacement module is configured to replace the target code segment in the to-be-processed source code with each reconstructed code segment, and the to-be-processed source code before replacement and the to-be-processed source code after replacement implement the same function.
9. A source code refactoring device, characterized by, The source code reconstruction device comprises: at least one processor; and a memory connected in communication with the at least one processor; wherein the memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor to enable the at least one processor to execute the source code reconstruction method in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer readable storage medium stores computer instructions for enabling a processor to execute the source code reconstruction method in any one of claims 1-7 when executed by the processor.
Citation Information
Cited By
Code incremental injection method and device, electronic equipment and storage medium
CN122086461A
