Code optimization method and system

By converting the model into a combined abstract function and optimizing the data type of variables, the problem of excessive memory usage caused by excessive data types in model development is solved, and the software performance and reliability are improved.

CN119938047APending Publication Date: 2025-05-06YINWANG INTELLIGENT TECHNOLOGIES CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311399965.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-10-26
Publication Date
2025-05-06

AI Technical Summary

Technical Problem

In model-based development, the data type definition of variables used in the model is too long, resulting in excessive memory usage of generated code when running, affecting the software performance and reliability of the automotive embedded environment.

Method used

By converting the model into a combined abstract function, the data type of the variable is optimized based on the value range of the variables of the combined abstract function, and the optimized code is generated to reduce memory usage.

Benefits of technology

The optimization of variable data types in the model is realized, saving memory usage during code runtime, and improving the performance and reliability of the software in automotive embedded environments.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119938047A_ABST
    Figure CN119938047A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a code optimization method and system. The method comprises the steps that a first module converts a model into a combined abstract function; the second module optimizes the data type of at least one variable according to the value range of the at least one variable of the combined abstract function to obtain an optimized combined abstract function; and the third module generates a software code corresponding to the business model based on the optimized combined abstract function. According to the scheme, the data type of the variable used in the model can be optimized, and the memory occupied when the code generated based on the model runs is saved; according to the scheme, the abstract function is used as the intermediate representation of the model, calculation logic which does not need to be concerned and control logic influencing variable optimization during variable optimization can be shielded, and the optimization effect and efficiency of the variable can be improved; besides, the scheme can be realized in a model-based development process, variable optimization can be completed without running software codes, and the implementation mode is simple, quick and low in cost.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of software technology, and in particular to a code optimization method and system. Background Art

[0002] Software development in the automotive field has the characteristics of large amount of code, complex module interaction, and embedded environment. The use of traditional code-based development will make software design and development prone to errors, software verification and debugging time-consuming and labor-intensive, and software maintenance and transplantation very difficult. The visualization and abstraction brought by model-based development not only allow developers to express complex inter-module relationships and system behaviors through graphical modeling, but also make it easier to understand, modify and reuse various parts of the system; and the simulation test capabilities provided by model-based development can help developers discover and solve potential problems before burning into the embedded environment; in addition, model-based development can automatically generate code, thereby reducing the workload of manual coding and the risk of errors. Therefore, in the automotive industry, model-based development is more mainstream and common than traditional code-based development.

[0003] However, when developing software based on models, the following problems are often encountered: the data type definition of the variables used in the model is too long, resulting in excessive memory usage when the generated code is running, which greatly affects the performance and reliability of software running in an automotive embedded environment. Summary of the invention

[0004] The embodiments of the present application provide a code optimization method and system to optimize the data types of variables used in the model and save memory occupied by the code when it is running.

[0005] In a first aspect, a code optimization method is provided, comprising: a first module converts a model into a combined abstract function; wherein the model is an abstract representation of a business model, the model includes multiple components with fixed input-output relationships, the combined abstract function includes multiple abstract functions with fixed input-output relationships, the multiple components correspond to the multiple abstract functions one-to-one, each of the multiple abstract functions is used to indicate the semantics of the component corresponding to each abstract function, and the variables of each abstract function are the same as the variables of the component corresponding to each abstract function; the second module optimizes the data type of at least one variable according to the value range of at least one variable of the combined abstract function to obtain an optimized combined abstract function; the third module generates software code corresponding to the business model based on the optimized combined abstract function.

[0006] This solution can optimize the data types of variables used in the model, and can save the memory occupied by the code generated based on the model when running. In addition, this solution uses abstract functions as the intermediate representation of the model, which can shield the calculation logic that does not need to be concerned about when optimizing variables and the control logic that affects variable optimization (such as loops, selections and other control structures), so that the optimization only needs to care about the parts related to the semantics of the model components, which can improve the optimization effect and efficiency of the variable data type. In addition, this solution can be implemented in the model-based development process (such as in the development tool), and can complete the optimization of variable data types without running software code (such as before deploying the code to the automotive embedded environment). The implementation method is simple, fast and low-cost.

[0007] In one possible design, the first module includes a first sub-module, a second sub-module and a third sub-module; the model also includes component configuration and data description, the component configuration is used to indicate the data type of the input port and / or output port of each component in the multiple components, and the data description is used to indicate the parameters configured for at least one component in the multiple components; the first sub-module determines a corresponding abstract function for each component in the multiple components to obtain multiple abstract functions; the second sub-module configures the input and output of the abstract function corresponding to each component according to the component configuration and the data description; the third sub-module configures the input and output relationship between each abstract function in the multiple abstract functions according to the input and output relationship between each component in the multiple components to obtain a combined abstract function.

[0008] This design method provides a specific implementation method for converting the model into a combined abstract function. The composition of the abstract function determined based on this method is very similar to the components in the model, and there are no control structures such as loops and selections in the abstract function, which is conducive to improving the efficiency of the subsequent analysis of the numerical range of variables.

[0009] In one possible design, the second module includes a fourth submodule and a fifth submodule; the fourth submodule determines the initial value range of each variable in at least one variable based on the component configuration and data description; the fifth submodule solves the minimum value range of each variable in at least one variable based on the initial value range of each variable in at least one variable and the combined abstract function to obtain the minimum value range of each variable in at least one variable; if the data type of any variable in at least one variable does not match the minimum value range of any variable, the data type of any variable is adjusted to a data type that matches the minimum value range of any variable.

[0010] This design method provides a specific method for tuning the data type of variables based on combined abstract functions. The data type of the variable and the minimum value range of the variable after tuning based on this method can avoid the problem of excessive memory usage when the generated code is running due to the variable data type definition being too long.

[0011] In one possible design, the fourth submodule can determine the numerical range and data type configured for the first variable based on the component configuration and data description, where the first variable is any variable among at least one variable; the fourth submodule determines the intersection of the numerical range configured for the first variable and the numerical range corresponding to the data type configured for the first variable as the initial value range of the first variable.

[0012] This design method determines the initial value range of the variable based on the intersection of the range that the variable's data type can represent and the configured numerical range, which can ensure that the initial value range is not too large, and helps to improve the efficiency and accuracy of variable numerical range analysis.

[0013] In one possible design, the second module may further include a sixth submodule; the sixth submodule may determine one or more candidate variables based on the combined abstract function, and the sixth submodule determines at least one variable based on the one or more candidate variables; wherein the candidate variables are internal variables in the model, and the internal variables are other variables except variables input from the outside to the model and variables output from the model to the outside; and / or, the candidate variables are called during the process of the model being executed.

[0014] This design method regards the variables used in the model as variables to be optimized, and deletes the port variables of components that will not be used in the model, which can narrow the scope of variables to be optimized and save computing resources.

[0015] In a possible design, the sixth submodule uses one or more candidate variables as at least one variable. In other words, all variables in the combined abstract function (or model) are used as variables to be optimized.

[0016] This design method is simple to implement and can realize automatic tuning of variables.

[0017] In one possible design, the sixth submodule outputs one or more candidate variables through the human-computer interaction interface, receives instructions through the human-computer interaction interface, and determines at least one variable according to the instructions.

[0018] In this design method, humans can participate in selecting the variables to be optimized, which can improve the user experience.

[0019] In a second aspect, a code optimization system is provided, which includes modules or units or technical means for implementing the method described in the first aspect or any possible design of the first aspect.

[0020] Exemplarily, the device may include:

[0021] The first module is used to convert the model into a combined abstract function; wherein the model is an abstract representation of the business model, the model includes multiple components with fixed input-output relationships, the combined abstract function includes multiple abstract functions with fixed input-output relationships, the multiple components correspond to the multiple abstract functions one-to-one, each of the multiple abstract functions is used to indicate the semantics of the component corresponding to each abstract function, and the variables of each abstract function are the same as the variables of the component corresponding to each abstract function;

[0022] The second module is used to optimize the data type of at least one variable according to the value range of at least one variable of the combined abstract function to obtain an optimized combined abstract function;

[0023] The third module is used to generate software codes corresponding to the business model based on the optimized combined abstract function.

[0024] In one possible design, the first module includes a first sub-module, a second sub-module and a third sub-module; the model also includes a component configuration and a data description, the component configuration is used to indicate the data type of the input port and / or output port of each component in the multiple components, and the data description is used to indicate the parameters configured for at least one component in the multiple components; the first sub-module is used to determine the corresponding abstract function for each component in the multiple components to obtain multiple abstract functions; the second sub-module is used to configure the input and output of the abstract function corresponding to each component according to the component configuration and the data description; the third sub-module is used to configure the input and output relationship between each abstract function in the multiple abstract functions according to the input and output relationship between each component in the multiple components to obtain a combined abstract function.

[0025] In one possible design, the second module includes a fourth sub-module and a fifth sub-module; the fourth sub-module is used to determine the initial value range of each variable in at least one variable according to the component configuration and data description; the fifth sub-module is used to solve the minimum value range of each variable in at least one variable based on the initial value range of each variable in at least one variable and the combined abstract function to obtain the minimum value range of each variable in at least one variable; if the data type of any variable in at least one variable does not match the minimum value range of any variable, the data type of any variable is adjusted to a data type that matches the minimum value range of any variable.

[0026] In one possible design, the fourth submodule is used to determine the numerical range and data type configured for the first variable based on the component configuration and data description, where the first variable is any variable among at least one variable; and determine the intersection of the numerical range configured for the first variable and the numerical range corresponding to the data type configured for the first variable as the initial value range of the first variable.

[0027] In one possible design, the second module may also include a sixth submodule; the sixth submodule is used to determine one or more candidate variables based on the combined abstract function, and the sixth submodule determines at least one variable based on the one or more candidate variables; wherein the candidate variables are internal variables in the model, and the internal variables are other variables except variables input from the outside to the model and variables output from the model to the outside; and / or, the candidate variables are called during the process of the model being executed.

[0028] In one possible design, the sixth submodule is used to take one or more candidate variables as at least one variable; or, output one or more candidate variables through the human-computer interaction interface, receive instructions through the human-computer interaction interface, and determine at least one variable according to the instructions.

[0029] In a third aspect, a processing device is provided, comprising: a processor and a memory; the memory is used to store computer-executable instructions; the processor is used to execute the computer-executable instructions stored in the memory, so that the communication device performs the method described in the first aspect or any possible design of the first aspect.

[0030] According to a fourth aspect, a computer-readable storage medium is provided, wherein the computer-readable storage medium is used to store instructions, and when the instructions are executed, the first aspect or any possible design of the first aspect is implemented.

[0031] According to a fifth aspect, a computer program product is provided, wherein instructions are stored in the computer program product, and when the computer program product is run on a computer, the computer is caused to execute the first aspect or any possible design of the first aspect.

[0032] The beneficial effects of each design method in the second to fifth aspects mentioned above can refer to the beneficial effects of the corresponding design method in the first aspect, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] Figure 1 A comparison chart of the code-based software development process and the model-based software development process;

[0034] Figure 2 Develop process diagrams for model-based software;

[0035] Figure 3 A schematic diagram of a code optimization system provided in an embodiment of the present application;

[0036] Figure 4 A schematic diagram of the structure of a code optimization system provided in an embodiment of the present application;

[0037] Figure 5 A flowchart of a code optimization method provided in an embodiment of the present application;

[0038] Figure 6 A schematic diagram of a possible model is shown below;

[0039] Figure 7 An example diagram of a code optimization process provided in an embodiment of the present application;

[0040] Figure 8 A schematic diagram of a combined abstract function based on an abstract syntax tree;

[0041] Fig. 9 A schematic diagram of the structure of a model-based development tool provided in an embodiment of the present application;

[0042] Fig.10 A schematic diagram of a possible model is shown below;

[0043] Fig.11 Example diagram for the code generation page in the development tool;

[0044] Fig.12 This is a sample image of the editing page in the development tool;

[0045] Fig.13 This is a sample diagram of the code generated after enabling data type tuning;

[0046] Fig.14 This is a sample diagram of the code generated without enabling data type tuning;

[0047] Fig.15 A schematic diagram of the structure of a processing device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0048] In order to better understand the implementation of this application, some technical terms involved in the implementation of this application are explained below:

[0049] (1) Embedded environment: The embedded environment includes embedded systems, embedded devices, embedded software, etc. The embedded environment has the characteristics of limited resources, low power consumption, and high real-time requirements. The embedded environment is widely used in fields such as automobiles, where software and hardware are tightly integrated to achieve control, management, and monitoring functions.

[0050] (2) Model-based development: Model-based development is a commonly used software development method. The design, development, and testing process of the software system is mainly based on the creation and use of models. Developers use graphical tools to create abstract models that describe the system structure, behavior, and interaction. They use the models to design, verify, and simulate the system, and generate software code and documentation.

[0051] See also Figure 1, which is a comparison chart of the traditional code-based software development process and the model-based software development process. The model-based software development method can help developers better understand and communicate system requirements, reduce errors in the development process, and provide better maintainability and scalability.

[0052] (3) Data type: Data type is used to describe the range and type of values ​​that a variable can store. Data types can be divided into basic data types and compound data types. Basic data types generally include integers, floating-point numbers, and characters, and have fixed sizes and ranges; compound data types are data types composed of basic data types or other compound data types, and common types include arrays, structures, classes, and enumerations. By using appropriate data types in a program, you can improve the performance of your code.

[0053] (4) Code optimization: Code optimization refers to the improvement and optimization of code during the software development process, including optimization of memory management, algorithm logic, etc., to improve the performance and efficiency of the program. The goal of code optimization is to reduce resource consumption during program operation, making it execute faster and more efficiently.

[0054] like Figure 2 As shown in the figure, in model-based software development, the main process of generating code is: ① The developer (or user) creates a model in a model-based development tool, including building the model logic based on components in the tool, and configuring the data types of variables used in the model through component configuration and / or data description; ② The tool's code generator generates software code in the target language based on the model.

[0055] In this process, the following problems exist:

[0056] 1) Developers cannot determine the range of data, so they configure longer data types (such as 64-bit integer (int64)) to prevent overflow. However, in the actual running of the code, shorter data types (such as 8-bit integer (int8)) can be used to carry it;

[0057] 2) The developer fills in the data description in an external data file, and the tool imports the file for the model to use. Since the tool does not know the range of changes of the data during the code running process, it will use a longer data type by default to carry it. However, in fact, a shorter data type can be used to carry it during the software running process.

[0058] These problems will result in unnecessary long data type variable definitions in the final generated code, which will increase the memory usage when the code is running. For software running in an automotive embedded environment, it will greatly affect its performance and reliability.

[0059] In view of this, a technical solution according to an embodiment of the present application is provided, which can achieve tuning of the data type of variables in a model-based development process.

[0060] The embodiments of the present application can be applied to model-based software development scenarios, including but not limited to software development in the automotive field.

[0061] See also Figure 3 , a code optimization system provided by an embodiment of the present application, the system can be deployed in a model-based development tool, specifically in the code generator of the tool, for generating an intermediate representation of the model, and optimizing the data types of the variables based on the intermediate representation. In other words, after the code generator receives the model, it does not directly generate software code in the target language according to the model, but first converts the model into an intermediate representation, then tunes the data type of the variable based on the intermediate representation, and finally translates the intermediate representation after the data type is optimized into the software code of the target language. It can be understood that the code optimization system is only an example of a name, and there can actually be other names, such as a code optimization module, or a variable data type tuning module, etc.

[0062] For example, see Figure 4 , is a structural diagram of a code optimization system provided in an embodiment of the present application, the system includes a first module, a second module and a third module.

[0063] Figure 5 This is a flow chart of a code optimization method executed by the above code optimization system, which specifically includes S501 to S503:

[0064] S501, the first module converts the model into a combined abstract function;

[0065] Among them, the model is an abstract representation of the business model. The business model refers to the model of the business provided by the software to be developed (i.e., the software code to be generated). The business model may include the system structure, behavior, or interaction of the business. The representation method of the model can be implemented in a variety of ways. In the embodiment of the present application, a graphical model is used as an example, that is, a graphic is used to abstractly represent and describe the system structure, behavior, or interaction.

[0066] See also Figure 6 , is a possible model diagram. The model includes multiple components with fixed input-output relationships (or connection relationships, execution logic, etc.). Multiple components with fixed input-output relationships constitute the logic of the model (hereinafter referred to as model logic). The model logic can describe the behavior and function of the software.

[0067] In some examples, a component's functionality may take a single parameter, such as Figure 6The Constant component shown indicates the use of a parameter, namely parameter, which may be imported from the outside; the Constant_1 component indicates another parameter parameter_1 imported from the outside.

[0068] In other examples, a component can be one that gets an output based on one or more inputs, such as Figure 6 The AddSubstract component shown in , represents the element-by-element addition operation of two arrays (i.e., in0 and in1) to obtain an output. Figure 6 The connection relationship between the components shown, the input-output relationship between the AddSubstract component and the Constant component and Constant_2 is: the two inputs of the AddSubstract component are the outputs of the Constant component and Constant_2 respectively.

[0069] The model may also include a data description. The data description indicates parameters configured for at least one of the multiple components. The data description may be carried in a data file and imported from an external tool in the form of a data file, i.e., parameters (such as parameter, parameter_1) are imported from at least one of the multiple components externally. In the embodiments of the present application, the data description may also be referred to as other names, which are not limited by the present application.

[0070] The model may also include a component configuration, which is used to indicate the data type of the port (including input port and / or output port) of each component in one or more components. In the embodiment of the present application, the component configuration may also be referred to as the port configuration of the component or other names, which are not limited in the present application. The component configuration may be configured by the developer.

[0071] The variables to be optimized in the embodiment of the present application are variables in the software code of the target language finally generated by the model, and the input of the components in the model (such as Figure 6 in0 or in1, etc.), the output of the component (such as Figure 6 output), the parameters used by the component (such as Figure 6 ) will be mapped to variables in the software code, so the component input, component output, and component parameters can be called model variables (or the form of variables in the model). Among them, the component input, component output, and component parameters can be called component input variables, component output variables, and component parameter variables, respectively. Component input variables and component output variables can also be called component port variables.

[0072] As an intermediate representation of the model, the combined abstract function includes multiple abstract functions with fixed input-output relationships (or connection relationships), or the combination of multiple abstract functions constitutes a combined abstract function. The multiple abstract functions have fixed input-output relationships, for example, the output of one abstract function is the input of another abstract function.

[0073] Among them, an abstract function refers to a function that does not give a specific analytical expression, but only gives special conditions or characteristics of the function. The general form is, for example, y=f(x). Among them, x is the independent variable of the abstract function. In the embodiment of the present application, x is also called the input of the abstract function (input variable or input parameter), etc. An abstract function can have one or more inputs. An abstract function with two inputs is, for example, y=f(x1, x2); y is the dependent variable of the abstract function. In the embodiment of the present application, it is also called the output of the abstract function (output variable or output parameter), etc. Optionally, the abstract function may also have a domain (i.e., the value range of the independent variable x), a range (i.e., the value range of the dependent variable y), etc.

[0074] In the embodiment of the present application, the multiple components of the model correspond one to one with the multiple abstract functions of the combined abstract function. In other words, each component has a corresponding abstract function, and different components correspond to different abstract functions. Each abstract function is used to indicate the semantics (or function) of the component corresponding to the abstract function. For example, the semantics of the AddSubstract component is the element-by-element addition of two sets of numbers, that is, the output of the AddSubstract component is the element-by-element addition of the two sets of inputs, and its abstract function can be out = $array_add (in0, in1), $array_add means element-by-element addition. Of course, this is just an example, and the actual abstract function can also be in other forms.

[0075] After the component is converted into an abstract function, the variables of the abstract function correspond one-to-one with the variables of the component corresponding to each abstract function. Optionally, after the component is converted into an abstract function, the variables of the abstract function are the same (or consistent) with the variables in the original component. It can be understood that the representation of variables in the component and the representation in the abstract function can be the same or different. This article takes the same representation as an example. For example, the input variables in0, in1 and the output variable out in the AddSubstract component are still represented as: in0, in1 and out in the abstract function $array_add, such as out = $array_add(in0, in1).

[0076] The input-output relationship between multiple components corresponds to the input-output relationship between multiple abstract functions. For example, if the input of the first component is the output of the second component, the input of the abstract function corresponding to the first component is the output of the abstract function corresponding to the second component.

[0077] In the embodiment of the present application, the composition of the abstract function is very similar to the components in the model, and the abstract function no longer has control structures such as loops and selections, which can make the subsequent inference of the numerical range of the variable more concise and efficient.

[0078] In a specific implementation, the first module may include a plurality of different submodules, each of which performs a different function. Figure 7 As shown, the first module specifically includes a first submodule, a second submodule and a third submodule. When the first module converts the model into a combined abstract function, it may specifically include:

[0079] 1) The first submodule performs abstract function matching, such as determining a corresponding abstract function for each of the multiple components to obtain multiple abstract functions;

[0080] For example Figure 7 Example: The abstract function corresponding to the AddSubstract component is out = $array_add(in1, in2). For example, the abstract function corresponding to the input conditional selection component can be: out = $select(cond, in1, in2). Of course, this is just an example, and the actual abstract function can also be in other specific forms.

[0081] 2) The second submodule performs the input and output configuration of the abstract function, such as configuring the input and output of the abstract function corresponding to each component according to the component configuration and data description;

[0082] Exemplarily, the second submodule may configure the storage category, data type, value range, etc. of the input and output of each abstract function according to the component configuration and / or data description.

[0083] Optionally, when configuring the numerical range of the input and / or output of the abstract function, the second submodule may also structure the configured numerical range as additional information of the variable. Figure 7 Example: Given a textual configuration of "0,1,127", it can be converted to the range [0-127] of type uint8, which not only determines the numerical range, but also the minimum accommodating data type.

[0084] 3) The third submodule executes abstract function splicing, such as configuring the input-output relationship between each abstract function in multiple abstract functions according to the input-output relationship between each component in multiple components to obtain a combined abstract function.

[0085] Specifically, the third submodule sequentially splices the abstract functions according to the input-output relationship of the components in the model to obtain a combined abstract function with a fixed input-output relationship. Figure 7In the example: out1 = $select(out3, out0, out2) means that the input of the abstract function out1 = $select() is the output of the other three abstract functions (i.e., out3, out0, out2). Since the components in the model are scheduled in sequence, the abstract functions in the generated combined abstract function are also in a sequential structure, and each abstract template will be executed in sequence, which is conducive to the analysis of the value range of the variable.

[0086] In a possible implementation, the concatenated result (i.e., the combined abstract function) can be stored in the form of an Abstract Syntax Tree. Figure 8 As shown, Figure 6 Taking the model shown as an example, an example of a combined abstract function is given. The variables in the combined abstract function are the leaf nodes of the abstract syntax tree. The data description, component configuration and other information corresponding to the variables are stored as the attribute information of the leaf nodes. The connection relationship between the nodes in the abstract syntax tree can represent the input and output relationship between the abstract functions. Of course, Figure 8 This is only an example of a combined abstract function, and the actual content and storage format are not limited thereto.

[0087] S502, the second module optimizes the data type of at least one variable according to the value range of at least one variable of the combined abstract function to obtain an optimized combined abstract function;

[0088] In a specific implementation, the second module may include a plurality of different submodules, each of which performs a different function. Figure 7 As shown, the second module may specifically include a fourth submodule and a fifth submodule. Optionally, the second module may also include a sixth submodule. It is understandable that Figure 7 The dashed box in the middle indicates an optional step for identifying the variables to be optimized.

[0089] The fourth submodule may perform variable initial value analysis, such as determining the initial value range of each variable in at least one variable according to the component configuration and data description. Exemplarily, the fourth submodule determines the numerical range and data type configured for the first variable according to the component configuration and data description, where the first variable is any variable in the at least one variable; the fourth submodule determines the intersection of the numerical range configured for the first variable and the numerical range corresponding to the data type configured for the first variable as the initial value range of the first variable.

[0090] For example Figure 7In the example given in , for the variable out2 whose data type is int32 and whose configured value range is [2 to 129], the initial value range of the variable [2 to 129] and the data type uint8 can be obtained based on the intersection of the range that the data type can represent and the configured value range, to ensure that the final data type is not larger than uint8.

[0091] The fifth submodule can perform variable value range inference (i.e., infer the value range of the variable during the code execution process), such as solving the minimum value range of each variable in at least one variable based on the initial value range of each variable in at least one variable and the combined abstract function, to obtain the minimum value range of each variable in at least one variable.

[0092] Exemplarily, based on the input-output relationship (or execution logic) between each abstract function in the combined abstract function, the numerical range of the variable can be transferred in the combined abstract function in the form of a data stream, and the numerical range of the variable of the previous component will affect the numerical range of the variable of the subsequent component. Specifically, after the fourth submodule determines the initial value range of the variable to be optimized, the fifth submodule can perform data flow analysis oriented to the numerical range based on the combined abstract function: the numerical range of the variable will be transferred according to the data stream of the combined abstract function, so as to reach each assignment point of the variable to be optimized, and then update the numerical range of the variable to be optimized according to the semantics of the abstract function operator, etc. Specifically, for example, when the right value of the assignment point is a constant, the range is directly determined by the constant; when it is a variable, the range is determined by the numerical range of the variable reaching the assignment point; when it is an abstract function representing the component semantics, the range is determined by the numerical range of the variable used and the component semantics; when it is a common expression operator, the range is determined by the operation rules of the operator.

[0093] For example Figure 7 In the given example: the range of x is [12~12], the range of c is [0~10], and the value range of y is determined by x+c and its initial value range, that is, [12~22]; in addition, for z, since the ranges of its input y and a are [12~22] and [10~10] respectively, and the semantics of $mul is integer multiplication, the value range of z can be obtained as [120~120], and then the value range information continues to be passed along the data stream.

[0094] If the data type of any variable in at least one variable does not match the minimum value range of any variable, the fifth submodule can also adjust the data type of any variable to a data type that matches the minimum value range of any variable, thereby optimizing the data type of any variable.

[0095] For example Figure 7In the given example, the minimum value range of y is 12 to 22, and int8 is sufficient to represent the numerical range of 12 to 22, so its data type can be adjusted from int32 to int8.

[0096] In a possible implementation, at least one variable is all variables in the combined abstract function (or model), that is, all variables in the combined abstract function (or model) need to be optimized.

[0097] In another possible implementation, at least one variable is a variable that meets a preset condition in the combined abstract function, that is, only variables that meet the preset condition need to be optimized, and variables that do not meet the preset condition may not be optimized (eg, no value range analysis is performed).

[0098] In this case, the sixth submodule can perform identification of variables to be optimized, such as determining one or more candidate variables based on the combined abstract function, and the candidate variables meet the above-mentioned preset conditions; the sixth submodule determines at least one variable based on the one or more candidate variables, and the at least one variable is the variable to be optimized.

[0099] Exemplarily, the candidate variable satisfies: being an internal variable in the model, where the internal variable is a variable other than variables input from the outside to the model and variables output from the model to the outside; and / or the candidate variable is called during the execution of the model.

[0100] In other words, the embodiments of the present application can treat the variables used inside the model as variables to be optimized, so except for the port variables (input variables, output variables) and parameter variables that need to be exposed to the outside, other variables can be optimized. In addition, in order to improve efficiency, based on static data flow analysis, the port variables of components that will not be used in the model are deleted to narrow the scope of variables to be optimized and save computing resources.

[0101] For example, Figure 7 In the given example, exported_param is a variable exported to the outside of the model; unused_var is an unused variable. In this case, these two variables do not need to be optimized, but the internal variable and the used variable Const y can be used as the optimized variable.

[0102] In one implementation, the sixth submodule uses the determined one or more candidate variables as at least one variable.

[0103] In another implementation, manual participation in selecting variables to be optimized is also possible. For example, the sixth submodule further outputs one or more candidate variables through a human-computer interaction interface, receives instructions (input by a developer or user) through the human-computer interaction interface, and determines at least one variable according to the instructions. That is, the developer (or user) can select the variables that need to be tuned, which can improve the user experience.

[0104] S503: The third module generates software codes corresponding to the business model based on the optimized combined abstract function.

[0105] Optionally, when the third module performs the target language code translation, the optimized combined abstract function can be directly translated into the software code of the target language. Alternatively, when the third module performs the target language code translation, the third module can first convert the optimized combined abstract function into other intermediate representations, and then translate the other intermediate representations into the software code of the target language.

[0106] Take the target language after translation as C language as an example, Figure 7 In the example given in : Finally, the type of c is optimized from int32 to int8, the type of y is optimized from int32 to int8, the type of z is optimized from long to uint8, and f, which is affected by the data type of z, is also optimized to uint8.

[0107] The above solution can optimize the data types of variables used in the model, and can save the memory occupied by the code finally generated based on the model when running.

[0108] In addition, this solution specifically uses abstract functions as the intermediate representation of the model, which can effectively shield the calculation logic that does not need to be concerned about during variable optimization and the control logic that affects variable optimization (such as loops, selections and other control structures), so that the optimization only needs to care about the parts related to the semantics of the model components, which can improve the optimization effect and efficiency of the variable data type.

[0109] In addition, this solution is implemented in a model-based development process (such as in a development tool), and the data type optimization of variables can be completed without running software code (such as before deploying the code to the automotive embedded environment). The implementation is simple, fast and low-cost.

[0110] It can be understood that the modules (or submodules) included in the code optimization system provided in the embodiment of the present application are modules (or submodules) divided from the perspective of functions (or method steps). In practical applications, the above modules (or submodules) can be integrated into one device or distributed in multiple devices without limitation.

[0111] In addition, in practical applications, the division method of modules (or submodules) is not limited to the above division method exemplified in the embodiments of the present application. For example, any of the above modules (or submodules) can be integrated into one module (or submodule), or any of the above modules (or submodules) can be split into multiple modules (or submodules), or some modules (or submodules) can be deleted or new modules (or submodules) can be added, etc.

[0112] In order to better understand the above solution, a more detailed example is given below in combination with the above solution:

[0113] like Fig. 9 As shown, it is a structural diagram of a model-based development tool provided in an embodiment of the present application. The code generator of the tool can present optimization options to the outside, such as providing check options and editing pages for data type tuning through the front-end developer interface, which not only supports automatic tuning of the tool, but also allows developers to tune on demand.

[0114] In terms of the operation process, first, the developer builds the model in the tool. Fig.10 An example model is given in the example, which describes how to first subtract the Var array from the In array element by element, then divide the result of the element-by-element subtraction (AddSubtract_Output) by In_1 element by element (MultipDivide), and finally output it. The example also gives the data description of the parameters used in the model and the component configuration.

[0115] The developer then uses the tool to generate C language code for the model. Fig.11 Take the code generation page shown as an example:

[0116] Developers can Fig.11 Select the "Data Type Tuning" option on the code generation page shown, and then click "Generate Code" to automatically tune the data type and generate code based on the model information;

[0117] Alternatively, developers can click the "Edit" option on the right side of the "Data Type Tuning" option on the code generation page to open the editing page. Fig.12 As shown in the figure, developers can select the variables to be tuned on the editing interface, and can also modify the variable's data type, value range and other information. Then, developers can click "Generate Code" again to enable the tool to tune the data type according to the developer's requirements and generate code.

[0118] Fig.13 and Fig.14The following table shows examples of the generated code with and without data type optimization enabled (no other optimizations are enabled). It can be seen that after enabling type optimization, the original global variable Var and the local variable AddSubtract_output are both int32 arrays with a length of 10 and are optimized to int8 arrays with a length of 10 (see Fig.13 and Fig.14 The space occupied by the device is reduced to 1 / 4 of the original space.

[0119] It can be seen that the code optimization solution provided based on the embodiment of the present application can improve the optimization effect and efficiency of the data type of the variable and save the memory occupied by the code during execution.

[0120] It can be understood that the embodiments of the present application can be applied not only to model-based development scenarios, but also to code-based development scenarios.

[0121] Based on the same technical concept, such as Fig.15 As shown, an embodiment of the present application also provides a processing device, which includes: a processor 1501 and a memory 1502; the memory 1502 is used to store computer execution instructions; the processor 1501 is used to execute the computer execution instructions stored in the memory 1502, so that the device performs the method steps in the above method embodiment.

[0122] It should be understood that the processor mentioned in the embodiments of the present application can be implemented by hardware or by software. When implemented by hardware, the processor can be a logic circuit, an integrated circuit, etc. When implemented by software, the processor can be a general-purpose processor implemented by reading software code stored in a memory.

[0123] Exemplarily, the processor may be a central processing unit (CPU), other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), field programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.

[0124] It should be understood that the memory mentioned in the embodiments of the present application may be a volatile memory or a non-volatile memory, or may include both volatile and non-volatile memories. Among them, the non-volatile memory may be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory may be a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link DRAM (SLDRAM), and direct memory bus random access memory (DR RAM).

[0125] It should be noted that when the processor is a general-purpose processor, DSP, ASIC, FPGA or other programmable logic device, discrete gate or transistor logic device, discrete hardware component, the memory (storage module) can be integrated into the processor.

[0126] It should be noted that the memory described herein is intended to include, but is not limited to, these and any other suitable types of memory.

[0127] Based on the same technical concept, an embodiment of the present application also provides a computer-readable storage medium, in which a computer program or instruction is stored. When the computer program or instruction is executed by a communication device, the method steps in the above method embodiment are implemented.

[0128] Based on the same technical concept, an embodiment of the present application also provides a computer program product, which includes a computer program or instructions. When the computer program or the instructions are run by a communication device, the method steps in the above method embodiment are executed.

[0129] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0130] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0131] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0132] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

Claims

1. A code optimization method, characterized in that: include: The first module converts the model into a combined abstract function; wherein the model is an abstract representation of the business model, the model includes multiple components with fixed input-output relationships, the combined abstract function includes multiple abstract functions with fixed input-output relationships, the multiple components correspond to the multiple abstract functions one-to-one, each of the multiple abstract functions is used to indicate the semantics of the component corresponding to each abstract function, and the variables of each abstract function are the same as the variables of the component corresponding to each abstract function; The second module optimizes the data type of at least one variable according to the value range of at least one variable of the combined abstract function to obtain an optimized combined abstract function; The third module generates software code corresponding to the business model based on the optimized combined abstract function.

2. The method according to claim 1, characterized in that The first module includes a first submodule, a second submodule and a third submodule; the model also includes a component configuration and a data description, the component configuration is used to indicate the data type of the input port and / or output port of each component in the multiple components, and the data description is used to indicate the parameters configured for at least one component in the multiple components; The first module converts the model into a combined abstract function, including: The first submodule determines a corresponding abstract function for each component in the multiple components to obtain the multiple abstract functions; The second submodule configures the input and output of the abstract function corresponding to each component according to the component configuration and the data description; The third submodule configures the input-output relationship between each abstract function in the multiple abstract functions according to the input-output relationship between each component in the multiple components to obtain the combined abstract function.

3. The method according to claim 2, characterized in that The second module includes a fourth submodule and a fifth submodule; The second module optimizes the data type of at least one variable according to the value range of at least one variable of the combined abstract function, including: The fourth submodule determines an initial value range of each variable in the at least one variable according to the component configuration and the data description; The fifth submodule solves the minimum value range of each variable in the at least one variable based on the initial value range of each variable in the at least one variable and the combined abstract function to obtain the minimum value range of each variable in the at least one variable; if the data type of any variable in the at least one variable does not match the minimum value range of any variable, the data type of any variable is adjusted to a data type that matches the minimum value range of any variable.

4. The method according to claim 3, characterized in that The fourth submodule determines the initial value range of each variable in the at least one variable according to the component configuration and the data description, including: The fourth submodule determines the value range and data type configured for a first variable according to the component configuration and the data description, the first variable being any variable of the at least one variable; The fourth submodule determines the intersection of the numerical range configured for the first variable and the numerical range corresponding to the data type configured for the first variable as the initial value range of the first variable.

5. The method according to any one of claims 1 to 4, characterized in that: The second module includes a sixth submodule; the second module optimizes the data type of at least one variable according to the value range of at least one variable of the combined abstract function, including: The sixth submodule determines one or more candidate variables based on the combined abstract function, and the sixth submodule determines the at least one variable according to the one or more candidate variables; The candidate variables are internal variables in the model, and the internal variables are other variables except variables input from the outside to the model and variables output from the model to the outside; and / or the candidate variables are called during the execution of the model.

6. The method according to claim 5, characterized in that The sixth submodule determines the at least one variable according to the one or more candidate variables, including: The sixth submodule uses the one or more candidate variables as the at least one variable; or, The sixth submodule outputs the one or more candidate variables through the human-computer interaction interface, receives instructions through the human-computer interaction interface, and determines the at least one variable according to the instructions.

7. A code optimization system, characterized in that: include: The first module is used to convert the model into a combined abstract function; wherein the model is an abstract representation of the business model, the model includes multiple components with fixed input-output relationships, the combined abstract function includes multiple abstract functions with fixed input-output relationships, the multiple components correspond to the multiple abstract functions one-to-one, each of the multiple abstract functions is used to indicate the semantics of the component corresponding to each abstract function, and the variables of each abstract function are the same as the variables of the component corresponding to each abstract function; A second module is used to optimize the data type of at least one variable according to the value range of at least one variable of the combined abstract function to obtain an optimized combined abstract function; The third module is used to generate software code corresponding to the business model based on the optimized combined abstract function.

8. The system according to claim 7, characterized in that The first module includes a first submodule, a second submodule and a third submodule; the model also includes a component configuration and a data description, the component configuration is used to indicate the data type of the input port and / or output port of each component in the multiple components, and the data description is used to indicate the parameters configured for at least one component in the multiple components; The first submodule is used to determine a corresponding abstract function for each component in the multiple components to obtain the multiple abstract functions; The second submodule is used to configure the input and output of the abstract function corresponding to each component according to the component configuration and the data description; The third submodule is used to configure the input-output relationship between each abstract function in the multiple abstract functions according to the input-output relationship between each component in the multiple components, so as to obtain the combined abstract function.

9. The system according to claim 8, characterized in that The second module includes a fourth submodule and a fifth submodule; The fourth submodule is used to determine the initial value range of each variable in the at least one variable according to the component configuration and the data description; The fifth submodule is used to solve the minimum value range of each variable in the at least one variable based on the initial value range of each variable in the at least one variable and the combined abstract function to obtain the minimum value range of each variable in the at least one variable; if the data type of any variable in the at least one variable does not match the minimum value range of any variable, the data type of any variable is adjusted to a data type that matches the minimum value range of any variable.

10. The system according to claim 9, characterized in that The fourth submodule is used to determine the value range and data type configured for a first variable according to the component configuration and the data description, wherein the first variable is any variable of the at least one variable; The intersection of the numerical range configured for the first variable and the numerical range corresponding to the data type configured for the first variable is determined as the initial value range of the first variable.

11. The system according to any one of claims 7 to 10, characterized in that: The second module includes a sixth submodule; The sixth submodule is used to determine one or more candidate variables based on the combined abstract function, and the sixth submodule determines the at least one variable according to the one or more candidate variables; The candidate variables are internal variables in the model, and the internal variables are other variables except variables input from the outside to the model and variables output from the model to the outside; and / or the candidate variables are called during the execution of the model.

12. The system according to claim 11, characterized in that The sixth submodule is used to use the one or more candidate variables as the at least one variable; or, output the one or more candidate variables through the human-computer interaction interface, receive instructions through the human-computer interaction interface, and determine the at least one variable according to the instructions.

13. A processing device, characterized in that: include: including a processor and a memory; The memory is used to store computer-executable instructions; The processor is configured to execute the computer-executable instructions stored in the memory, so as to enable the communication device to perform the method according to any one of claims 1 to 6.

14. A computer-readable storage medium, characterized in that: The readable storage medium is used to store instructions, and when the instructions are executed, the method according to any one of claims 1 to 6 is implemented.

15. A computer program product, characterized in that The computer program product stores instructions, and when the computer program product is run on a computer, the computer is caused to execute the method according to any one of claims 1 to 6.