A method and apparatus for initializing a data object, and an electronic device
Patent Information
- Application Number
- CN202310813213.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-30
- Publication Date
- 2026-10-30
- Estimated Expiration
- 2043-06-30
AI Technical Summary
[0005]本申请实施例提供一种数据对象的初始化方法、装置,以解决现有技术方案中,对于对象中的多个成员变量进行初始化时重复调用对象指针,影响系统运行性能的问题
[0018]在本申请实施例中,根据初始化需求获取预设的对象构造函数;并响应于对象构造函数单次调用对象指针的操作,通过遍历数据对象中的成员变量,根据对象指针的值确定成员变量的地址信息;其中,数据对象包括至少两个成员变量,对象指针用于对数据对象中的成员变量进行访问;根据对象构造函数,以及每个成员变量的地址信息,对每个成员变量进行初始化操作。通过单次轮询遍历与指针调用一次性确定所有成员变量的初始化存储地址,并在此基础上进行初始化操作,减少了对多个成员变量进行初始化时反复调用对象指针时,重复加载对系统性能的影响。
Smart Images

Figure CN116909565B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of virtual machine compiler technology, and in particular to a method, apparatus, and electronic device for initializing data objects. Background Technology
[0002] Objects are a common data structure in programming. Developers can define objects as specific data variables or functions according to their actual development needs. Regardless of the form, objects need to be initialized during the compilation phase when used in a program.
[0003] In related technologies, when initializing member variables in an object, it is necessary to first obtain the starting address of the data object where the member variable is located, then calculate the offset address in the data object where it is located, and finally determine its final storage address before completing the initialization by assignment.
[0004] However, in existing technical solutions, when a data object contains multiple member variables, each initialization operation for a member variable requires calling the object pointer that points to the data object. As the number of members increases, the repeated loading of pointers during initialization will severely impact system performance. Summary of the Invention
[0005] This application provides a method and apparatus for initializing a data object, in order to solve the problem in the prior art where the object pointer is repeatedly called when initializing multiple member variables of an object, which affects the system's operating performance.
[0006] In a first aspect, embodiments of this application provide a method for initializing a data object, the method comprising:
[0007] Obtain a preset object constructor function in the virtual machine. The object constructor function is used to initialize the member variables in the data object when the data object is created.
[0008] Based on the characteristic type of the object constructor, in response to the single call to the object pointer by the object constructor, the address information corresponding to each member variable is determined by traversing the member variables in the data object; wherein, the data object includes at least two member variables, and the object pointer is used to access the member variables in the data object;
[0009] Based on the object constructor and the address information corresponding to each member variable, each member variable is initialized.
[0010] Secondly, embodiments of this application provide a data object initialization apparatus, the apparatus comprising:
[0011] The acquisition module is used to acquire a preset object constructor in the virtual machine. The object constructor is used to initialize the member variables in the data object when the data object is created.
[0012] The traversal module is used to determine the address information corresponding to each member variable by traversing the member variables in the data object, based on the characteristic type of the object constructor and in response to the single call to the object pointer by the object constructor; wherein, the data object includes at least two member variables, and the object pointer is used to access the member variables in the data object;
[0013] The initialization execution module is used to perform initialization operations on each member variable according to the object constructor and the address information corresponding to each member variable.
[0014] Thirdly, embodiments of this application provide an electronic device, including: a processor;
[0015] Memory used to store the processor's executable instructions;
[0016] The processor is configured to execute the instructions to implement the method.
[0017] Fourthly, embodiments of this application provide a storage medium that, when instructions in the storage medium are executed by a processor of an electronic device, enables the electronic device to perform the method.
[0018] In this embodiment, a preset object constructor is obtained according to initialization requirements; and in response to a single call to the object pointer by the object constructor, the address information of the member variables is determined by traversing the member variables in the data object and based on the value of the object pointer; wherein, the data object includes at least two member variables, and the object pointer is used to access the member variables in the data object; and each member variable is initialized according to the object constructor and the address information of each member variable. By determining the initialization storage address of all member variables at once through a single polling traversal and pointer call, and performing initialization operations on this basis, the impact of repeated loading on system performance when repeatedly calling the object pointer when initializing multiple member variables is reduced. Attached Figure Description
[0019] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. In the accompanying drawings:
[0020] Figure 1 This is a flowchart illustrating the simplified implementation steps of a data object initialization method provided in this application embodiment;
[0021] Figure 2 This is a flowchart illustrating the execution of a virtual machine object constructor function, as provided in an embodiment of this application.
[0022] Figure 3 This is a flowchart detailing the implementation steps of a data object initialization method provided in an embodiment of this application;
[0023] Figure 4 This is a bytecode generation result interface diagram provided in an embodiment of this application;
[0024] Figure 5 This is a schematic diagram of an optimized object constructor initialization member flow provided in an embodiment of this application;
[0025] Figure 6 This is a schematic diagram illustrating the effect of optimizing the call to an empty constructor according to an embodiment of this application;
[0026] Figure 7 This is a diagram illustrating the effect of an optimized constructor that repeatedly loads object pointers, as provided in an embodiment of this application.
[0027] Figure 8 This is a schematic diagram of the functional modules of a data object initialization device provided in an embodiment of this application.
[0028] Figure 9 This is a functional component relationship diagram of an electronic device provided in an embodiment of this application;
[0029] Figure 10 This is a functional component relationship diagram of another electronic device provided in the embodiments of this application. Detailed Implementation
[0030] Exemplary embodiments of this application will now be described in more detail with reference to the accompanying drawings.
[0031] Reference Figure 1 , Figure 1 This is a simplified implementation flowchart of a data object initialization method provided in an embodiment of this application. For example... Figure 1 As shown, the steps of the method include:
[0032] Step 101: Obtain the preset object constructor in the virtual machine. The object constructor is used to initialize the member variables in the data object when creating the data object.
[0033] An object constructor is a special method. It is mainly used to initialize an object when it is created, that is, to assign initial values to the object's member variables.
[0034] The method for initializing a data object provided in this application mainly targets the invocation of the constructor function of an object compiled by a JIT compiler. For the JIT compiler, the source code of a data object-oriented programming language is a file of intermediate bytecode (or intermediate code / intermediate language IL) generated during the first pre-compilation. This intermediate bytecode file, as an intermediate product of compilation, can be further compiled by the JIT compiler to obtain a sequence of machine code instructions that can be directly executed by the computer's central processing unit.
[0035] In practical applications, developers can customize the constructor and its calling relationship according to the specific type required. If it is not defined in advance, the compiler will automatically generate one during compilation, and the JIT compiler will automatically call it when creating an object of the custom class.
[0036] During the importer phase within the virtual machine compiler—specifically, the phase where bytecode in object constructors is converted into a syntax tree—object constructors can be distinguished from regular functions based on their names during the bytecode processing and conversion of all function calls. The character encoding rules for function names and object constructor names are standardized by different virtual machines according to their respective standards. Object constructor names generally have specific suffix keywords, the specific rules of which are determined by the corresponding virtual machine's standard specification. For example, the C# language virtual machine standard specification, ECMA-335, stipulates that object constructor names must end with the ".ctor" keyword.
[0037] Step 102: Based on the characteristic type of the object constructor, in response to the single call to the object pointer by the object constructor, determine the address information corresponding to each member variable by traversing the member variables in the data object; wherein, the data object includes at least two member variables, and the object pointer is used to access the member variables in the data object.
[0038] In object-oriented programming languages, the constructor of a class or object often initializes its internal member variables. The bytecode expression logic for initializing each member variable first obtains the starting address of the object in which the member variable is located, then calculates the offset address in the object, and finally calculates and determines its final storage address.
[0039] Specific references Figure 2 This illustration shows a flowchart of the execution process of a virtual machine object constructor according to an embodiment of this application. Figure 2As shown, in step S1, the compiler requests storage space from the system to initialize the data object. Then, in step S2, the acquired object constructor is called to begin the initialization process. After step S3 determines that the member variables in the data object will be initialized, step S4 loads the object constructor and the `this` pointer corresponding to the data object.
[0040] Currently, most mainstream object constructors pass the `this` pointer implicitly through the first parameter register. When the intermediate bytecode expresses the object constructor initialization, it first loads this `this` pointer parameter, then calculates the actual memory address of the member variable based on the address offset of the data object, and then stores the initialization value in that memory address.
[0041] It's important to note that the `this` pointer here refers to the object pointer of the data object itself. The `this` pointer is not part of the data object and does not affect the result of `sizeof(object)`. The scope of `this` is within the data object. When accessing non-static members of the data object within its non-static member functions, the compiler automatically passes the address of the data object itself as an implicit parameter to the object constructor. The pointer points to the data object itself, and its value represents the specific memory address where the data object is stored. In other words, the reuse mechanism of the `this` pointer allows the object constructor to access all member variables of the data object.
[0042] Step 103: Based on the object constructor and the address information corresponding to each member variable, perform initialization operations on each member variable.
[0043] After the traversal operation in step 102, once the address information of each member variable in the data object is determined, the initial value of the set member variable is stored in the storage space corresponding to the address information, thus completing the initialization operation of all member variables.
[0044] In this embodiment, when initializing members within the object constructor, the number of members initialized is recorded. When the number of initialized members is greater than one, the object pointer of the first member that has already been loaded is reused, and all member variable initialization operations are linked together in a linked list within the virtual machine for tracking. Thus, for the initialization operations of all member variables in the data object, only the object pointer needs to be called once to determine the initialization address of all member variables, greatly reducing the waste of system resources caused by repeated calls when the number of member variables is large, and improving operating efficiency.
[0045] In summary, the data object initialization method provided in this application obtains a preset object constructor according to initialization requirements; and in response to a single call to the object pointer by the object constructor, it determines the address information of the member variables by traversing the member variables in the data object and based on the value of the object pointer; wherein the data object includes at least two member variables, and the object pointer is used to access the member variables in the data object; and each member variable is initialized according to the object constructor and the address information of each member variable. The initialization storage address of all member variables is determined at once through a single polling traversal and pointer call. This reduces the impact of repeated loading on system performance when repeatedly calling the object pointer to initialize multiple member variables.
[0046] Reference Figure 3 The diagram illustrates a detailed implementation flowchart of a data object initialization method provided in an embodiment of this application. Figure 3 As shown, the steps of the method include:
[0047] Step 201: Request storage space from the system to initialize the data object.
[0048] Before the JIT compiler compiles the intermediate code, the object pointer of the data object does not yet point to a specific memory address. Assigning values to this ambiguous memory space after entering the program is meaningless and unreasonable, and could very likely cause the program to crash. Therefore, before officially calling the object constructor for initialization, it is necessary to actively request memory space from the system.
[0049] Specifically, in the C++ language environment, the malloc function instruction is commonly used to allocate memory space. The specific form is:
[0050] void*calloc(int num, size_t size);
[0051] calloc initialization + allocation of dynamic memory space
[0052] / / Header file
[0053] #include<malloc.h>
[0054] This means that `malloc` allocates `num` contiguous memory locations of size `size` on the heap, returns the starting address of this memory location, and initializes all elements to 0. Here, `num` represents the number of elements. It's worth noting that the `malloc` function is often used in conjunction with the `free` function; `malloc` allocates memory, and `free` releases it. You can think of it this way: when `malloc` allocates memory, it marks a contiguous block of memory on the heap to indicate that this memory has been allocated, while `free` removes this mark, indicating that the memory is no longer in use.
[0055] Step 202: Obtain the preset object constructor in the virtual machine. The object constructor is used to initialize the member variables in the data object when creating the data object.
[0056] For details of this step, please refer to step 101 above. This embodiment will not repeat the details here.
[0057] Step 203: Determine the characteristic type of the object constructor.
[0058] In the data object initialization method provided in this application embodiment, in addition to solving the problem of system performance impact caused by repeated calls to object pointers, it also focuses on the scheduling strategy and utilization method of system resources in other situations. When the bytecode expression of the object constructor of the data object has only one bytecode return instruction, the object constructor can be defined as empty in the virtual machine JIT compiler. In this case, there is no need to continue to call the object constructor to implement subsequent initialization operations.
[0059] Before formally calling the constructor of the obtained object, the characteristic type of the object's constructor is first obtained to determine whether it is an empty function. Only when the characteristic type determination result is a non-empty function will the relevant measures for optimizing object pointer calls mentioned above be taken.
[0060] In an optional embodiment, step 203 may further include:
[0061] Sub-step 2031: Obtain the bytecode in the object constructor, the bytecode being translated to obtain machine instructions for execution by an electronic device.
[0062] In the JIT editor's operating environment, obtain the relevant bytecode of the object constructor under the intermediate bytecode expression.
[0063] Specific references Figure 4The diagram illustrates a bytecode generation result interface provided in an embodiment of this application. Unlike the original compiled code instructions that are easy for users to read, bytecode is relatively less readable, and will not be discussed further in this embodiment.
[0064] It should be noted that the ldarg.0 command, marked with a dashed box, pushes the 0th parameter of the method onto the stack. In the above embodiment, the `this` pointer is always the 0th parameter. Therefore, the purpose of ldarg.0 is to push the object pointer (`this`) onto the stack for scheduling. Figure 4 The bytecode interface shown is the scheduling result before object pointer optimization. It can be seen that there are multiple scheduling instructions for ldarg.0, which repeatedly loads the object pointer, which will seriously affect the system operation.
[0065] Sub-step 2032: If the bytecode contains only a return instruction, determine that the characteristic type of the object constructor is an empty function.
[0066] When the bytecode representation of an object constructor contains only a bytecode return instruction, the object constructor can be defined as an empty function within the JIT compiler of the virtual machine. For example, the standard specification for the C# language virtual machine, ECMA-335, stipulates that the function return bytecode is the "ret" bytecode.
[0067] Sub-step 2033: If the bytecode contains at least one of the member variables, determine that the characteristic type of the object constructor is a non-empty function.
[0068] Conversely, if the bytecode contains not only a return instruction but also at least one member variable to be initialized, then the current object's constructor function can be determined to be non-empty.
[0069] Specifically, for example, the source code of the constructor function for the Object class in the C# runtime library:
[0070] / / Creates a new instance of an Object.
[0071] [NonVersionable]
[0072] public Object()
[0073] {
[0074] }
[0075] After being compiled into bytecode, its name is Object:.ctor():this. If the bytecode of this object constructor only contains the "ret" instruction, then the object constructor is determined to be an empty function.
[0076] Sub-step 2034: If the feature type is a non-empty function, determine that the feature type meets the preset conditions.
[0077] If the current object's constructor is determined to be a non-empty function in the characteristic type judgment result, then it can be considered to meet the conditions for being called.
[0078] Step 204: If the feature type meets the preset conditions, proceed to the step of making a single call to the object pointer through the object constructor.
[0079] Reference Figure 5 This illustration shows a schematic diagram of an optimized object constructor initialization member flow provided in an embodiment of this application. For example... Figure 5 As shown, after requesting memory space from the system in step S8, the process proceeds to step S9, which determines whether the constructor is empty. If the object constructor is determined to be not empty, it is assumed that the object constructor's characteristic type meets the preset conditions, and the process then proceeds to step S2, which calls the initialization function. Conversely, if the determination result indicates that the object constructor is empty, the call ends directly.
[0080] In an optional embodiment, step 204 may further include:
[0081] Sub-step 2041: Obtain the number of member variables in the data object.
[0082] Following step 203, even if the characteristic type of the object constructor is determined to be a non-empty function, further judgment is still needed regarding the number of its member variables. For the case containing only a single member variable and the case containing multiple member variables, this application proposes two corresponding initialization implementation methods.
[0083] Sub-step 2042: If the number of member variables is greater than or equal to 2, proceed to the step of determining the address information of the member variables by traversing the member variables in the data object and based on the value of the object pointer in response to the single call to the object pointer of the object constructor.
[0084] When identifying the number of member variables, the assignment instruction stfld bytecode is checked when reading the bytecode of the object constructor. If the target of the stfld bytecode operation is the object and the stfld bytecode appears more than twice consecutively, it can be determined that the number of initialized member variables of the object is greater than 2. Member variables are local variables defined inside a certain object.
[0085] The following is a feasible example of source code:
[0086]
[0087] The traversal process involves checking member variables using stfld bytecode while reading the object constructor bytecode. Bytecode refers to compiled but machine-independent intermediate code that needs to be translated by an interpreter to become machine code. Unlike the source code shown in the code snippet above, bytecode is usually not human-readable; instead, it is a sequence of encoded numerical constants, references, instructions, etc. Therefore, this embodiment will not elaborate on the specific content of stfld bytecode.
[0088] The `stfld` bytecode contains two operands: the `this` pointer of the object, which points to the address of the data object, and the offset of the member variable being operated on relative to the `this` pointer. When the target of the `stfld` bytecode operation is detected as the current object, and two consecutive `stfld` bytecode operations occur, indicating that the number of member variables to be initialized in the constructor is 2, a list representing the initialization list will be created to merge the two `stfld` operations into the list.
[0089] This list has two nodes. The first node stores the `this` pointer of the object, and the second node stores a linked list that links all member variables corresponding to `stfld` operations. If a third consecutive `stfld` operation is found, indicating a new member variable, the bytecode of the third `stfld` operation is appended to the second node of the list and placed at the end of the linked list. This list construction ensures that all member variables corresponding to the `stfld` bytecode within it share the `this` pointer stored in the first node during initialization, avoiding the need to repeatedly load the `this` pointer with each `stfld` operation—a single call to the object's `this` pointer, as mentioned earlier—thus reducing the impact of repeated loading on system performance.
[0090] The `this` pointer is a technical term used in object-oriented programming languages. By default, the constructor of each object implicitly passes it as the first argument, and the `this` pointer points to the starting memory address where the object is stored.
[0091] After the program's source code is compiled, the position of each member variable in the object is fixed. That is, the relative offset of each member variable to the object's starting address (this) is fixed, and access to each member variable is determined by the access address determined by the this pointer plus the offset.
[0092] When the number of members is greater than or equal to two (i.e., multiple members), since each member variable needs to first obtain the starting address of its data object, the existing initialization method would inevitably result in repeated calls to the object pointer. Therefore, in this embodiment, a polling traversal method is adopted to uniformly determine the address information of multiple member variables before performing the initialization operation. (Continue to refer to...) Figure 5 If it is determined in step S10 that the initialized member variable is greater than 1, then proceed to the processing steps S11-S13.
[0093] It is worth noting that, in the embodiments of this application, if the initialization of member variables is completed through the return value of a function call, the object pointer this pointer can be bound to a non-volatile register during the register allocation stage to ensure that the operation of eliminating duplicate loading of the this pointer will not be interrupted by the function call.
[0094] Sub-step 2043: When the number of member variables is less than 2, obtain the address information of a single member variable in the data object by calling the object pointer once according to the object constructor, and perform initialization operation on the single member variable according to the object constructor and the address information of the single member variable.
[0095] Correspondingly, since the current object constructor's bytecode has been determined to contain at least one member variable, if the initialized member variable is determined to be equal to 1 in step S10, the process jumps to the relevant steps in steps S4-S6.
[0096] The initialization method adopted by the system at this time is the same as that of the prior art. For details, please refer to the description of steps S4-S6 in step 102 above. This embodiment will not repeat the description here.
[0097] Step 205: Based on the characteristic type of the object constructor, in response to the single call to the object pointer by the object constructor, determine the address information corresponding to each member variable by traversing the member variables in the data object.
[0098] For details of this step, please refer to step 102 above. This embodiment will not repeat the details here.
[0099] In an alternative embodiment, step 205 may further include:
[0100] Sub-step 2051: Establish the initialization list of the data object.
[0101] An initialization list is a list generated when initializing member variables. It has two phases: the initialization phase and the calculation phase. Only operations performed here can be called initialization. The initialization list includes a first node that stores the object pointer and a second node that stores the member linked list, which represents the chained connection relationship between member variables.
[0102] Sub-step 2052: Determine the address offset of the traversed member variable relative to the data object, and save the address offset corresponding to each member variable to the initialization list.
[0103] Offset address of object member variables: After the source code of the program is compiled, the position of the member variables in each object is fixed. The address offset is the byte distance of the storage unit address in the current segment relative to the segment head unit. The address of the segment head unit is the address pointed to by the this pointer. That is, the relative offset of each member variable to the starting address of the object and this is fixed. Accessing each member variable is determined by the access address determined by the this pointer + offset.
[0104] The address offset of a member variable can be calculated using predefined library functions, such as when the header file `#include <stdio.h>` is called during the compilation phase.<stddef.h> This is achieved through the library function `offsetof(struct s, i)`, where `offsetof` is the function name, the parentheses contain the function parameter list, `struct` is the identifier of the structure object, `s` is the name of the object whose constructor is called, and `i` is the name of the specific member variable. The `offsetof` function can directly obtain the address offset of the member variable.
[0105] Continue to refer to Figure 5 In step S11, after the member variables in the data object are traversed for the first time, their address offsets are determined and then saved to the initialization list for subsequent initialization operations.
[0106] Sub-step 2053: In response to the traversal operation of the initialization list, load the object pointer, and determine the address information of the member variables in sequence according to the address offset and the value of the object pointer. The address information is used to characterize the actual storage address of the member variables.
[0107] exist Figure 5 After determining the address offsets of all member variables, the initialization list is traversed, and the object pointer of the data object is loaded. The actual storage address of each member variable in the storage space can be determined using the object pointer and address offset. After subsequent initialization and assignment, the value will be saved to the storage space corresponding to that address.
[0108] Optionally, sub-step 2053 may specifically include:
[0109] Sub-step 20531: Obtain the object pointer from the first node.
[0110] Sub-step 20532: During the traversal of the member variables, the sum of the value of the object pointer and the address offset of the member variable is used as the address information of the member variable.
[0111] Sub-step 20533: During the traversal, for non-first member variables, according to the second node, the non-first member variables are appended to the tail of the member linked list, and the sum of the value of the object pointer already stored in the first node and the address offset corresponding to each non-first member variable is used as the address information of the non-first member variable.
[0112] For sub-steps 20531-20533:
[0113] Specifically, during the reading of the object constructor bytecode, when it is detected that the target of the bytecode `stfld` operation is the current object, and two consecutive `stfld` bytecode operations occur, a list is created to merge the two `stfld` operations into the list. This list has two nodes. The first node stores the object's `this` pointer, and the second node stores a linked list that links all member variables operated on by `stfld`. If a third consecutive `stfld` operation is subsequently found, indicating a new member variable, the bytecode of the third `stfld` is appended to the second storage node of the list and placed at the end of the linked list corresponding to that node. This list construction allows all member variables corresponding to `stfld` bytecodes stored in the linked list of the second node to share the `this` pointer stored in the first node, avoiding the need to repeatedly load the `this` pointer with each `stfld` operation, thus reducing the impact of repeated loading on system performance.
[0114] Sub-step 2054: If it is determined that the characteristic type of the object constructor is an empty function, replace the call operation of the object constructor with an empty operation.
[0115] Once it is determined that the object constructor is an empty function, the function call that initializes the object by calling the constructor is replaced with an empty operation. This avoids meaningless compilation operations and meaningless empty function calls by the virtual machine's JIT compiler, thereby improving the compilation efficiency of the virtual machine and the execution efficiency of the program.
[0116] Specific references Figure 6 This diagram illustrates the effect of optimizing the invocation of an empty object constructor according to an embodiment of this application. Figure 6 The left-hand diagram shows the assembly code generated before optimization. The first five lines of instructions call the object constructor `Object:.ctor():this`. During the JIT compiler compilation process, the name `.ctor` identifies this function as an object constructor. Then, reading the bytecode instructions of this object constructor reveals only the `ret` bytecode instruction, indicating that the object constructor is an empty function. Therefore, the optimization condition of an empty object constructor is met. After optimizing away the code calling this object constructor, the result is as follows: Figure 6 The instructions in the right-hand diagram reduce the performance overhead of function calls and significantly improve the efficiency of virtual machine compilation.
[0117] Step 206: Based on the object constructor and the address information corresponding to each member variable, perform initialization operations on each member variable respectively.
[0118] For details of this step, please refer to step 103 above. This embodiment will not repeat the details here.
[0119] In an optional embodiment, step 206 may further include:
[0120] Sub-step 2061: Determine the variable type of the member variable based on the type identifier of the member variable.
[0121] Member variables in a data object may contain multiple variable types. Before initializing and assigning values, it is necessary to first clarify the specific type of the member variable to ensure that the initial value assigned to it is consistent with its variable type.
[0122] Specifically, taking C# as an example, member variables may include double-precision floating-point numbers, integers, characters, etc. The system can determine the variable type based on the type identifier of each variable.
[0123] Sub-step 2062: Obtain the initial variable value of the member variable according to the variable type.
[0124] Once the variable type is determined, initial values can be assigned to the member variables. For example, for an integer variable `int`, the system can assign it any integer value without the developer defining it. For a floating-point variable `double`, its initial value is any number including decimal places; a character variable `char` is typically a single character of size 8 bits, such as an English letter.
[0125] Sub-step 2063: According to the address information of the member variable, store the initial variable value of the member variable in the storage space corresponding to the address information.
[0126] After the system assigns an initial value to the variable, the initial value is saved to the storage space corresponding to the address information of the variable object through the object constructor, thus completing the initialization of the variable.
[0127] Reference Figure 7 The diagram illustrates the effect of optimizing the constructor to repeatedly load the object pointer according to an embodiment of this application. The left side of the diagram shows the intermediate code instructions generated before optimization; the dashed box indicates that the `this` pointer was called five times during this initialization operation. After optimization, the generated intermediate code instructions only call the pointer once, eliminating the need to repeatedly load the object's `this` pointer during member initialization.
[0128] Specifically, corresponding Figure 7 In the left-hand block diagram, the first three lines of instructions are the instructions generated by the first stfld bytecode. The first line of instructions constructs an immediate constant value of 106 and stores it in register a0. The second line of instructions obtains the this pointer of the object. The third line of instructions uses stfld bytecode to store the immediate constant value of 106 stored in register a0 into the address this+8, that is, into the first member variable of the object.
[0129] Figure 7 The instructions in lines four through six on the left are generated by the second stfld bytecode and are used to initialize the second member variable, which is offset by 16 relative to the this pointer.
[0130] Figure 7 The instructions generated from the third stfld bytecode in lines 7 to 9 on the left are used to initialize the third member variable, which has an offset of 30 relative to the this pointer.
[0131] Figure 7 The instructions generated from the fourth stfld bytecode in lines 10 to 12 on the left are used to initialize the fourth member variable, which is offset by 24 relative to the this pointer.
[0132] Figure 7 The instructions generated by the fourth stfld bytecode in lines 13 to 15 on the left are used to initialize the fifth member variable, which is offset by 28 relative to the this pointer.
[0133] The `stfld` bytecode has two operands: the `this` pointer of the object and the offset of the operated member variable relative to the `this` pointer. During the reading of the object constructor bytecode, by detecting two consecutive `stfld` bytecode operations targeting the current object, a list is created to merge the two `stfld` operations. This list has two nodes: the first node stores the object's `this` pointer, and the second node stores a linked list connecting all the member variables operated on by `stfld`. If a third `stfld` operation corresponding to a member variable is detected immediately following the second, it is appended to the second node of the list and placed at the end of the linked list. Then, if a fourth `stfld` operation corresponding to a member variable is detected immediately following the third, it is appended to the second node of the list and placed at the end of the linked list. The list constructed in this way can be optimized by sharing the this pointer, eliminating the need to repeatedly load the this pointer every time stfld is used, thus reducing the impact of repeated loading on system performance.
[0134] The assembly instruction sequence generated from this list is as follows: Figure 7 As shown on the right, this reduces the loading of duplicate object `this` pointers. First, the list information is read, which stores four `stfld` operation information entries:
[0135] Figure 7 The first to third lines on the right are instructions generated based on the first stfld in the list. The second line loads the object's this pointer, and the third line saves register a0, which stores the immediate constant 106, to the address of this+8 offset.
[0136] The subsequent fourth and fifth instructions, generated by the second stfld in the list, will save register a0, which stores the immediate constant 107, to the address at offset this+16.
[0137] The subsequent sixth and seventh instructions, generated by the third stfld in the list, will save register a0, which stores the immediate constant 108, to the address at offset this+30.
[0138] The subsequent instructions in lines eight and nine, generated by the third stfld in the list, will save register a0, which stores the immediate constant 109, to the address at offset this+24.
[0139] The subsequent eleventh line instruction, generated by the fourth stfld instruction in the list, saves register a0, which stores the immediate constant 110, to the address at offset this+28.
[0140] In summary, the data object initialization method provided in this application obtains a preset object constructor according to initialization requirements; and in response to a single call to the object pointer by the object constructor, it determines the address information of the member variables by traversing the member variables in the data object and based on the value of the object pointer; wherein the data object includes at least two member variables, and the object pointer is used to access the member variables in the data object; and each member variable is initialized according to the object constructor and the address information of each member variable. The initialization storage address of the member variable is determined in one go through a single polling traversal and pointer call. This reduces the impact of repeated loading on system performance when repeatedly calling the object pointer to initialize multiple member variables.
[0141] Reference Figure 8 This diagram illustrates the functional module composition of a data object initialization device 300 provided in an embodiment of this application. Figure 7 As shown, the device includes:
[0142] The acquisition module 301 is used to acquire a preset object constructor in the virtual machine. The object constructor is used to initialize the member variables in the data object when the data object is created.
[0143] The traversal module 302 is used to determine the address information corresponding to each member variable by traversing the member variables in the data object according to the characteristic type of the object constructor and in response to the single call to the object pointer by the object constructor; wherein, the data object includes at least two member variables, and the object pointer is used to access the member variables in the data object;
[0144] The initialization execution module 303 is used to perform initialization operations on each member variable according to the object constructor and the address information corresponding to each member variable.
[0145] Optionally, the traversal module 302 further includes:
[0146] The list creation submodule is used to create an initialization list for the data object;
[0147] The offset determination submodule is used to determine the address offset of each of the traversed member variables relative to the data object, and save the address offset corresponding to each member variable to the initialization list;
[0148] The address information determination submodule is used to load the object pointer in response to the traversal operation of the initialization list, and determine the address information of the member variables in sequence according to the address offset and the value of the object pointer. The address information is used to characterize the actual storage address of the member variables.
[0149] Optionally, the address information determination submodule further includes:
[0150] An object pointer acquisition unit is used to acquire the object pointer from the first node.
[0151] The address information determination unit is used to, during the process of traversing the member variables, use the sum of the value of the object pointer and the address offset of the member variable as the address information of the member variable.
[0152] The multi-member face-changing address processing unit is used to append the non-first member variable to the end of the member linked list according to the second node during the traversal process, and use the sum of the value of the object pointer already stored in the first node and the address offset of the non-first member variable as the address information of the non-first member variable.
[0153] Optionally, the initialization execution module 303 further includes:
[0154] The variable type determination submodule is used to determine the variable type of the member variable based on the type identifier of the member variable;
[0155] The initial value acquisition submodule is used to obtain the initial variable value of the member variable according to the variable type;
[0156] The initial value storage submodule is used to store the initial variable values of the member variables into the storage space corresponding to the address information, according to the address information of the member variables.
[0157] Optionally, the device further includes:
[0158] The feature type determination module is used to determine the feature type of the object constructor.
[0159] The preset condition judgment module is used to proceed to the step of making a single call to the object pointer through the object constructor when the feature type meets the preset conditions.
[0160] Optionally, the feature type determination module further includes:
[0161] The bytecode acquisition submodule is used to acquire the bytecode in the constructor of the object, and the bytecode is used to obtain machine instructions for execution by an electronic device through translation;
[0162] The type determination submodule is used to determine that the characteristic type of the object constructor is a non-empty function when the bytecode contains at least one of the member variables; and to determine that the characteristic type of the object constructor is an empty function when the bytecode only contains a return instruction.
[0163] The preset condition judgment submodule is used to determine that the feature type meets the preset conditions when the feature type is a non-empty function.
[0164] The no-operation replacement submodule is used to replace the call operation of the object constructor with a no-operation when it is determined that the characteristic type of the object constructor is an empty function.
[0165] Optionally, the device further includes:
[0166] The member count acquisition module is used to obtain the number of member variables in the data object;
[0167] The first initialization execution module is used to, when the number of member variables is greater than or equal to 2, enter the operation of responding to the single call of the object pointer by the object constructor, and determine the address information of the member variables by traversing the member variables in the data object and based on the value of the object pointer;
[0168] The second initialization execution module is used to obtain the address information of a single member variable in the data object by calling the object pointer once according to the object constructor when the number of member variables is less than 2, and to perform initialization operation on the single member variable according to the object constructor and the address information of the single member variable.
[0169] Optionally, the device further includes:
[0170] The application module is used to request storage space from the system for initializing the data object.
[0171] An initialization response module is used to perform initialization operations on each member variable in the storage space according to the object constructor and the address information of each member variable.
[0172] In summary, the data object initialization device provided in this application obtains a preset object constructor according to initialization requirements; and in response to a single call to the object pointer by the object constructor, it determines the address information of the member variables by traversing the member variables in the data object and based on the value of the object pointer; wherein the data object includes at least two member variables, and the object pointer is used to access the member variables in the data object; and each member variable is initialized according to the object constructor and the address information of each member variable. The initialization storage address of the member variable is determined in one go through a single polling traversal and pointer call. This reduces the impact of repeated loading on system performance when repeatedly calling the object pointer to initialize multiple member variables.
[0173] Figure 9 This is a block diagram illustrating an electronic device 600 according to an exemplary embodiment. For example, the electronic device 600 may be a mobile phone, computer, digital broadcasting terminal, messaging device, game console, tablet device, medical device, fitness equipment, personal digital assistant, etc.
[0174] Reference Figure 9 The electronic device 600 may include one or more of the following components: a processing component 602, a memory 604, a power supply component 606, a multimedia component 608, an audio component 610, an input / output (I / O) interface 612, a sensor component 614, and a communication component 616.
[0175] Processing component 602 typically controls the overall operation of electronic device 600, such as operations associated with display, telephone calls, data communication, camera operation, and recording operations. Processing component 602 may include one or more processors 620 to execute instructions to perform all or part of the steps of the methods described above. Furthermore, processing component 602 may include one or more modules to facilitate interaction between processing component 602 and other components. For example, processing component 602 may include a multimedia module to facilitate interaction between multimedia component 608 and processing component 602.
[0176] Memory 604 is used to store various types of data to support the operation of electronic device 600. Examples of such data include instructions for any application or method operating on electronic device 600, contact data, phonebook data, messages, pictures, multimedia, etc. Memory 604 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.
[0177] Power supply component 606 provides power to various components of electronic device 600. Power supply component 606 may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to electronic device 600.
[0178] Multimedia component 608 includes a screen that provides an output interface between the electronic device 600 and the user. In some embodiments, the screen may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touchscreen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touches, swipes, and gestures on the touch panel. The touch sensors may sense not only the boundaries of touch or swipe actions but also the duration and pressure associated with the touch or swipe operation. In some embodiments, multimedia component 608 includes a front-facing camera and / or a rear-facing camera. When the electronic device 600 is in an operating mode, such as a shooting mode or a multimedia mode, the front-facing camera and / or the rear-facing camera may receive external multimedia data. Each front-facing camera and rear-facing camera may be a fixed optical lens system or have focal length and optical zoom capabilities.
[0179] Audio component 610 is used to output and / or input audio signals. For example, audio component 610 includes a microphone (MIC) used to receive external audio signals when electronic device 600 is in an operating mode, such as call mode, recording mode, and voice recognition mode. The received audio signals may be further stored in memory 604 or transmitted via communication component 616. In some embodiments, audio component 610 also includes a speaker for outputting audio signals.
[0180] I / O interface 612 provides an interface between processing component 602 and peripheral interface modules, such as keyboards, click wheels, buttons, etc. These buttons may include, but are not limited to, home buttons, volume buttons, power buttons, and lock buttons.
[0181] Sensor assembly 614 includes one or more sensors for providing state assessments of various aspects of electronic device 600. For example, sensor assembly 614 can detect the on / off state of electronic device 600, the relative positioning of components such as the display and keypad of electronic device 600, changes in position of electronic device 600 or a component of electronic device 600, the presence or absence of user contact with electronic device 600, orientation or acceleration / deceleration of electronic device 600, and temperature changes of electronic device 600. Sensor assembly 614 may include a proximity sensor configured to detect the presence of nearby objects without any physical contact. Sensor assembly 614 may also include a light sensor, such as a CMOS or CCD image sensor, for use in imaging applications. In some embodiments, sensor assembly 614 may also include an accelerometer, gyroscope, magnetometer, pressure sensor, or temperature sensor.
[0182] Communication component 616 facilitates wired or wireless communication between electronic device 600 and other devices. Electronic device 600 can access wireless networks based on communication standards, such as WiFi, carrier networks (such as 2G, 3G, 4G, or 5G), or combinations thereof. In one exemplary embodiment, communication component 616 receives broadcast signals or broadcast-related information from an external broadcast management system via a broadcast channel. In one exemplary embodiment, communication component 616 also includes a near-field communication (NFC) module to facilitate short-range communication. For example, the NFC module may be implemented based on radio frequency identification (RFID) technology, Infrared Data Association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology, and other technologies.
[0183] In an exemplary embodiment, the electronic device 600 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field-programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components to implement a data object initialization method provided in the embodiments of this application.
[0184] In an exemplary embodiment, a non-transitory computer-readable storage medium including instructions is also provided, such as a memory 604 including instructions, which can be executed by a processor 620 of an electronic device 600 to perform the above-described method. For example, the non-transitory storage medium may be a ROM, random access memory (RAM), CD-ROM, magnetic tape, floppy disk, and optical data storage device, etc.
[0185] Figure 10This is a block diagram illustrating an electronic device 700 according to an exemplary embodiment. For example, the electronic device 700 may be provided as a server. (Refer to...) Figure 10 The electronic device 700 includes a processing component 722, which further includes one or more processors, and memory resources represented by a memory 732 for storing instructions, such as application programs, that can be executed by the processing component 722. The application programs stored in the memory 732 may include one or more modules, each corresponding to a set of instructions. Furthermore, the processing component 722 is configured to execute instructions to perform a data object initialization method provided in embodiments of this application.
[0186] Electronic device 700 may also include a power supply component 726 configured to perform power management of electronic device 700, a wired or wireless network interface 750 configured to connect electronic device 700 to a network, and an input / output (I / O) interface 758. Electronic device 700 may operate on an operating system stored in memory 732, such as Windows Server™, Mac OS X™, Unix™, Linux™, FreeBSD™, or similar.
[0187] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.
Claims
1. A method for initializing a data object, characterized in that, The method includes: Obtain the preset object constructor in the virtual machine; the object constructor is used to initialize the member variables in the data object when the data object is created; Based on the characteristic type of the object constructor, in response to the single call to the object pointer by the object constructor, the address information corresponding to each member variable is determined by traversing the member variables in the data object; wherein, the data object includes at least two member variables, and the object pointer is used to access the member variables in the data object; Based on the object constructor and the address information corresponding to each member variable, each member variable is initialized.
2. The method according to claim 1, characterized in that, The step of determining the address information corresponding to each member variable includes: Establish the initialization list for the data object; Determine the address offset of each of the traversed member variables relative to the data object, and save the address offset corresponding to each member variable to the initialization list; In response to the traversal operation of the initialization list, the object pointer is loaded, and the address information is determined sequentially based on the address offset and the value of the object pointer. The address information is used to characterize the actual storage address of the member variable.
3. The method according to claim 2, characterized in that, The initialization list includes a first node for storing pointers to the objects; The process of loading the object pointer and determining the address information sequentially based on the address offset and the value of the object pointer includes: Obtain the object pointer from the first node; During the traversal of the member variables, the sum of the value of the object pointer and the address offset corresponding to each member variable is used as the address information of the member variable.
4. The method according to claim 3, characterized in that, The initialization list further includes a second node for storing a member linked list, the member linked list being used to represent the chained connection relationship of the member variables; the method further includes: During the traversal, for each member variable being traversed, according to the second node, the member variables are appended to the tail of the member linked list, and the sum of the value of the object pointer already stored in the first node and the address offset corresponding to each member variable is used as the address information of the member variable.
5. The method according to claim 1, characterized in that, The initialization operation for each member variable includes: The variable type of the member variable is determined based on its type identifier. Based on the variable type, obtain the initial variable value of the member variable; According to the address information, the initial variable values of the member variables are stored in the storage space corresponding to the address information.
6. The method according to claim 1, characterized in that, The method further includes: Obtain the bytecode in the constructor of the object, the bytecode being used to obtain machine instructions for execution by an electronic device through translation; If the bytecode contains at least one of the member variables, the characteristic type of the object constructor is determined to be a non-empty function; If the feature type is a non-empty function, proceed to the step of responding to the single call to the object pointer in response to the object constructor.
7. The method according to claim 6, characterized in that, The method further includes: If the bytecode contains only a return instruction, the characteristic type of the object constructor is determined to be an empty function; If it is determined that the characteristic type of the object constructor is an empty function, the call operation to the object constructor is replaced with an empty operation.
8. The method according to claim 1, characterized in that, The method further includes: When the number of member variables is less than 2, the address information of a single member variable in the data object is obtained by calling the object pointer once according to the object constructor, and the single member variable is initialized according to the object constructor and the address information of the single member variable.
9. The method according to claim 1, characterized in that, Before obtaining the object constructor, the method also includes: Request storage space from the system to initialize the data object; Based on the object constructor and the address information of each member variable, an initialization operation is performed on each member variable, including: Based on the object constructor and the address information of each member variable, each member variable is initialized in the storage space.
10. A data object initialization device, characterized in that, The device includes: The acquisition module is used to acquire a preset object constructor in the virtual machine. The object constructor is used to initialize the member variables in the data object when the data object is created. The traversal module is used to determine the address information corresponding to each member variable by traversing the member variables in the data object, based on the characteristic type of the object constructor and in response to the single call to the object pointer by the object constructor; wherein, the data object includes at least two member variables, and the object pointer is used to access the member variables in the data object; The initialization execution module is used to perform initialization operations on each member variable according to the object constructor and the address information corresponding to each member variable.
11. An electronic device, characterized in that, include: processor; Memory used to store the processor's executable instructions; The processor is configured to execute the instructions to implement the method as described in any one of claims 1 to 9.
12. A storage medium, characterized in that, When the instructions in the storage medium are executed by the processor of the electronic device, the electronic device is able to perform the method as described in any one of claims 1 to 9.
Citation Information
Patent Citations
Inter-process communication method and system based on shared memory
CN112860458A
Memory processing method and device, equipment and medium
CN115952017A
