The content of the invention
The embodiment of the present application provides a kind of method and apparatus for improving code execution performance, to solve existing weak type language
Say the problem of execution performance is low.The embodiment of the present application also provide a kind of corresponding relation for generating marking variable and type method and
Device, and a kind of method and device for being used to generate the source code of containing type mark sentence.
The application provides a kind of method for improving code execution performance, including:
Obtain previously generated for source code, marking variable and the corresponding relation of type;
According to the corresponding relation, the compilation of source code stored with grammer tree format is generated and carries out types of variables limit
Executable code after fixed.
Optionally, before the corresponding relation for obtaining previously generated for source code, marking variable and type, bag
Include:
The type mark sentence of the preset format contained according at least to the source code package, generates the marking variable and type
Corresponding relation.
Optionally, the type mark sentence of the preset format contained according at least to the source code package, generates the variable mark
Know the corresponding relation with type, including:
The sentence in the source code is read sentence by sentence, and operations described below is performed for each sentence:
Judge the sentence whether be preset format type mark sentence;
If so, perform operations described below:Generated according to the marking variable and respective type information extracted from the sentence
Corresponding relation entry, and be added in the marking variable and the corresponding relation of type.
Optionally, the type mark sentence of the preset format contained according at least to the source code package, generates the variable mark
Know the corresponding relation with type, including:
The sentence in the source code is read sentence by sentence, and operations described below is performed for each sentence:
Judge the sentence whether be preset format type mark sentence;
If so, perform operations described below:Type information is extracted from the sentence;Defined according to the sentence and relevant variable
The position relationship of sentence, read corresponding variable-definition sentence and obtain the marking variable corresponding with the type information extracted;
The marking variable and type will be added to by the marking variable that obtains and the corresponding relation entry that respective type information forms
In corresponding relation.
Optionally, the sentence and the position relationship of relevant variable definition statement, including:
The sentence is adjacent with relevant variable definition statement and before the variable-definition sentence or afterwards.
Optionally, the form of the type mark sentence includes:Character string statement, or comment statement.
Optionally, during the source code stored with grammer tree format is generated by syntax parsing, extremely
The type mark sentence of few preset format contained according to the source code package, generates marking variable pass corresponding with type
System.
Optionally, it is described according to the corresponding relation, by the compilation of source code stored with grammer tree format generate into
Executable code after the restriction of row types of variables, including:
Read sentence by sentence with the sentence in the source code of grammer tree format storage, and following behaviour are performed for each sentence
Make:
Judge whether the sentence meets following condition:The sentence is for variable arithmetic statement and wherein at least in the presence of one
Individual computing variable has corresponding type information in the corresponding relation;
If satisfied, the computing type of variables information included according to sentence obtained from the corresponding relation, described, raw
Executable code into after for sentence progress types of variables restriction;
Otherwise, the executable code of types of variables restriction is not carried out for sentence generation.
Optionally, methods described also includes:
Perform the executable code.
Optionally, the source code is write with weak type language, and the weak type language includes:JavaScript languages
Speech.
Accordingly, the application also provides a kind of device for improving code execution performance, including:
Corresponding relation acquiring unit, for obtaining, being previously generated for source code, marking variable is corresponding with type closes
System;
Executable code generation unit, for according to the corresponding relation, the source generation that will be stored with grammer tree format
Code compiling generation carries out the executable code after types of variables restriction.
Optionally, described device also includes:Corresponding relation generation unit;
The corresponding relation generation unit, the type of the preset format for containing according at least to the source code package mark language
Sentence, generates the marking variable and the corresponding relation of type.
Optionally, the corresponding relation generation unit, including:
Code is read and loop control subelement, for reading the sentence in the source code sentence by sentence, and is directed to each language
Sentence triggers following subelement work;
Type mark sentence judgment sub-unit, for judge the sentence whether be preset format type mark sentence;
First corresponding relation entry generates and addition subelement, for marking the defeated of sentence judgment sub-unit when the type
When going out to be, corresponding relation entry is generated according to the marking variable and respective type information extracted from the sentence, and add
It is added in the marking variable and the corresponding relation of type.
Optionally, the corresponding relation generation unit, including:
Code is read and loop control subelement, for reading the sentence in the source code sentence by sentence, and is directed to each language
Sentence triggers following subelement work;
Type mark sentence judgment sub-unit, for judge the sentence whether be preset format type mark sentence;
Type information extracts subelement, and the output for marking sentence judgment sub-unit when the type is when being, from institute
Type information is extracted in predicate sentence;
Marking variable obtains subelement, for the position relationship according to the sentence and relevant variable definition statement, reads
Corresponding variable-definition sentence simultaneously obtains the marking variable corresponding with the type information extracted;
Second corresponding relation entry generates and addition subelement, for by by the marking variable that obtains and respective type information
The corresponding relation entry of composition is added in the marking variable and the corresponding relation of type.
Optionally, the corresponding relation generation unit, specifically for described with syntax tree lattice by syntax parsing generation
During the source code of formula storage, the type mark sentence of the preset format contained according at least to the source code package is raw
Into the marking variable and the corresponding relation of type.
Optionally, the executable code generation unit, including:
Loop control subelement, for reading sentence by sentence with the sentence in the source code of grammer tree format storage, and pin
Following subelement work is triggered to each sentence;
Optimal conditions judgment sub-unit, for judging whether the sentence meets following condition:The sentence is transported for variable
Calculating sentence and a computing variable being wherein at least present has corresponding type information in the corresponding relation;
Optimized code generates subelement, for when the output of the optimal conditions judgment sub-unit is is, according to from institute
The computing type of variables information that sentence obtained in corresponding relation, described includes is stated, generation carries out variable for the sentence
Executable code after type restriction;
Conventional code generate subelement, for when the optimal conditions judgment sub-unit output for it is no when, for described
Sentence generates the executable code for not carrying out types of variables restriction.
Optionally, described device also includes:
Executable code execution unit, for performing the executable code.
In addition, the application also provides a kind of method for generating marking variable and the corresponding relation of type, including:
The type mark sentence of the preset format contained according at least to source code package, generation marking variable is corresponding with type to close
System, during for being compiled for the source code generation carry out the executable code after types of variables restriction.
Optionally, the type mark sentence of the preset format contained according at least to source code package, generates the variable mark
Know the corresponding relation with type, including:
The sentence in the source code is read sentence by sentence, and operations described below is performed for each sentence:
Judge the sentence whether be preset format type mark sentence;
If so, perform operations described below:Generated according to the marking variable and respective type information extracted from the sentence
Corresponding relation entry, and be added in the marking variable and the corresponding relation of type.
Optionally, the preset format contained according at least to source code package type mark sentence, generate the variable with
The corresponding relation of type, including:
The sentence in the source code is read sentence by sentence, and operations described below is performed for each sentence:
Judge the sentence whether be preset format type mark sentence;
If so, perform operations described below:Type information is extracted from the sentence;Defined according to the sentence and relevant variable
The position relationship of sentence, read corresponding variable-definition sentence and obtain the marking variable corresponding with the type information extracted;
The marking variable and type will be added to by the marking variable that obtains and the corresponding relation entry that respective type information forms
In corresponding relation.
Optionally, the sentence and the position relationship of relevant variable definition statement include:
The sentence is adjacent with relevant variable definition statement and before the variable-definition sentence or afterwards.
Optionally, the form of the type mark sentence includes:Character string statement, or comment statement.
Optionally, during being generated by syntax parsing with the source code of grammer tree format storage, according at least to institute
The type mark sentence for the preset format that source code package contains is stated, marking variable and the corresponding relation of type are generated, for for institute
State generation when source code is compiled and carry out the executable code after types of variables restriction.
Accordingly, the application also provides a kind of device for generating marking variable and the corresponding relation of type, including:
Corresponding relation generation unit, the type of the preset format for containing according at least to source code package mark sentence, generation
The corresponding relation of marking variable and type;
Correspondence relation storage, for storing the corresponding relation of the marking variable generated and type, for for institute
State generation when source code is compiled and carry out the executable code after types of variables restriction.
Optionally, the corresponding relation generation unit includes:
Code is read and loop control subelement, for reading the sentence in the source code sentence by sentence, and is directed to each language
Sentence triggers following subelement work;
Type marks sentence judgment sub-unit, for judging whether the sentence is type mark sentence;
First corresponding relation entry generates and addition subelement, for marking the defeated of sentence judgment sub-unit when the type
When going out to be, corresponding relation entry is generated according to the marking variable and respective type information extracted from the sentence, and add
It is added in the marking variable and the corresponding relation of type.
Optionally, the corresponding relation generation unit includes:
Code is read and loop control subelement, for reading the sentence in the source code sentence by sentence, and is directed to each language
Sentence triggers following subelement work;
Type mark sentence judgment sub-unit, for judge the sentence whether be preset format type mark sentence;
Type information extracts subelement, and the output for marking sentence judgment sub-unit when the type is when being, from institute
Type information is extracted in predicate sentence;
Marking variable obtains subelement, for the position relationship according to the sentence and relevant variable definition statement, reads
Corresponding variable-definition sentence simultaneously obtains the marking variable corresponding with the type information extracted;
Second corresponding relation entry generates and addition subelement, for by by the marking variable that obtains and respective type information
The corresponding relation list item of composition is added in the marking variable and the corresponding relation of type.
Optionally, the corresponding relation generation unit, specifically for being deposited with grammer tree format being generated by syntax parsing
During the source code of storage, the type mark sentence of the preset format contained according at least to the source code package, variable mark is generated
Know with the corresponding relation of type, during for being compiled for the source code generation carry out executable after types of variables restriction
Code.
In addition, the application also provides a kind of method for being used to generate the source code of containing type mark sentence, including:
Receive type for source code input, for specifying variable type and mark sentence;
According to the type mark sentence received and its input position in the source code, the type is marked
Sentence is stored into the source code;
Wherein, the type mark sentence is preset format.
Optionally, the form of the type mark sentence includes:Character string statement, or comment statement.
Optionally, the source code is write using weak type language, and the weak type language includes:JavaScript languages
Speech.
Accordingly, the application also provides a kind of device for being used to generate the source code of containing type markup information, including:
Type marks sentence receiving unit, for receiving type for source code input, for specifying variable type
Sentence is marked, wherein, the type mark sentence is preset format;
Type marks sentence memory cell, for according to the type mark sentence received and its in the source code
Input position, by the type mark sentence storage into the source code.
Compared with prior art, the application has advantages below:
That a kind of method for raising code execution performance that the application provides, first acquisition previously generate for source code,
The corresponding relation of marking variable and type, then according to the corresponding relation, the source code that will be stored with grammer tree format
Compiling generation carries out the executable code after types of variables restriction.Using the above method, due to previously generated marking variable with
The corresponding relation of type, therefore types of variables can be defined and be given birth to according to the corresponding relation when compiling source code
Executable code into after progress types of variables restriction, so as to reduce the memory headroom of executable code occupancy, Er Qieyou
In reducing the process that is judged for various possible types, therefore the execution speed of the executable code is also mutually deserved
To lifting, improved overall execution performance.
When it is implemented, preset format can be added during code is write using weak type language by developer
Type mark sentence, and the application provide be used for generate containing type mark sentence source code method, pass through reception
The source code of the above-mentioned sentence generation containing type mark sentence of developer's input, the life that can then utilize the application to provide
Into the method for marking variable and the corresponding relation of type, the source code that sentence is marked according to the containing type generates marking variable
With the corresponding relation of type, and the method for the raising code execution performance provided using the application utilizes the corresponding pass generated
System, generation carry out the executable code after types of variables restriction for the source code.
Because the type mark sentence added in source code meets weak type language syntax rule, therefore JS need not be changed
Language (such as:Addition carries out the keyword of type definition), so as to which weak type language can't be changed over by force from speech level
Type language, therefore can still retain the flexibility of weak type language, developer still can practise according to original programming
It is used to write source code, it is only necessary to the place addition type mark sentence of types of variables can be specified, it is possible to which realization is compiling
Stage carries out the purpose of types of variables restriction.
The form of type mark sentence can be character string sentence or comment statement or other forms, no
Influence the code statement that source code function is realized.The comment statement refers to the effect without the realization of any function, is only
The sentence illustrated to source code, two examples of comment statement in JavaScript language are provided here:
The code statement is different from comment statement, typically refers to the sentence for realizing source code function, such as:Variable
Definition statement, variable arithmetic statement etc., source code are typically to be made up of comment statement and code statement.Implementing the technical program
When, the code statement for not influenceing the realization of source code function can be used, such as:Language is marked as type using character string statement
Sentence.
In order to make it easy to understand, below in an example emphasis using use character string statement as type mark sentence as
Example, is illustrated to the embodiment of the technical program.As for the code statement using other forms or using comment statement
Sentence is marked as type, can also be practiced based on identical principle.
Embodiments herein is described in detail below, in order to make it easy to understand, the one kind for first describing the application is used for
Generate the embodiment of the method for the source code of containing type mark sentence.
Fig. 1 is refer to, it is a kind of reality for being used to generate the method for the source code that containing type marks sentence of the application
Apply the flow chart of example.Methods described comprises the following steps:
Step 101, receive type mark sentence for source code input, for specifying variable type.
Source code described in this step can be a code snippet, wherein can include function realize code,
Or the whole codes realized in code or source file for realizing some allomeric function of several functions.
The mode that developer adds the type mark sentence for specifying variable type in source code can be advance
Set, also in compliance with identical predetermined manner when extracting the corresponding relation of scalar mark and type subsequently from source code.Example
Such as:Sentence can be marked to some or some variables that are defined in this function using type in the beginning of function
Type clearly specify, and the variable-definition sentence that can also be directed in function adds corresponding type mark sentence.Consider
The second way is more directly perceived, is easy to developer to operate, therefore typically developer adds the preferred of type mark sentence
Mode.
Using aforesaid way, developer can add the type mark language in the position adjacent with variable-definition sentence
Sentence, such as:Before variable-definition sentence, or after variable-definition sentence, the type for specifying variable type is added
Mark sentence.
Also, the class of in relevant variable definition statement, at least one variable is comprised at least in the type mark sentence
Type information.If the definition of n variable is included in variable-definition sentence, then for the type of variable-definition sentence addition
Mark sentence can include wherein each type of variables information, can also only include the class of wherein some or several variables
Type information, as long as correct corresponding relation can be set up between variable and type information to be just all possible.
The form of the type mark sentence is typically set in advance, such as:Type mark sentence uses predetermined symbol
Beginning, for different type of variables information default separator can be used to be separated from each other, the default separator can be:Point
Number, comma or other symbols, such as:@.The preset format that the present embodiment does not mark sentence to type is defined, as long as energy
Type mark sentence is identified in the sentence enough contained by preset format from source code package and extracts type information.
Illustrated below exemplified by type mark sentence is added in the source code using written in JavaScript, under
In the specific example in face, the type mark sentence of preset format is:Using "@" character or "<-@" characters are as beginning character
Character string statement.Wherein, beginning character is that "@" represents that type is added before variable-definition sentence marks sentence, "<-@" tables
Show and type mark sentence is added after variable-definition sentence.
It is not added with the source code of type mark sentence:
Var num=1;
Const str=" hello ", isTrue=true;
Var func=function (num, str) { };
1) type mark sentence (being added in different code lines) is added before variable-definition sentence:
“@int”;
Var num=1;
“@string,@boolean”;
Const str=" hello ", isTrue=true;
“@boolean function(@int,@string)”;
Var func=function () { };
2) type mark sentence (being added in identical code line) is added before variable-definition sentence:
“@int”;Var num=1;
“@string,@boolean”;Const str=" hello ", isTrue=true;
“@boolean function(@int,@string)”;Var func=function (num, str) { };
3) type mark sentence is added after variable-definition sentence:
Var num=1;“<-@int”;
Const str=" hello ", isTrue=true;“<-@string,@boolean”;
Var func=function (num, str) { };“<-@boolean function(@int,@string)”;
Above-mentioned three specific examples for 1), 2), 3) each providing addition type mark sentence, in these three specific examples
The effect of the type mark sentence of middle addition is identical:Num types of variables are appointed as int, str types of variables are referred to
It is set to string, isTrue types of variables is appointed as boolean, func types of variables is appointed as function.
The type of the Partial Variable in relevant variable definition statement can also be only specified in type mark sentence, for example, it is following
The source code of type mark sentence is with the addition of, wherein the type information before first separator is sky, shows not to be corresponding
Num variable specified types, behind have type information before two separators, i.e.,:It is for variable str specified types
String, it is that variable i sTrue specified types are boolean.
“@object{,@string,@boolean}”;
Const obj={ num:1,str:“hello”,isTrue:true};
In the specific implementation, marking variable can also be clearly write in type mark sentence, i.e.,:Can in type mark sentence
With including one or two tuples that more than one is made up of marking variable and respective type information, such as:
“@object{@str:string,@isTrue:boolean}”;
Const obj={ num:1,str:“hello”,isTrue:true};
In the specific implementation, it generally can in the editing window load and show source code, developer is in editor
Input cursor at input the type mark sentence of preset format in the manner described above, this step correspondingly received will arrive the class
Type marks sentence and knows input position of the type received the mark sentence in source code.
The type mark sentence and its input position in the source code that step 102, basis receive, by described in
Type mark sentence storage is into the source code.
This step is according to the type mark sentence received and its input position in the source code, by type mark
Sentence storage is noted into the source code, so as to generate the source code of containing type mark sentence.
In the specific implementation, the type that step 101 receives can be marked under sentence and its input position information cache
Come, and after storage order is received, the type of caching is marked into sentence and arrives the source generation according to the storage of its input position information
In code.
Above by taking JavaScript as an example, the embodiment of the present embodiment is described in detail.People in the art
Member can also be applied in the source code write using other weak type languages, institute it should be understood that the method for the present embodiment description
Stating weak type language includes:PHP, Lua etc..
In the specific implementation, the preset format of type mark sentence and the position of addition can be according to weak type languages
Syntax rule and actual demand preset, as long as can identify that type mark and refers to sentence according to preset format
Determining the corresponding relation between variable and type is just all possible.On the basis of type mark sentence is added in source code,
When needing to perform the source code, it is possible to which the preset format that sentence is marked according to type extracts marking variable and pair of type
It should be related to, and the executable code after carrying out types of variables restriction is generated according to corresponding relation.
The embodiment of the present embodiment is illustrated by using exemplified by character string statement above.In other embodiment
In, type mark sentence can also use the form of comment statement, can equally add in the beginning of function, or addition
Before or after variable-definition sentence.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, the method for being used to generate the source code that containing type marks sentence that the present embodiment provides, by connecing
Narrow and sentence is marked to the type of source code input and stores type mark sentence into the source code, generate and include
Type marks the source code of sentence.So as to make up weak type language can not specifying variable type the defects of, to be subsequently generated
The executable code for carrying out types of variables restriction provides possibility.The mode of sentence is marked as a result of addition type, no
Need to modify to original code statement in source code, be easy to developer's implementation;Especially with variable-definition language
The mode of the corresponding addition of sentence, it is only necessary to add a type mark sentence for variable-definition is corresponding, not only corresponding relation
Clearly, and the mark workload of developer can be reduced.
In the above-described embodiment, there is provided a kind of method for being used to generate the source code of containing type mark sentence, with
It is corresponding, the application also provide it is a kind of be used for generate containing type mark sentence source code device.It refer to Fig. 2,
It is a kind of schematic diagram for being used to generate the embodiment of the device for the source code that containing type marks sentence of the application.Due to dress
Put embodiment and be substantially similar to embodiment of the method, so describing fairly simple, referring to the part of embodiment of the method in place of correlation
Explanation.Device embodiment described below is only schematical.
A kind of device for being used to generate the source code of containing type mark sentence of the present embodiment, including:Type marks language
Sentence receiving unit 201, sentence is marked for receiving type for source code input, for specifying variable type, wherein, institute
It is preset format to state type mark sentence, and this unit can be used for performing the step 101 in above-mentioned correlation method embodiment;
Type mark sentence memory cell 202, for according to the type mark sentence received and its in the source code
In input position, by the type mark sentence storage into the source code, this unit can be used for perform above-mentioned respective party
Step 102 in method embodiment.
In addition, the application also provides a kind of method for generating marking variable and the corresponding relation of type, Fig. 3 is refer to, its
For the application provide a kind of generation marking variable and type corresponding relation embodiment of the method flow chart, the present embodiment with
Each embodiment content identical part provided before repeats no more, below emphasis difference is described.The one of the application offer
The method of kind generation marking variable and the corresponding relation of type includes:
Step 301, the type of the preset format contained according at least to source code package mark sentence, generate marking variable and type
Corresponding relation, during for being compiled for the source code generation carry out the executable code after types of variables restriction.
Before this step is performed, the source code of the type mark sentence comprising preset format has generally been generated, this
Step generates marking variable and the corresponding relation of type on the basis of the type mark sentence in reading source code, so that
The executable code after carrying out types of variables restriction can be generated according to the type information in corresponding relation during compiling.
This step can mark the preset format of sentence according to type, identify that type marks language from the source code
Sentence, and the corresponding relation is generated on this basis.The preset format of type mark sentence is different, the processing procedure of this step
Can be different.
If the beginning of function is set in advance in using type mark sentence to the variable that is defined in this function
Type clearly specify, then this step can read the type mark sentence in function beginning, correspondingly generate
The corresponding relation entry of marking variable and type, and be added in marking variable and the corresponding relation of type.
If type mark sentence is preset for being wrapped in the addition of variable-definition sentence and type mark sentence
Containing marking variable and corresponding type information, then this step can read each sentence in source code sentence by sentence, if identification
The sentence for going out reading marks sentence for the type of preset format, then the marking variable extracted according to being marked from the type in sentence
And respective type information generation corresponding relation entry, and be added in marking variable and the corresponding relation of type.
If preset type mark sentence to mark in sentence not for the addition of variable-definition sentence and type
Comprising variable identifies, then needs to mark position relationship of the sentence with corresponding variable-definition sentence according to type, generate variable mark
Know the corresponding relation entry with type, and be added in marking variable and the corresponding relation of type.With reference to a concrete example
Son illustrates to this mode.
In the specific example, character string forms are with the addition of before or after the variable-definition sentence in source code
Type mark sentence, this step reads each sentence in the source code sentence by sentence, and is handled for each sentence, locate
Reason process includes step 301-1 to step 301-5, is described in detail with reference to Fig. 4.
Step 301-1, judge the sentence whether be preset format type mark sentence;If so, perform step 301-
2。
Whether this step can meet the preset format of type mark sentence according to the sentence, whether judge the sentence
Sentence is marked for type.In the specific example, the type mark sentence of preset format is:Using "@" character or "<-@”
Character string statement of the character as beginning character, then this step can judge as follows:Whether the sentence is character string language
Sentence and with@" or "<Beginning characters of-the@" as character string, if the sentence currently read meets the requirement, illustrate institute
Predicate sentence is type mark sentence, then performs step 301-2.
Step 301-2, type information is extracted from the sentence.
This step can mark the preset format of sentence according to type, extract type information, such as:According to the finger of separator
Show extraction type information.
Step 301-3, according to the sentence and the position relationship of relevant variable definition statement, corresponding variable-definition is read
Sentence simultaneously obtains the marking variable corresponding with the type information extracted.
The type mark sentence and the position relationship of relevant variable definition statement, can be in developer into source code
It is just pre-set during addition type definition sentence, such as:Preset type and mark sentence before variable-definition sentence,
So this step read positioned at the type mark sentence after next variable-definition sentence (by taking Java Script as an example,
Variable-definition sentence can be var sentences or const sentences);If presetting type marks sentence in variable-definition language
After sentence, then this step reads the upper variable-definition sentence before type mark sentence.
The type mark sentence and the position relationship of relevant variable definition statement, can also be marked in sentence by type
Presupposed information specify, in the specific example, if type mark sentence using "@" character start, represent type mark
Sentence is before variable-definition sentence;If type mark sentence using "<-@" characters start, then represent type mark sentence and exist
After variable-definition sentence, then this step can mark all characters of sentence character string according to type, read corresponding become
Measure definition statement.
After the relevant variable definition statement for reading the type mark sentence, further it can be marked according to type in sentence
Type and relevant variable definition statement in variable corresponding relation, obtain relative with the type information that step 301-2 is extracted
The marking variable answered, the marking variable are the identification informations for referring to the variable in source code described in unique identification.If institute
It is a function to state source code, because the variable name of each variable defined in function is all unique, therefore can be with from institute
The variable name extracted in variable-definition sentence is stated as marking variable;If the source code includes two or more
Function, for the variable defined in function, then it can be used as and become by the use of " function name+extracted from variable-definition sentence variable name "
Amount mark.
Step 301-4, institute will be added to by the marking variable obtained and the corresponding relation entry that respective type information forms
State in marking variable and the corresponding relation of type.
This step is gone to, according to the type identification sentence and corresponding variable-definition sentence, obtains change
Amount mark and type information corresponding with the marking variable, therefore this step can be formed according to the information obtained and corresponded to
Relationship entries, and the corresponding relation entry is added in marking variable and the corresponding relation of type.
For example, the type mark sentence read is:“@string,@boolean”;Corresponding variable-definition sentence is
Const str=" hello ", isTrue=true;So this step generates two corresponding relation entries as follows, and can
So that the two entries to be added in marking variable and the corresponding relation of type.
1)str:string
2)isTrue:boolean
For each sentence in the source code, above-mentioned steps 301-1 is performed to 301-4, and the corresponding of generation is closed
It is that entry is all added in marking variable and the corresponding relation of type, then the sentence in the source code is all disposed
Afterwards, marking variable and the corresponding relation of type just are generated for the source code.
So far, the generating process of marking variable and the corresponding relation of type is said by a specific example
It is bright.In the specific implementation, because the position that type mark sentence is added in source code may be different, type mark sentence pre-
If form may also be different, therefore the embodiment for generating the corresponding relation can also have differences, in actual applications,
It can accordingly be changed, be adjusted as the case may be on the basis of above-mentioned example.
For example, the form of type mark sentence is character string statement, in other embodiments, type in the above example
The form of comment statement can also be used by marking sentence, in this case, can be judged as follows in step 301-1:Institute
Predicate sentence whether be comment statement and with@" or "<Beginning characters of-the@" as annotation information.If the sentence currently read
Meet the requirement, then it is type mark sentence to illustrate the sentence.The embodiment of other steps is similar with character string statement
, here is omitted.
It should be noted that generation marking variable and the method for the corresponding relation of type that the present embodiment provides, can be only
It is vertical to implement, it can also implement during being generated by syntax parsing with the source code of grammer tree format storage.I.e.:Passing through
During syntax parsing generation is with the source code of grammer tree format storage, the preset format that contains according at least to the source code package
Type mark sentence, generate the corresponding relation of marking variable and type.This embodiment is illustrated below.
Generally before source code is performed, syntax parsing can be first carried out.During syntax parsing, source is read sentence by sentence
Sentence in code simultaneously determines whether to generate corresponding syntax tree or grammer for the sentence currently read according to preset strategy
Tree node.Source code is disposed using aforesaid way, then the source code is converted to the storage of grammer tree format
Source code, the source code stored with grammer tree format is compiled, it is possible to generate executable code.
The generation marking variable and the method for the corresponding relation of type that the present embodiment provides, can be merged in above-mentioned grammer solution
Completed during analysis.
For example, during being generated by syntax parsing with the source code of grammer tree format storage, for reading sentence by sentence
Each sentence, not only perform the processing related to generative grammar tree and operate, also perform operations described below:Whether judge the sentence
Sentence is marked for the type of preset format;If so, according to the marking variable extracted from type mark sentence and accordingly
Type information generates corresponding relation entry, and is added in the marking variable and the corresponding relation of type.
For another example, during being generated by syntax parsing with the source code of grammer tree format storage, for reading sentence by sentence
Each sentence, not only perform the processing related to generative grammar tree and operate, also perform operations described below:Whether judge the sentence
Sentence is marked for the type of preset format;If so, extract type information from the sentence;According to the sentence and relevant variable
The position relationship of definition statement, read corresponding variable-definition sentence and obtain the variable mark corresponding with the type information extracted
Know;The marking variable and type will be added to by the marking variable obtained and the corresponding relation entry that respective type information forms
Corresponding relation in.
Using above-mentioned embodiment, by carrying out syntax parsing (process is also commonly referred to as Parse processes) to source code,
The source code stored with grammer tree format is not only generated, while also creates marking variable and the corresponding relation of type, so as to
Without in order to generate the operation that the corresponding relation additionally performs reading code, being improved in the technical scheme provided using the application
While code execution performance, the extra occupancy to processor resource is avoided, parsing is not interfered with, compiles the globality of source code
Energy.
By above description as can be seen that the side for generating marking variable and the corresponding relation of type that the present embodiment provides
Method, on the basis of the type mark sentence that source code package contains is read, marking variable and type are generated for the source code
Corresponding relation, so as to be compiled the executable code that generation carried out after types of variables restriction to be follow-up for source code and carry out
Preparation.Due to having extracted the corresponding relation of marking variable and type in advance, compilation process need not performance variable again
The parsing operation of type, type of variables information is obtained directly from the correspondence set, can be improved compiled
The efficiency of journey.
In the above-described embodiment, there is provided a kind of method for the corresponding relation for generating marking variable and type, therewith phase
Corresponding, the application also provides a kind of device for generating marking variable and the corresponding relation of type.Fig. 5 is refer to, it is this Shen
A kind of generation marking variable and the schematic diagram of the embodiment of the device of the corresponding relation of type please.Because device embodiment is basic
Similar in appearance to embodiment of the method, so describing fairly simple, the relevent part can refer to the partial explaination of embodiments of method.It is following
The device embodiment of description is only schematical.
A kind of device of the generation marking variable of the present embodiment and the corresponding relation of type, including:Corresponding relation generation is single
Member 501, the type of the preset format for containing according at least to source code package mark sentence, and generation marking variable is corresponding with type
Relation, this unit can be used for performing the step 301 in above-mentioned correlation method embodiment;
Correspondence relation storage 502, for storing the corresponding relation of the marking variable that has generated and type, for for
Generation carries out the executable code after types of variables restriction when the source code is compiled.
Optionally, the corresponding relation generation unit includes:
Code is read and loop control subelement, for reading the sentence in the source code sentence by sentence, and is directed to each language
Sentence triggers following subelement work;
Type marks sentence judgment sub-unit, for judging whether the sentence is type mark sentence;
First corresponding relation entry generates and addition subelement, for marking the defeated of sentence judgment sub-unit when the type
When going out to be, corresponding relation entry is generated according to the marking variable and respective type information extracted from the sentence, and add
It is added in the marking variable and the corresponding relation of type.
Optionally, the corresponding relation generation unit includes:
Code is read and loop control subelement, for reading the sentence in the source code sentence by sentence, and is directed to each language
Sentence triggers following subelement work;
Type mark sentence judgment sub-unit, for judge the sentence whether be preset format type mark sentence;
Type information extracts subelement, and the output for marking sentence judgment sub-unit when the type is when being, from institute
Type information is extracted in predicate sentence;
Marking variable obtains subelement, for the position relationship according to the sentence and relevant variable definition statement, reads
Corresponding variable-definition sentence simultaneously obtains the marking variable corresponding with the type information extracted;
Second corresponding relation entry generates and addition subelement, for by by the marking variable that obtains and respective type information
The corresponding relation list item of composition is added in the marking variable and the corresponding relation of type.
Optionally, the corresponding relation generation unit, specifically for being deposited with grammer tree format being generated by syntax parsing
During the source code of storage, the type mark sentence of the preset format contained according at least to the source code package, variable mark is generated
Know with the corresponding relation of type, during for being compiled for the source code generation carry out executable after types of variables restriction
Code.
In addition, the application also provides a kind of method for improving code execution performance, Fig. 6 is refer to, it is the application
A kind of flow chart of the embodiment of the method for the raising code execution performance provided, the present embodiment is therewith in each embodiment of preceding offer
Hold identical part to repeat no more, below emphasis difference is described.A kind of raising code execution performance that the application provides
Method includes:
Step 601, obtain previously generated for source code, marking variable and the corresponding relation of type.
Before this step is performed, generally marking variable pass corresponding with type is generated for source code in advance
System, several corresponding relation entries are generally comprised in the corresponding relation, comprising variable identifies in each corresponding relation entry
And corresponding type information.The corresponding relation can be stored in internal memory or be in disk file, and this step is for institute
State before source code is compiled, the corresponding relation can be obtained by reading the modes such as internal memory or disk file.
Step 602, according to the corresponding relation, the compilation of source code stored with grammer tree format generation is become
Measure the executable code after type limits.
Due to obtaining the corresponding relation of the marking variable previously generated for source code and type in step 601, because
This, can be according to the corresponding relation to variable during the source code to being stored with grammer tree format is compiled
Type is defined, and generates the executable code after carrying out types of variables restriction, i.e.,:Carry out the executable generation after type optimization
Code.
Due to limiting types of variables primarily directed to variable arithmetic statement, thus the present embodiment using variable arithmetic statement as
Emphasis, the process for generating executable code is illustrated, in the specific implementation, other can also be directed to using similar method
The sentence of type carries out type optimization.
This step can be successively read with each sentence in the source code of grammer tree format storage, and for each
Sentence generates corresponding executable code by following steps 602-1 to 602-3, is described in detail with reference to Fig. 7.
Step 602-1, judge whether the sentence meets following condition:The sentence is variable arithmetic statement and wherein
A computing variable at least be present has corresponding type information in the corresponding relation, if satisfied, step 602-2 is performed,
Otherwise 602-3 is performed.
The main task of this step is:Judge whether to carry out types of variables restriction for the sentence currently read.
Can first identify whether the sentence is variable arithmetic statement, so-called variable arithmetic statement refer to comprising operator and
The sentence of computing variable, wherein the operator can be monocular, binocular either ternary operator, corresponding computing variable
Number can be one, two or three.
If variable arithmetic statement, then determine whether the variable arithmetic statement at least has a computing and become
Amount has corresponding type information in the corresponding relation.When it is implemented, each computing that can be included according to the sentence
Variable is searched in the corresponding relation that step 601 obtains, if with the marking variable of certain computing variable corresponding to it is corresponding
Relationship entries, then read type information therein.After performing above-mentioned search operation, if at least one computing variable is described
There is corresponding type information in corresponding relation, then can performs step 602-2.
If the sentence is unsatisfactory for above-mentioned two condition, step 602-3 is performed.
Step 602-2, the computing type of variables letter included according to sentence obtained from the corresponding relation, described
Breath, generation carry out the executable code after types of variables restriction for the sentence.
Go to this step, explanation can be directed to the sentence and carry out types of variables restriction, therefore this step is according to from institute
The computing type of variables information that sentence obtained in corresponding relation, described includes is stated, after generation carries out types of variables restriction
Executable code.
In the specific implementation, the efficient finger for the type that platform can be selected to provide according to computing type of variables
Order, such as on certain platform, integer adds computing to be instructed using add, and floating type adds computing to be instructed using addf, to have in this specification
The z=x+y that body embodiment beginning provides;Exemplified by arithmetic statement, this step can obtain according to from the corresponding relation
X, y type of variables taken generates corresponding executable code, for example, understanding that x is integer and y is whole according to the corresponding relation
Type, i.e., x and y type are defined, then generating corresponding executable code is:add z,x,y;For another example, according to institute
X knowable to stating corresponding relation is integer, y is double-precision floating point type, then generating corresponding executable code is:addf z,x,y.
As can be seen here, z=x+y arithmetic statements are directed to what is above provided in this specification embodiment beginning
The executable code of generation compares, carry out the executable code that generates after types of variables restriction there is no substantial amounts of branch to judge with
And the different instruction code for each branch, but only generate an executable code, and using with computing class
The special efficient operational order that type matches, therefore the memory headroom of executable statement occupancy is not only reduced, and can be with
The execution speed of executable code is improved, so that overall execution performance is improved.
It should be noted that above for readily appreciating, given only for add operation after carrying out types of variables restriction
A schematical example of executable code is generated, can be according to platform and arithmetic type when actually implementing this method
Difference, be selected differently from other above-mentioned powerful instructions and generate corresponding executable code.
Step 602-3, the executable code of types of variables restriction is not carried out for sentence generation.
Go to this step, illustrate the sentence be not variable arithmetic statement or be variable arithmetic statement but wherein
Comprising computing variable in the corresponding relation all without corresponding type information, in such a case, it is possible to using conventional
Mode generates the executable code for not carrying out types of variables restriction.
For each sentence in the source code that is stored with grammer tree format, above-mentioned steps 602-1 is performed extremely successively
602-3 processing, so as to the corresponding relation obtained using step 601, the source generation that will be stored with grammer tree format
Code compiling generation carries out the executable code after types of variables restriction.
In the specific implementation, can be with after generation executable code (being referred to as intermediate code or local code)
The executable code is directly run by the compiler or engine of implementation this method, other actuators can also be called to run institute
State executable code.Because during the executable code is generated, the type information in corresponding relation is to corresponding
Type of variables is defined, therefore the executable code generated not only diminishes in committed memory space, and performs speed
Can accordingly it accelerate, overall execution performance significantly improves.
In the above-described embodiment, there is provided a kind of method for improving code execution performance, corresponding, the application
A kind of device for improving code execution performance is also provided.Fig. 8 is refer to, it is a kind of raising code execution performance of the application
The schematic diagram of the embodiment of device.Because device embodiment is substantially similar to embodiment of the method, so describe fairly simple, phase
Part is closed referring to the part of embodiment of the method to illustrate.Device embodiment described below is only schematical.
A kind of device of the raising code execution performance of the present embodiment, including:Corresponding relation acquiring unit 801, for obtaining
Previously generated for source code, marking variable and the corresponding relation of type are taken, this unit can be used for performing above-mentioned correlation method
Step 601 in embodiment;
Executable code generation unit 802, for according to the corresponding relation, the source that will be stored with grammer tree format
Code compilation generation carries out the executable code after types of variables restriction, and this unit can be used for performing above-mentioned correlation method embodiment
In step 602.
Optionally, described device also includes:Corresponding relation generation unit;
The corresponding relation generation unit, the type of the preset format for containing according at least to the source code package mark language
Sentence, generates the marking variable and the corresponding relation of type.
Optionally, the corresponding relation generation unit, including:
Code is read and loop control subelement, for reading the sentence in the source code sentence by sentence, and is directed to each language
Sentence triggers following subelement work;
Type mark sentence judgment sub-unit, for judge the sentence whether be preset format type mark sentence;
First corresponding relation entry generates and addition subelement, for marking the defeated of sentence judgment sub-unit when the type
When going out to be, corresponding relation entry is generated according to the marking variable and respective type information extracted from the sentence, and add
It is added in the marking variable and the corresponding relation of type.
Optionally, the corresponding relation generation unit, including:
Code is read and loop control subelement, for reading the sentence in the source code sentence by sentence, and is directed to each language
Sentence triggers following subelement work;
Type mark sentence judgment sub-unit, for judge the sentence whether be preset format type mark sentence;
Type information extracts subelement, and the output for marking sentence judgment sub-unit when the type is when being, from institute
Type information is extracted in predicate sentence;
Marking variable obtains subelement, for the position relationship according to the sentence and relevant variable definition statement, reads
Corresponding variable-definition sentence simultaneously obtains the marking variable corresponding with the type information extracted;
Second corresponding relation entry generates and addition subelement, for by by the marking variable that obtains and respective type information
The corresponding relation entry of composition is added in the marking variable and the corresponding relation of type.
Optionally, the corresponding relation generation unit, specifically for described with syntax tree lattice by syntax parsing generation
During the source code of formula storage, the type mark sentence of the preset format contained according at least to the source code package is raw
Into the marking variable and the corresponding relation of type.
Optionally, the executable code generation unit, including:
Loop control subelement, for reading sentence by sentence with the sentence in the source code of grammer tree format storage, and pin
Following subelement work is triggered to each sentence;
Optimal conditions judgment sub-unit, for judging whether the sentence meets following condition:The sentence is transported for variable
Calculating sentence and a computing variable being wherein at least present has corresponding type information in the corresponding relation;
Optimized code generates subelement, for when the output of the optimal conditions judgment sub-unit is is, according to from institute
The computing type of variables information that sentence obtained in corresponding relation, described includes is stated, generation carries out variable for the sentence
Executable code after type restriction;
Conventional code generate subelement, for when the optimal conditions judgment sub-unit output for it is no when, for described
Sentence generates the executable code for not carrying out types of variables restriction.
Optionally, described device also includes:
Executable code execution unit, for performing the executable code.
Although the application is disclosed as above with preferred embodiment, it is not for limiting the application, any this area skill
Art personnel are not being departed from spirit and scope, can make possible variation and modification, therefore the guarantor of the application
Shield scope should be defined by the scope that the application claim is defined.
In a typical configuration, computing device includes one or more processors (CPU), input/output interface, net
Network interface and internal memory.
Internal memory may include computer-readable medium in volatile memory, random access memory (RAM) and/or
The forms such as Nonvolatile memory, such as read-only storage (ROM) or flash memory (flash RAM).Internal memory is computer-readable medium
Example.
1st, computer-readable medium can be by any side including permanent and non-permanent, removable and non-removable media
Method or technology realize that information stores.Information can be computer-readable instruction, data structure, the module of program or other numbers
According to.The example of the storage medium of computer includes, but are not limited to phase transition internal memory (PRAM), static RAM
(SRAM), dynamic random access memory (DRAM), other kinds of random access memory (RAM), read-only storage
(ROM), Electrically Erasable Read Only Memory (EEPROM), fast flash memory bank or other memory techniques, read-only optical disc are read-only
Memory (CD-ROM), digital versatile disc (DVD) or other optical storages, magnetic cassette tape, tape magnetic rigid disk storage or
Other magnetic storage apparatus or any other non-transmission medium, the information that can be accessed by a computing device available for storage.According to
Herein defines, and computer-readable medium does not include non-temporary computer readable media (transitory media), such as modulates
Data-signal and carrier wave.
2nd, it will be understood by those skilled in the art that embodiments herein can be provided as method, system or computer program production
Product.Therefore, the application can use the embodiment in terms of complete hardware embodiment, complete software embodiment or combination software and hardware
Form.Moreover, the application can use the computer for wherein including computer usable program code in one or more can use
The computer program product that storage medium is implemented on (including but is not limited to magnetic disk storage, CD-ROM, optical memory etc.)
Form.