CN107515739B - Method and device for improving code execution performance - Google Patents

Method and device for improving code execution performance Download PDF

Info

Publication number
CN107515739B
CN107515739B CN201610430445.2A CN201610430445A CN107515739B CN 107515739 B CN107515739 B CN 107515739B CN 201610430445 A CN201610430445 A CN 201610430445A CN 107515739 B CN107515739 B CN 107515739B
Authority
CN
China
Prior art keywords
type
statement
variable
source code
corresponding relation
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201610430445.2A
Other languages
Chinese (zh)
Other versions
CN107515739A (en
Inventor
杨扬
张继鹏
王雷
苏丽丽
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Banma Zhixing Network Hongkong Co Ltd
Original Assignee
Banma Zhixing Network Hongkong Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Banma Zhixing Network Hongkong Co Ltd filed Critical Banma Zhixing Network Hongkong Co Ltd
Priority to CN201610430445.2A priority Critical patent/CN107515739B/en
Publication of CN107515739A publication Critical patent/CN107515739A/en
Application granted granted Critical
Publication of CN107515739B publication Critical patent/CN107515739B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/443Optimisation
    • G06F8/4434Reducing the memory space required by the program code
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/443Optimisation
    • G06F8/4441Reducing the execution time required by the program code
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/447Target code generation

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The application discloses a method and a device for improving code execution performance, and also discloses a method and a device for generating a corresponding relation between variable identifications and types, and a method and a device for generating a source code containing type labeling statements. The method for improving the code execution performance comprises the following steps: acquiring a corresponding relation between a variable identifier and a type, which is generated in advance aiming at a source code; and compiling the source code stored in a syntax tree format to generate an executable code subjected to variable type limitation according to the corresponding relation. By adopting the method, the variable type can be limited according to the pre-generated corresponding relation when the source code is compiled, and the executable code after the variable type limitation is generated, so that the memory space occupied by the executable code can be reduced, and the execution rate of the executable code is correspondingly improved due to the reduction of the process of judging various possible types, and the overall execution performance is improved.

Description

Method and device for improving code execution performance
Technical Field
The present application relates to the field of software development technologies, and in particular, to a method and an apparatus for improving code execution performance. The application also relates to a method and a device for generating the corresponding relation between the variable identification and the type, and a method and a device for generating a source code containing the type annotation statement.
Background
Programming languages (programming languages) are formal languages used to define computer programs, commonly called "computer languages", and are diversified in variety, some languages belong to weak type languages, and some languages belong to strong type languages. In the strong type language, there are various types of variables, such as int, char, float, borolan, etc., and the types of variables are explicitly specified when defining the variables; while weak type languages, when defining variables, usually do not specify the variable type, and also cannot accurately deduce the variable type at compile time. The "weak type" provides flexibility and convenience for developers in the code development stage because the variable type does not need to be specified, but at the same time, the corresponding code cannot be optimized efficiently.
Taking the JavaScript scripting language as an example, since the JavaScript scripting language has the characteristics of simplicity, dynamics, prototype-based, cross-platform, and the like, the JavaScript language is widely used in Web application development, and variables in the JavaScript language are usually defined by var or const keywords, but have no explicit type information, and therefore belong to a weak type language. An interpreter for interpreting and executing JavaScript is generally called a JavaScript engine (JS engine for short), and in order to optimize the execution efficiency of the code, a JS engine (JS V8, for example) which is commonly used at present has the capability of compiling JS code into executable code immediately and then interpreting the execution. However, since JS is a weak type language, the type of a variable is uncertain and variable at any time, the JS engine usually has to generate complex processing codes for all possible types, which results in that the generated executable codes not only occupy a large amount of memory, but also are long in execution time and low in efficiency, and the overall execution performance is greatly influenced.
Disclosure of Invention
The embodiment of the application provides a method and a device for improving code execution performance, so as to solve the problem that the existing weak type language is low in execution performance. The embodiment of the application also provides a method and a device for generating the corresponding relation between the variable identification and the type, and a method and a device for generating a source code containing the type annotation statement.
The application provides a method for improving code execution performance, which comprises the following steps:
acquiring a corresponding relation between a variable identifier and a type, which is generated in advance aiming at a source code;
and compiling the source code stored in a syntax tree format to generate an executable code subjected to variable type limitation according to the corresponding relation.
Optionally, before the obtaining of the correspondence between the variable identifier and the type, which is generated in advance for the source code, the method includes:
and generating the corresponding relation between the variable identification and the type at least according to the type marking statement in the preset format contained in the source code.
Optionally, generating a corresponding relationship between the variable identifier and the type at least according to the type labeling statement in the preset format included in the source code, including:
reading the sentences in the source code sentence by sentence, and executing the following operations for each sentence:
judging whether the statement is a type labeling statement with a preset format or not;
if yes, the following operations are executed: and generating a corresponding relation entry according to the variable identification extracted from the statement and the corresponding type information, and adding the corresponding relation entry into the corresponding relation between the variable identification and the type.
Optionally, generating a corresponding relationship between the variable identifier and the type at least according to the type labeling statement in the preset format included in the source code, including:
reading the sentences in the source code sentence by sentence, and executing the following operations for each sentence:
judging whether the statement is a type labeling statement with a preset format or not;
if yes, the following operations are executed: extracting type information from the sentence; reading the corresponding variable definition statement and acquiring a variable identifier corresponding to the extracted type information according to the position relation between the statement and the corresponding variable definition statement; and adding a corresponding relation entry consisting of the acquired variable identification and the corresponding type information into the corresponding relation between the variable identification and the type.
Optionally, the statement and the corresponding variable define a position relationship of the statement, including:
the statement is adjacent to the corresponding variable definition statement and is located before or after the variable definition statement.
Optionally, the form of the type annotation statement includes: a string statement, or an annotation statement.
Optionally, in the process of generating the source code stored in the syntax tree format through syntax parsing, the corresponding relationship between the variable identifier and the type is generated at least according to a type marking statement in a preset format included in the source code.
Optionally, the compiling the source code stored in the syntax tree format according to the corresponding relationship to generate an executable code with a limited variable type includes:
reading sentences in the source code stored in a syntax tree format sentence by sentence, and executing the following operations for each sentence:
judging whether the statement meets the following conditions: the statement is a variable operation statement, and at least one operational variable has corresponding type information in the corresponding relationship;
if yes, generating an executable code for limiting the variable type of the statement according to the type information of the operation variable contained in the statement, which is acquired from the corresponding relation;
otherwise, generating executable code without variable type definition for the statement.
Optionally, the method further includes:
the executable code is executed.
Optionally, the source code is written in a weak type language, and the weak type language includes: the JavaScript language.
Correspondingly, the present application also provides an apparatus for improving code execution performance, including:
the corresponding relation acquisition unit is used for acquiring the corresponding relation between the variable identification and the type, which is generated in advance aiming at the source code;
and the executable code generating unit is used for compiling the source code stored in the syntax tree format to generate an executable code subjected to variable type limitation according to the corresponding relation.
Optionally, the apparatus further comprises: a correspondence relationship generation unit;
and the corresponding relation generating unit is used for generating the corresponding relation between the variable identification and the type at least according to the type marking statement in the preset format contained in the source code.
Optionally, the correspondence generating unit includes:
the code reading and circulating control subunit is used for reading the sentences in the source code sentence by sentence and triggering the following subunits to work aiming at each sentence;
the type labeling sentence judging subunit is used for judging whether the sentence is a type labeling sentence with a preset format;
and the first corresponding relation item generating and adding subunit is used for generating a corresponding relation item according to the variable identification extracted from the statement and the corresponding type information and adding the corresponding relation item into the corresponding relation between the variable identification and the type when the output of the type labeling statement judging subunit is yes.
Optionally, the correspondence generating unit includes:
the code reading and circulating control subunit is used for reading the sentences in the source code sentence by sentence and triggering the following subunits to work aiming at each sentence;
the type labeling sentence judging subunit is used for judging whether the sentence is a type labeling sentence with a preset format;
a type information extracting subunit, configured to extract type information from the statement when the output of the type labeling statement judging subunit is yes;
a variable identifier obtaining subunit, configured to read a corresponding variable definition statement and obtain a variable identifier corresponding to the extracted type information according to a position relationship between the statement and the corresponding variable definition statement;
and the second corresponding relation item generating and adding subunit is used for adding the corresponding relation item consisting of the acquired variable identifier and the corresponding type information into the corresponding relation between the variable identifier and the type.
Optionally, the correspondence generating unit is specifically configured to, in a process of generating the source code stored in the syntax tree format through syntax parsing, generate a correspondence between the variable identifier and the type at least according to a type marking statement in a preset format included in the source code.
Optionally, the executable code generating unit includes:
the loop control subunit is used for reading the sentences in the source code stored in the syntax tree format sentence by sentence and triggering the following subunits to work aiming at each sentence;
an optimization condition judgment subunit, configured to judge whether the statement satisfies the following condition: the statement is a variable operation statement, and at least one operational variable has corresponding type information in the corresponding relationship;
an optimized code generation subunit, configured to, when the output of the optimization condition judgment subunit is yes, generate an executable code after variable type limitation is performed on the statement according to type information of an operation variable included in the statement, which is acquired from the correspondence relationship;
and the regular code generation subunit is used for generating executable code without variable type limitation for the statement when the output of the optimization condition judgment subunit is negative.
Optionally, the apparatus further comprises:
an executable code execution unit for executing the executable code.
In addition, the present application also provides a method for generating a correspondence between a variable identifier and a type, including:
and generating a corresponding relation between the variable identification and the type at least according to the type marking statement in the preset format contained in the source code, so as to generate an executable code subjected to variable type limitation when compiling the source code.
Optionally, the generating, according to at least a type labeling statement in a preset format included in the source code, a correspondence between the variable identifier and the type includes:
reading the sentences in the source code sentence by sentence, and executing the following operations for each sentence:
judging whether the statement is a type labeling statement with a preset format or not;
if yes, the following operations are executed: and generating a corresponding relation entry according to the variable identification extracted from the statement and the corresponding type information, and adding the corresponding relation entry into the corresponding relation between the variable identification and the type.
Optionally, the generating, according to at least a type labeling statement in a preset format included in the source code, a correspondence between the variable and the type includes:
reading the sentences in the source code sentence by sentence, and executing the following operations for each sentence:
judging whether the statement is a type labeling statement with a preset format or not;
if yes, the following operations are executed: extracting type information from the sentence; reading the corresponding variable definition statement and acquiring a variable identifier corresponding to the extracted type information according to the position relation between the statement and the corresponding variable definition statement; and adding a corresponding relation entry consisting of the acquired variable identification and the corresponding type information into the corresponding relation between the variable identification and the type.
Optionally, the position relationship between the statement and the corresponding variable definition statement includes:
the statement is adjacent to the corresponding variable definition statement and is located before or after the variable definition statement.
Optionally, the form of the type annotation statement includes: a string statement, or an annotation statement.
Optionally, in the process of generating the source code stored in the syntax tree format through syntax parsing, at least according to a type marking statement in a preset format included in the source code, a corresponding relationship between a variable identifier and a type is generated, so that an executable code subjected to variable type limitation is generated when the source code is compiled.
Correspondingly, the present application also provides an apparatus for generating a corresponding relationship between a variable identifier and a type, including:
the corresponding relation generating unit is used for generating the corresponding relation between the variable identification and the type at least according to the type marking statement in the preset format contained in the source code;
and the corresponding relation storage unit is used for storing the corresponding relation between the generated variable identification and the type so as to generate the executable code subjected to variable type limitation when compiling the source code.
Optionally, the correspondence generating unit includes:
the code reading and circulating control subunit is used for reading the sentences in the source code sentence by sentence and triggering the following subunits to work aiming at each sentence;
a type labeling sentence judgment subunit, configured to judge whether the sentence is a type labeling sentence;
and the first corresponding relation item generating and adding subunit is used for generating a corresponding relation item according to the variable identification extracted from the statement and the corresponding type information and adding the corresponding relation item into the corresponding relation between the variable identification and the type when the output of the type labeling statement judging subunit is yes.
Optionally, the correspondence generating unit includes:
the code reading and circulating control subunit is used for reading the sentences in the source code sentence by sentence and triggering the following subunits to work aiming at each sentence;
the type labeling sentence judging subunit is used for judging whether the sentence is a type labeling sentence with a preset format;
a type information extracting subunit, configured to extract type information from the statement when the output of the type labeling statement judging subunit is yes;
a variable identifier obtaining subunit, configured to read a corresponding variable definition statement and obtain a variable identifier corresponding to the extracted type information according to a position relationship between the statement and the corresponding variable definition statement;
and the second corresponding relation item generating and adding subunit is used for adding a corresponding relation item consisting of the acquired variable identifier and the corresponding type information into the corresponding relation between the variable identifier and the type.
Optionally, the correspondence generating unit is specifically configured to, in a process of generating a source code stored in a syntax tree format through syntax parsing, generate a correspondence between a variable identifier and a type according to at least a type marking statement in a preset format included in the source code, so as to generate an executable code subjected to variable type limitation when compiling the source code.
In addition, the present application also provides a method for generating source code containing type annotation statements, comprising:
receiving a type marking statement which is input aiming at the source code and used for specifying a variable type;
storing the type marking statement into the source code according to the received type marking statement and the input position of the type marking statement in the source code;
and the type marking statement is in a preset format.
Optionally, the form of the type annotation statement includes: a string statement, or an annotation statement.
Optionally, the source code is written using a weak type language, where the weak type language includes: the JavaScript language.
Correspondingly, the present application also provides an apparatus for generating source code containing type tagging information, including:
the device comprises a type marking statement receiving unit, a variable type marking statement generating unit and a variable type marking statement generating unit, wherein the type marking statement receiving unit is used for receiving a type marking statement which is input aiming at a source code and used for specifying a variable type, and the type marking statement is in a preset format;
and the type labeling statement storage unit is used for storing the type labeling statement into the source code according to the received type labeling statement and the input position of the type labeling statement in the source code.
Compared with the prior art, the method has the following advantages:
the method for improving the code execution performance includes the steps of firstly obtaining a corresponding relation between a variable identifier and a type, which is generated in advance aiming at a source code, and then compiling the source code stored in a syntax tree format to generate an executable code subjected to variable type limitation according to the corresponding relation. By adopting the method, the corresponding relation between the variable identification and the type is generated in advance, so that the variable type can be limited according to the corresponding relation when the source code is compiled, and the executable code after the variable type limitation is generated, thereby reducing the memory space occupied by the executable code, and reducing the process of judging various possible types, so that the execution rate of the executable code is correspondingly improved, and the overall execution performance is improved.
Drawings
FIG. 1 is a flow diagram of an embodiment of a method for generating source code containing type annotation statements of the present application;
FIG. 2 is a schematic diagram of an embodiment of an apparatus for generating source code containing type annotation statements of the present application;
FIG. 3 is a flow diagram of an embodiment of a method of generating a correspondence of variable identifications and types according to the present application;
fig. 4 is a flowchart of processing for generating a correspondence entry for a statement according to an embodiment of the present application;
FIG. 5 is a diagram illustrating an embodiment of an apparatus for generating a correspondence between variable identifications and types according to the present application;
FIG. 6 is a flow diagram of one embodiment of a method of improving code execution performance of the present application;
FIG. 7 is a flowchart of a process for generating executable code for a statement provided by an embodiment of the present application;
FIG. 8 is a diagram of an embodiment of an apparatus for improving code execution performance according to the present application.
Detailed Description
In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present application. This application is capable of implementation in many different ways than those herein set forth and of similar import by those skilled in the art without departing from the spirit and scope of this application, and it is therefore not limited to the specific implementations disclosed below.
In the present application, a method and an apparatus for improving code execution performance, a method and an apparatus for generating a correspondence between a variable identifier and a type, and a method and an apparatus for generating a source code including a type tagging statement are provided, respectively, and are described in detail in the following embodiments one by one. Before describing the embodiments, the technical solution of the present application is briefly described.
The weak type language refers to a programming language which does not have syntax for describing the variable type and can not accurately derive the variable type during compiling. The weak type language is often used to bring some flexibility and convenience to the code writing process of developers, but at the same time, efficient code optimization cannot be performed on the execution process, so that the execution performance of the code written by the weak type language is generally low. Taking JavaScript (JS for short) as an example, for JS codes: z is x + y; since JS is of a weak type, JS engines (e.g., V8 engines) typically need to consider operations of all possible variable types when generating executable code for them, so the pseudo code of the generated executable code is as follows:
if x is integer, the following operations are performed:
if y is integer, add z, x, y is performed;
if y is a double precision floating point type, performing addf z, x, y;
if y is a character string, firstly converting y into an integer value y _ int, and then executing add z, x, y _ int;
if y is an object, firstly converting y into a shaping value y _ int, and then executing add z, x, y _ int;
if x is a double precision floating point number, the following operations are performed:
......
therefore, the JS engine generates a corresponding branch for each possible variable type and includes a corresponding processing code under each branch, so that the generated executable code usually occupies a large amount of memory space, and the execution speed is slow due to the fact that multiple judgments are required in the execution process, resulting in poor overall execution performance. Although the example of the JS language is listed above, the same problem as described above generally exists for weak type languages.
Although the weak type language cannot specify an explicit type for a variable, in the process of actually developing using the weak type language, most of the cases that a developer has an explicit "type tendency", that is: the variables defined by the developer usually have specific types and are not transformed at will, but the types of the variables cannot be specified in the weak type language.
In view of the above situation, the present application provides a method for improving code execution performance, which has the following core: for a source code to be compiled, a variable identifier of a determined type and the type thereof may be recorded in a corresponding relationship, and when the source code is compiled, an executable code subjected to variable type limitation is generated according to type information in the corresponding relationship, that is: and generating executable code with optimized types. By adopting the method, a large number of judgment branches are reduced, so that the memory space occupied by the executable code can be reduced, the execution rate of the generated executable code can be correspondingly improved, and the overall execution performance is improved.
The corresponding relation between the variable identification and the type comprises a plurality of corresponding relation items, and each corresponding relation item comprises the variable identification and corresponding type information. The correspondence may be stored in the memory in the form of a data table, or in a file.
In specific implementation, a developer may add a type annotation statement in a preset format in a process of writing a code by using a weak type language, and the method for generating a source code including the type annotation statement provided by the present application generates the source code including the type annotation statement by receiving the statement input by the developer, and then may generate a corresponding relationship between a variable identifier and a type according to the source code including the type annotation statement by using a method for generating a corresponding relationship between the variable identifier and the type provided by the present application, and generate an executable code after variable type definition for the source code by using the generated corresponding relationship by using the method for improving code execution performance provided by the present application.
Because the type labeling sentences added in the source codes conform to the grammar rules of the weak type language, the JS language does not need to be modified (for example, keywords for performing type definition are added), so that the weak type language is not changed from a language level to the strong type language, the flexibility of the weak type language can still be kept, developers can still write the source codes according to the original programming habits, and the purpose of performing variable type definition in a compiling stage can be realized only by adding the type labeling sentences in places where variable types can be made clear.
The type marking statement can be in the form of a character string statement or an annotation statement, and can also be in other forms of code statements which do not influence the realization of the functions of the source code. The comment statement refers to a statement which does not have any function of realizing functions and is only used for explaining source code, and two examples of the comment statement in the JavaScript language are given here:
Figure BDA0001019485110000101
the code statements are different from comment statements and generally refer to statements used to implement the functionality of the source code, such as: variable definition statements, variable operation statements, etc., and the source code is usually composed of comment statements and code statements. When the technical scheme is implemented, a code statement which does not affect the realization of the function of the source code can be adopted, for example: and adopting character string sentences as type labeling sentences.
For convenience of understanding, in the following embodiments, description will be given of embodiments of the present invention with emphasis on the case of using a character string sentence as a type labeling sentence. As for the code statements in other forms or the comment statements as type marking statements, they can also be implemented based on the same principle.
In the following, embodiments of the present application are described in detail, and for convenience of understanding, an embodiment of a method for generating source code containing a type annotation statement of the present application is described first.
Please refer to fig. 1, which is a flowchart of an embodiment of a method for generating source code containing type annotation statements of the present application. The method comprises the following steps:
step 101, receiving a type marking statement which is input aiming at a source code and used for specifying a variable type.
The source code in this step may be a code segment, which may include an implementation code of a function, or implementation codes of several functions, or all codes in a source file for implementing a certain overall function.
The way that the developer adds the type marking statement for specifying the variable type in the source code can be preset, and the same preset way is followed when the corresponding relation between the scalar identification and the type is extracted from the source code subsequently. For example: the type of a certain variable or some variables defined in the function can be explicitly specified by adopting a type annotation statement at the beginning part of the function, or a corresponding type annotation statement can be added aiming at the variable definition statement in the function. Considering that the second method is more intuitive and convenient for developers to operate, the second method is generally the preferred method for developers to add type annotation sentences.
In this way, the developer can add the type annotation statement at a position adjacent to the variable definition statement, for example: before the variable definition statement or after the variable definition statement, a type annotation statement for specifying the variable type is added.
And the type marking statement at least comprises type information of at least one variable in the corresponding variable definition statement. If the variable definition statement includes definitions of n variables, the type annotation statement added to the variable definition statement may include type information of each variable, or may include only type information of one or more variables, as long as a correct correspondence relationship can be established between the variables and the type information.
The format of the type markup sentence is usually preset, for example: the type annotation statement starts with a preset symbol, and for the type information of different variables, preset separators may be used to separate from each other, where the preset separators may be: semicolon, comma, or other symbols, such as: @. The preset format of the type annotation statement is not limited in this embodiment, as long as the type annotation statement can be identified from the statements contained in the source code through the preset format, and the type information is extracted.
In the following description, a type annotation statement is added to a source code written in JavaScript as an example, and in the following specific example, the type annotation statement in a preset format is: a string statement that employs an "@" character or an "< - @" character as a start character. Wherein the start character is "@" indicates that the type annotation statement is added before the variable definition statement, and "< - @" indicates that the type annotation statement is added after the variable definition statement.
Source code of type-not-added annotation statements:
var num=1;
const str=“hello”,isTrue=true;
var func=function(num,str){};
1) type annotation statements are added before the variable definition statements (added on different lines of code):
“@int”;
var num=1;
“@string,@boolean”;
const str=“hello”,isTrue=true;
“@boolean function(@int,@string)”;
var func=function(){};
2) type annotation statements are added before the variable definition statements (in the same code line):
“@int”;var num=1;
“@string,@boolean”;const str=“hello”,isTrue=true;
“@boolean function(@int,@string)”;var func=function(num,str){};
3) adding type marking sentences after the variable definition sentences:
var num=1;“<-@int”;
const str=“hello”,isTrue=true;“<-@string,@boolean”;
var func=function(num,str){};“<-@boolean function(@int,@string)”;
the above 1), 2), 3) respectively provide three specific examples of adding type annotation statements, in which the roles of the added type annotation statements are the same: the type of num variable is designated int, the type of str variable is designated string, the type of isTrue variable is designated coolean, and the type of func variable is designated function.
The type marking statement may also only specify the type of a part of variables in the corresponding variable definition statement, for example, the following source code of the type marking statement is added, where the type information before the first delimiter is null, which indicates that no type is specified for the corresponding num variable, and the two following delimiters are both preceded by type information, that is: the variable str is assigned a type of string and the variable isTrue is assigned a type of coolean.
“@object{,@string,@boolean}”;
const obj={num:1,str:“hello”,isTrue:true};
In specific implementation, variable identifiers can also be explicitly written in the type labeling statements, that is: the type labeling statement may include one or more tuples consisting of variable identifications and corresponding type information, such as:
“@object{@str:string,@isTrue:boolean}”;
const obj={num:1,str:“hello”,isTrue:true};
in specific implementation, the source code may be generally loaded and displayed in the editing window, and a developer inputs a type annotation statement in a preset format at an input cursor of the editing window in the above manner, so that the step receives the type annotation statement correspondingly, and knows an input position of the received type annotation statement in the source code.
And 102, storing the type labeling statement into the source code according to the received type labeling statement and the input position of the type labeling statement in the source code.
According to the received type labeling statement and the input position of the type labeling statement in the source code, the type labeling statement is stored in the source code, and therefore the source code containing the type labeling statement is generated.
In a specific implementation, the type annotation statement and the input location information thereof received in step 101 may be cached, and after receiving the storage command, the cached type annotation statement is stored in the source code according to the input location information thereof.
The embodiment of the present embodiment is described in detail above using JavaScript as an example. It should be understood by those skilled in the art that the method described in this embodiment can also be applied to source code written in other weak type languages, including: PHP, Lua, etc.
In specific implementation, the preset format of the type labeling statement and the adding position can be preset according to the grammar rule of the weak type language and the actual requirement, and the type labeling statement can be recognized according to the preset format and the corresponding relation between the variables and the types can be specified. On the basis of adding type marking statements in the source code, when the source code needs to be executed, the corresponding relation between the variable identification and the type can be extracted according to the preset format of the type marking statements, and the executable code after variable type limitation is generated according to the corresponding relation.
The embodiment of the present embodiment has been described above by taking a character string sentence as an example. In other embodiments, the type annotation statement may also take the form of an annotation statement, and may also be added at the beginning of the function, or before or after the variable definition statement. Several specific examples are given below.
1)//@string,@boolean
const str=“hello”,isTrue=true;
2)/*@int*/var num=1;
3)const str=“hello”,isTrue=true;//<-@string,@boolean
In summary, in the method for generating a source code including a type annotation statement provided in this embodiment, the source code including the type annotation statement is generated by receiving the type annotation statement input for the source code and storing the type annotation statement in the source code. Therefore, the defect that the weak type language cannot specify the variable type can be overcome, and the possibility is provided for subsequently generating the executable code for limiting the variable type. Because the mode of adding type marking statements is adopted, original code statements in the source code do not need to be modified, and the method is easy to implement by developers; particularly, by adopting a mode of adding corresponding to variable definition sentences, only type marking sentences need to be added for one time correspondingly to the variable definitions, not only the corresponding relation is clear, but also the marking workload of developers can be reduced.
In the above embodiment, a method for generating source code containing type annotation statements is provided, and correspondingly, the application also provides a device for generating source code containing type annotation statements. Please refer to fig. 2, which is a schematic diagram of an embodiment of an apparatus for generating source code including type annotation statements of the present application. Since the apparatus embodiments are substantially similar to the method embodiments, they are described in a relatively simple manner, and reference may be made to some of the descriptions of the method embodiments for relevant points. The device embodiments described below are merely illustrative.
An apparatus for generating source code containing a type annotation statement according to this embodiment includes: a type annotation statement receiving unit 201, configured to receive a type annotation statement input for a source code and used for specifying a variable type, where the type annotation statement is in a preset format, and the unit may be configured to execute step 101 in the corresponding method embodiment;
a type annotation statement storage unit 202, configured to store the type annotation statement into the source code according to the received type annotation statement and the input location thereof in the source code, where the unit may be configured to perform step 102 in the corresponding method embodiment described above.
In addition, the present application also provides a method for generating a corresponding relationship between a variable identifier and a type, please refer to fig. 3, which is a flowchart of an embodiment of a method for generating a corresponding relationship between a variable identifier and a type provided in the present application. The method for generating the corresponding relation between the variable identification and the type comprises the following steps:
step 301, generating a corresponding relation between the variable identifier and the type at least according to a type marking statement in a preset format included in the source code, so as to generate an executable code subjected to variable type limitation when compiling the source code.
Before executing the step, a source code containing a type marking statement in a preset format is usually generated, and in the step, on the basis of reading the type marking statement in the source code, a corresponding relation between a variable identifier and a type is generated, so that an executable code subjected to variable type limitation can be generated according to type information in the corresponding relation during compiling.
In this step, the type labeling statement is identified from the source code according to the preset format of the type labeling statement, and the corresponding relationship is generated on the basis. The preset format of the type labeling statement is different, and the processing procedure in this step is also different.
If it is preset that the type of the variable defined in the function is explicitly specified by using a type tagging statement at the beginning of the function, the step may read the type tagging statement at the beginning of the function, correspondingly generate a corresponding relationship entry between the variable identifier and the type, and add the entry to the corresponding relationship between the variable identifier and the type.
If the preset type annotation statement is added for the variable definition statement and the type annotation statement comprises the variable identification and the corresponding type information, each statement in the source code can be read sentence by sentence, and if the read statement is identified to be the type annotation statement with the preset format, a corresponding relation entry is generated according to the variable identification extracted from the type annotation statement and the corresponding type information and is added into the corresponding relation between the variable identification and the type.
If the preset type marking statement is added aiming at the variable definition statement and the type marking statement does not contain the variable identification, generating a corresponding relation entry of the variable identification and the type according to the position relation between the type marking statement and the corresponding variable definition statement, and adding the corresponding relation entry into the corresponding relation between the variable identification and the type. This is explained below with reference to a specific example.
In the specific example, a type tagging statement in a character string form is added before or after a variable definition statement in a source code, each statement in the source code is read sentence by sentence in the step, and processing is performed on each statement, and the processing procedure includes steps 301-1 to 301-5, which is described in detail below with reference to fig. 4.
Step 301-1, judging whether the statement is a type labeling statement with a preset format; if yes, go to step 301-2.
In this step, it may be determined whether the sentence is a type annotation sentence according to whether the sentence conforms to a preset format of the type annotation sentence. In the specific example, the type labeling statement in the preset format is: the "@" character or "< - @" character is used as the character string sentence of the starting character, so the step can be judged as follows: and if the statement is a string statement and the start character of the string is @ or '< - @', if the currently read statement meets the requirement, the statement is a type labeling statement, and step 301-2 is executed.
And 301-2, extracting type information from the statement.
In this step, the type information may be extracted according to a preset format of the type annotation statement, for example: the type information is extracted according to the indication of the delimiter.
And 301-3, reading the corresponding variable definition statement and acquiring the variable identifier corresponding to the extracted type information according to the position relationship between the statement and the corresponding variable definition statement.
The position relationship between the type labeling statement and the corresponding variable definition statement may be preset when a developer adds the type definition statement to the source code, for example: presetting a type mark statement before a variable definition statement, and then reading the next variable definition statement after the type mark statement (taking Java Script as an example, the variable definition statement can be a var statement or a const statement); if the preset type marking statement follows the variable definition statement, the step reads the last variable definition statement before the type marking statement.
The position relationship between the type labeling statement and the corresponding variable definition statement can also be specified by preset information in the type labeling statement, and in the specific example, if the type labeling statement starts with a '@' character, the type labeling statement stands for that the type labeling statement is before the variable definition statement; if the type labeling statement starts with the "< - @" character, the type labeling statement is after the variable definition statement, and then the corresponding variable definition statement can be read in the step according to the starting character of the type labeling statement character string.
After reading the corresponding variable definition statement of the type annotation statement, the variable identifier corresponding to the type information extracted in step 301-2 may be further obtained according to the correspondence between the type in the type annotation statement and the variable in the corresponding variable definition statement, where the variable identifier is identifier information capable of uniquely identifying the variable in the source code. If the source code is a function, the variable name of each variable defined in the function is unique, so that the variable name extracted from the variable definition statement can be used as variable identification; if the source code includes two or more functions, for a variable defined in the function, a "function name + a variable name extracted from the variable definition statement" may be used as a variable identification.
And 301-4, adding a corresponding relation item consisting of the acquired variable identification and the corresponding type information into the corresponding relation between the variable identification and the type.
Executing the step, acquiring the variable identifier and the type information corresponding to the variable identifier according to the type identifier statement and the corresponding variable definition statement, so that the step can form a corresponding relation entry according to the acquired information, and add the corresponding relation entry to the corresponding relation between the variable identifier and the type.
For example, the read type annotation statement is: "@ string, @ borolean"; the corresponding variable definition statements are conststr ═ hello and isTrue ═ true; then this step generates two correspondence entries as shown below and may add these two entries to the correspondence of variable identification to type.
1)str:string
2)isTrue:boolean
And executing the steps 301-1 to 301-4 for each statement in the source code, and adding the generated corresponding relation entry to the corresponding relation between the variable identifier and the type, so that after the statements in the source code are processed, the corresponding relation between the variable identifier and the type is generated for the source code.
Thus, the generation process of the correspondence between the variable identifier and the type is described by a specific example. In specific implementation, since the positions of adding the type markup sentences in the source code may be different and the preset formats of the type markup sentences may also be different, the specific implementation manner for generating the correspondence relationship may also be different, and in practical application, the corresponding change and adjustment may be performed according to specific situations on the basis of the above example.
For example, in the above example, the type labeling statement is in the form of a character string statement, in other embodiments, the type labeling statement may also be in the form of an annotation statement, and in this case, in step 301-1, the following determination may be made: whether the statement is an annotation statement, and with @ "or" < - @ "as the starting character of the annotation information. If the currently read statement meets the requirement, the statement is a type marking statement. The implementation of other steps is similar to the string statement, and is not described herein again.
It should be noted that the method for generating the correspondence between the variable identifier and the type according to this embodiment may be implemented independently, or may be implemented in a process of generating a source code stored in a syntax tree format through syntax parsing. Namely: in the process of generating a source code stored in a syntax tree format through syntax analysis, generating a corresponding relation between a variable identification and a type at least according to a type marking statement in a preset format contained in the source code. This embodiment will be explained below.
Often syntax parsing may be performed prior to executing the source code. In the process of syntax analysis, reading sentences in the source code sentence by sentence, and determining whether to generate a corresponding syntax tree or syntax tree node for the currently read sentences according to a preset strategy. After the source code is processed in the above manner, the source code is converted into the source code stored in the syntax tree format, and the source code stored in the syntax tree format is compiled to generate the executable code.
The method for generating the corresponding relationship between the variable identifier and the type provided by this embodiment can be completed in the process of parsing the syntax.
For example, in generating source code stored in a syntax tree format by syntax parsing, not only the processing operations related to generating a syntax tree but also the following operations are performed for each statement read sentence by sentence: judging whether the statement is a type labeling statement with a preset format or not; if yes, generating a corresponding relation entry according to the variable identification extracted from the type marking statement and the corresponding type information, and adding the corresponding relation entry into the corresponding relation between the variable identification and the type.
As another example, in generating source code stored in a syntax tree format by syntax parsing, not only the processing operations related to generating a syntax tree but also the following operations are performed for each statement read sentence by sentence: judging whether the statement is a type labeling statement with a preset format or not; if yes, extracting type information from the statement; reading the corresponding variable definition statement and acquiring a variable identifier corresponding to the extracted type information according to the position relation between the statement and the corresponding variable definition statement; and adding a corresponding relation entry consisting of the acquired variable identification and the corresponding type information into the corresponding relation between the variable identification and the type.
By adopting the above embodiment, through performing syntax parsing on the source code (this process is also commonly referred to as a Parse process), not only the source code stored in a syntax tree format is generated, but also the corresponding relationship between the variable identifier and the type is generated, so that it is not necessary to additionally perform an operation of reading the code in order to generate the corresponding relationship.
As can be seen from the above description, in the method for generating a correspondence between a variable identifier and a type provided in this embodiment, on the basis of reading a type marking statement included in a source code, a correspondence between a variable identifier and a type is generated for the source code, so that a preparation is made for compiling the source code to generate an executable code subjected to variable type definition. Because the corresponding relation between the variable identification and the type is extracted in advance, the compiling process does not need to execute the analysis operation of the variable type, and the type information of the variable can be directly obtained from the corresponding relation set, so that the efficiency of the compiling process can be improved.
In the foregoing embodiment, a method for generating a corresponding relationship between a variable identifier and a type is provided, and correspondingly, the present application also provides a device for generating a corresponding relationship between a variable identifier and a type. Please refer to fig. 5, which is a schematic diagram of an embodiment of an apparatus for generating a correspondence between a variable identifier and a type according to the present application. Since the apparatus embodiments are substantially similar to the method embodiments, they are described in a relatively simple manner, and reference may be made to some of the descriptions of the method embodiments for relevant points. The device embodiments described below are merely illustrative.
An apparatus for generating a correspondence between a variable identifier and a type in this embodiment includes: a correspondence generating unit 501, configured to generate a correspondence between a variable identifier and a type at least according to a type marking statement in a preset format included in the source code, where the unit is configured to execute step 301 in the corresponding method embodiment;
a corresponding relation storage unit 502, configured to store a corresponding relation between the generated variable identifier and the type, so as to generate an executable code subjected to variable type limitation when compiling the source code.
Optionally, the correspondence generating unit includes:
the code reading and circulating control subunit is used for reading the sentences in the source code sentence by sentence and triggering the following subunits to work aiming at each sentence;
a type labeling sentence judgment subunit, configured to judge whether the sentence is a type labeling sentence;
and the first corresponding relation item generating and adding subunit is used for generating a corresponding relation item according to the variable identification extracted from the statement and the corresponding type information and adding the corresponding relation item into the corresponding relation between the variable identification and the type when the output of the type labeling statement judging subunit is yes.
Optionally, the correspondence generating unit includes:
the code reading and circulating control subunit is used for reading the sentences in the source code sentence by sentence and triggering the following subunits to work aiming at each sentence;
the type labeling sentence judging subunit is used for judging whether the sentence is a type labeling sentence with a preset format;
a type information extracting subunit, configured to extract type information from the statement when the output of the type labeling statement judging subunit is yes;
a variable identifier obtaining subunit, configured to read a corresponding variable definition statement and obtain a variable identifier corresponding to the extracted type information according to a position relationship between the statement and the corresponding variable definition statement;
and the second corresponding relation item generating and adding subunit is used for adding a corresponding relation item consisting of the acquired variable identifier and the corresponding type information into the corresponding relation between the variable identifier and the type.
Optionally, the correspondence generating unit is specifically configured to, in a process of generating a source code stored in a syntax tree format through syntax parsing, generate a correspondence between a variable identifier and a type according to at least a type marking statement in a preset format included in the source code, so as to generate an executable code subjected to variable type limitation when compiling the source code.
In addition, the present application also provides a method for improving code execution performance, please refer to fig. 6, which is a flowchart of an embodiment of the method for improving code execution performance provided by the present application. The method for improving the code execution performance comprises the following steps:
step 601, acquiring a correspondence relation between a variable identifier and a type, which is generated in advance for a source code.
Before executing this step, a correspondence between the variable identifier and the type is usually generated in advance for the source code, where the correspondence usually includes a number of correspondence entries, and each correspondence entry includes the variable identifier and corresponding type information. The corresponding relationship may be stored in a memory or a disk file, and the corresponding relationship may be obtained by reading the memory or the disk file before compiling the source code.
Step 602, compiling the source code stored in the syntax tree format to generate an executable code subjected to variable type limitation according to the corresponding relation.
Since the correspondence between the type and the variable identifier generated in advance for the source code is obtained in step 601, in the process of compiling the source code stored in the syntax tree format, the variable type may be defined according to the correspondence, and an executable code after the variable type definition is generated, that is: and performing type optimization on the executable code.
Since the limited variable type is mainly for the variable operation statement, the present embodiment takes the variable operation statement as a key point, and explains the process of generating the executable code, and in specific implementation, similar methods can be used to perform type optimization for other types of statements.
This step may read each statement in the source code stored in syntax tree format in turn and generate corresponding executable code for each statement through the following steps 602-1 to 602-3, which will be described in detail below with reference to fig. 7.
Step 602-1, judging whether the statement meets the following conditions: the statement is a variable operation statement, and at least one operational variable has corresponding type information in the corresponding relationship, if yes, step 602-2 is executed, otherwise step 602-3 is executed.
The main tasks of the step are as follows: and judging whether variable type limitation is carried out on the currently read statement or not.
Whether the statement is a variable operation statement or not can be identified, the variable operation statement refers to a statement comprising an operator and an operation variable, wherein the operator can be a monocular, binocular or trinocular operator, and the number of the corresponding operation variables can be one, two or three.
If the statement is a variable operation statement, further judging whether the variable operation statement at least has one operation variable with corresponding type information in the corresponding relation. In specific implementation, the corresponding relationship obtained in step 601 may be searched for according to each operational variable included in the statement, and if there is a corresponding relationship entry corresponding to a variable identifier of a certain operational variable, the type information in the corresponding relationship entry is read. After the above lookup operation is performed, if at least one of the operation variables has corresponding type information in the corresponding relationship, step 602-2 may be performed.
If the statement does not satisfy both of the above conditions, step 602-3 is performed.
Step 602-2, generating an executable code after the variable type limitation is performed on the statement according to the type information of the operation variable included in the statement, which is acquired from the corresponding relationship.
Since the variable type limitation can be performed on the statement in the present step, the present step generates an executable code subjected to variable type limitation, based on the type information of the operation variable included in the statement, which is acquired from the correspondence relationship.
In specific implementation, the efficient instruction provided by the platform for the type may be selected according to the type of the operation variable, for example, on a certain platform, the integer addition operation uses an add instruction, and the floating-point addition operation uses an addf instruction, where z is x + y given in the beginning of this specification; for example, the step may generate corresponding executable codes according to the types of the x and y variables obtained from the corresponding relationship, for example, if it is known that x is integer and y is integer according to the corresponding relationship, that is, the types of x and y are defined, then the generating of the corresponding executable codes is: add z, x, y; for another example, if x is integer and y is double-precision floating point according to the corresponding relationship, the corresponding executable code is generated as follows: addf z, x, y.
Therefore, compared with the executable code generated for the z-x + y operation statement given in the beginning of the detailed description of the present invention, the executable code generated after the variable type limitation does not have a large number of branch judgments and different instruction codes for each branch, but only generates one executable code, and adopts a special efficient operation instruction matched with the operation type, so that the memory space occupied by the executable statement is reduced, the execution speed of the executable code can be increased, and the overall execution performance is improved.
It should be noted that, for the convenience of understanding, only one illustrative example of generating executable code after performing variable type limitation is given for the addition operation, and when the method is actually implemented, other efficient instructions different from the above may be selected and corresponding executable code may be generated according to different platforms and operation types.
Step 602-3, generating executable code without variable type definition for the statement.
Executing to this step, it is stated that the statement is not a variable operation statement, or is a variable operation statement, but none of the operation variables contained in the statement has corresponding type information in the corresponding relationship, in this case, executable code without variable type limitation may be generated in a conventional manner.
For each statement in the source code stored in the syntax tree format, the above steps 602-1 to 602-3 are sequentially executed for processing, so that the source code stored in the syntax tree format can be compiled to generate an executable code subjected to variable type limitation by using the correspondence relationship obtained in step 601.
In a specific implementation, after the executable code (which may also be referred to as intermediate code or native code) is generated, the compiler or engine implementing the method may directly run the executable code, or may invoke other executors to run the executable code. In the process of generating the executable code, the types of the corresponding variables are limited according to the type information in the corresponding relationship, so that the generated executable code not only occupies less memory space, but also has correspondingly accelerated execution speed, and the overall execution performance is obviously improved.
In the foregoing embodiments, a method for improving code execution performance is provided, and correspondingly, an apparatus for improving code execution performance is also provided. Please refer to fig. 8, which is a diagram illustrating an embodiment of an apparatus for improving code execution performance according to the present application. Since the apparatus embodiments are substantially similar to the method embodiments, they are described in a relatively simple manner, and reference may be made to some of the descriptions of the method embodiments for relevant points. The device embodiments described below are merely illustrative.
An apparatus for improving code execution performance of this embodiment includes: a correspondence obtaining unit 801, configured to obtain a correspondence between a variable identifier and a type, which is generated in advance for a source code, where the unit may be configured to execute step 601 in the corresponding method embodiment;
an executable code generating unit 802, configured to compile the source code stored in the syntax tree format according to the corresponding relationship to generate an executable code with a variable type being defined, where the unit may be configured to execute step 602 in the above corresponding method embodiment.
Optionally, the apparatus further comprises: a correspondence relationship generation unit;
and the corresponding relation generating unit is used for generating the corresponding relation between the variable identification and the type at least according to the type marking statement in the preset format contained in the source code.
Optionally, the correspondence generating unit includes:
the code reading and circulating control subunit is used for reading the sentences in the source code sentence by sentence and triggering the following subunits to work aiming at each sentence;
the type labeling sentence judging subunit is used for judging whether the sentence is a type labeling sentence with a preset format;
and the first corresponding relation item generating and adding subunit is used for generating a corresponding relation item according to the variable identification extracted from the statement and the corresponding type information and adding the corresponding relation item into the corresponding relation between the variable identification and the type when the output of the type labeling statement judging subunit is yes.
Optionally, the correspondence generating unit includes:
the code reading and circulating control subunit is used for reading the sentences in the source code sentence by sentence and triggering the following subunits to work aiming at each sentence;
the type labeling sentence judging subunit is used for judging whether the sentence is a type labeling sentence with a preset format;
a type information extracting subunit, configured to extract type information from the statement when the output of the type labeling statement judging subunit is yes;
a variable identifier obtaining subunit, configured to read a corresponding variable definition statement and obtain a variable identifier corresponding to the extracted type information according to a position relationship between the statement and the corresponding variable definition statement;
and the second corresponding relation item generating and adding subunit is used for adding the corresponding relation item consisting of the acquired variable identifier and the corresponding type information into the corresponding relation between the variable identifier and the type.
Optionally, the correspondence generating unit is specifically configured to, in a process of generating the source code stored in the syntax tree format through syntax parsing, generate a correspondence between the variable identifier and the type at least according to a type marking statement in a preset format included in the source code.
Optionally, the executable code generating unit includes:
the loop control subunit is used for reading the sentences in the source code stored in the syntax tree format sentence by sentence and triggering the following subunits to work aiming at each sentence;
an optimization condition judgment subunit, configured to judge whether the statement satisfies the following condition: the statement is a variable operation statement, and at least one operational variable has corresponding type information in the corresponding relationship;
an optimized code generation subunit, configured to, when the output of the optimization condition judgment subunit is yes, generate an executable code after variable type limitation is performed on the statement according to type information of an operation variable included in the statement, which is acquired from the correspondence relationship;
and the regular code generation subunit is used for generating executable code without variable type limitation for the statement when the output of the optimization condition judgment subunit is negative.
Optionally, the apparatus further comprises:
an executable code execution unit for executing the executable code.
Although the present application has been described with reference to the preferred embodiments, it is not intended to limit the present application, and those skilled in the art can make variations and modifications without departing from the spirit and scope of the present application, therefore, the scope of the present application should be determined by the claims that follow.
In a typical configuration, a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
The memory may include forms of volatile memory in a computer readable medium, Random Access Memory (RAM) and/or non-volatile memory, such as Read Only Memory (ROM) or flash memory (flash RAM). Memory is an example of a computer-readable medium.
1. Computer-readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), Static Random Access Memory (SRAM), Dynamic Random Access Memory (DRAM), other types of Random Access Memory (RAM), Read Only Memory (ROM), Electrically Erasable Programmable Read Only Memory (EEPROM), flash memory or other memory technology, compact disc read only memory (CD-ROM), Digital Versatile Discs (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include non-transitory computer readable media (transient media), such as modulated data signals and carrier waves.
2. As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.

Claims (18)

1. A method for improving code execution performance, comprising:
generating a corresponding relation between a variable identifier and a type according to a type marking statement in a preset format contained in a source code, wherein the variable identifier is identifier information capable of identifying a variable in the source code; the form of the type annotation statement comprises: a string statement, or an annotation statement;
acquiring a corresponding relation between the variable identification and the type, which is generated in advance aiming at the source code; the source code is written in a weak type language;
compiling the source code stored in a syntax tree format to generate an executable code subjected to variable type limitation according to the corresponding relation;
the generating a corresponding relation between the variable identifier and the type according to the type labeling statement in the preset format included in the source code includes:
reading the statement in the source code sentence by sentence when the type labeling statement in the preset format is added for the variable definition statement and the type labeling statement comprises the variable identification and the corresponding type information, if the read statement is identified to be the type labeling statement in the preset format, generating a corresponding relation entry according to the variable identification extracted from the type labeling statement and the corresponding type information, and adding the corresponding relation entry into the corresponding relation between the variable identification and the type;
or
And when the type marking statement in the preset format is added aiming at the variable definition statement and does not contain a variable identifier, generating a corresponding relation entry of the variable identifier and the type according to the position relation between the type marking statement and the corresponding variable definition statement, and adding the corresponding relation entry into the corresponding relation between the variable identifier and the type.
2. The method of claim 1, wherein the statement and the corresponding variable define a positional relationship of the statement, comprising:
the statement is adjacent to the corresponding variable definition statement and is located before or after the variable definition statement.
3. The method according to claim 1, wherein in the process of generating the source code stored in the syntax tree format through syntax parsing, the corresponding relationship between the variable identifier and the type is generated at least according to a type marking statement in a preset format included in the source code.
4. The method according to claim 1, wherein compiling the source code stored in the syntax tree format to generate an executable code with a variable type definition according to the correspondence comprises:
reading sentences in the source code stored in a syntax tree format sentence by sentence, and executing the following operations for each sentence:
judging whether the statement meets the following conditions: the statement is a variable operation statement, and at least one operational variable has corresponding type information in the corresponding relationship;
if yes, generating an executable code for limiting the variable type of the statement according to the type information of the operation variable contained in the statement, which is acquired from the corresponding relation;
otherwise, generating executable code without variable type definition for the statement.
5. The method of claim 1, further comprising:
the executable code is executed.
6. The method of any of claims 1-5, wherein the weak type language comprises: the JavaScript language.
7. An apparatus for improving code execution performance, comprising:
the device comprises a corresponding relation acquisition unit, a processing unit and a processing unit, wherein the corresponding relation acquisition unit is used for acquiring a corresponding relation between a variable identifier and a type, which is generated in advance aiming at a source code, and the variable identifier refers to identifier information capable of identifying a variable in the source code;
an executable code generating unit, configured to compile the source code stored in a syntax tree format according to the corresponding relationship to generate an executable code subjected to variable type limitation;
the corresponding relation generating unit is used for generating the corresponding relation between the variable identification and the type at least according to the type marking statement in the preset format contained in the source code;
wherein the source code is written in a weak type language; the generating the corresponding relation between the variable identifier and the type at least according to the type marking statement in the preset format contained in the source code comprises: reading the statement in the source code sentence by sentence when the type labeling statement in the preset format is added for the variable definition statement and the type labeling statement comprises the variable identification and the corresponding type information, if the read statement is identified to be the type labeling statement in the preset format, generating a corresponding relation entry according to the variable identification extracted from the type labeling statement and the corresponding type information, and adding the corresponding relation entry into the corresponding relation between the variable identification and the type; or when the type labeling statement in the preset format is added for the variable definition statement and the type labeling statement does not contain the variable identifier, generating a corresponding relation entry of the variable identifier and the type according to the position relation between the type labeling statement and the corresponding variable definition statement, and adding the corresponding relation entry into the corresponding relation between the variable identifier and the type.
8. The apparatus according to claim 7, wherein the correspondence generating unit is specifically configured to, in a process of generating the source code stored in the syntax tree format through syntax parsing, generate the correspondence between the variable identifier and the type at least according to a type labeling statement in a preset format included in the source code.
9. The apparatus of claim 7, wherein the executable code generation unit comprises:
the loop control subunit is used for reading the sentences in the source code stored in the syntax tree format sentence by sentence and triggering the following subunits to work aiming at each sentence;
an optimization condition judgment subunit, configured to judge whether the statement satisfies the following condition: the statement is a variable operation statement, and at least one operational variable has corresponding type information in the corresponding relationship;
an optimized code generation subunit, configured to, when the output of the optimization condition judgment subunit is yes, generate an executable code after variable type limitation is performed on the statement according to type information of an operation variable included in the statement, which is acquired from the correspondence relationship;
and the regular code generation subunit is used for generating executable code without variable type limitation for the statement when the output of the optimization condition judgment subunit is negative.
10. The apparatus of claim 7, further comprising:
an executable code execution unit for executing the executable code.
11. A method for generating a corresponding relation between variable identification and type is characterized by comprising the following steps:
generating a corresponding relation between a variable identifier and a type at least according to a type marking statement in a preset format contained in a source code, so as to generate an executable code subjected to variable type limitation when compiling the source code; wherein the form of the type annotation statement comprises: a string statement, or an annotation statement; the source code is written in a weak type language;
the generating the corresponding relation between the variable identifier and the type at least according to the type marking statement in the preset format contained in the source code comprises:
reading the statement in the source code sentence by sentence when the type labeling statement in the preset format is added for the variable definition statement and the type labeling statement comprises the variable identification and the corresponding type information, if the read statement is identified to be the type labeling statement in the preset format, generating a corresponding relation entry according to the variable identification extracted from the type labeling statement and the corresponding type information, and adding the corresponding relation entry into the corresponding relation between the variable identification and the type;
or
And when the type marking statement in the preset format is added aiming at the variable definition statement and does not contain a variable identifier, generating a corresponding relation entry of the variable identifier and the type according to the position relation between the type marking statement and the corresponding variable definition statement, and adding the corresponding relation entry into the corresponding relation between the variable identifier and the type.
12. The method of claim 11, wherein defining the positional relationship of the statement to the corresponding variable comprises:
the statement is adjacent to the corresponding variable definition statement and is located before or after the variable definition statement.
13. The method according to claim 11, wherein in the process of generating the source code stored in the syntax tree format through syntax parsing, at least according to a type marking statement in a preset format included in the source code, a corresponding relationship between a variable identifier and a type is generated, so that an executable code subjected to variable type limitation is generated when compiling is performed on the source code.
14. An apparatus for generating a correspondence between a variable identifier and a type, comprising:
the corresponding relation generating unit is used for generating the corresponding relation between the variable identification and the type at least according to the type marking statement in the preset format contained in the source code;
the corresponding relation storage unit is used for storing the corresponding relation between the generated variable identification and the type so as to generate an executable code subjected to variable type limitation when compiling the source code;
wherein the form of the type annotation statement comprises: a string statement, or an annotation statement; the source code is written in a weak type language; the generating the corresponding relation between the variable identifier and the type at least according to the type marking statement in the preset format contained in the source code comprises: reading the statement in the source code sentence by sentence when the type labeling statement in the preset format is added for the variable definition statement and the type labeling statement comprises the variable identification and the corresponding type information, if the read statement is identified to be the type labeling statement in the preset format, generating a corresponding relation entry according to the variable identification extracted from the type labeling statement and the corresponding type information, and adding the corresponding relation entry into the corresponding relation between the variable identification and the type; or when the type labeling statement in the preset format is added for the variable definition statement and the type labeling statement does not contain the variable identifier, generating a corresponding relation entry of the variable identifier and the type according to the position relation between the type labeling statement and the corresponding variable definition statement, and adding the corresponding relation entry into the corresponding relation between the variable identifier and the type.
15. The apparatus according to claim 14, wherein the correspondence generating unit is specifically configured to, in a process of generating a source code stored in a syntax tree format through syntax parsing, generate a correspondence between a variable identifier and a type according to at least a type marking statement in a preset format included in the source code, so as to generate an executable code subjected to variable type limitation when compiling the source code.
16. A method for generating source code containing type annotation statements, comprising:
receiving a type marking statement which is input aiming at the source code and used for specifying a variable type; the form of the type annotation statement comprises: a string statement, or an annotation statement; the source code is written using a weak type language;
storing the type marking statement into the source code according to the received type marking statement and the input position of the type marking statement in the source code;
generating a corresponding relation between the variable identifier and the type according to the type marking statement in the preset format contained in the source code, wherein the corresponding relation comprises the following steps:
reading the statement in the source code sentence by sentence when the type labeling statement in the preset format is added for the variable definition statement and the type labeling statement comprises the variable identification and the corresponding type information, if the read statement is identified to be the type labeling statement in the preset format, generating a corresponding relation entry according to the variable identification extracted from the type labeling statement and the corresponding type information, and adding the corresponding relation entry into the corresponding relation between the variable identification and the type;
or
And when the type marking statement in the preset format is added aiming at the variable definition statement and does not contain a variable identifier, generating a corresponding relation entry of the variable identifier and the type according to the position relation between the type marking statement and the corresponding variable definition statement, and adding the corresponding relation entry into the corresponding relation between the variable identifier and the type.
17. The method of claim 16, wherein the weak type language comprises: the JavaScript language.
18. An apparatus for generating source code containing type tagging information, comprising:
a type annotation statement receiving unit, configured to receive a type annotation statement for specifying a variable type, which is input for a source code;
the type labeling statement storage unit is used for storing the type labeling statement into the source code according to the received type labeling statement and the input position of the type labeling statement in the source code;
wherein the form of the type annotation statement comprises: a string statement, or an annotation statement; the source code is written using a weak type language;
the apparatus is further configured to generate a corresponding relationship between the variable identifier and the type according to a type tagging statement in a preset format included in the source code, where the generating the corresponding relationship between the variable identifier and the type according to the type tagging statement in the preset format included in the source code includes: reading the statement in the source code sentence by sentence when the type labeling statement in the preset format is added for the variable definition statement and the type labeling statement comprises the variable identification and the corresponding type information, if the read statement is identified to be the type labeling statement in the preset format, generating a corresponding relation entry according to the variable identification extracted from the type labeling statement and the corresponding type information, and adding the corresponding relation entry into the corresponding relation between the variable identification and the type; or when the type labeling statement in the preset format is added for the variable definition statement and the type labeling statement does not contain the variable identifier, generating a corresponding relation entry of the variable identifier and the type according to the position relation between the type labeling statement and the corresponding variable definition statement, and adding the corresponding relation entry into the corresponding relation between the variable identifier and the type.
CN201610430445.2A 2016-06-16 2016-06-16 Method and device for improving code execution performance Active CN107515739B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201610430445.2A CN107515739B (en) 2016-06-16 2016-06-16 Method and device for improving code execution performance

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201610430445.2A CN107515739B (en) 2016-06-16 2016-06-16 Method and device for improving code execution performance

Publications (2)

Publication Number Publication Date
CN107515739A CN107515739A (en) 2017-12-26
CN107515739B true CN107515739B (en) 2021-02-26

Family

ID=60720486

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201610430445.2A Active CN107515739B (en) 2016-06-16 2016-06-16 Method and device for improving code execution performance

Country Status (1)

Country Link
CN (1) CN107515739B (en)

Families Citing this family (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110347576B (en) * 2018-04-04 2023-09-12 创新先进技术有限公司 Pseudo code debugging method and device
CN110659200B (en) * 2018-06-29 2023-05-02 中国航发商用航空发动机有限责任公司 Method and system for comparing and analyzing source code and target code of aviation onboard software
CN109710304B (en) * 2018-12-27 2022-06-24 潍柴动力股份有限公司 Format adjustment method and device
CN110018829B (en) * 2019-04-01 2022-11-11 北京东方国信科技股份有限公司 Method and device for improving execution efficiency of PL/SQL language interpreter
CN113157256B (en) * 2021-03-25 2024-03-19 北京达佳互联信息技术有限公司 Method and device for generating interface code, electronic equipment, storage medium and product

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2007082830A1 (en) * 2006-01-17 2007-07-26 International Business Machines Corporation System and method for storing text annotations with associated type information in a structured data store
CN101689112A (en) * 2007-06-21 2010-03-31 微软公司 Late bound programmatic assistance
CN104115120A (en) * 2012-02-07 2014-10-22 微软公司 Transferring program execution from compiled code to interpreted code
CN106133684A (en) * 2014-03-27 2016-11-16 微软技术许可有限责任公司 Support the dynamic behaviour in static compilation program

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2007082830A1 (en) * 2006-01-17 2007-07-26 International Business Machines Corporation System and method for storing text annotations with associated type information in a structured data store
CN101689112A (en) * 2007-06-21 2010-03-31 微软公司 Late bound programmatic assistance
CN104115120A (en) * 2012-02-07 2014-10-22 微软公司 Transferring program execution from compiled code to interpreted code
CN106133684A (en) * 2014-03-27 2016-11-16 微软技术许可有限责任公司 Support the dynamic behaviour in static compilation program

Also Published As

Publication number Publication date
CN107515739A (en) 2017-12-26

Similar Documents

Publication Publication Date Title
CN107515739B (en) Method and device for improving code execution performance
CN106919434B (en) Code generation method and device
CN108139891B (en) Method and system for generating suggestions to correct undefined token errors
US9619211B2 (en) Code suggestion in a software development tool
CN107704382B (en) Python-oriented function call path generation method and system
CN103077064B (en) A kind of parsing also executive language method and interpreting means
US8458681B1 (en) Method and system for optimizing the object code of a program
US11579856B2 (en) Multi-chip compatible compiling method and device
CN109491658A (en) The generation method and device of computer-executable code data
US11288062B2 (en) Automatic source code refactoring
US20230315416A1 (en) Code translation method and apparatus, and device
CN111427583A (en) Component compiling method and device, electronic equipment and computer readable storage medium
CN114706660B (en) DAS-based custom function expansion method, device, equipment and storage medium
CN106598828A (en) Method and device for determining invalid class in source code
KR20080038306A (en) Nullable and late binding
CN116028028A (en) Request function generation method, device, equipment and storage medium
CN105867886B (en) Method and device for writing table
US8914782B2 (en) Optimization of declarative queries
CN110941655A (en) Data format conversion method and device
CN111158665B (en) Code generation method and device, electronic equipment and storage medium
Rosen A compiler-building system developed by Brooker and Morris: including a comprehensive discussion of the major features of the system
CN107239264B (en) Method and device for generating code prompt information
CN116432185B (en) Abnormality detection method and device, readable storage medium and electronic equipment
CN105786465A (en) Scripting language execution method and device
US8341607B2 (en) Condensing pattern matcher generation for intermediate language patterns

Legal Events

Date Code Title Description
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
REG Reference to a national code

Ref country code: HK

Ref legal event code: DE

Ref document number: 1248345

Country of ref document: HK

TA01 Transfer of patent application right

Effective date of registration: 20201130

Address after: Room 603, 6 / F, Roche Plaza, 788 Cheung Sha Wan Road, Kowloon, China

Applicant after: Zebra smart travel network (Hong Kong) Limited

Address before: A four-storey 847 mailbox in Grand Cayman Capital Building, British Cayman Islands

Applicant before: Alibaba Group Holding Ltd.

TA01 Transfer of patent application right
GR01 Patent grant
GR01 Patent grant