[go: up one dir, main page]

CN118170385A - Code compiling method and device, electronic equipment and storage medium - Google Patents

Code compiling method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN118170385A
CN118170385A CN202211587040.1A CN202211587040A CN118170385A CN 118170385 A CN118170385 A CN 118170385A CN 202211587040 A CN202211587040 A CN 202211587040A CN 118170385 A CN118170385 A CN 118170385A
Authority
CN
China
Prior art keywords
type
target
data type
variable
code
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.)
Pending
Application number
CN202211587040.1A
Other languages
Chinese (zh)
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.)
Tencent Technology Shenzhen Co Ltd
Original Assignee
Tencent Technology Shenzhen 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 Tencent Technology Shenzhen Co Ltd filed Critical Tencent Technology Shenzhen Co Ltd
Priority to CN202211587040.1A priority Critical patent/CN118170385A/en
Publication of CN118170385A publication Critical patent/CN118170385A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • G06F8/427Parsing
    • 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
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

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 present application relates to the field of computer technologies, and in particular, to a code compiling method, device, electronic device, and storage medium, for improving code execution efficiency. The method comprises the following steps: acquiring a code to be compiled, wherein the code to be compiled comprises subcodes and interface functions called by the subcodes; compiling the sub-codes to obtain initial byte codes; analyzing the interface function to obtain at least one reference data type which can be transmitted by the interface function; based on at least one reference data type, obtaining a target data type corresponding to each target variable which can be transmitted by a calling function in the initial byte code; and updating the initial byte code based on each target data type to obtain target byte codes. The application can more accurately determine the target data type of the target variable based on the reference data type which can be transmitted by the interface function written by the static language type, further execute the target byte code obtained based on the target data type and improve the code execution efficiency.

Description

Code compiling method and device, electronic equipment and storage medium
Technical Field
The present application relates to the field of computer technologies, and in particular, to a code compiling method, a device, an electronic apparatus, and a storage medium.
Background
When the dynamic type language is executed, the data type of the current variable is required to be judged, and the corresponding function is called according to the data type to process the current variable. For a target variable in a code, the data type of the target variable can be determined before the code is executed, so that the corresponding function is directly called without type judgment during execution, and the efficiency during execution can be improved.
In the related art, in order to improve the code execution efficiency, the following two methods are mainly adopted to obtain the data type of the target variable:
mode one: and supporting type annotation by expanding grammar, and obtaining the data type of the target variable based on a manual annotation mode.
In the above manner, the accuracy cannot be ensured by manually marking the data type, the efficiency is low, and the type judgment still needs to be performed when the type marking is wrong, so that the code execution efficiency cannot be effectively improved.
Mode two: based on traditional static program analysis means such as data flow analysis, abstract interpretation, satisfaction model theory and the like, the data type of the target variable is subjected to type deduction according to the data type of the variable which has appeared, and the deduced data type is used as the data type of the target variable.
In the above manner, under a complex environment, the data types of the variables are changeable, the type deduction accuracy is low, the data types of the target variables cannot be accurately determined, the analysis time is long, and the code execution efficiency cannot be effectively improved.
Therefore, how to improve the code execution efficiency is a problem to be solved.
Disclosure of Invention
The embodiment of the application provides a code compiling method, a device, electronic equipment and a storage medium, which are used for improving code execution efficiency.
The first code compiling method provided by the embodiment of the application comprises the following steps:
Acquiring a code to be compiled, wherein the code to be compiled comprises subcodes and interface functions called by the subcodes, the subcodes are written in a dynamic type language, and the interface functions are written in a static type language;
Compiling the subcode to obtain an initial byte code;
analyzing the interface function to obtain at least one reference data type which can be transmitted by the interface function;
Based on the at least one reference data type, obtaining a target data type corresponding to each target variable which can be transmitted by a calling function in the initial byte code; wherein the calling function is used for calling the interface function;
and updating the initial byte codes based on each target data type to obtain target byte codes.
The first code compiling device provided by the embodiment of the application comprises:
The device comprises an acquisition unit, a compiling unit and a compiling unit, wherein the acquisition unit is used for acquiring codes to be compiled, the codes to be compiled comprise subcodes and interface functions called by the subcodes, the subcodes are written in a dynamic type language, and the interface functions are written in a static type language;
The compiling unit is used for compiling the subcode to obtain an initial byte code;
the analyzing unit is used for analyzing the interface function and obtaining at least one reference data type which can be transmitted by the interface function;
The processing unit is used for obtaining the corresponding target data type of each target variable which can be transmitted by the calling function in the initial byte code based on the at least one reference data type; wherein the calling function is used for calling the interface function;
And the updating unit is used for updating the initial byte codes based on each target data type to obtain target byte codes.
Optionally, the parsing unit is specifically configured to:
Analyzing the interface function to obtain at least one input variable capable of being input into the interface function and at least one output variable capable of being output by the interface function;
Taking the input data type of the at least one input variable as at least one reference data type;
and taking the output data type of the at least one output variable as at least one reference data type.
Optionally, the dynamic language type has a first language type, and the static language type has a second language type;
the processing unit is specifically configured to:
Determining a type corresponding relation based on the first language type and the second language type, wherein the type corresponding relation is used for recording a target data type corresponding to each reference data type;
And determining the target data type corresponding to each reference data type based on the type corresponding relation and the at least one reference data type.
Optionally, the reference data type includes a first input type and a first output type, and the target data type includes a second input type and a second output type;
the processing unit is specifically configured to:
Determining a second input type corresponding to the first input type based on the type corresponding relation, wherein the first input type is a data type capable of inputting an input variable of the interface function, and the second input type is a data type capable of inputting a target variable of the calling function;
And determining a second output type corresponding to the first output type based on the type corresponding relation, wherein the first output type is the data type of the output variable which can be output by the interface function, and the second output type is the data type of the target variable which can be output by the calling function.
Optionally, the subcode comprises a plurality of subcode instructions; the initial byte code comprises a plurality of initial byte code instructions;
the updating unit is specifically configured to:
searching target code instructions containing the target variables from the plurality of subcode instructions;
Searching an initial byte code instruction corresponding to the target code instruction from the plurality of initial byte code instructions;
and updating the initial byte code instruction based on the target data types to obtain the target byte code.
Optionally, the updating unit is specifically configured to:
Compiling the target code instruction based on the target data types to obtain a target byte code instruction;
and replacing the initial byte code instruction with the target byte code instruction to obtain the target byte code.
Optionally, the apparatus further comprises a recording unit for:
for each target variable, the following operations are performed:
For a target variable, if the target variable has at least one historical data type and the target data type corresponding to the target variable does not contain the at least one historical data type, recording the at least one historical data type into a candidate data type set corresponding to the target variable, wherein each historical data type represents: the data type of the target variable which appears in the history stage;
And obtaining a target data type set corresponding to the target variable based on the target data type corresponding to the target variable and the candidate data type set.
Optionally, the recording unit is specifically configured to:
And updating the initial byte codes based on the target data type sets corresponding to the target variables respectively to obtain target byte codes.
Optionally, the apparatus further comprises a determining unit for:
Determining attribute information of the calling function based on the interface function;
and searching the calling function from the initial byte code based on the attribute information.
An electronic device provided by an embodiment of the present application includes a processor and a memory, where the memory stores a computer program, and when the computer program is executed by the processor, the processor is caused to execute any one of the steps of the code compiling method.
An embodiment of the present application provides a computer-readable storage medium including a computer program for causing an electronic device to execute the steps of any one of the code compiling methods described above, when the computer program is run on the electronic device.
Embodiments of the present application provide a computer program product comprising a computer program stored in a computer readable storage medium; when the processor of the electronic device reads the computer program from the computer-readable storage medium, the processor executes the computer program, so that the electronic device performs the steps of any one of the code compiling methods described above.
The application has the following beneficial effects:
The embodiment of the application provides a code compiling method, a device, electronic equipment and a storage medium. Firstly, a code to be compiled is obtained, the code to be compiled comprises a subcode and an interface function called by the subcode, then the subcode is compiled to obtain an initial byte code, because the interface function is written by adopting a static type language, at least one reference data type which can be transmitted by the interface function can be obtained through analyzing the interface function, further, based on the at least one reference data type, the corresponding target data type of each target variable which can be transmitted by a calling function in the initial byte code can be accurately obtained, based on the mode, the data type of the target variable can be simply and directly obtained, the accuracy and efficiency of type determination are improved, finally, the initial byte code is updated based on each target data type to obtain the target byte code, and compared with the initial byte code, a large number of byte code instructions which need to judge the data type of the target variable are reduced in the target byte code, and the execution efficiency of the target byte code is effectively improved.
Additional features and advantages of the application will be set forth in the description which follows, and in part will be obvious from the description, or may be learned by practice of the application. The objectives and other advantages of the application will be realized and attained by the structure particularly pointed out in the written description and claims thereof as well as the appended drawings.
Drawings
The accompanying drawings, which are included to provide a further understanding of the application and are incorporated in and constitute a part of this specification, illustrate embodiments of the application and together with the description serve to explain the application and do not constitute a limitation on the application. In the drawings:
FIG. 1 is an alternative schematic diagram of an application scenario in an embodiment of the present application;
FIG. 2 is a flowchart illustrating a code compiling method according to an embodiment of the application;
FIG. 3 is a diagram illustrating a code division method according to an embodiment of the present application;
FIG. 4 is a schematic diagram of a function parsing method according to an embodiment of the present application;
FIG. 5 is a flow chart of a method for determining a target data type according to an embodiment of the present application;
FIG. 6 is a schematic diagram of a type of correspondence in an embodiment of the present application;
FIG. 7 is a schematic diagram of one type of conversion method in an embodiment of the present application;
FIG. 8 is a flow chart of a type of specialization method in an embodiment of the application;
FIG. 9 is a schematic diagram of a type of determination method in an embodiment of the application;
FIG. 10 is a diagram illustrating a method for updating byte codes according to an embodiment of the present application;
FIG. 11 is a schematic diagram of a runtime device in accordance with an embodiment of the present application;
FIG. 12 is a schematic diagram of an execution flow of a runtime device according to an embodiment of the present application;
FIG. 13 is a flowchart of a method for acquiring a data type set according to an embodiment of the present application;
FIG. 14 is a schematic diagram of a code compiling method according to an embodiment of the application;
FIG. 15 is a flowchart illustrating a code compiling method according to an embodiment of the application;
FIG. 16 is a schematic diagram of a code compiling apparatus according to an embodiment of the application;
FIG. 17 is a schematic diagram of a hardware configuration of an electronic device to which embodiments of the present application are applied;
fig. 18 is a schematic diagram of a hardware configuration of another electronic device to which the embodiment of the present application is applied.
Detailed Description
For the purpose of making the objects, technical solutions and advantages of the embodiments of the present application more apparent, the technical solutions of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application, and it is apparent that the described embodiments are some embodiments of the technical solutions of the present application, but not all embodiments. All other embodiments, based on the embodiments described in the present document, which can be obtained by a person skilled in the art without any creative effort, are within the scope of protection of the technical solutions of the present application.
Some of the concepts involved in the embodiments of the present application are described below.
Dynamic type language: refers to a language in which data type checking is not done during execution. When programming in a dynamic type language, the data type does not need to be assigned to the variable, and the dynamic type language records the data type internally when the variable is assigned for the first time. Therefore, when executing the code written by the dynamic language type (dynamic type code for short), the data type of the variable needs to be determined first, and then the corresponding function is called according to the data type to execute.
Static type language: refers to a language in which a data type check is made during compilation. In code written in a static type language, the data types of variables are declared and variables of different data types are not allowed to be directly assigned. Therefore, the data type of each variable is written in the code written in the static type language (static type code for short), and the data type of the variable in the static type code can be obtained by analyzing the static type code.
Python virtual machine: for interpreting the program for executing Python code. At present, a plurality of Python virtual machines exist, and the code compiling method can be implemented in any Python virtual machine and any running device, and is triggered when the Python virtual machine and the running device execute compiling on the Python code, so that optimization is realized, and execution efficiency is improved. In the embodiment of the application, CPython is mainly used for code compiling.
Python bytecode: the binary format stack machine byte code instruction compiled by the Python source code CPython is referred to, and the Python source code is compiled into the Python byte code and then interpreted for execution. For example, python code: a+b, compiled into three bytecode instructions:
1 0LOAD_NAME 0(a);
2LOAD_NAME 0(b);
4BINARY_ADD;
Where load_name indicates that the variable is pushed onto the stack, load_name a and load_name b indicate that the variable a and the variable b are pushed onto the stack, respectively, BINARY_ADD indicates that the two variables on the stack are popped and an addition operation is performed, and the result of the addition execution is returned onto the stack.
Local (Native) function: refers to a function written in C/C++ code. A large number of Python package libraries adopt a mode that Python codes are mixed with C/C++ codes to be used, the C/C++ codes write Native functions (interface functions), and then the interface functions are called by the Python codes. The interface function is mapped to a Python function (calling function) through a specific interface provided by CPython, and the corresponding interface function can be called only by calling the mapped calling function in Python codes.
Type specialization: for the dynamic type language, the type of the current variable needs to be judged when the dynamic type language is executed, a corresponding function is called according to the data type to process the current variable, such as a code a+b, if the variable a and the variable b are integer, an integer addition function is called, if the variable a and the variable b are list, an addition function of the list is called, and the like. If the data types of the variable a and the variable b can be determined before execution, the corresponding function is directly called without type judgment so as to achieve the effect of optimization, and the optimization means is called type specialization.
Cloud technology (Cloud technology) refers to a hosting technology for integrating hardware, software, network and other series resources in a wide area network or a local area network to realize calculation, storage, processing and sharing of data.
The cloud technology is based on the general names of network technology, information technology, integration technology, management platform technology, application technology and the like applied by the cloud computing business mode, can form a resource pool, and is flexible and convenient as required. Cloud computing technology will become an important support. Background services of technical networking systems require a large amount of computing, storage resources, such as video websites, picture-like websites, and more portals. Along with the high development and application of the internet industry, each article possibly has an own identification mark in the future, the identification mark needs to be transmitted to a background system for logic processing, data with different levels can be processed separately, and various industry data needs strong system rear shield support and can be realized only through cloud computing.
The following briefly describes the design concept of the embodiment of the present application:
when the dynamic type language is executed, the data type of the current variable is required to be judged, and the corresponding function is called according to the data type to process the current variable. For a target variable in a code, the data type of the target variable can be determined before the code is executed, so that the corresponding function is directly called without type judgment during execution, and the efficiency during execution can be improved.
In the related art, in order to improve the code execution efficiency, the following three modes are mainly adopted for type specification to obtain the data type of the target variable:
mode one: the annotation type is specialized, the annotation of the type is supported by expanding grammar, and the data type of the target variable is obtained based on a manual annotation mode.
In the above manner, the type labeling is not mandatory, the correctness cannot be guaranteed by manually labeled data types, the efficiency is low, the type judgment still needs to be performed when the type labeling is performed due to errors, meanwhile, the source code needs to be manually modified, and the code execution efficiency cannot be effectively improved.
Mode two: static type deduction specialization is carried out, based on traditional static program analysis means such as data flow analysis, abstract interpretation, satisfaction model theory and the like, the data type of a target variable is deduced according to the data type of the variable which has appeared, and the deduced data type is used as the data type of the target variable.
In the above manner, under a complex environment, the data types of the variables are changeable, the type deduction accuracy is low, the data types of the target variables cannot be accurately determined, the analysis time is long, and the code execution efficiency cannot be effectively improved.
Mode three: dynamic type specialization. When dynamically executing, selecting one (or more) types which are already appeared to realize type specialization aiming at a variable and an expression;
Among the above approaches, the dynamic type-specific approach is the most widely used type-specific at present. The disadvantage is that a large number of check and rollback codes are needed to be inserted to ensure the correctness of the program, so that the code volume is increased and the memory occupation is increased; the few scene variable types are themselves variable, and frequent rollbacks can occur, resulting in performance degradation and even lower than before optimization.
Therefore, how to improve the code execution efficiency is a problem to be solved.
In view of this, the embodiments of the present application provide a code compiling method, apparatus, electronic device and storage medium. Firstly, a code to be compiled is obtained, the code to be compiled comprises a subcode and an interface function called by the subcode, then the subcode is compiled to obtain an initial byte code, because the interface function is written by adopting a static type language, at least one reference data type which can be transmitted by the interface function can be obtained through analyzing the interface function, further, based on the at least one reference data type, the corresponding target data type of each target variable which can be transmitted by a calling function in the initial byte code can be accurately obtained, based on the mode, the data type of the target variable can be simply and directly obtained, the accuracy and efficiency of type determination are improved, finally, the initial byte code is updated based on each target data type to obtain the target byte code, and compared with the initial byte code, a large number of byte code instructions which need to judge the data type of the target variable are reduced in the target byte code, and the execution efficiency of the target byte code is effectively improved.
The preferred embodiments of the present application will be described below with reference to the accompanying drawings of the specification, it being understood that the preferred embodiments described herein are for illustration and explanation only, and not for limitation of the present application, and embodiments of the present application and features of the embodiments may be combined with each other without conflict.
Fig. 1 is a schematic diagram of an application scenario according to an embodiment of the present application. The application scenario diagram includes two terminal devices 110 and a server 120.
In the embodiment of the present application, the terminal device 110 includes, but is not limited to, a smart phone, a tablet computer, a notebook computer, a desktop computer, an electronic book reader, an intelligent voice interaction device, an intelligent home appliance, a vehicle-mounted terminal, and the like; the terminal device may be provided with a code compiling related client, and the client may be used for exposing the code, modifying the code, etc. The client may be software (e.g. browser, code compiling software, etc.), web page, applet, etc., and the server 120 may be a background server corresponding to the software, web page, applet, etc., or a server dedicated to code compiling, which is not particularly limited in the present application.
The server 120 may be an independent physical server, a server cluster or a distributed system formed by a plurality of physical servers, or a cloud server providing cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, a content delivery network (Content Delivery Network, CDN), basic cloud computing services such as big data and an artificial intelligent platform.
It should be noted that, the code compiling method in the embodiment of the present application may be executed by an electronic device, which may be the server 120 or the terminal device 110, that is, the method may be executed by the server 120 or the terminal device 110 separately, or may be executed by both the server 120 and the terminal device 110 together. For example, when the server 120 and the terminal device 110 jointly execute the code, the terminal device 110 obtains the code to be compiled, and sends the subcode contained in the code to be compiled and the interface function called by the subcode to the server 120, the server 120 compiles the subcode to obtain an initial byte code, parses the interface function to obtain at least one reference data type that can be transmitted by the interface function, and then obtains the target data types corresponding to each target variable that can be transmitted by the calling function in the initial byte code based on the at least one reference data type; finally, the initial byte codes are updated based on each target data type to obtain target byte codes, and the server 120 sends the target byte codes to the terminal equipment 110, so that the code execution efficiency is improved when the terminal equipment 110 executes the target byte codes.
It should be noted that the above-mentioned interaction manner between the terminal device 110 and the server 120 is merely illustrative, and in fact, there are many interaction manners performed by the server and the terminal device together, which are not specifically limited herein.
For example, python code: a+b, this code is equivalent to the following pseudocode in the case of uncertainty of the data type of variable a and variable b:
However, after determining the data types of the variable a and the variable b according to the code compiling method of the present application, assuming that the data types of the variable a and the variable b are Integer (INT), the binary_add instruction may be specified as a binary_add_int instruction, and Python code: a+b is equivalent to the following pseudocode:
implementation of #BINARY_ADD_INT
value=long_add(a,b)
By comparing the pseudo codes, after the data types of the variable a and the variable b are determined based on the code compiling method in the application, the quantity of the pseudo codes is obviously reduced, the corresponding byte code instructions to be executed are also greatly reduced, and the code execution efficiency can be effectively improved.
In an alternative embodiment, the terminal device 110 and the server 120 may communicate via a communication network.
In an alternative embodiment, the communication network is a wired network or a wireless network.
It should be noted that, the number of terminal devices and servers shown in fig. 1 is merely illustrative, and the number of terminal devices and servers is not limited in practice, and is not particularly limited in the embodiment of the present application.
In the embodiment of the application, when the number of the servers is multiple, the multiple servers can be formed into a blockchain, and the servers are nodes on the blockchain; the code compiling method disclosed by the embodiment of the application can save related code related data on a blockchain, such as code to be compiled, target byte codes and the like.
In an alternative implementation manner, the code related data, such as the code to be compiled, the target byte code, and the like, in the embodiment of the present application may be stored by using a cloud storage technology. Cloud storage (cloud storage) is a new concept which extends and develops in the concept of cloud computing, and a distributed cloud storage system refers to a storage system which integrates a large number of storage devices (or storage nodes) of different types in a network through application software or application interfaces to work cooperatively through functions of cluster application, grid technology, distributed storage file systems and the like, and provides data storage and service access functions together.
In addition, the embodiment of the application can be applied to various scenes, including not only code compiling scenes, but also cloud technology, artificial intelligence, intelligent traffic, auxiliary driving and other scenes.
The code compiling method provided by the exemplary embodiments of the present application will be described below with reference to the accompanying drawings in conjunction with the above-described application scenario, and it should be noted that the above-described application scenario is merely shown for the convenience of understanding the spirit and principle of the present application, and the embodiments of the present application are not limited in any way in this respect.
Referring to fig. 2, a flowchart of an implementation of a code compiling method according to an embodiment of the present application is shown, taking an execution subject as a server as an example, where the implementation process of the method includes steps S21 to S25 as follows:
s21: the server acquires a code to be compiled;
The code to be compiled comprises subcodes and interface functions called by the subcodes, the subcodes are written by adopting dynamic type languages, the dynamic type languages are very common in the interpretation languages, such as JavaScript, python, ruby and the like, and in the embodiment of the application, the dynamic type languages are mainly used for explaining by Python; the interface functions are written in a static type language, including C, C ++, scala, java, etc., and in the embodiment of the present application, the static type language is mainly described as C/C++.
After the code to be compiled is obtained, the code to be compiled can be divided according to language components, as shown in fig. 3, which is a schematic diagram of a code dividing method in an embodiment of the present application, wherein the code to be compiled is a Python and C/c++ hybrid program, and is divided into Python files and C/c++ files according to the language components, and the Python files are distinguished according to different file suffixes, and the C/c++ file suffixes are.
S22: compiling the subcodes by the server to obtain initial byte codes;
Specifically, it is sufficient to execute a normal virtual machine compiling process for the sub-code, and generate a bytecode (initial bytecode) that can be interpreted, for example, execute a normal Python virtual machine compiling process for Python code, generate a bytecode, execute a normal JavaScript virtual machine compiling process for JavaScript code, generate a bytecode, and do not list one by one here.
S23: the server analyzes the interface function to obtain at least one reference data type which can be transmitted by the interface function;
Specifically, the interface function is a static type code, so that a relatively clear type operation exists, at least one reference data type capable of being transmitted is written in the interface function, and the reference data type can be obtained directly through analyzing the interface function. For the interface function, the reference data types capable of being transmitted are the data types corresponding to the parameter variables and the return value variables of the interface function, one or more parameter variables corresponding to one interface function can be provided, one or more return value variables corresponding to the same can be provided, the data types of the parameter variables can be the same or different, and the data types corresponding to the return value variables can be the same or different. According to the data types of each parameter variable and each return value variable, the obtained reference data types which can be transmitted by the interface function can be divided into the following two types:
(1) The data types of the parameter variables and the return value variables are the same, and a reference data type which can be transmitted by the interface function is obtained;
For example, the input variables of the interface function 1 are parameter variable 1 and parameter variable 2, the output variables are return value variable 1 and return value variable 2, and the data types of the parameter variable 1, the parameter variable 2, the return value variable 1 and the return value variable 2 are all integer types, so that one type of reference data which can be transmitted by the obtained interface function 1 is the integer type.
(2) If the data types of the parameter variables and the return value variables are not the same, each data type is used as a reference data type, and at least two reference data types which can be transmitted by the interface function are obtained;
Taking the interface function 1 as an example, the data type of the parameter variable 1 is integer, the data type of the parameter variable 2 is floating point, the data type of the return value variable 1 is integer, the data type of the return value variable 2 is list, and the reference data types which can be transmitted by the interface function 1 include: integer, floating point, and list; for another example, the data type of the parameter variable 1 is integer, the data type of the parameter variable 2 is floating point, the data type of the return value variable 1 is list, the data type of the return value variable 2 is long integer (long), and the reference data types that the interface function 1 can transmit include: integer, floating point, list, and long integer.
It should be noted that the interface function, the parameter variable, the return value variable, the data type and the reference data type are all illustrated above, and may be virtually any data that meets the programming specification, and are not specifically limited herein.
In an alternative embodiment, as shown in fig. 3, step S23 may be implemented as the following steps:
S231: analyzing the interface function to obtain at least one input variable capable of being input into the interface function and at least one output variable capable of being output by the interface function;
S232: an input data type of at least one input variable as at least one reference data type;
S233: the output data type of the at least one output variable is used as the at least one reference data type.
Specifically, the interface function corresponds to at least one input variable (parameter variable) and at least one output variable (return value variable), and the input data type of each input variable may be the same or different, if the input data types are the same, each input data type is used as a reference data type, and if the input data types are not the same, each input data type is used as a reference data type; the output data types of each output variable can be the same or different, if the output data types are the same, each output data type is used as a reference data type, and if the output data types are not the same, each output data type is used as a reference data type.
For example, the interface function 2 corresponds to the input variable 1, the input variable 2 and the output variable 1, the input data types of the input variable 1 and the input variable 2 are integer, and the output data type of the output variable 1 is long integer, and the obtained reference data types that can be transmitted by the interface function 2 include: integer and long integer.
Based on the mode, the interface function is analyzed to obtain the data type of the input variable and the data type of the output variable, and the interface function is written by adopting the static language type as the reference data type, so that the accuracy of the obtained reference data type is extremely high, the target data type can be accurately determined based on the reference data type, the target byte code can be further generated based on the target data type, and the code execution efficiency is improved.
Referring to fig. 4, which is a schematic diagram of a function analysis method in an embodiment of the present application, an interface function a is analyzed to obtain an input variable a, an input variable b, and an output variable c, wherein the input data type of the input variable a is int, the input data type of the input variable b is long, and the output data type of the output variable c is int, and then int and long are respectively used as a reference data type to obtain two reference data types.
S24: the server obtains the corresponding target data types of each target variable which can be transmitted by the calling function in the initial byte code based on at least one reference data type;
Specifically, the calling function is used for calling the interface function, in the process of calling the interface function by the calling function, the parameter variable of the calling function is converted into the parameter variable of the interface function, and the return value variable of the interface function is converted into the return value variable of the calling function, so that after the reference data type of the interface function is obtained, the respective target data types of each target variable (including the parameter variable and the return value variable) of the calling function can be obtained.
For example, based on the reference data type 1 of the interface function, the data types of the target variable 1 and the target variable 2 of the call function are both the target data type 1, the interface function is a C/c++ function, the call function is a Python function, the reference data type 1 is long, and the target data type is PyLongObject.
In addition, the interface function may also have multiple reference data types, where each reference data type corresponds to a target data type, for example, the interface function corresponds to reference data type 1 and reference data type 2, where reference data type 1 corresponds to target data type 1, the data type of the target variable 1 of the calling function is target data type 1, reference data type 2 corresponds to target data type 2, and the data type of the target variable 2 is target data type 2.
It should be noted that the descriptions of the interface function, the call function and the data type are all exemplified above, and practically any function and data type in a dynamic language type and a static language type can be used in the embodiments of the present application, which are not limited herein specifically.
In step S24, the calling function may be found by: first, attribute information of a calling function is determined based on an interface function, and then the calling function is searched from an initial bytecode based on the attribute information.
Specifically, the interface function is mapped into a calling function through a specific interface provided by the virtual machine, and the corresponding interface function can be called only by calling the mapped calling function in the subcode, so that the calling function and the interface function have a one-to-one correspondence relationship, and the corresponding calling function can be found based on the interface function.
Based on the above mode, because the calling function is obtained through the interface function mapping when the function is called, the attribute information of the calling function can be obtained through the interface function, and the calling function can be quickly found in the initial byte code so as to obtain the target variable of the calling function.
In combination with the method for searching the calling function and step S24, as shown in fig. 5, a flow chart of a method for determining a target data type in an embodiment of the application is shown, which includes the following steps:
s501: mapping the interface function to obtain attribute information of the calling function;
S502: searching for a calling function from the initial byte code based on the attribute information;
s503: based on the reference data type, obtaining a target data type which can be transmitted by a calling function;
S504: and marking the data type of each target variable which can be transmitted by the calling function according to the target data type.
In the embodiment of the application, the interface function is analyzed, the attribute information of the calling function can be determined based on the interface function, the attribute information can be the name information, parameters, return values and other information of the calling function, the specific limitation is not made here, and after the attribute information is obtained, the calling function can be directly searched in the initial byte code.
It should be noted that, for a calling function, the calling function may be called multiple times, and when the calling function is found in the initial bytecode, the function call point of the calling function is actually found, and based on the above manner, the target data type of the target variable at each function call point may be obtained.
For example, the call function is msvcrt. Setmode, the call point of the msvcrt. Setmode function is found from the Python code,
From the above code, the msvcrt. Setmode function, and the corresponding target variables ret, fd, and mode can be found.
It should be noted that, since the initial bytecode is a binary format file, the content of searching the calling function from the initial bytecode cannot be effectively displayed, the bytecode is obtained by compiling the code, and the bytecode has a direct correspondence relationship with the code, so that, taking searching the calling function from the code as an example, the content of searching the calling function from the bytecode is displayed above,
As described above, the dynamic language type and the static language type each have a plurality of language types, and when the dynamic language type has a first language type and the static language type has a second language type, in an alternative embodiment, step S24 may be implemented as the following steps:
S241: determining a type corresponding relation based on the first language type and the second language type;
S242: and determining the target data type corresponding to each reference data type based on the type corresponding relation and at least one reference data type.
Specifically, the subcode is written by adopting a first language type, the corresponding calling function is also written by adopting a first language type, the interface function is written by adopting a second language type, in the process of calling the interface function, the target data type of the target variable is required to be converted into a reference data type through a type conversion function, each target data type and the reference data type are in one-to-one correspondence, and the type correspondence records the target data type corresponding to each reference data type.
For different language types, the contained data types are different, and when the types of the different language types are converted, the different types of the different language types also have different corresponding relations. For example, the type correspondence is a relationship 1 when the data of the first language type and the second language type are subjected to type conversion, the type correspondence is a relationship 2 when the data of the first language type and the third language type are subjected to type conversion, and the type correspondence is a relationship 3 when the data of the second language type and the third language type are subjected to type conversion.
Based on the mode, the type corresponding relation is determined through the first language type and the second language type, the reference data type and the target data type are obtained, the data type of the target variable can be accurately obtained, the accuracy of the data type is improved, the initial byte code is optimized based on the target data type, and the code execution efficiency is improved.
In step S241, based on the first language type and the second language type, a type correspondence is determined, and then a target data type corresponding to each reference data type may be obtained according to the type correspondence. Taking the first language type as Python and the second language type as C/C++ as an example, referring to the data type long, and the corresponding target data type is PyLongObject.
As shown in fig. 6, which is a schematic diagram of a type correspondence in the embodiment of the present application, a first language type and a second language type correspond to type correspondence 1, a first language type and a third language type correspond to type correspondence 2, a second language type and a third language type correspond to type correspondence 3, wherein for different type correspondences, the target data types corresponding to the recorded reference data types are also different, for example, in type correspondence 1: reference data type 1 corresponds to target data type 1, reference data type 2 corresponds to target data type 2, and in type correspondence 3: reference data type 1 corresponds to target data types 1-11 and reference data type 2 corresponds to target data types 2-11.
In the process of calling the interface function by the function, as shown in fig. 7, which is a schematic diagram of a type conversion method in the embodiment of the present application, for parameter variables of the calling function, the type conversion function converts a Python variable into a C/c++ variable, and the Python type is converted into a C/c++ type, where the Python type that can be converted is a type constraint of the parameter; for the return value variable, a C/C++ variable is converted to a Python variable, and the C/C++ type is converted to a Python type, where the Python type that can be converted is the type constraint for the Python variable. That is, the type conversion function constrains the input (or output) Python variable to necessarily satisfy a type constraint, which is determined by the interface function, and after the type of the reference data that can be transmitted by the interface function is acquired, the type conversion function can determine the target data type of the target variable, so that after the type constraint of the interface function is acquired, the type specification can be implemented on the operation instruction of the target variable.
The Python code type specialization is optimized using the static type constraint of Native functions. FIG. 8 is a flow chart of a specific method according to an embodiment of the application, which includes the following steps:
S801: acquiring static type constraints of parameter variables and return value variables of the Native function;
S802: type labeling is carried out on the Python variable corresponding to the parameter variable of the incoming Native function and the return value variable of the outgoing Native function, so that type information of the Python variable is obtained;
S803: type specialization is performed based on the type information and Python bytecodes are regenerated again.
In the above, the procedure of determining the target data type by referring to the data type is described, and in fact, the reference data type may be classified into a first input type and a first output type, and when the target data type may be classified into a second input type and a second output type, in an alternative embodiment, step S242 may be implemented as follows:
S2421: determining a second input type corresponding to the first input type based on the type correspondence;
S2422: determining a second output type corresponding to the first output type based on the type correspondence;
Specifically, the first input type is a data type of an input variable capable of inputting an interface function, the second input type is a data type of a target variable capable of inputting a call function, the variable capable of inputting the function may be referred to as a parameter variable, the first output type is a data type of an output variable capable of being output by the interface function, the second output type is a data type of a target variable capable of being output by the call function, and the variable capable of being output by the function may be referred to as a return value variable. As can be seen from step S2421 and step S2422, when determining the target data type based on the reference data type, the data type of the parameter variable of the calling function is determined based on the data type of the parameter variable of the interface function, and the data type of the return value variable of the calling function is determined based on the data type of the return value variable of the interface function.
Fig. 9 is a schematic diagram of a type determining method in an embodiment of the present application, where an interface function corresponds to a first input variable and a first output variable, a call function corresponds to a second input variable and a second output variable, and a second input type and a second output type are obtained through a type correspondence under the condition that a first input type and a first output type are known.
In the embodiment of the application, the modes for acquiring the target data types of the target variables comprise the following four modes;
Mode one: modeling a type conversion function called in the interface function. For example, a type conversion function PyLong _ FromLong, common in the internal implementation of CPython, constructs a long type of C/C++ data as PyLongObject type of data in Python. Thus, in the case where C/c++ data has been obtained, the data type of Python data can be obtained by modeling the type conversion function;
mode two: a signing tool is used that automatically generates an interface function interface. For example, the signing tool (clinic) implemented in CPython may automatically parse the formatted string into C/c++ function signatures and automatically generate parameter parsing and return value constructs. The type constraint of the interface function can be directly obtained by means of clinic. For example, what is shown below is an interface definition written by clinic, by parsing, it can be obtained that the calling function of the interface function map is msvcrt.
/*[clinic input]
msvcrt.setmode->long
fd:int
mode as flags:int
/
Mode three: the type derivation is performed by the static analysis tool in combination with known facts.
Mode four: the C/C++ type information provided by the application programming interface (Application Programming Interface, API) statistics tool can record the data type of parameter variables, the data type of return value variables and other information in advance to form a document in the process of programming interface functions, and then the document can be directly analyzed to obtain the reference data type. However, in the fourth application mode, it should be noted that in the process of modifying the code, the document needs to be updated, otherwise, the data type of the document record may not be consistent with the actual data type.
S25: the server updates the initial bytecode based on each target data type to obtain the target bytecode.
Specifically, after the target data types of the target variables are obtained, if the data types are known, the initial byte code does not need to contain a byte code instruction for judging the data types of the target variables, and the initial byte code is updated to obtain the target byte code.
In the embodiment of the application, firstly, a code to be compiled is obtained, the code to be compiled comprises a sub-code and an interface function called by the sub-code, then the sub-code is compiled to obtain an initial byte code, because the interface function is written by adopting a static type language, at least one reference data type which can be transmitted by the interface function can be obtained through analyzing the interface function, further, based on the at least one reference data type, the corresponding target data type of each target variable which can be transmitted by the calling function in the initial byte code can be accurately obtained, based on the mode, the data type of the target variable can be simply and directly obtained, the accuracy and the efficiency of type determination are improved, finally, the initial byte code is updated based on each target data type to obtain the target byte code, and compared with the initial byte code, a large number of byte code instructions which need to judge the data type of the target variable are reduced in the target byte code, and the execution efficiency of the target byte code is effectively improved.
The subcode comprises a plurality of subcode instructions, the initial bytecode comprises a plurality of initial bytecode instructions, and because the subcode is written by adopting a dynamic language type, the initial bytecode comprises a plurality of initial bytecode instructions for judging the data type of the target variable, and after the target data type of the target variable is obtained based on the code compiling method in the application, the initial bytecode can be correspondingly updated; in an alternative embodiment, step S25 may be implemented as the following steps:
S251: searching target code instructions containing target variables from a plurality of subcode instructions;
S252: searching an initial byte code instruction corresponding to the target code instruction from a plurality of initial byte code instructions;
S253: and updating the initial byte code instruction based on each target data type to obtain the target byte code.
Specifically, first, target code instructions including target variables are searched, each target code instruction includes at least one target variable, and then, since the initial bytecode is obtained by compiling the subcode, each subcode instruction in the subcode has a corresponding initial bytecode instruction, so that the corresponding initial bytecode instruction can be searched based on the target code instruction, and the initial bytecode instruction is an instruction that needs to be updated.
For example, the target variables are fd, mode, and ret, then the following target code instructions are found from the Python code:
(1)def example(fd):
(2) mode=get_mode()
(3)ret=msvcrt.setmode(fd,mode)
(4)if isinstance(ret,int)and ret>0:
Based on the above manner, the initial byte code instructions are updated according to the target data types, so that the number of the initial byte code instructions for judging the data types can be reduced, and when the obtained target byte codes are executed, the code execution efficiency can be improved, and the execution time can be reduced.
The method for updating the initial bytecode instruction in step S253 can be divided into the following two types:
The method comprises the following steps: compiling the target code instruction based on each target data type to obtain a target byte code instruction; and replacing the initial byte code instruction with a target byte code instruction to obtain the target byte code.
Specifically, the first method is an alternative method, that is, in the case where the target data type is known, recompilation is performed on the target code instruction, and the initial bytecode instruction is replaced with the obtained target bytecode instruction.
The second method is as follows: and screening the initial byte code instructions based on each target data type, and reserving target byte code instructions to obtain target byte codes.
Specifically, in the second mode, the initial bytecode instruction is filtered, the instruction which needs to judge the data type is deleted according to the data type, and the executed target bytecode instruction is reserved.
Taking the above object code instructions (1) - (4) as an example, since the variable ret is a long type as the return value variable of msvcrt.
Referring to fig. 10, a schematic diagram of a byte code updating method in an embodiment of the present application is shown, for a target variable 1 and a target variable 2, a target code instruction 1 and a target code instruction 2 are found from subcode instructions, a target byte code instruction 1 of the target code instruction 1 and a target byte code instruction 2 of the target code instruction 2 are generated based on a target data type of the target variable 1 and a target data type of the target variable 2, an initial byte code instruction 1 corresponding to the target code instruction 1 is found from initial byte code instructions, an initial byte code instruction 2 corresponding to the target code instruction 2 is replaced with the target code instruction 1, and the initial byte code instruction 2 is replaced with the target code instruction 2, so as to obtain a target byte code.
It was introduced above that after the target data type of the target variable is obtained, the initial bytecode instruction containing the target variable may be updated, while the type constraint at the interface function call location may be acted on to a greater extent through data flow analysis, obtaining more optimization opportunities. For example:
(1)def example(fd):
(2) mode=get_mode()
(3) ret=msvcrt.setmode(fd,mode)
(4) mode_plus= mode+1
(5)return mode_plus
Knowing the type constraint of msvcrt. Based on the data flow analysis, the value of the parameter variable mode at line (3) comes from the assigned expression at line (2). The mode used by the (4) line addition expression mode+1 is also from the assignment expression of line 2, and is therefore also known as the int type. As discussed previously, in Python implementations, the addition needs to be distributed to the corresponding addition functions at execution time, depending on the type of two operands. At this time, through static type constraint, if setmode functions of the (3) th row call successfully, then the add of the (4) th row call the add function of the int type, then the add of the int can be directly compiled without the need of type judgment, and the pseudo code is as follows:
After the propagation of the type constraint is realized through data flow analysis, the optimization can be performed according to the type information, and a new byte code is generated, so that the execution efficiency of the common Python function is improved.
It should be noted that, because the bytecode is a binary format file, it is difficult to intuitively show the changes before and after the optimization, and the pseudo code has high readability and can illustrate the logic implementation of the bytecode, in the embodiment of the present application, the changes before and after the optimization of the bytecode are mainly represented by the pseudo code.
The code compiling method in the embodiment of the application can be implemented in a virtual machine and a runtime device, taking a code to be compiled as a Python and C/c++ hybrid program as an example, as shown in fig. 11, which is a schematic structural diagram of the runtime device in the embodiment of the application, and includes the following modules:
Program dividing module: the method is used for dividing the Python and C/C++ mixed program into Python files and C/C++ files according to language components;
A compiler module: the method comprises the steps of executing a Python virtual machine compiling flow on a Python file;
and a function analysis module: a type constraint for acquiring a Python function to which the Native function is mapped, and acquiring parameters and return values of the Native function for which the mapping is completed;
A type optimizer module: for performing type specialization on variables that have been marked for a particular type, and generating new bytecodes after type specialization.
As shown in fig. 12, which is a schematic diagram of an execution flow of a runtime device in an embodiment of the present application, the specific flow of the runtime device shown in fig. 11 when executing the code compiling method in the present application is as follows:
s1201: respectively processing language components of Python and C/C++ mixed programs to obtain Python files and C/C++ files;
S1202: executing a Python virtual machine compiling process on the Python file to generate a byte code which can be interpreted and executed;
S1203: analyzing the C/C++ file to obtain a Python function mapped to the Native function;
S1204: analyzing the Native function to obtain type constraints of the parameter variables and the return value variables;
S1205: analyzing the byte codes to obtain call points of the Python functions mapped by all Native functions, acting the type constraint obtained in the step S1204 on parameter variables and return value variables at the call points, and marking the data types of the parameter variables and the return value variables;
S1206: type specialization is performed on the variables whose specific data types have been marked in step S1205 by data flow analysis, and new bytecodes after the type specialization are generated.
In the embodiment of the application, through the type analysis of the function interface written by the C/C++ code, the type specific optimization of the common Python code is provided, and the execution performance of the optimized Python code can be improved by 2-10 times under different service scenes.
Based on the step S24, the target data type corresponding to each target variable can be obtained, and the code compiling method can be combined with a dynamic specialization means to improve the optimization range and achieve better applicability. In an alternative implementation, as shown in fig. 13, which is a schematic flow chart of a data type set obtaining method in an embodiment of the present application, after obtaining the target data types corresponding to the target variables in step S24, the following steps are performed for each target variable:
s1301: for a target variable, if the target variable has at least one historical data type and the target data type corresponding to the target variable does not contain at least one historical data type, recording the at least one historical data type into a candidate data type set corresponding to the target variable;
S1302: and obtaining the target data type set corresponding to the target variable based on the target data type and the candidate data type set corresponding to the target variable.
Specifically, for the code to be compiled, since different input and different deployment environments are performed each time, the data types of the variables are changed, after the target data types corresponding to the target variables are obtained, whether the target variables have data types which occur in a history stage or not, namely, whether the target data types contain at least one history data type or not can be judged, if so, the history data types are recorded in candidate data types, the target data types and the candidate data types form a target data type set, after the target data type set corresponding to the target variables is obtained, the initial byte codes can be updated based on the target data type sets corresponding to the target variables, and the target byte codes are obtained.
Taking the example that the target data type corresponding to the target variable 1 is the int type, the target data type set of the target variable 1 may include the following two cases:
Case 1: the target variable 1 does not have a historical data type, and the target data type set contains an int type;
case 2: the target variable 1 has a history data type, and can be further classified into the following cases according to the history data type:
(1) The target data type completely contains the historical data type, and then the target data type set still contains only the int type;
(2) The target data type partially contains the historical data type, for example, the historical data type is an int type and a long type, and then the target data type set contains the int type and the long type;
(3) The target data type does not contain a historical data type, for example, the historical data type is a long class and a list type, and the target data type set contains the list type and the long type.
Based on the mode, the target data type set is obtained according to the historical data type of the target variable, the data type of the target variable is expanded, the precision of the variable type can be improved, the optimization range is improved, and the code compiling method has better applicability.
As shown in fig. 14, a logic diagram of a code compiling method in an embodiment of the present application is shown, taking a code to be compiled as a Python and C/c++ hybrid program as an example, firstly, dividing the hybrid program into a subcode and an interface function, compiling the subcode to obtain an initial bytecode, analyzing the interface function to obtain a integer type of reference data type a which can be transmitted by the interface function, a floating point type of reference data type b, then the integer type of target data type of target variable a which can be transmitted by the calling function is the integer type, the floating point type of target data type of target variable b, the integer type of target variable a of the calling function is the target variable b, the floating point type of target variable b is the target variable b, and then updating the initial bytecode based on the respective data types of target variable a and target variable b to obtain the target bytecode.
In the embodiment of the application, the method is applied to the Python code by a simple program analysis means, so that the defect of analysis duration is avoided, and meanwhile, the type information of the Native function is more definite in most cases, so that the type analysis precision is improved. Under the condition that the advantage of realizing specialization of program analysis is maintained, the main problem of program analysis means is effectively avoided.
Fig. 15 is a schematic overall flow chart of a code compiling method according to an embodiment of the application, which includes the following steps:
S1501: acquiring a code to be compiled;
S1502: dividing the code to be compiled into subcodes and interface functions;
S1503: compiling the sub-codes to obtain initial byte codes;
S1504: analyzing the interface function to obtain a first input type of a first input variable and a first output type of a first output variable;
S1505: determining a type corresponding relation according to the first language type and the second language type;
S1506: determining a second input type of the calling function corresponding to the first input type and a second output type of the calling function corresponding to the first output type according to the type corresponding relation;
S1507: determining attribute information of a calling function according to the interface function;
S1508: searching for a calling function in the initial byte code according to the attribute information;
s1509: obtaining a target data type of a target variable which can be input by the calling function according to the second input type, and obtaining a target data type of the target variable which can be output by the calling function based on the second output type;
s1510: searching target code instructions containing target variables from a plurality of subcode instructions;
s1511: generating a target byte code instruction corresponding to the target code instruction based on each target data type;
S1512: and replacing the initial byte code instruction corresponding to the target code instruction with the target byte code instruction to obtain the target byte code.
Based on the same inventive concept, the embodiment of the application also provides a code compiling device. As shown in fig. 16, which is a schematic structural diagram of the code compiling apparatus 1600, may include:
an obtaining unit 1601, configured to obtain a code to be compiled, where the code to be compiled includes a subcode and an interface function called by the subcode, the subcode is written in a dynamic type language, and the interface function is written in a static type language;
a compiling unit 1602, configured to compile the sub-code to obtain an initial byte code;
The parsing unit 1603 is configured to parse the interface function to obtain at least one reference data type that can be transmitted by the interface function;
The processing unit 1604 is configured to obtain, based on at least one reference data type, a target data type corresponding to each target variable that can be transmitted by the calling function in the initial bytecode; the calling function is used for calling the interface function;
an updating unit 1605, configured to update the initial bytecode based on each target data type, and obtain the target bytecode.
In the embodiment of the application, firstly, a code to be compiled is obtained, the code to be compiled comprises a sub-code and an interface function called by the sub-code, then the sub-code is compiled to obtain an initial byte code, because the interface function is written by adopting a static type language, at least one reference data type which can be transmitted by the interface function can be obtained through analyzing the interface function, further, based on the at least one reference data type, the corresponding target data type of each target variable which can be transmitted by the calling function in the initial byte code can be accurately obtained, based on the mode, the data type of the target variable can be simply and directly obtained, the accuracy and the efficiency of type determination are improved, finally, the initial byte code is updated based on each target data type to obtain the target byte code, and compared with the initial byte code, a large number of byte code instructions which need to judge the data type of the target variable are reduced in the target byte code, and the execution efficiency of the target byte code is effectively improved.
Optionally, the parsing unit 1603 is specifically configured to:
Analyzing the interface function to obtain at least one input variable capable of being input into the interface function and at least one output variable capable of being output by the interface function;
An input data type of at least one input variable as at least one reference data type;
The output data type of the at least one output variable is used as the at least one reference data type.
Optionally, the dynamic language type has a first language type, and the static language type has a second language type;
the processing unit 1604 specifically is configured to:
determining a type corresponding relation based on the first language type and the second language type, wherein the type corresponding relation is used for recording target data types corresponding to each reference data type;
And determining the target data type corresponding to each reference data type based on the type corresponding relation and at least one reference data type.
Optionally, the reference data type includes a first input type and a first output type, and the target data type includes a second input type and a second output type;
the processing unit 1604 specifically is configured to:
Determining a second input type corresponding to the first input type based on the type corresponding relation, wherein the first input type is a data type capable of inputting an input variable of the interface function, and the second input type is a data type capable of inputting a target variable of the calling function;
And determining a second output type corresponding to the first output type based on the type correspondence, wherein the first output type is the data type of the output variable which can be output by the interface function, and the second output type is the data type of the target variable which can be output by the calling function.
Optionally, the subcode comprises a plurality of subcode instructions; the initial byte code comprises a plurality of initial byte code instructions;
The updating unit 1605 specifically functions to:
Searching target code instructions containing target variables from a plurality of subcode instructions;
Searching an initial byte code instruction corresponding to the target code instruction from a plurality of initial byte code instructions;
and updating the initial byte code instruction based on each target data type to obtain the target byte code.
Optionally, the updating unit 1605 is specifically configured to:
Compiling the target code instruction based on each target data type to obtain a target byte code instruction;
and replacing the initial byte code instruction with a target byte code instruction to obtain the target byte code.
Optionally, the apparatus further comprises a recording unit 1606 for:
for each target variable, the following operations are performed:
For a target variable, if the target variable has at least one historical data type and the target data type corresponding to the target variable does not contain at least one historical data type, recording the at least one historical data type into a candidate data type set corresponding to the target variable, wherein each historical data type represents: the type of data that the target variable has appeared in the history phase;
and obtaining the target data type set corresponding to the target variable based on the target data type and the candidate data type set corresponding to the target variable.
Optionally, the recording unit 1606 is specifically configured to:
and updating the initial byte code based on the target data type set corresponding to each target variable to obtain the target byte code.
Optionally, the apparatus further comprises a determining unit 1607 for:
Determining attribute information of a calling function based on the interface function;
Based on the attribute information, the calling function is looked up from the initial bytecode.
For convenience of description, the above parts are described as being functionally divided into modules (or units) respectively. Of course, the functions of each module (or unit) may be implemented in the same piece or pieces of software or hardware when implementing the present application.
Those skilled in the art will appreciate that the various aspects of the application may be implemented as a system, method, or program product. Accordingly, aspects of the application may be embodied in the following forms, namely: an entirely hardware embodiment, an entirely software embodiment (including firmware, micro-code, etc.) or an embodiment combining hardware and software aspects may be referred to herein as a "circuit," module "or" system.
The embodiment of the application also provides electronic equipment based on the same conception as the embodiment of the method. In one embodiment, the electronic device may be a server, such as server 120 shown in FIG. 1. In this embodiment, the electronic device may be configured as shown in fig. 17, including a memory 1701, a communication module 1703, and one or more processors 1702.
A memory 1701 for storing computer programs for execution by the processor 1702. The memory 1701 may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, a program required for running an instant messaging function, and the like; the storage data area can store various instant messaging information, operation instruction sets and the like.
The memory 1701 may be a volatile memory (RAM) such as a random-access memory (RAM); the memory 1701 may also be a nonvolatile memory (non-volatile memory), such as a read-only memory, a flash memory (flash memory), a hard disk (HARD DISK DRIVE, HDD) or a solid state disk (solid-state drive (SSD); or any other medium that can be used to carry or store a desired computer program in the form of instructions or data structures and that can be accessed by a computer, without limitation. The memory 1701 may be a combination of the above.
The processor 1702 may include one or more central processing units (central processing unit, CPUs) or digital processing units, or the like. Processor 1702 is configured to implement the code compilation method described above when calling a computer program stored in memory 1701.
The communication module 1703 is used for communicating with a terminal device and other servers.
The specific connection medium between the memory 1701, the communication module 1703 and the processor 1702 is not limited to the above embodiments of the present application. The embodiment of the present application is illustrated in fig. 17 by a bus 1704 between the memory 1701 and the processor 1702, and the bus 1704 is illustrated in fig. 17 by a bold line, and the connection between other components is merely illustrative and not limiting. The bus 1704 may be classified as an address bus, a data bus, a control bus, or the like. For ease of description, only one thick line is depicted in fig. 17, but only one bus or one type of bus is not depicted.
The memory 1701 stores therein a computer storage medium having stored therein computer executable instructions for implementing the code compiling method of the embodiment of the application. The processor 1702 is configured to perform the code compilation method described above, as shown in fig. 2.
In another embodiment, the electronic device may also be other electronic devices, such as terminal device 110 shown in fig. 1. In this embodiment, the structure of the electronic device may include, as shown in fig. 18: communication component 1810, memory 1820, display unit 1830, camera 1840, sensor 1850, audio circuitry 1860, bluetooth module 1870, processor 1880, and the like.
The communication component 1810 is for communicating with a server. In some embodiments, a circuit wireless fidelity (WIRELESS FIDELITY, WIFI) module may be included, the WiFi module belongs to a short-range wireless transmission technology, and the electronic device may help the user to send and receive information through the WiFi module.
Memory 1820 may be used for storing software programs and data. The processor 1880 performs various functions and data processing of the terminal device 110 by executing software programs or data stored in the memory 1820. Memory 1820 may include high-speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid-state storage device. The memory 1820 stores an operating system that enables the terminal device 110 to operate. The memory 1820 of the present application may store an operating system and various application programs, and may also store computer programs for performing the code compiling method according to the embodiment of the present application.
The display unit 1830 may also be used to display information input by a user or information provided to the user and a graphical user interface (GRAPHICAL USER INTERFACE, GUI) of various menus of the terminal device 110. In particular, the display unit 1830 may include a display screen 1832 disposed on a front surface of the terminal device 110. The display 1832 may be configured in the form of a liquid crystal display, light emitting diodes, or the like. The display unit 1830 may be used to display a code compiling user interface or the like in the embodiment of the present application.
The display unit 1830 may also be used to receive input numeric or character information, generate signal inputs related to user settings and function control of the terminal device 110, and in particular, the display unit 1830 may include a touch screen 1831 provided on the front of the terminal device 110, and may collect touch operations on or near the user, such as clicking buttons, dragging scroll boxes, and the like.
The touch screen 1831 may be covered on the display screen 1832, or the touch screen 1831 may be integrated with the display screen 1832 to implement input and output functions of the terminal device 110, and the integrated touch screen may be simply referred to as a touch screen. The display unit 1830 may display an application program and corresponding operation steps in the present application.
The camera 1840 may be used to capture still images and the user may comment the image captured by the camera 1840 through the application. The camera 1840 may be one or more. The object generates an optical image through the lens and projects the optical image onto the photosensitive element. The photosensitive element may be a charge coupled device (charge coupled device, CCD) or a Complementary Metal Oxide Semiconductor (CMOS) phototransistor. The photosensitive elements convert the optical signals to electrical signals, which are then passed to a processor 1880 for conversion to digital image signals.
The terminal device may further comprise at least one sensor 1850, such as an acceleration sensor 1851, a distance sensor 1852, a fingerprint sensor 1853, a temperature sensor 1854. The terminal device may also be configured with other sensors such as gyroscopes, barometers, hygrometers, thermometers, infrared sensors, light sensors, motion sensors, and the like.
Audio circuitry 1860, speaker 1861, microphone 1862 may provide an audio interface between a user and terminal device 110. The audio circuit 1860 may transmit the received electrical signal converted from audio data to the speaker 1861, and may be converted into a sound signal by the speaker 1861 for output. The terminal device 110 may also be configured with a volume button for adjusting the volume of the sound signal. On the other hand, microphone 1862 converts the collected sound signals into electrical signals, which are received by audio circuit 1860 and converted into audio data, which are output to communication component 1810 for transmission to, for example, another terminal device 110, or to memory 1820 for further processing.
The bluetooth module 1870 is used for exchanging information with other bluetooth devices having a bluetooth module through a bluetooth protocol. For example, the terminal device may establish a bluetooth connection with a wearable electronic device (e.g., a smart watch) that also has a bluetooth module through the bluetooth module 1870, thereby performing data interaction.
The processor 1880 is a control center of the terminal device, connects various parts of the entire terminal using various interfaces and lines, and performs various functions of the terminal device and processes data by running or executing software programs stored in the memory 1820, and calling data stored in the memory 1820. In some embodiments, the processor 1880 may include one or more processing units; the processor 1880 may also integrate an application processor that primarily processes operating systems, user interfaces, applications, etc., and a baseband processor that primarily processes wireless communications. It will be appreciated that the baseband processor described above may not be integrated into the processor 1880. The processor 1880 of the present application may run an operating system, application programs, user interface displays and touch responses, as well as code compilation methods of embodiments of the present application. In addition, the processor 1880 is coupled to a display unit 1830.
In some possible embodiments, aspects of the code compiling method provided by the application may also be implemented in the form of a program product comprising a computer program for causing an electronic device to carry out the steps in the code compiling method according to the various exemplary embodiments of the application described in the present specification when the program product is run on the electronic device, for example, the electronic device may carry out the steps as shown in fig. 2.
The program product may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium can be, for example, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples (a non-exhaustive list) of the readable storage medium would include the following: an electrical connection having one or more wires, a portable disk, a hard disk, random Access Memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
The program product of embodiments of the present application may take the form of a portable compact disc read only memory (CD-ROM) and comprise a computer program and may be run on an electronic device. However, the program product of the present application is not limited thereto, and in this document, a readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with a command execution system, apparatus, or device.
The readable signal medium may comprise a data signal propagated in baseband or as part of a carrier wave in which a readable computer program is embodied. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A readable signal medium may also be any readable medium that is not a readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with a command execution system, apparatus, or device.
A computer program embodied on a readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
Computer programs for performing the operations of the present application may be written in any combination of one or more programming languages, including an object oriented programming language such as Java, C++ or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The computer program may execute entirely on the consumer electronic device, partly on the consumer electronic device, as a stand-alone software package, partly on the consumer electronic device and partly on a remote electronic device or entirely on the remote electronic device or server. In the case of remote electronic devices, the remote electronic device may be connected to the consumer electronic device through any kind of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or may be connected to an external electronic device (e.g., connected through the internet using an internet service provider).
It should be noted that although several units or sub-units of the apparatus are mentioned in the above detailed description, such a division is merely exemplary and not mandatory. Indeed, the features and functions of two or more of the elements described above may be embodied in one element in accordance with embodiments of the present application. Conversely, the features and functions of one unit described above may be further divided into a plurality of units to be embodied.
Furthermore, although the operations of the methods of the present application are depicted in the drawings in a particular order, this is not required or suggested that these operations must be performed in this particular order or that all of the illustrated operations must be performed in order to achieve desirable results. Additionally or alternatively, certain steps may be omitted, multiple steps combined into one step to perform, and/or one step decomposed into multiple steps to perform.
It will be appreciated by those skilled in the art that 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 on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) having a computer-usable computer program embodied therein.
The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems) and computer program products according to embodiments of the application. It will be understood that each flow and/or block of the flowchart illustrations and/or block diagrams, and combinations of flows and/or blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program commands may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the commands executed by the processor of the computer or other programmable data processing apparatus produce means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program commands may also be stored in a computer readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the commands stored in the computer readable memory produce an article of manufacture including command means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
While preferred embodiments of the present application have been described, additional variations and modifications in those embodiments may occur to those skilled in the art once they learn of the basic inventive concepts. It is therefore intended that the following claims be interpreted as including the preferred embodiments and all such alterations and modifications as fall within the scope of the application.
It will be apparent to those skilled in the art that various modifications and variations can be made to the present application without departing from the spirit or scope of the application. Thus, it is intended that the present application also include such modifications and alterations insofar as they come within the scope of the appended claims or the equivalents thereof.

Claims (13)

1. A method of compiling code, the method comprising:
Acquiring a code to be compiled, wherein the code to be compiled comprises subcodes and interface functions called by the subcodes, the subcodes are written in a dynamic type language, and the interface functions are written in a static type language;
Compiling the subcode to obtain an initial byte code;
analyzing the interface function to obtain at least one reference data type which can be transmitted by the interface function;
Based on the at least one reference data type, obtaining a target data type corresponding to each target variable which can be transmitted by a calling function in the initial byte code; wherein the calling function is used for calling the interface function;
and updating the initial byte codes based on each target data type to obtain target byte codes.
2. The method of claim 1, wherein parsing the interface function to obtain at least one reference data type that the interface function is capable of transmitting, comprises:
Analyzing the interface function to obtain at least one input variable capable of being input into the interface function and at least one output variable capable of being output by the interface function;
Taking the input data type of the at least one input variable as at least one reference data type;
and taking the output data type of the at least one output variable as at least one reference data type.
3. The method of claim 1, wherein the dynamic language type has a first language type and the static language type has a second language type;
The obtaining, based on the at least one reference data type, a target data type corresponding to each target variable that can be transmitted by a calling function in the initial bytecode includes:
Determining a type corresponding relation based on the first language type and the second language type, wherein the type corresponding relation is used for recording a target data type corresponding to each reference data type;
And determining the target data type corresponding to each reference data type based on the type corresponding relation and the at least one reference data type.
4. The method of claim 3, wherein the reference data type comprises a first input type and a first output type, and the target data type comprises a second input type and a second output type;
Determining, based on the type correspondence and the at least one reference data type, a target data type corresponding to each reference data type, including:
Determining a second input type corresponding to the first input type based on the type corresponding relation, wherein the first input type is a data type capable of inputting an input variable of the interface function, and the second input type is a data type capable of inputting a target variable of the calling function;
And determining a second output type corresponding to the first output type based on the type corresponding relation, wherein the first output type is the data type of the output variable which can be output by the interface function, and the second output type is the data type of the target variable which can be output by the calling function.
5. The method of any of claims 1-4, wherein the subcode comprises a plurality of subcode instructions; the initial byte code comprises a plurality of initial byte code instructions;
updating the initial bytecode based on each target data type to obtain target bytecode, including:
searching target code instructions containing the target variables from the plurality of subcode instructions;
Searching an initial byte code instruction corresponding to the target code instruction from the plurality of initial byte code instructions;
and updating the initial byte code instruction based on the target data types to obtain the target byte code.
6. The method of claim 5, wherein updating the initial bytecode instruction based on the target data types to obtain the target bytecode comprises:
Compiling the target code instruction based on the target data types to obtain a target byte code instruction;
and replacing the initial byte code instruction with the target byte code instruction to obtain the target byte code.
7. The method according to any one of claims 1-4, further comprising, after said obtaining, based on said at least one reference data type, a respective target data type for each target variable in said initial bytecode that can be transferred by a calling function:
for each target variable, the following operations are performed:
For a target variable, if the target variable has at least one historical data type and the target data type corresponding to the target variable does not contain the at least one historical data type, recording the at least one historical data type into a candidate data type set corresponding to the target variable, wherein each historical data type represents: the data type of the target variable which appears in the history stage;
And obtaining a target data type set corresponding to the target variable based on the target data type corresponding to the target variable and the candidate data type set.
8. The method of claim 7, wherein updating the initial bytecode based on each target data type to obtain target bytecode comprises:
And updating the initial byte codes based on the target data type sets corresponding to the target variables respectively to obtain target byte codes.
9. The method according to any of claims 1-4, further comprising, prior to said obtaining, based on said at least one reference data type, a respective target data type for each target variable in said initial bytecode that can be transferred by a calling function:
Determining attribute information of the calling function based on the interface function;
and searching the calling function from the initial byte code based on the attribute information.
10. A code compiling apparatus, comprising:
The device comprises an acquisition unit, a compiling unit and a compiling unit, wherein the acquisition unit is used for acquiring codes to be compiled, the codes to be compiled comprise subcodes and interface functions called by the subcodes, the subcodes are written in a dynamic type language, and the interface functions are written in a static type language;
The compiling unit is used for compiling the subcode to obtain an initial byte code;
the analyzing unit is used for analyzing the interface function and obtaining at least one reference data type which can be transmitted by the interface function;
The processing unit is used for obtaining the corresponding target data type of each target variable which can be transmitted by the calling function in the initial byte code based on the at least one reference data type; wherein the calling function is used for calling the interface function;
And the updating unit is used for updating the initial byte codes based on each target data type to obtain target byte codes.
11. An electronic device comprising a processor and a memory, wherein the memory stores a computer program which, when executed by the processor, causes the processor to perform the steps of the method of any of claims 1 to 9.
12. A computer readable storage medium, characterized in that it comprises a computer program for causing an electronic device to perform the steps of the method according to any one of claims 1-9 when said computer program is run on the electronic device.
13. A computer program product comprising a computer program, the computer program being stored on a computer readable storage medium; when the computer program is read from the computer readable storage medium by a processor of an electronic device, the processor executes the computer program, causing the electronic device to perform the steps of the method of any one of claims 1-9.
CN202211587040.1A 2022-12-09 2022-12-09 Code compiling method and device, electronic equipment and storage medium Pending CN118170385A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211587040.1A CN118170385A (en) 2022-12-09 2022-12-09 Code compiling method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211587040.1A CN118170385A (en) 2022-12-09 2022-12-09 Code compiling method and device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN118170385A true CN118170385A (en) 2024-06-11

Family

ID=91349201

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211587040.1A Pending CN118170385A (en) 2022-12-09 2022-12-09 Code compiling method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN118170385A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN118939275A (en) * 2024-10-10 2024-11-12 杭州长川科技股份有限公司 Compiling method, calling method, device, equipment and program product of classification parameters

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN118939275A (en) * 2024-10-10 2024-11-12 杭州长川科技股份有限公司 Compiling method, calling method, device, equipment and program product of classification parameters

Similar Documents

Publication Publication Date Title
CN111666526B (en) Page generation method, device, equipment and storage medium
CN111740948B (en) Data packet issuing method, dynamic updating method, device, equipment and medium
CN110532020A (en) A kind of data processing method of micro services layout, apparatus and system
CN114064024A (en) Method, apparatus, device, storage medium, and program product for developing micro-application
CN107783766B (en) Method and device for cleaning files of application program
CN113326030A (en) Service request processing method and device
CN112748927A (en) Project interface analysis method and related device
CN110851211A (en) Method, apparatus, electronic device, and medium for displaying application information
CN109933381A (en) A kind of loading method and device of kernel
CN113778897A (en) Automatic test method, device, equipment and storage medium of interface
WO2022127196A1 (en) Application identification method and apparatus, and device and storage medium
CN116931910A (en) Development method and device based on protocol framework, electronic equipment and storage medium
CN112114804B (en) Application program generation method, device and system
CN112527386A (en) Application program issuing method and device
CN118170385A (en) Code compiling method and device, electronic equipment and storage medium
CN116360735A (en) Form generation method, device, equipment and medium
CN110874216B (en) Complete code generation method, device, equipment and storage medium
CN112882698A (en) Development environment generation method and device, computer storage medium and electronic device
CN114979533A (en) Video recording method, device and terminal
CN118502992A (en) Service orchestration method and related equipment
CN113869016A (en) Chart configuration method, device and computer program product
WO2025066588A1 (en) Hot upgrade method for kernel subsystem, and device and storage medium
CN110297639B (en) Method and apparatus for detecting code
CN118502799A (en) Buried point method, buried point device, buried point equipment, storage medium and computer program product
CN114546436A (en) Micro front-end service updating method and device, electronic equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication