A type conversion method, device, equipment and storage medium

By determining the memory layout consistency of the structure type in golang and building the conversion function, the problem of performance loss of complex structure conversion during the compilation process is solved, and legality and performance optimization are achieved.

CN114756240BActive Publication Date: 2024-11-26DOUYIN VISION CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202210405946.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-04-18
Publication Date
2024-11-26
Estimated Expiration
2042-04-18

AI Technical Summary

Technical Problem

Golang cannot effectively convert complex structures during compilation, resulting in large system performance losses. There are memory replication problems in existing technologies such as deep copying or serialization/deserialization.

Method used

By determining the memory layout consistency of the variable to be converted and the target conversion variable, a target conversion function is constructed, and the memory address of the variable to be converted is assigned to the target conversion variable to realize the conversion of complex structure types.

Benefits of technology

Reduces system performance loss, ensures the legality of type conversion, and avoids error problems caused by inconsistent memory layout.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114756240B_ABST
    Figure CN114756240B_ABST
Patent Text Reader

Abstract

The present disclosure provides a type conversion method, device, equipment and storage medium, the method comprising: determining a variable to be converted and a target conversion variable; wherein the variable to be converted and the target conversion variable both belong to a structure type, determining whether the memory layout of the variable to be converted and the target conversion variable are consistent, and if it is determined that the memory layout of the variable to be converted and the target conversion variable are consistent, then constructing a target conversion function; wherein the target conversion function is used to assign the memory address of the variable to be converted to the target conversion variable. The disclosed embodiment realizes complex structure type conversion by creating a target conversion function, which can reduce system performance loss. In addition, the disclosed embodiment ensures the legality of type conversion by judging the consistency of the memory layout of the variable to be converted and the target conversion variable.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of data processing, and in particular to a type conversion method, device, equipment and storage medium. Background Art

[0002] Golang, also known as the Go language, is a statically strongly typed, compiled, concurrent programming language developed by Google that has garbage collection capabilities. During the compilation process, complex structures (such as structures with other structures nested within them) cannot be converted.

[0003] In related technologies, deep copy or serialization / deserialization is usually used to achieve complex structure conversion. However, during the compilation process, if the above technologies are used to convert complex structures, there may be a large amount of memory copying, resulting in a large loss of system performance. Summary of the invention

[0004] In order to solve the above technical problems, an embodiment of the present disclosure provides a type conversion method.

[0005] In a first aspect, the present disclosure provides a type conversion method, the method comprising:

[0006] Determine a variable to be transformed and a target transformation variable; wherein the variable to be transformed and the target transformation variable are both of structure type;

[0007] Determine whether the memory layout of the variable to be converted is consistent with the memory layout of the target conversion variable;

[0008] If it is determined that the memory layouts of the variable to be converted and the target conversion variable are consistent, a target conversion function is constructed; wherein the target conversion function is used to assign the memory address of the variable to be converted to the target conversion variable.

[0009] In an optional implementation, the input parameter type of the target conversion function is the type of the variable to be converted, and the output parameter type of the target conversion function is the type of the target conversion variable.

[0010] In an optional implementation manner, after constructing the target conversion function, the method further includes:

[0011] When it is detected that the variable to be converted exists in the first code block, the variable to be converted is input into the target conversion function, and the target conversion function converts the variable to be converted in the first code block into the target conversion variable; wherein the memory address of the target conversion variable in the first code block points to the memory address of the variable to be converted.

[0012] In an optional implementation manner, determining whether the memory layout of the variable to be converted is consistent with the memory layout of the target conversion variable includes:

[0013] It is determined whether the field types of the fields respectively included in the data structures of the variable to be converted and the target conversion variable and the order of the field types are the same.

[0014] In an optional implementation manner, determining whether the field types of the fields respectively included in the data structures of the variable to be converted and the target conversion variable and the order of the field types are the same includes:

[0015] Determine a first field in the data structure of the variable to be converted; wherein the first field belongs to any field in the data structure of the variable to be converted;

[0016] Determine a second field in the data structure of the target conversion variable that is located at a corresponding position to the first field;

[0017] Determine whether the first field and the second field have the same field type.

[0018] In an optional implementation manner, after determining the variable to be converted and the target conversion variable, the method further includes:

[0019] The types corresponding to the variable to be converted and the target conversion variable are stored in a preset type list;

[0020] Correspondingly, after determining whether the field types of the first field and the second field are the same, the method further includes:

[0021] If it is determined that the field types of the first field and the second field are different, determining whether the field types of the first field and the second field exist in the preset type list;

[0022] If it is determined that the field types of the first field and the second field do not exist in the preset type list, it is determined that the field types of the first field and the second field are different.

[0023] In an optional implementation manner, before determining whether the field types of the fields respectively included in the data structures of the to-be-converted variable and the target conversion variable and the order of the field types are the same, the method further includes:

[0024] Determining whether the number of fields respectively included in the data structures of the variable to be converted and the target conversion variable is the same;

[0025] Accordingly, the determining whether the field types of the fields respectively included in the data structures of the to-be-converted variable and the target conversion variable and the order of the field types are the same includes:

[0026] If it is determined that the number of fields respectively included in the data structures of the variable to be converted and the target conversion variable is the same, it is determined whether the field types and the order of the fields respectively included in the data structures of the variable to be converted and the target conversion variable are the same.

[0027] In an optional implementation, if it is determined that the memory layouts of the variable to be converted and the target conversion variable are inconsistent, the process exits and reports an error.

[0028] In a second aspect, the present disclosure provides a type conversion device, the device comprising:

[0029] A first determination module is used to determine a variable to be transformed and a target transformation variable; wherein the variable to be transformed and the target transformation variable are both of a structure type;

[0030] A second determination module is used to determine whether the memory layout of the variable to be converted is consistent with that of the target conversion variable;

[0031] A construction module is used to construct a target conversion function if it is determined that the memory layout of the variable to be converted is consistent with that of the target conversion variable; wherein the target conversion function is used to assign the memory address of the variable to be converted to the target conversion variable.

[0032] In a third aspect, the present disclosure provides a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, and when the instructions are executed on a terminal device, the terminal device implements the above method.

[0033] In a fourth aspect, the present disclosure provides a type conversion device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above method when executing the computer program.

[0034] In a fifth aspect, the present disclosure provides a computer program product, wherein the computer program product comprises a computer program / instructions, and the computer program / instructions implement the above method when executed by a processor.

[0035] Compared with the prior art, the technical solution provided by the embodiments of the present disclosure has at least the following advantages:

[0036] The disclosed embodiment provides a type conversion method, which determines a variable to be converted and a target conversion variable; wherein the variable to be converted and the target conversion variable both belong to a structure type, determines whether the memory layout of the variable to be converted and the target conversion variable are consistent, and if it is determined that the memory layout of the variable to be converted and the target conversion variable are consistent, then constructs a target conversion function; wherein the target conversion function is used to assign the memory address of the variable to be converted to the target conversion variable. The disclosed embodiment realizes complex structure type conversion by creating a target conversion function that can assign the memory address of the variable to be converted to the target conversion variable. Compared with the prior art, the type conversion method provided by the disclosed embodiment is used to convert complex structures, which can reduce system performance loss.

[0037] In addition, the embodiment of the present disclosure ensures the legality of type conversion by judging the consistency of the memory layout of the variable to be converted and the target conversion variable, thereby avoiding the occurrence of errors caused by inconsistent memory layout during the process of using the embodiment of the present disclosure to convert complex structure types. BRIEF DESCRIPTION OF THE DRAWINGS

[0038] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.

[0039] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0040] Figure 1 A flow chart of a type conversion method provided in an embodiment of the present disclosure;

[0041] Figure 2 A schematic diagram of memory layout of a WildCat type variable provided in an embodiment of the present disclosure;

[0042] Figure 3 A schematic diagram of a WildCat type variable data structure provided in an embodiment of the present disclosure;

[0043] Figure 4 A schematic diagram of a DomesticCat type variable data structure provided in an embodiment of the present disclosure;

[0044] Figure 5 A flow chart of another type conversion method provided by an embodiment of the present disclosure;

[0045] Figure 6 A schematic diagram of the structure of a type of conversion device provided in an embodiment of the present disclosure;

[0046] Figure 7 A schematic diagram of the structure of a type of conversion device provided in an embodiment of the present disclosure. DETAILED DESCRIPTION

[0047] In order to more clearly understand the above-mentioned objectives, features and advantages of the present disclosure, the scheme of the present disclosure will be further described below. It should be noted that the embodiments of the present disclosure and the features in the embodiments can be combined with each other without conflict.

[0048] In the following description, many specific details are set forth to facilitate a full understanding of the present disclosure, but the present disclosure may also be implemented in other ways different from those described herein; it is obvious that the embodiments in the specification are only part of the embodiments of the present disclosure, rather than all of the embodiments.

[0049] At present, during the compilation process, the Golang language only supports the conversion of simple types, but does not support the conversion of complex structures. In related technologies, deep copy or serialization / deserialization is usually used to achieve complex structure conversion. However, during the compilation process, if the above technologies are used to convert complex structures, there may be a large amount of memory copying, resulting in a large loss of system performance.

[0050] To this end, the disclosed embodiment provides a type conversion method to determine a variable to be converted and a target conversion variable; wherein the variable to be converted and the target conversion variable both belong to a structure type, determine whether the memory layout of the variable to be converted and the target conversion variable are consistent, and if it is determined that the memory layout of the variable to be converted and the target conversion variable are consistent, then construct a target conversion function; wherein the target conversion function is used to assign the memory address of the variable to be converted to the target conversion variable. The disclosed embodiment realizes the type conversion of a complex structure by creating a target conversion function that can assign the memory address of the variable to be converted to the target conversion variable. Compared with the prior art, the type conversion method provided by the disclosed embodiment is used to convert a complex structure, which can reduce system performance loss. In addition, the disclosed embodiment ensures the legitimacy of the type conversion by judging the consistency of the memory layout of the variable to be converted and the target conversion variable, and avoids the occurrence of errors caused by inconsistent memory layout in the process of using the disclosed embodiment to convert the type of a complex structure.

[0051] Based on this, the present disclosure provides a type conversion method, referring to Figure 1 , is a flow chart of a type conversion method provided by an embodiment of the present disclosure, the method comprising:

[0052] S101: Determine the variables to be converted and the target conversion variables.

[0053] Among them, the variable to be converted and the target conversion variable are both of structure type.

[0054] A structure type is a type of data structure, which may include several data items of the same or different types. The variables to be converted and the target conversion variables in the embodiments of the present disclosure are both structure types, as shown below, which is an example of a variable of a structure type:

[0055] / / Declare the WildCat type

[0056] type WildCat struct{

[0057] Color string

[0058] Age int32

[0059] Child*WildCat}

[0060] Here, a structure variable of type WildCat is declared, which contains three fields, namely Color, Age, and Child, and the field types corresponding to each field, namely string, int32, and WildCat. Different field types occupy different numbers of bytes in memory, such as string type occupies 64 bytes, int32 type occupies 32 bytes, and *WildCat type occupies 64 bytes.

[0061] The variable to be converted refers to a structure variable that needs to be converted, for example, a structure that contains other structures or its own structure variables. Based on the conversion requirements, the variable to be converted and the target conversion variable can be determined in advance, so that the target conversion function can be used to point the memory address of the target conversion variable to the memory address of the variable to be converted, thereby realizing the type conversion from the variable to be converted to the target conversion variable.

[0062] S102: Determine whether the memory layouts of the variable to be converted and the target conversion variable are consistent.

[0063] The memory layout refers to the arrangement of a variable in memory. Specifically, the memory layout of the variable to be converted and the target conversion variable is a byte string with a certain length. Specifically, taking the layout of the WildCat type variable in memory as an example, refer to Figure 2 , is a schematic diagram of a memory layout of a WildCat type variable provided in an embodiment of the present disclosure, such as Figure 2As shown in the figure, the fields in the WildCat type variable are arranged in order in the memory. Among them, Color is of string type, occupies 64 bytes and is arranged on the far left. Age is of int32 type, occupies 32 bytes and is arranged in the middle. However, in the golang language, in order to reduce the number of times the CPU (central processing unit) accesses the memory and increase the throughput of the CPU accessing the memory during compilation, the memory needs to be aligned, that is, the 32 bytes of the int32 type mentioned above will be expanded to 64 bytes for parsing. Child is of WildCat type, occupies 64 bytes and is arranged on the far right.

[0064] In the embodiment of the present disclosure, if it is necessary to determine whether the memory layout of the variable to be converted is consistent with that of the target conversion variable, it is only necessary to determine whether the field type and field order of the variable to be converted is consistent with that of the target conversion variable. If the field type and field order of the variable to be converted is consistent with that of the target conversion variable, it means that the memory layout of the variable to be converted is consistent with that of the target conversion variable.

[0065] Specifically, the field type and field order of the variable to be converted and the target conversion variable can be determined by the reflect.Type Of() function.

[0066] In order to more clearly describe the embodiments of the present disclosure, reference may be made to Figure 3 and Figure 4 , Figure 3 A schematic diagram of a data structure of a WildCat type variable provided in an embodiment of the present disclosure, wherein the WildCat type variable contains three fields, namely Color, Age and Child, and the field types corresponding to each field, namely string, int32, and *WildCat. Different field types occupy different numbers of bytes in memory, such as string type occupies 64 bytes, int32 type occupies 32 bytes, and *WildCat type occupies 64 bytes.

[0067] Figure 4 A schematic diagram of a DomesticCat type variable data structure provided in an embodiment of the present disclosure, wherein the DomesticCat type variable contains three fields, namely Color, Age and Child, and the field types corresponding to each field, namely string, int32, and *DomesticCat. Different field types occupy different numbers of bytes in memory, such as string type occupies 64 bytes, int32 type occupies 32 bytes, and *DomesticCat type occupies 64 bytes.

[0068] In the disclosed embodiment, if it is necessary to determine whether the memory layout of the WildCat type variable is consistent with that of the DomesticCat type, it is only necessary to determine whether the field types of the fields respectively included in the data structures of the WildCat type variable and the DomesticCat type variable and the order of the field types are the same.

[0069] Specifically, first determine the first field in the data structure of WildCat, such as the Color field, where the first field can be any field in the data structure of WildCat, and then determine the second field in the data structure of DomesticCat that is in a corresponding position to the first field in the data structure of WildCat, such as the Color field. Figure 4 The first field is the same as the second field. After traversing all the fields in the WildCat data structure in the above manner, it is determined whether the field types of the corresponding fields of the two are the same. If it is determined that the field types of the fields respectively included in the data structures of the WildCat type variable and the DomesticCat type variable and the order of the field types are the same, it is determined that the memory layout of the WildCat type variable is consistent with that of the DomesticCat type.

[0070] In order to improve the efficiency of judging the consistency of the memory layout of the variable to be converted and the target conversion variable, the embodiment of the present disclosure can first determine whether the number of fields respectively included in the data structures of the variable to be converted and the target conversion variable is the same. If the number of fields included in the two is different, it means that the memory layout of the two is inconsistent. At this time, the process can exit and report an error.

[0071] If it is determined that the number of fields respectively included in the data structures of the variable to be converted and the target conversion variable is the same, it is determined whether the field types and the order of the field types respectively included in the data structures of the variable to be converted and the target conversion variable are the same.

[0072] In the embodiment of the present disclosure, if it is determined that the number of fields included in the data structures of the variable to be converted and the target conversion variable are different, then it is determined that the memory layout of the variable to be converted and the target conversion variable are inconsistent. For example, the data structure of the variable to be converted A includes 3 fields, and the data structure of the target conversion variable B includes 5 fields. Since the number of fields included in the data structure of A is inconsistent with the number of fields included in the data structure of B, it can be determined that the memory layout of A and B is inconsistent.

[0073] In another embodiment of the present disclosure, if it is determined that the number of fields respectively included in the data structures of the variable to be converted and the target conversion variable is the same, it is further determined whether the field types of the fields respectively included in the data structures of the variable to be converted and the target conversion variable and the order of the field types are the same. If they are the same, the memory layout is consistent, otherwise the memory layout is inconsistent.

[0074] S103: If it is determined that the memory layout of the variable to be converted is consistent with the target conversion variable, then construct a target conversion function.

[0075] The target conversion function is used to assign the memory address of the variable to be converted to the target conversion variable, that is, the memory address accessed when querying the target conversion variable is the memory address of the variable to be converted.

[0076] In practical applications, the memory layout of the variable to be converted is consistent with that of the target conversion variable, which can ensure the legality of subsequent type conversion. At this time, a target conversion function can be constructed to assign the memory address of the variable to be converted to the target conversion variable.

[0077] In an optional implementation, in the process of constructing the target conversion function, the type of the variable to be converted is used as the input parameter type of the target conversion function, and the type of the target conversion variable is used as the output parameter type of the target conversion function, and the function of the target conversion function is implemented as a function function that can assign the memory address of the variable to be converted to the target conversion variable, so as to obtain the target conversion function. Specifically, the reflect package and the unsafe package in the golang programming language can be used to realize the generation of the target conversion function.

[0078] In an optional implementation, after constructing the target conversion function, the target conversion function can be used to perform type conversion on the variables to be converted in the first code block. When it is detected that there are variables to be converted in the first code block, the variables to be converted are input into the target conversion function, and the target conversion function converts the variables to be converted in the first code block into target conversion variables.

[0079] The memory address of the target conversion variable in the first code block points to the memory address of the variable to be converted; the first code block may be a code corresponding to any business function in the target application source code.

[0080] In an embodiment of the present disclosure, after constructing the target conversion function, the target conversion function may also detect the variables to be converted in the first code block. When it is detected that there are variables to be converted in the first code block, the variables to be converted are input as input parameters to the target conversion function, so that the memory address of the target conversion variable points to the memory address of the variable to be converted.

[0081] In an application scenario, the variables to be converted and the target conversion variables in the above embodiment can be variables in any functional module in the target APP (application). During the operation of the target APP, a target conversion function is dynamically generated to make the memory address of the target conversion variable point to the memory address of the variable to be converted. Specifically, during the software development process, for functional modules with type conversion requirements, the variables to be converted and the target conversion variables are pre-determined. After starting the target APP, a corresponding target conversion function is dynamically generated for each variable to be converted and target conversion variable to realize the function of pointing the memory address of the target conversion variable to the memory address of the variable to be converted. There is no need for manual assignment, and the target conversion function is reusable, that is, for the same variable to be converted and target conversion variable, if there are multiple type conversion requirements, the type conversion can be realized based on the same target conversion function.

[0082] In an optional implementation, if it is determined that the memory layout of the variable to be converted and the target conversion variable are inconsistent, the process exits and reports an error. Specifically, when the golang language is compiling the code, if it is determined that the memory layout of the variable to be converted and the target conversion variable are inconsistent, it means that the type conversion is illegal. At this time, the process exits and reports an error to avoid system crashes and other problems.

[0083] In the type conversion method provided by the embodiment of the present disclosure, the variable to be converted and the target conversion variable are determined; wherein, the variable to be converted and the target conversion variable both belong to the structure type, and it is determined whether the memory layout of the variable to be converted and the target conversion variable are consistent. If it is determined that the memory layout of the variable to be converted and the target conversion variable are consistent, a target conversion function is constructed; wherein the target conversion function is used to assign the memory address of the variable to be converted to the target conversion variable. The embodiment of the present disclosure realizes the type conversion of a complex structure by creating a target conversion function that can assign the memory address of the variable to be converted to the target conversion variable. Compared with the prior art, the type conversion method provided by the embodiment of the present disclosure is used to convert a complex structure, which can reduce system performance loss. In addition, the embodiment of the present disclosure ensures the legitimacy of the type conversion by judging the consistency of the memory layout of the variable to be converted and the target conversion variable, and avoids the occurrence of errors caused by inconsistent memory layout in the process of using the embodiment of the present disclosure to convert the type of a complex structure.

[0084] In some embodiments of the present disclosure, after determining the variables to be converted and the target conversion variables, the types corresponding to the variables to be converted and the target conversion variables are respectively stored in a preset type list. If it is determined that the field types of the first field and the second field are different, it is determined whether the field types of the first field and the second field exist in the preset type list. If it is determined that the field types of the first field and the second field do not exist in the preset type list, it is determined that the field types of the first field and the second field are different.

[0085] The preset type list includes the types of variables to be converted and target conversion variables that have been checked.

[0086] By using the preset type list, you can avoid the infinite recursion problem that may occur when checking the field types of the variable to be converted and the target conversion variable for their own types. For details, continue to refer to Figure 3 and Figure 4 , Figure 3 The Child field type of the WildCat type variable is WildCat type. Figure 4 The Child field type of the DomesticCat type variable is DomesticCat type. When the Child field of the WildCat type and the Child field of the DomesticCat type are recursively checked, an infinite recursion problem may occur because the checked field type is its own type. To prevent the above infinite recursion problem from occurring, a recursive end condition may be set through a preset type list. When the field type is checked to be a checked type (i.e., a field type belonging to the preset type list), the recursion is terminated. Specifically, the preset type list is searched for the currently matching field type. If the Child field of the WildCat type variable and the Child field of the DomesticCat type variable do not exist in the preset type list, it is determined that the Child field of the WildCat type variable and the Child field of the DomesticCat type variable are not of the same type.

[0087] Based on the content of the above embodiment, the embodiment of the present disclosure also provides a type conversion method, referring to Figure 5 , is a flow chart of another type conversion method provided by an embodiment of the present disclosure, the method comprising:

[0088] First, determine the variable to be converted and the target conversion variable, and obtain the field number, field type and order of the fields respectively included in the data structure of the variable to be converted and the target conversion variable based on the reflect.Type Of() function, so as to determine whether the memory layout of the variable to be converted and the target conversion variable is consistent. If the number of fields of the variable to be converted and the target conversion variable is the same, continue to check whether the field type and order of the variable to be converted and the target conversion variable are consistent. If the field type and order of the fields of the variable to be converted and the target conversion variable are consistent, the memory layout is determined to be consistent, otherwise, the process exits and reports an error.

[0089] After confirming that the memory layout of the variable to be converted is consistent with that of the target conversion variable, use the reflect package and the unsafe package to create the target conversion function. Specifically, 1. Get the types of the variable to be converted and the target conversion variable; 2. Use reflect.FuncOf to construct a new function type that is consistent with the type of the target conversion variable; 3. Use unsafe.Pointer to get the memory address of the variable to be converted; 4. Use reflect.NewAt to assign the memory address of the variable to be converted to the target conversion variable; 5. Use reflect.MakeFunc to encapsulate steps 3-4 into a target conversion function; 6. Return the encapsulated target conversion function. After obtaining the target conversion function, input the variable to be converted into the target conversion function so that the memory address of the variable to be converted is assigned to the target conversion variable.

[0090] In an application scenario, the type conversion method provided by the embodiment of the present disclosure can be applied to the operation process of the target APP. For example, during the operation of the target APP, when the variables to be converted in the video function module need to be converted into the target conversion variables in the chat function module, after the user starts the target APP, the generation of the target conversion function can be triggered. In addition, when the variables to be converted in the video function module need to be converted using the target conversion function, the target conversion function can be triggered to achieve the type conversion of the variables to be converted in the video function module of the target APP, thereby ensuring that the code block after type conversion can ensure the normal operation of the target APP.

[0091] Based on the above method embodiment, the present disclosure also provides a type conversion device, referring to Figure 6 , is a schematic diagram of a type of conversion device provided in an embodiment of the present disclosure, the device comprising:

[0092] The first determination module 601 is used to determine the variable to be transformed and the target transformation variable; wherein the variable to be transformed and the target transformation variable are both of structure type;

[0093] A second determination module 602 is used to determine whether the memory layout of the variable to be converted is consistent with the memory layout of the target conversion variable;

[0094] The construction module 603 is used to construct a target conversion function if it is determined that the memory layout of the variable to be converted is consistent with that of the target conversion variable; wherein the target conversion function is used to assign the memory address of the variable to be converted to the target conversion variable.

[0095] In an optional implementation, the input parameter type of the target conversion function is the type of the variable to be converted, and the output parameter type of the target conversion function is the type of the target conversion variable.

[0096] In an optional implementation manner, after constructing the target conversion function, the method further includes:

[0097] A conversion module is used to input the variable to be converted into the target conversion function when it is detected that the variable to be converted exists in the first code block, and the target conversion function converts the variable to be converted in the first code block into the target conversion variable; wherein the memory address of the target conversion variable in the first code block points to the memory address of the variable to be converted.

[0098] In an optional implementation manner, the second determining module is specifically configured to:

[0099] It is determined whether the field types of the fields respectively included in the data structures of the variable to be converted and the target conversion variable and the order of the field types are the same.

[0100] In an optional implementation manner, the second determining module includes:

[0101] A first determination submodule is used to determine a first field in the data structure of the variable to be converted; wherein the first field belongs to any field in the data structure of the variable to be converted;

[0102] A second determination submodule, configured to determine a second field in a data structure of the target conversion variable that is located at a corresponding position to the first field;

[0103] The third determining submodule is used to determine whether the field types of the first field and the second field are the same.

[0104] In an optional implementation manner, after determining the variable to be converted and the target conversion variable, the method further includes:

[0105] A storage module, used for storing the types corresponding to the variable to be converted and the target conversion variable in a preset type list;

[0106] Correspondingly, after the third determination submodule, the following is further included:

[0107] a third determining module, configured to determine whether the field types of the first field and the second field exist in the preset type list if it is determined that the field types of the first field and the second field are different;

[0108] The fourth determining module is configured to determine that the field types of the first field and the second field are different if it is determined that the field types of the first field and the second field do not exist in the preset type list.

[0109] In an optional implementation manner, before the second determining module, the method further includes:

[0110] a fifth determination module, configured to determine whether the number of fields respectively included in the data structures of the variable to be converted and the target conversion variable is the same;

[0111] Accordingly, the second determination module is specifically used for:

[0112] If it is determined that the number of fields respectively included in the data structures of the variable to be converted and the target conversion variable is the same, it is determined whether the field types and the order of the fields respectively included in the data structures of the variable to be converted and the target conversion variable are the same.

[0113] In an optional implementation, if it is determined that the memory layouts of the variable to be converted and the target conversion variable are inconsistent, the process exits and reports an error.

[0114] In the type conversion device provided by the implementation of the present disclosure, a variable to be converted and a target conversion variable are determined; wherein, the variable to be converted and the target conversion variable both belong to the structure type, and it is determined whether the memory layout of the variable to be converted and the target conversion variable are consistent. If it is determined that the memory layout of the variable to be converted and the target conversion variable are consistent, a target conversion function is constructed; wherein the target conversion function is used to assign the memory address of the variable to be converted to the target conversion variable. The embodiment of the present disclosure realizes the type conversion of a complex structure by creating a target conversion function that can assign the memory address of the variable to be converted to the target conversion variable. Compared with the prior art, the type conversion method provided by the embodiment of the present disclosure is used to convert a complex structure, which can reduce system performance loss. In addition, the embodiment of the present disclosure ensures the legitimacy of the type conversion by judging the consistency of the memory layout of the variable to be converted and the target conversion variable, and avoids the occurrence of errors caused by inconsistent memory layout in the process of using the embodiment of the present disclosure to convert the type of a complex structure.

[0115] In addition to the above-mentioned method and apparatus, the embodiments of the present disclosure further provide a computer-readable storage medium, in which instructions are stored. When the instructions are executed on a terminal device, the terminal device implements the type conversion method described in the embodiments of the present disclosure.

[0116] The embodiments of the present disclosure further provide a computer program product, which includes a computer program / instruction. When the computer program / instruction is executed by a processor, the type conversion method described in the embodiments of the present disclosure is implemented.

[0117] In addition, the present disclosure also provides a type conversion device, see Figure 7 As shown, this may include:

[0118] Processor 701, memory 702, input device 703 and output device 704. The number of processors 701 in the type conversion device can be one or more. Figure 7 In some embodiments of the present disclosure, the processor 701, the memory 702, the input device 703 and the output device 704 may be connected via a bus or other means, wherein: Figure 7 The example of connecting through bus is taken in the following.

[0119] The memory 702 can be used to store software programs and modules. The processor 701 executes various functional applications and data processing of the type conversion device by running the software programs and modules stored in the memory 702. The memory 702 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function, etc. In addition, the memory 702 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices. The input device 703 may be used to receive input digital or character information, and to generate signal input related to user settings and function control of the type conversion device.

[0120] Specifically in this embodiment, the processor 701 will load the executable files corresponding to the processes of one or more applications into the memory 702 according to the following instructions, and the processor 701 will run the applications stored in the memory 702, thereby realizing the various functions of the above-mentioned type conversion device.

[0121] It should be noted that, in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.

[0122] The above description is only a specific embodiment of the present disclosure, so that those skilled in the art can understand or implement the present disclosure. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present disclosure. Therefore, the present disclosure will not be limited to the embodiments described herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A type conversion method, characterized in that: The method comprises: Determine a variable to be transformed and a target transformation variable; wherein the variable to be transformed and the target transformation variable are both of structure type; Determine whether the memory layout of the variable to be converted is consistent with the memory layout of the target conversion variable; If it is determined that the memory layouts of the variable to be converted and the target conversion variable are consistent, then constructing a target conversion function; wherein the target conversion function is used to assign the memory address of the variable to be converted to the target conversion variable; The determining whether the memory layout of the variable to be converted is consistent with the memory layout of the target conversion variable includes: It is determined whether the field types of the fields respectively included in the data structures of the variable to be converted and the target conversion variable and the order of the field types are the same.

2. The method according to claim 1, characterized in that The input parameter type of the target conversion function is the type of the variable to be converted, and the output parameter type of the target conversion function is the type of the target conversion variable.

3. The method according to claim 1, characterized in that: After constructing the target conversion function, the following steps are also included: When it is detected that the variable to be converted exists in the first code block, the variable to be converted is input into the target conversion function, and the target conversion function converts the variable to be converted in the first code block into the target conversion variable; wherein the memory address of the target conversion variable in the first code block points to the memory address of the variable to be converted.

4. The method according to claim 1, characterized in that: The determining whether the field types of the fields respectively included in the data structures of the to-be-converted variable and the target conversion variable and the order of the field types are the same includes: Determine a first field in the data structure of the variable to be converted; wherein the first field belongs to any field in the data structure of the variable to be converted; Determine a second field in the data structure of the target conversion variable that is located at a corresponding position to the first field; Determine whether the first field and the second field have the same field type.

5. The method according to claim 4, characterized in that After determining the variables to be converted and the target conversion variables, the method further includes: The types corresponding to the variable to be converted and the target conversion variable are stored in a preset type list; Correspondingly, after determining whether the field types of the first field and the second field are the same, the method further includes: If it is determined that the field types of the first field and the second field are different, determining whether the field types of the first field and the second field exist in the preset type list; If it is determined that the field types of the first field and the second field do not exist in the preset type list, it is determined that the field types of the first field and the second field are different.

6. The method according to any one of claims 1 to 5, characterized in that: Before determining whether the field types of the fields respectively included in the data structures of the to-be-converted variable and the target conversion variable and the order of the field types are the same, the method further includes: Determining whether the number of fields respectively included in the data structures of the variable to be converted and the target conversion variable is the same; Accordingly, the determining whether the field types of the fields respectively included in the data structures of the variable to be converted and the target conversion variable and the order of the field types are the same includes: If it is determined that the number of fields respectively included in the data structures of the variable to be converted and the target conversion variable is the same, it is determined whether the field types and the order of the fields respectively included in the data structures of the variable to be converted and the target conversion variable are the same.

7. The method according to claim 1, characterized in that The method further comprises: If it is determined that the memory layouts of the variable to be converted and the target conversion variable are inconsistent, the process exits and reports an error.

8. A type conversion device, characterized in that: The device comprises: A first determination module is used to determine a variable to be transformed and a target transformation variable; wherein the variable to be transformed and the target transformation variable are both of a structure type; A second determination module is used to determine whether the memory layout of the variable to be converted is consistent with that of the target conversion variable; A construction module, configured to construct a target conversion function if it is determined that the memory layouts of the variable to be converted and the target conversion variable are consistent; wherein the target conversion function is configured to assign the memory address of the variable to be converted to the target conversion variable; The second determination module is specifically used to determine whether the field types of the fields respectively included in the data structures of the variable to be converted and the target conversion variable and the order of the field types are the same.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores instructions, and when the instructions are executed on a terminal device, the terminal device implements the method according to any one of claims 1 to 7.

10. A type conversion device, characterized in that: include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the method according to any one of claims 1 to 7 is implemented.

11. A computer program product, characterized in that The computer program product comprises a computer program / instruction, and when the computer program / instruction is executed by a processor, the method according to any one of claims 1 to 7 is implemented.