Memory allocation method and device, computer device, storage medium and program product
Patent Information
- Application Number
- CN202111145986.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-28
- Publication Date
- 2026-09-29
- Estimated Expiration
- 2041-09-28
AI Technical Summary
[0005]本申请提供了一种内存分配方法、装置、计算机设备、存储介质及程序产品,可以解决相关技术中实际分配效率较低的问题
虚拟机在基于内存分配指令为目标数据分配内存空间时,该内存分配指令包括边界指示信息,通过该边界指示信息确定待分配给目标数据的内存空间的边界对齐地址,该边界对齐地址是待分配的空间的边界对齐到内存的地址,以便基于该边界对齐地址和目标数据的存储空间待用量,为目标数据分配物理内存空间,使得所分配的物理内存空间的边界能够对齐到指定的边界对齐地址,降低内存访问的开销;通过从指令层面提供地址按需对齐的内存分配能力,提高了访问效率从而提升了虚拟机的系统性能,提高了实际内存分配效率。
Smart Images

Figure CN115878254B_ABST
Abstract
Description
Technical Field
[0001] This application relates to computer technology, cloud technology, transportation, and autonomous driving. It also relates to a memory allocation method, apparatus, computer equipment, storage medium, and program product. Background Technology
[0002] A virtual machine (VM) is a computer that is created on a physical machine. The VM simulates the execution of programs on the physical machine through an execution engine. Specifically, the VM first allocates memory space for the program so that the physical machine's CPU (Central Processing Unit) can execute it.
[0003] In related technologies, the memory allocation process may include: typically allocating memory space globally according to the default 8-byte alignment mode, where the 8-byte alignment mode means that the memory address is aligned to a starting address that is an integer multiple of 8; for example, the virtual machine allocates memory space for object a based on the object to be stored indicated by the memory allocation instruction, with the memory address being an integer multiple of 8 as the starting address.
[0004] The memory allocation described above requires global data to be uniformly aligned to 8 bytes. However, for a 256-bit (32-byte) object b or a 512-bit (64-byte) object c, aligning both to 8 bytes could significantly increase memory access overhead, leading to a decrease in system performance. Therefore, the actual allocation efficiency of the memory allocation process described above is relatively low. Summary of the Invention
[0005] This application provides a memory allocation method, apparatus, computer device, storage medium, and program product, which can solve the problem of low actual allocation efficiency in related technologies. The technical solution is as follows: On the one hand, a memory allocation method is provided, the method comprising: In response to a virtual machine memory allocation instruction, the amount of storage space to be used for the target data is determined, wherein the memory allocation instruction is used to indicate the allocation of memory space for the target data of the program source code; Based on the boundary indication information included in the memory allocation instruction, the boundary alignment address of the memory space to be allocated to the target data is determined, wherein the boundary alignment address refers to the address of the memory space to be allocated to the target data whose boundary is aligned to the memory. Based on the available storage space for the target data and the boundary alignment address, allocate physical memory space for the target data.
[0006] On the other hand, a memory allocation device is provided, the device comprising: The space availability determination module is used to determine the amount of storage space available for the target data in response to the memory allocation instruction of the virtual machine. The memory allocation instruction is used to indicate the allocation of memory space for the target data of the program source code. A boundary determination module is used to determine the boundary alignment address of the memory space to be allocated to the target data based on the boundary indication information included in the memory allocation instruction. The boundary alignment address refers to the address of the memory space to be allocated to the target data whose boundary is aligned to the memory address. The memory allocation module is used to allocate physical memory space for the target data based on the available storage space of the target data and the boundary alignment address.
[0007] In one possible implementation, the boundary determination module includes: The extraction unit is configured to extract the boundary indication information from the first target location of the memory allocation instruction based on the target instruction type of the memory allocation instruction; The determining unit is used to determine the boundary alignment address corresponding to the boundary indication information based on the target mapping relationship and the boundary indication information, wherein the target mapping relationship refers to the mapping rule between the boundary indication information and the boundary alignment address.
[0008] In one possible implementation, the determining unit is used for any of the following: Based on the enumerated mapping relationship between boundary indication information and boundary alignment address, the boundary alignment address corresponding to the boundary indication information is determined; Based on the linear mapping relationship between boundary indication information and boundary alignment address, the product of the target encoding value of the boundary indication information and the target coefficient is determined as the boundary alignment address; Based on the exponential mapping relationship between boundary indication information and boundary alignment address, the function value with the target value as the base and the target encoded value as the exponent is determined as the boundary alignment address.
[0009] In one possible implementation, the boundary indication information is encoded data, and the enumeration mapping relationship includes a boundary alignment address associated with the first encoded data and a reserved address associated with the second encoded data, wherein the reserved address refers to a pre-reserved address to be configured.
[0010] In one possible implementation, the extraction unit is configured to extract the boundary indication information from the fourth byte of a first instruction in response to the target instruction type being an alignment allocation object, wherein the first instruction is used to indicate the memory space of the alignment allocation target object.
[0011] In one possible implementation, the extraction unit is configured to extract the boundary indication information from the third byte of a second instruction in response to the target instruction type being an aligned allocation basic array, the second instruction being used to indicate the memory space of the aligned allocation target basic array, the target basic array including data of basic data types.
[0012] In one possible implementation, the extraction unit is configured to extract the boundary indication information from the fourth byte of a third instruction in response to the target instruction type being an aligned allocation object array, the third instruction being used to indicate the memory space of the aligned allocation target object array, the target object array including object type data.
[0013] In one possible implementation, the device further includes: A vectorization processing module is used to, in response to the detection of program source code to be executed, obtain the vector length used in the vectorization processing based on the vectorization processing process of the program source code; wherein, the boundary alignment address is an integer multiple of the vector length, the vectorization processing process refers to the process of converting the program source code into vector instructions, the vector length refers to the data length corresponding to the vector instruction, and the vector instruction refers to the instruction that the physical machine of the virtual machine supports for processing multiple data simultaneously. A generation module is used to generate the memory allocation instruction based on the vector length and the target data of the program source code, wherein the target data includes data to be accessed when executing the program source code.
[0014] In one possible implementation, the generation module is configured to determine a target instruction type based on the data type of the target data; determine boundary indication information based on the vector length; store the boundary indication information in a first target location of an empty instruction based on the target instruction type; and store the data length indication information of the target data in a second target location of the empty instruction to obtain the memory allocation instruction.
[0015] In one possible implementation, the space availability determination module is used to decode the opcode of the memory allocation instruction to obtain the target instruction type of the memory allocation instruction; extract the data length indication information of the target data from the second target location of the memory allocation instruction according to the target instruction type; and determine the storage space availability of the target data according to the data length indicated by the data length indication information of the target data.
[0016] In one possible implementation, the space availability determination module is used for: In response to the target data being a target object, based on the constant pool index of the target object, the first data length corresponding to the target object is found from the constant pool of the virtual machine, and the first data length is determined as the amount of storage space to be used; In response to the target data being a target basic array, based on the basic data type of the data included in the target basic array, the basic data length corresponding to the basic data type of the data included in the target basic array is found from the virtual machine's basic type table, and based on the data included in the target basic array and the basic data length, the second data length of the target basic array is determined, and the second data length is determined as the storage space to be used. In response to the target data being a target object array, based on the constant pool index of the objects included in the target object array, the third data length corresponding to the objects included in the target object array is found from the constant pool of the virtual machine, and the third data length is determined as the amount of storage space to be used.
[0017] On the other hand, a computer device is provided, including a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the memory allocation method described above.
[0018] On the other hand, a computer-readable storage medium is provided on which a computer program is stored, which, when executed by a processor, implements the above-described memory allocation method.
[0019] On the other hand, a computer program product is provided, including a computer program that, when executed by a processor, implements the memory allocation method described above.
[0020] The beneficial effects of the technical solution provided in this application are: When a virtual machine allocates memory space for target data based on memory allocation instructions, these instructions include boundary indication information. This boundary indication information determines the boundary alignment address of the memory space to be allocated to the target data. This boundary alignment address is the address on which the boundary of the space to be allocated is aligned to memory. Based on this boundary alignment address and the available storage space of the target data, physical memory space is allocated to the target data, ensuring that the boundary of the allocated physical memory space is aligned to the specified boundary alignment address, thus reducing memory access overhead. By providing on-demand address alignment for memory allocation at the instruction level, access efficiency is improved, thereby enhancing the system performance of the virtual machine and increasing actual memory allocation efficiency. Attached Figure Description
[0021] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments of this application will be briefly introduced below.
[0022] Figure 1 A schematic diagram illustrating the implementation environment of a memory allocation method provided in this application embodiment; Figure 2 A flowchart illustrating a memory allocation method provided in an embodiment of this application; Figure 3 A schematic diagram of a memory allocation instruction provided in an embodiment of this application; Figure 4 This application provides a schematic diagram of a memory allocation process as an embodiment of the present application. Figure 5 This is a schematic diagram of the structure of a memory allocation device provided in an embodiment of this application; Figure 6 This is a schematic diagram of the structure of a computer device provided in an embodiment of this application. Detailed Implementation
[0023] The embodiments of this application are described below with reference to the accompanying drawings. It should be understood that the embodiments described below with reference to the accompanying drawings are exemplary descriptions for explaining the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions of the embodiments of this application.
[0024] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the terms “comprising” and “including” as used in embodiments of this application mean that the corresponding feature can be implemented as the presented feature, information, data, step, operation, element, and / or component, but do not exclude implementation as other features, information, data, step, operation, element, component, and / or combinations thereof supported by the art. It should be understood that when we say that an element is “connected” or “coupled” to another element, the one element can be directly connected or coupled to the other element, or it can mean that the one element and the other element establish a connection relationship through an intermediate element. Furthermore, “connected” or “coupled” as used herein can include wireless connection or wireless coupling. The term “and / or” as used herein indicates at least one of the items defined by the term; for example, “A and / or B” indicates implementation as “A,” or implementation as “A,” or implementation as “A and B.”
[0025] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings.
[0026] The memory allocation method provided in this application relates to cloud computing technology. For example, cloud computing technology can be used to allocate physical memory space for virtual machines included in a cloud computing resource pool. For instance, virtual resources deployed in the resource pool, such as the virtual machine's interpreter, can be used to decode and execute memory allocation instructions in the virtual machine's method area to allocate physical memory space with boundaries aligned to specified memory addresses for target data.
[0027] Cloud computing is a computing model that distributes computing tasks across a large pool of computers, enabling various application systems to access computing power, storage space, and information services as needed. The network providing these resources is called the "cloud." From the user's perspective, resources in the "cloud" appear infinitely scalable, readily available, on-demand, and expandable, with payment based on usage.
[0028] As a provider of fundamental cloud computing capabilities, a cloud resource pool (referred to as a cloud platform, generally called an IaaS (Infrastructure as a Service) platform) is established. Various types of virtual resources are deployed in the resource pool for external customers to choose from. The cloud resource pool mainly includes: computing devices (virtualized machines containing operating systems), storage devices, and network devices.
[0029] Based on logical function, a PaaS (Platform as a Service) layer can be deployed on top of the IaaS (Infrastructure as a Service) layer, and a SaaS (Software as a Service) layer can be deployed on top of the PaaS layer. Alternatively, SaaS can be deployed directly on top of IaaS. PaaS is a platform for running software, such as databases and web containers. SaaS refers to various types of business software, such as web portals and bulk SMS senders. Generally speaking, SaaS and PaaS are upper layers compared to IaaS.
[0030] The applicant, through research, discovered that in related technologies, memory allocation can only be performed according to a globally unified alignment mode. That is, objects a and b are either both aligned to 8 bytes or both to 64 bytes, without granular control over alignment at the individual object level. Through inventive research, the applicant found that the drawback of these technologies is the inability to specify address alignment attributes during memory allocation. For example, most Java Virtual Machines default to a globally unified 8-byte alignment mode for memory allocation. During program runtime, some data in 8-byte alignment mode may cause significant performance issues. For instance, for a 256-bit vector object, if the vector object is 8-byte aligned but not 32-byte aligned, it increases the overhead of vector memory access, leading to a decrease in program performance. Similarly, for a 512-bit vector, if it is not 64-byte aligned, the memory access overhead of the vector will also increase significantly, thus affecting performance. Through inventive research, the applicant further explored the following technical problem: Is it possible to adjust the globally unified alignment mode of objects to 64-byte alignment? Further investigation by the applicant revealed that while this method can ensure that 128, 256, and 512-bit vectors are all memory-aligned, it wastes a significant amount of memory, leading to memory explosion. For example, considering objects a and b, assuming that both a and b are 16 bytes in size, under 8-byte alignment, objects a and b only require 32 bytes of memory. However, if the default alignment is set to 64-byte alignment, objects a and b will require a total of 128 bytes of memory, which is 4 times the original 128 / 32.
[0031] Through extensive creative work and research by the applicant, it was found that the Java Virtual Machine of the relevant technology cannot achieve memory allocation with address alignment on demand, which increases the overhead of memory access, resulting in excessive memory consumption and a decline in system performance. The actual allocation efficiency is low, which affects the performance of the virtual machine.
[0032] Therefore, the applicant further exerted creative effort to obtain the memory allocation method of this application. By designing new virtual machine instructions, it is possible to align memory allocation on demand, thereby improving the performance of the virtual machine.
[0033] It should be noted that the on-demand alignment of memory allocation in this application means that the boundary of the allocated memory space (e.g., the starting address) corresponds to a specified address, that is, the boundary of the space to be allocated is aligned to a specified address in memory. For example, the starting address can be an integer multiple of the data length. For example, when allocating memory space for 512 bits (64 bytes) of data, the starting address must be 64, 128, 256 (an integer multiple of 64), etc.
[0034] Figure 1 This is a schematic diagram illustrating the implementation environment of a memory allocation method provided in this application. For example... Figure 1As shown, the implementation environment includes a virtual machine, which corresponds to an interpreter. When the virtual machine contains program source code that needs to be executed, the interpreter allocates memory space for the target data of the program source code. In this application, the virtual machine can allocate physical memory space for the target data according to boundary-aligned addresses through the interpreter, thereby reducing the overhead of memory access. The target data may include data to be accessed when executing the program source code, such as arrays, objects, etc.
[0035] In one possible scenario, the implementation environment may also include the physical machine where the virtual machine resides, which is configured with physical memory, a processor, etc. For example... Figure 1 As shown, multiple virtual machines, such as Virtual Machine 1, Virtual Machine 2, ..., Virtual Machine n, can be virtualized on this physical machine. Each virtual machine can be configured with an interpreter. The interpreter can obtain memory allocation instructions from the virtual machine's method area and allocate memory space using the boundary alignment address indicated by the boundary indication information included in the memory allocation instructions. For example, the virtual machine's compiler can vectorize the program source code to convert it into corresponding vector instructions. A vector instruction is a single instruction capable of processing multiple data simultaneously. The processor can simultaneously process the multiple data included in the vector instruction, thereby implementing the corresponding operation of the program source code. For example, the virtual machine can be a Java Virtual Machine, and the compiler can be a Java just-in-time compiler; of course, the virtual machine can also be an Android Virtual Machine, and the corresponding compiler can be an Android Virtual Machine compiler. This application embodiment does not specifically limit this. In this application, the vector length used during vectorization can be used to allocate memory space whose alignment address is an integer multiple of the vector length to the target data. That is, the boundary address of the memory space to be allocated to the target data is an integer multiple of the vector length; for example, if the vector length is 64 bytes, the starting address of the memory space allocated to the target data can be 64, 128, 256, etc.
[0036] In one possible scenario, the physical machine can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server or server cluster providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN (Content Delivery Network), and big data and artificial intelligence platforms. The server can establish a communication connection with the user's terminal to provide various service functions based on this connection. The terminal and server can be connected directly or indirectly through wired or wireless communication, but are not limited to these methods. The specific connection can be determined based on the actual application scenario requirements and is not limited here. In another possible scenario, the physical machine can also be the user's terminal. The aforementioned networks can include, but are not limited to, wired networks and wireless networks. Wired networks include local area networks (LANs), metropolitan area networks (MANs), and wide area networks (WANs). Wireless networks include Bluetooth, Wi-Fi, and other networks that enable wireless communication. The terminals involved in the above two scenarios include, but are not limited to: mobile phones (such as Android phones, iOS phones, etc.), computers, smart voice interaction devices, smart home appliances (such as smart TVs, smart refrigerators, smart speakers, smart robot vacuum cleaners, etc.), in-vehicle terminals (such as in-vehicle navigation terminals, in-vehicle computers, etc.), smartwatches, etc. The embodiments of this application can be applied to various scenarios, including but not limited to cloud technology, artificial intelligence, smart transportation, and assisted driving.
[0037] The virtual machine involved in this application and the applicant's concept are explained below: A virtual machine (VM) primarily uses software (or with minimal hardware support) to virtualize a computer. This virtual computer typically supports its own instruction set, called the virtual machine instruction set. The VM relies on the local physical machine and uses its execution engine to simulate the execution of a given sequence of virtual machine instructions (target program) on the local machine. The VM's execution engine can be implemented using an interpreter. Due to the high portability, low implementation cost, and small memory footprint of interpreters, most virtual machine execution engines include an interpreter. The interpreter performs instruction fetching, instruction dispatching, operand fetching, and execution on a per-instruction basis. However, simple interpretation execution has poor performance; therefore, a dynamic compilation system is introduced into the VM to improve its performance. The compiler is the core of the dynamic compilation system. The compiler compiles a set of consecutive virtual machine instruction sequences (basic blocks, functions, or methods) into a native CPU instruction sequence, called a native method, which is then directly executed by the hardware. Because compilers eliminate repetitive instruction fetching and dispatching processes, and perform numerous effective optimizations during translation, their performance can be more than 10 times higher than that of interpreters. Since compilers perform dynamic compilation during program execution, they are also called dynamic compilers.
[0038] Modern processors widely support vector instructions. Especially for computationally intensive applications such as big data and intelligent machine learning, program vectorization is crucial for improving system performance. Dynamic compilation occurs during program execution, allowing access to underlying platform and runtime information, making dynamic compilers well-suited for vectorization. Vectors typically come in various lengths, including 128-bit, 256-bit, and 512-bit. To improve the efficiency of vector operations, the virtual machine should align the memory allocation of vector objects to the vector length as much as possible. For example, a 256-bit vector is allocated with 32-byte alignment, and a 512-bit vector with 64-byte alignment.
[0039] The following is an introduction and explanation of the technical terms and related nouns used in this application: The target data of the program source code may include data that needs to be accessed when executing the program source code. The data type of the target data may include, but is not limited to: objects, basic arrays, and object arrays; among them, basic arrays include data of basic data types; and object arrays include data of object types.
[0040] Memory allocation instructions: used to instruct the allocation of memory space for target data in the program source code.
[0041] Boundary alignment address: The memory address to which the boundary of the memory space to be allocated to the target data is aligned. This boundary alignment address is an integer multiple of the vector length. This boundary alignment address can also be the start alignment address, that is, the start alignment address is an integer multiple of the vector length.
[0042] Storage space available: The amount of memory space to be allocated to the target data. This storage space available can be the length of the target data.
[0043] Vector instructions are single instructions capable of processing multiple data simultaneously. In this application, vector instructions refer to instructions that allow the virtual machine's physical machine to process multiple data simultaneously. The virtual machine can use a compiler to vectorize the program source code, thereby converting the program source code into vector instructions.
[0044] Vector length: The length of data that a vector instruction can process, representing the length of data (usually in bits) that the vector instruction can process simultaneously. Due to hardware limitations, processors typically only support a limited number of fixed-length vector instructions. For example, some x86 processors can process 256-bit or 512-bit data simultaneously.
[0045] Vectorization: The process of converting program source code into vector instructions is called vectorization. Conversion methods include manual conversion and compiler conversion, usually referring to conversion using a compiler.
[0046] Compiler: A dynamic compiler that performs dynamic compilation during the execution of program source code, such as a JIT (just-in-time) compiler.
[0047] Figure 2 This is a flowchart illustrating a memory allocation method provided in an embodiment of this application. The execution subject of this method can be a computer device, which can be a physical machine or a virtual machine virtualized on a physical machine. This embodiment uses a virtual machine as an example for illustration. Figure 2 As shown, the method includes the following steps.
[0048] Step 201: The virtual machine generates a memory allocation instruction and stores the memory allocation instruction in the method area.
[0049] This memory allocation instruction is used to instruct the allocation of memory space for target data in the program source code. This target data may include data to be accessed when the program source code is executed. When the virtual machine detects the program source code to be executed, it generates this memory allocation instruction based on the target data of the program source code. For example, the virtual machine can generate this memory allocation instruction through a compiler and store it in the virtual machine's method area.
[0050] In one possible implementation, when the program source code is executed, it needs to access multiple data. The virtual machine can use a compiler to vectorize the program source code. In this application, the vector length used in the vectorization process can be used to generate memory allocation instructions for allocating memory for the data to be accessed. Accordingly, this step may include the following steps 2011-2012.
[0051] Step 2011: In response to the detection of the program source code to be executed, the virtual machine obtains the vector length used in the vectorization process based on the vectorization process of the program source code.
[0052] A virtual machine can use a compiler to vectorize the source code of a program to be executed. For example, the virtual machine can combine information such as the processor's maximum data processing length, the data to be accessed when executing the source code, and the operation types of the source code to vectorize the source code. In this step, the virtual machine can obtain the vector length used for vectorization. This vector length refers to the data length of multiple data points that the generated vector instructions can support processing simultaneously; this vector length does not exceed the processor's maximum data processing length. For example, the maximum data processing length refers to the maximum data length that the processor can support processing simultaneously. Due to hardware limitations, processors typically support several fixed and finite-length vector instructions; for example, some x86 processors can process 256-bit or 512-bit data simultaneously. For example, the vector instruction can be a SIMD (Single Instruction Multiple Data) instruction. The operation types of the source code include arithmetic and logical operations, such as addition and subtraction.
[0053] Step 2012: The virtual machine generates the memory allocation instruction based on the vector length and the target data of the program source code. The target data includes the data to be accessed when executing the program source code.
[0054] The virtual machine can generate memory space instructions based on the data type of the target data and the boundary alignment address to which the target data needs to be aligned to memory. In one possible example, step 2012 may include the following steps S1-S3.
[0055] Step S1: The virtual machine determines the target instruction type based on the data type of the target data.
[0056] Virtual machines can allocate memory space for data of multiple data types using various types of memory allocation instructions. In this application, the virtual machine can generate memory allocation instructions of a target instruction type corresponding to the data type of the target data.
[0057] For example, the data type of the target data may include, but is not limited to: object, basic array, and object array; the corresponding target instruction type may include, but is not limited to: aligned allocation object, aligned allocation basic array, and aligned allocation object array.
[0058] It should be noted that the target data can be data accessed during the execution of the program's source code. In a possible example, for a program source code that performs cumulative execution, for instance, a basic array M containing 1000 integer data points, and the summation of these 1000 data points is performed. Each integer data point has a length of 4 bytes, and the CPU supports a maximum data size of 32 bytes that can be processed simultaneously. Therefore, the SIMD instructions to be generated by the compiler can handle 8 integer data points simultaneously, with a vector length of 32 bytes. For example, the CPU can simultaneously process the summation of the first to eighth integer data points (8 integer data points, totaling 32 bytes). When allocating memory space for the basic array M containing 1000 integer data points, the target instruction type corresponding to the basic array M can be an aligned allocation basic array.
[0059] Step S2: The virtual machine determines the boundary indication information based on the vector length.
[0060] The boundary indication information is used to indicate the boundary alignment address of the memory space to be allocated to the target data. The boundary alignment address refers to the memory address to which the boundary of the memory space to be allocated to the target data is aligned. In this application, the boundary can be aligned to a memory address that is an integer multiple of the vector length. That is, the boundary alignment address can be an integer multiple of the vector length.
[0061] In one possible example, the virtual machine can align the boundary address of the memory space to be allocated to the target data to a memory address that is an integer multiple of the vector length. In this step, the virtual machine can determine the boundary alignment address of the target data as a value that is an integer multiple of the vector length. For example, the boundary alignment address can be the memory address that the starting boundary of the memory space to be allocated to the target data needs to be aligned to, that is, the boundary alignment address can be the starting alignment address; then the virtual machine can use a memory address that is an integer multiple of the vector length in the memory space as the starting alignment address. For example, if the vector length is 512 bits (64 bytes), the corresponding starting alignment address can be 64 or an integer multiple of 64 (64, 128, etc.), that is, allocate memory space with a starting alignment address of 64, 128, etc.; if the vector length is 128 bits (16 bytes), the corresponding starting alignment address can be 16, 32, 48, etc.
[0062] In one possible example, the virtual machine may have a pre-configured target mapping relationship. Based on the boundary alignment address, the virtual machine retrieves the boundary indication information corresponding to the boundary alignment address from the target mapping relationship. The target mapping relationship includes the association between the boundary alignment address and the boundary indication information.
[0063] In one possible example, during vectorization, the virtual machine can configure the vector length parameter to a specified vector length by default. For example, the virtual machine can configure the vector length by default to the maximum data length that the physical machine's processor can process simultaneously; alternatively, it can configure the vector length parameter based on a user-selected value. In this step, the virtual machine obtains the system's default configured vector length or the vector length selected by the user based on the vectorization process.
[0064] Step S3: The virtual machine can, based on the target instruction type, store the boundary indication information in the first target location of the null instruction, and store the data length indication information of the target data in the second target location of the null instruction to obtain the memory allocation instruction.
[0065] For example, the first target location refers to the location in the memory allocation instructions of each instruction type that applies to the storage boundary indication information. The data length indication information is used to indicate the data length of the target data, that is, the size of the storage space required by the target data. For example, when allocating memory space for a basic array M containing 1000 int data, the storage space required by the basic array M is 4000 bytes. The second target location refers to the location in the memory allocation instructions of each instruction type that applies to the storage data length indication information. In a possible example, the virtual machine can also obtain the opcode corresponding to the target instruction type and store the opcode in the third target location of the null instruction. The opcode is used to indicate the target instruction type of the memory allocation instruction, that is, the opcode indicates which type of data the memory allocation instruction is used to allocate memory for.
[0066] Based on the possible data types and corresponding target instruction types in step S1, the memory allocation instruction in this step may include, but is not limited to, a first instruction, a second instruction, and a third instruction. The first instruction is used to instruct the allocation of memory space for the target object; the second instruction is used to instruct the allocation of memory space for the target basic array; and the third instruction is used to instruct the allocation of memory space for the target object array. For example, the first instruction may be a `new-aligned` (object-aligned allocation) instruction, the second instruction may be a `newarray-aligned` (basic data type array-aligned allocation) instruction, and the third instruction may be a `anewarray-aligned` (object type array-aligned allocation) instruction.
[0067] For the first instruction, the virtual machine can store the corresponding opcode in the first byte of the first empty instruction; the virtual machine can store the data length indication information of the target object in the second and third bytes of the first empty instruction, which is the second target position of the first instruction; the virtual machine can store the boundary indication information of the target object in the fourth byte of the first empty instruction, which is the first target position of the first instruction; thus obtaining the first instruction. In one possible example, the data length indication information of the first instruction can be the first constant pool index of the target object. This first constant pool index is used to index the target object in the constant pool to find the data length corresponding to the target object.
[0068] For the second instruction, the virtual machine can store the corresponding opcode in the first byte of the second empty instruction; store the data type of the target basic array in the second byte of the second empty instruction, which is the second target location of the second instruction; and store the boundary indication information of the target basic array in the third byte of the second empty instruction, which is the first target location of the second instruction. In one possible example, the target basic array can include data of any basic data type, and the virtual machine can store the atype (basic data type) value corresponding to the data included in the target basic array in the second byte. This atype value is used to indicate the basic data type of the data included in the target basic array. For the third instruction, the virtual machine can store the corresponding opcode in the first byte of the second null instruction; store the data length indication information of the target object array in the second and third bytes of the second null instruction, which is the second target location of the third instruction; the virtual machine can store the boundary indication information of the target object array in the fourth byte, which is the first target location of the third instruction. In one possible example, the data length indication information of the target object array can be a second constant pool index. The target object array includes multiple objects, each with corresponding attribute data. The virtual machine can store the second constant pool indexes of each object included in the target object array in the second and third bytes. The second constant pool index is used to index the objects included in the target object array in the constant pool to find the data length corresponding to the included objects.
[0069] Figure 3 The internal data structures for the three instructions mentioned above are provided, including the `new-aligned` instruction, the `newarray-aligned` instruction, and the `anewarray-aligned` instruction. For example... Figure 3As shown, for the `new-aligned` instruction: it is used to align the memory space allocated for an object; the instruction length of `new-aligned` is four bytes; where the first byte (`new-aligned`) represents the opcode of the `new-aligned` instruction; the second byte (`index-byte1`) and the third byte (`index-byte2`) together represent the constant pool index; the fourth byte (`alignment`) represents boundary indication information. For the `newarray-aligned` instruction: it is used to align the memory space allocated for a basic array, which can include multiple data of basic data types. This instruction is three bytes long; the first byte (`newarray-aligned`) represents the opcode, the second byte (`atype`) represents the basic data type of the data included in the basic array, and the third byte (`alignment`) represents boundary indication information. The `anewarray-aligned` instruction is used to align the memory space of an array of objects, for example, to align an array that is not allocated to the eight primitive data types. The instruction is four bytes long. The first byte (`anewarray-aligned`) represents the opcode of the instruction. The second byte (`index-byte1`) and the third byte (`index-byte2`) together represent the constant pool index. The fourth byte (`alignment`) represents boundary indication information.
[0070] It should be noted that the above steps 2011-2012 are only introduced using vector instructions as an example. Of course, the memory allocation method of this application is also applicable to memory allocation of other non-vector instructions. For example, for data to be stored indicated by scalar instructions, physical memory space can also be allocated based on boundary-aligned addresses and available storage space in accordance with the memory allocation method of this application, so as to align the memory addresses of the corresponding data of scalar instructions in memory, thereby optimizing data access efficiency and reducing memory access overhead.
[0071] In another possible implementation, the boundary alignment address can be a pre-configured configuration boundary address; for example, pre-configured according to 32 bytes, 64 bytes, 128 bytes, etc., so that subsequent memory allocation is aligned to 32, 64, or 128 bytes. For example, the virtual machine can also pre-store the configuration boundary address of the target data. In this step, the virtual machine can obtain the configuration boundary address and use it as the boundary alignment address. Based on the boundary alignment address, it can retrieve the boundary indication information corresponding to the boundary alignment address from the target mapping relationship. Of course, the virtual machine can pre-configure the configuration indication information of the data, and directly obtain the configuration indication information of the target data as the boundary indication information.
[0072] Step 202: The virtual machine obtains memory allocation instructions from the method area.
[0073] For example, the virtual machine's method area can be used to store the virtual machine's instruction set, and the memory allocation instructions can be pre-generated and stored in the virtual machine's method area. In this step, the virtual machine can use the interpreter to execute the step of retrieving the memory allocation instructions to be executed from the method area. For example, the memory allocation instructions can be bytecode instructions, where bytecode is the intermediate code between the program source code and the CPU-executable machine code.
[0074] Step 203: The virtual machine determines the amount of storage space to be used for the target data based on memory allocation instructions.
[0075] This memory allocation instruction is used to instruct the allocation of memory space for target data in the program source code. The virtual machine can obtain the amount of storage space to be used corresponding to the target data based on the data length indication information included in the memory allocation instruction. In one possible implementation, the virtual machine can obtain the data type from the memory allocation instruction and calculate the size of the memory space required by the target data. For example, step 203 can be implemented through the following steps 2031-2033.
[0076] Step 2031: The virtual machine can decode the opcode of the memory allocation instruction to obtain the target instruction type of the memory allocation instruction.
[0077] The opcode indicates the target instruction type of the memory allocation instruction. For example, the opcode can represent the nature of the operation to be performed by the memory allocation instruction, i.e., what operation to perform. For instance, the virtual machine can decode the opcode using an interpreter to obtain the target instruction type. For instance, the virtual machine can extract the opcode from the first byte of the memory allocation instruction.
[0078] Step 2032: The virtual machine can extract the data length indication information of the target data from the second target location of the memory allocation instruction according to the target instruction type.
[0079] When the target instruction type is an aligned allocation object, for the first instruction, the virtual machine can extract the data length indication information from the second and third bytes of the first instruction; for example, the virtual machine can extract the first constant pool index from the second and third bytes of the first instruction. Figure 3 As shown, the first instruction is a new-aligned instruction, which allows the virtual machine to extract the index value from the second and third bytes for traversing the target object and its attribute data.
[0080] When the target instruction type is an aligned allocation basic array, for the second instruction, the virtual machine can extract the data length indication information from the second byte of the second instruction. For example, the virtual machine extracts the atype value from the second byte of the second instruction. Figure 3 As shown, the second instruction can be a newarray-aligned instruction, which allows the virtual machine to extract the atype value of the data included in the array from the second byte.
[0081] When the target instruction type is an aligned allocation object array, for the third instruction, the virtual machine can extract the data length indication information from the second and third bytes of the third instruction; for example, the virtual machine can extract the second constant pool index from the second and third bytes of the third instruction. Figure 3 As shown, the third instruction is the anewarray-aligned instruction, which allows the virtual machine to extract the index value from the second and third bytes, and use it to traverse the objects and their attribute data in the object array.
[0082] Step 2033: The virtual machine can determine the amount of storage space to be used for the target data based on the data length indicated by the data length indication information of the target data.
[0083] The available storage space refers to the size of the memory space to be allocated to the target data, which is not less than the length of the target data. The virtual machine can determine the length of the target data based on the data length indication information and determine the available storage space that is not less than that data length. For example, the data length can be used as the available storage space; in this case, the virtual machine determines the data length indicated by the data length indication information as the available storage space. For example, based on several different forms of data length indication information, the steps by which the virtual machine determines the available storage space based on the data type can include the following three cases: In the first case, in response to the target data being a target object, the virtual machine can look up the first data length corresponding to the target object from the virtual machine's constant pool based on the constant pool index of the target object, and determine the first data length as the amount of storage space to be used.
[0084] The constant pool can include the attribute data of objects. The virtual machine, through the interpreter, traverses the constant pool for the target object based on the first constant pool index and finds the corresponding first data length. This first data length can be the size of the storage space required for the target object. For example, if the target object is a user, and the target object has attribute data such as the user's age and height, then the target object could be User A: Age 20, Height 180. In this case, the first data length including the age and height attribute data can be used as the available storage space.
[0085] In the second scenario, in response to the target data being a target basic array, the virtual machine can look up the basic data length corresponding to the basic data type of the data included in the target basic array from the virtual machine's basic type table, and determine the second data length of the target basic array based on the data included in the target basic array and the basic data length, and determine the second data length as the amount of storage space to be used.
[0086] When the target basic array contains a single data element, the length of the basic data element corresponding to that data's basic data type is used as the second data length. When the target basic array contains multiple data elements, the virtual machine can look up the corresponding basic data length from the virtual machine's basic type table based on the atype values of the data elements included in the target basic array. It then calculates the product of the number of data elements in the target basic array and the corresponding basic data length of the atype values, and uses this product as the second data length, thus obtaining the second data length of the target basic array. This second data length can be the size of the storage space required for the target basic array. The basic type table includes various basic data types and their corresponding data lengths. For example, if the target basic array contains 100 int (integer) data elements, and the storage space size for each int data element can be 4 bytes, then the second data length can be 400 bytes.
[0087] The data included in a basic array can belong to any basic data type. For example, the basic data types can include, but are not limited to, the following eight: byte, short, int, long, float, double, boolean, and char. The basic type table stores the length of each basic data type, that is, the amount of storage space required. For example, the storage space for byte can be 8 bits; short can be 16 bits; int can be 32 bits; long can be 64 bits; float can be 32 bits; double can be 64 bits; boolean can be 1 byte (8 bits); and char can be 2 bytes (16 bits).
[0088] In the third case, in response to the target data being an array of target objects, the virtual machine can look up the third data length corresponding to the objects included in the target object array from the virtual machine's constant pool based on the constant pool index of the objects included in the target object array, and determine the third data length as the amount of storage space to be used.
[0089] The target data can be an array of classes (object arrays), containing one or more objects. The constant pool can include the objects included in the target object array. When the target object array contains one object, the virtual machine, through the interpreter, traverses the objects included in the target object array in the constant pool according to the second constant pool index, finds the data length of the object, and uses the data length of the object as the third data length, which is the amount of storage space to be used for the target object array. When the target object array contains multiple objects, the virtual machine, through the interpreter, traverses the multiple objects in the constant pool according to the second constant index, finds the data length of the multiple objects, and uses the sum of the data lengths of the multiple objects as the third data length, which is the amount of storage space to be used for the target object array. Each object corresponds to attribute data; for example, the objects in the object array can be users, and the attribute data can be the user's height and age; for example, the target object array can be: User A: height 180, age 20; User B: height 178, age 18; User C: height 182, age 19. Then the length of the third data corresponding to the three users can be used as the size of the storage space required by the three users.
[0090] By obtaining the data length indication information, the data length of the target data can be calculated, thereby obtaining the size of the space to be allocated. Subsequently, space of the corresponding size can be allocated according to the data volume, improving the accuracy of memory allocation and improving resource utilization. Furthermore, based on the constant pool index and basic data types extracted from the second target location of the memory allocation instruction, the data length of the data can be further obtained using the constant pool or basic type table, thereby accurately locating the data volume of the data to be stored, and thus improving the accuracy of memory allocation.
[0091] It should be noted that steps 201-203 above can be one implementation of the step "determining the available storage space for the target data in response to the virtual machine's memory allocation instructions". Steps 201-203 are essentially the process of generating memory allocation instructions in real time, fetching instructions from the method area, and executing the memory allocation instructions in real time. In another possible implementation, the virtual machine can also pre-generate memory allocation instructions and store them in the method area. That is, the step "determining the available storage space for the target data in response to the virtual machine's memory allocation instructions" can be implemented through steps 202-203: the virtual machine can detect the instructions included in the method area in real time. When it detects that the method area includes memory allocation instructions, the virtual machine directly executes step 202 to obtain the memory allocation instructions from the method area, and determines the available storage space based on step 203. Of course, in another possible implementation, the virtual machine can also directly execute step 203, that is, the step "determining the amount of storage space to be used for the target data in response to the virtual machine's memory allocation instruction" can be implemented through step 203; since the virtual machine's interpreter has obtained the memory allocation instruction, the virtual machine in this application can directly start executing from the above-mentioned step 203. The method of this application embodiment does not specifically limit the process of steps 201-202.
[0092] Step 204: The virtual machine determines the boundary-aligned address of the memory space to be allocated to the target data based on the boundary indication information included in the memory allocation instruction.
[0093] The boundary alignment address refers to the address of the memory space to be allocated to the target data that is aligned to the boundary of the memory space. The virtual machine can extract the boundary indication information from the memory allocation instruction and determine the boundary alignment address corresponding to the boundary indication information based on the target mapping relationship between the boundary indication information and the boundary alignment address. In one possible implementation, the virtual machine can extract the boundary indication information based on the storage location of the boundary indication information corresponding to the instruction type, and then use the extracted information to obtain the boundary alignment address. Accordingly, step 204 can be implemented through the following steps 2041-2042.
[0094] Step 2041: The virtual machine extracts the boundary indication information from the first target location of the memory allocation instruction based on the target instruction type of the memory allocation instruction.
[0095] The virtual machine can extract boundary indication information from the first target location corresponding to the target instruction type based on the target instruction type. This boundary indication information can be represented by encoded data; for example, it can be single-byte (8 bits) binary encoded data, such as 0000000, 00000010, etc. Based on the several possible cases of the target instruction type involved in step 2012, this step can include at least the following three cases.
[0096] In the first scenario, in response to the target instruction type being an aligned allocation object, the first instruction is used to indicate the memory space of the aligned allocation target object, and the virtual machine extracts this boundary indication information from the fourth byte of the first instruction. For example... Figure 3 As shown, for the new-aligned instruction, the boundary indication information (alignment) can be extracted from the fourth byte.
[0097] In the second scenario, in response to the target instruction type being an aligned allocation of a basic array, the second instruction indicates the memory space of the aligned allocation target basic array, and the virtual machine extracts this boundary indication information from the third byte of the second instruction. For example... Figure 3 As shown, for the newarray-aligned instruction, boundary indication information (alignment) can be extracted from the third byte.
[0098] In the third case, in response to the target instruction type being an aligned allocation object array, the third instruction is used to indicate the memory space of the aligned allocation target object array, and the virtual machine extracts this boundary indication information from the fourth byte of the third instruction. For example... Figure 3 As shown, for the anewarray-aligned instruction, boundary indication information (alignment) can be extracted from the fourth byte.
[0099] Step 2042: The virtual machine determines the boundary alignment address corresponding to the boundary indication information based on the target mapping relationship and the boundary indication information.
[0100] The virtual machine can obtain the boundary alignment address corresponding to the boundary indication information from the target mapping relationship between the boundary indication information and the boundary alignment address. This target mapping relationship refers to the mapping rule between the boundary indication information and the boundary alignment address.
[0101] For example, the target mapping relationship can be an enumerated mapping, a linear mapping, or an exponential mapping, etc. Based on the several forms of the target mapping relationship, this step can be implemented in any of the following three ways.
[0102] The first method involves the virtual machine determining the boundary alignment address corresponding to the boundary indication information based on the enumerated mapping relationship between the boundary indication information and the boundary alignment address.
[0103] The boundary indication information is encoded data. The enumerated mapping relationship includes the boundary alignment address associated with the first encoded data and the reserved address associated with the second encoded data. The reserved address refers to a pre-reserved address to be configured. That is, in addition to the boundary alignment address corresponding to the first encoded data, the user can also use the second encoded data to customize the boundary address that needs to be aligned at any time according to the alignment requirements.
[0104] In one possible example, the single-byte boundary indication information alignment can be 8 bits of binary data, and the enumerated mapping relationship can be represented in the following form: 00000000: No additional alignment requirements, i.e., alignment is performed according to the virtual machine's default global alignment method; 00000001: Aligned to 4 bytes; 00000010: Aligned to 8 bytes; 00000100: Aligned to 16 bytes; 00001000: Aligned to 32 bytes; 00010000: Aligned to 64 bytes; 00100000: Aligned to 128 bytes; 01000000: Aligned to 256 bytes; 10000000: Custom alignment (reserved for implementing custom alignment requirements).
[0105] 10000000 can be the second encoded data, associated with the corresponding reserved address. All other binary encoded data besides 10000000 is the first encoded data.
[0106] Similarly, in step S3 of step 2012, if it is necessary to align data with a vector length of 512 bits, that is, a vector length of 64 bytes, then it is only necessary to set the alignment in the corresponding memory allocation instruction to 00010000. That is, the boundary indication information corresponding to a vector length of 64 bytes is 00010000.
[0107] The second method involves the virtual machine determining the boundary alignment address based on the linear mapping relationship between the boundary indication information and the boundary alignment address, using the product of the target encoding value and the target coefficient of the boundary indication information.
[0108] This linear mapping relationship includes a linear function expression and target coefficients. The virtual machine can calculate the target encoded value corresponding to the boundary indication information in the form of encoded data, and calculate the product between the target encoded value and the target coefficients according to the linear function expression. When the target encoded value of the boundary indication information is a positive integer, the value of the target coefficient can be the same as the smallest partition granularity of the vector length; for example, the smallest partition granularity of the vector length can be 4, 8, etc., that is, the minimum alignment is 4 bytes, or the minimum alignment is 8 bytes. The encoded value of the binary data corresponding to the 8-bit boundary indication information can range from 0 to 255. For example, the linear function expression can be y=kx, the current alignment encoding value is x, and x ranges from 0 to 255; k is the target coefficient, for example, k can be 8; y is the boundary alignment address; then the memory allocation address alignment boundary is: 8 times x; for example, if the alignment encoding value x is 4, it indicates that the memory allocation is aligned to 4×8=32 bytes.
[0109] Similarly, in step S3 of step 2012, if it is necessary to align data with a vector length of 512 bits, that is, 64 bytes, then it is only necessary to set the alignment in the corresponding memory allocation instruction to 64 / 8 = 8. That is, the encoding value of the boundary indication information corresponding to the data length of 64 bytes is 8.
[0110] The third method involves the virtual machine determining the boundary alignment address based on the exponential mapping relationship between the boundary indication information and the boundary alignment address. This function value, with the target value as the base and the target encoded value as the exponent, is used as the boundary alignment address.
[0111] The indication mapping relationship can include an exponential function expression. The virtual machine can calculate a function value, with the target value as the base and the target code value as the exponent, as the boundary alignment address based on the target encoded value of the boundary indication information and using this indication function expression. The target value can be configured as needed; for example, the target value can be 2. For instance, the indication function expression could be y=2. x (2 to the power of x); x is the encoded value of the current alignment, ranging from 0 to 255; y is the boundary alignment address; for example, if alignment=4, it indicates that memory allocation should be performed according to 2. 4 =16-byte alignment.
[0112] Similarly, in step S3 of step 2012, if it is necessary to align a 512-bit vector length, that is, 64 bytes, then it is only necessary to set the alignment in the corresponding memory allocation instruction to 6. That is, the encoding value of the boundary indication information corresponding to a vector length of 64 bytes is 6.
[0113] By utilizing boundary indication information included in memory allocation instructions, boundary-aligned addresses can be obtained. This allows subsequent alignment of the boundaries of the memory space to be allocated to specified memory addresses, such as addresses that are integer multiples of the vector length. This reduces memory access overhead and improves system performance. Furthermore, based on various mapping relationships, the boundary-aligned addresses mapped to the boundary indication information can be calculated more accurately. This allows for mapping larger boundary-aligned addresses even with smaller data lengths, ensuring accurate alignment to the specified boundary-aligned addresses during subsequent memory allocation. This enhances the flexibility of memory allocation and further improves virtual machine performance.
[0114] Step 205: The virtual machine allocates physical memory space for the target data based on the available storage space for the target data and the boundary-aligned address.
[0115] The physical memory space can be the memory space of the physical machine where the virtual machine resides. The virtual machine aligns the starting alignment address of the memory space to be allocated to the target data according to the boundary alignment address, and allocates memory space of the size to be used for the target data. For example, a target space with the starting alignment address as the boundary alignment address and the size of the available storage space can be allocated from memory, and the target data can be stored in the target space later. Alternatively, an address that is an integer multiple of the boundary alignment address can be allocated as the starting alignment address. For example, when the vector length is 512 bits (64 bytes) and the target data length is 1000 bytes, the starting alignment address of the allocated memory space can be aligned to the 64th, 128th, 256th (64 or an integer multiple of 64) address in memory. For example, a memory space with a starting address of 128 bytes and a size of 1000 bytes can be allocated.
[0116] Through steps 201-205 above, by designing new memory allocation instructions and utilizing these instructions which include boundary indication information, data is aligned to specified boundaries during memory allocation. For example, the boundaries of the allocated physical memory space are aligned to addresses that are integer multiples of the vector's length. This ensures that vectors are aligned to their own length during memory allocation, avoiding the extra access overhead associated with accessing unaligned data from memory. This is significant for improving vectorization performance. Furthermore, for memory allocation using other non-vector instructions, data alignment in memory can also optimize memory access efficiency.
[0117] Steps 202-205 above can be implemented by the virtual machine's interpreter. Figure 4 A schematic diagram of a memory allocation process is provided for this application, such as Figure 4As shown, the virtual machine fetches instructions through the interpreter, such as memory allocation instructions from the virtual machine's method area, and performs instruction decoding operations. For example, it decodes the opcode of the memory allocation instruction and proceeds to the next instruction execution process based on the decoding result. During instruction execution, the specific virtual machine instruction can be identified based on the decoding result of the opcode, thus obtaining the instruction type of the memory allocation instruction. For example, it can identify the new-aligned instruction, the newarray-aligned instruction, or the anewarray-aligned instruction. For the new-aligned and anewarray-aligned instructions, the constant pool index of the instruction can be obtained, i.e., the index value, and the data length of the target data can be obtained based on the index value using the constant pool. For the newarray-aligned instruction, the atype value of the instruction can be obtained, and the data length can be obtained based on the atype value and the basic type table, thereby calculating the size of the space to be allocated. For the above three types of instructions, the virtual machine obtains the boundary indication information alignment information included in the instruction, calculates the boundary alignment address, and allocates physical memory space for the target data according to the boundary alignment address and the size of the space to be allocated, so that the boundary of the allocated physical memory space is aligned to the boundary alignment address.
[0118] The memory allocation method provided in this application embodiment, when a virtual machine allocates memory space for target data based on a memory allocation instruction, includes boundary indication information. This boundary indication information determines the boundary alignment address of the memory space to be allocated to the target data. This boundary alignment address is the address where the boundary of the space to be allocated is aligned to memory. Based on this boundary alignment address and the available storage space of the target data, physical memory space is allocated to the target data, ensuring that the boundary of the allocated physical memory space is aligned to the specified boundary alignment address, thus reducing memory access overhead. By providing on-demand address alignment for memory allocation at the instruction level, access efficiency is improved, thereby enhancing the system performance of the virtual machine and increasing actual memory allocation efficiency.
[0119] Furthermore, based on various mapping relationships between boundary indication information and boundary alignment addresses, the boundary alignment address mapped to by the boundary indication information can be calculated more accurately. This allows for the mapping of a larger boundary alignment address even with a smaller data length, ensuring accurate alignment to the specified boundary alignment address during subsequent memory alignment allocation. This improves the flexibility of memory allocation and further enhances the performance of the virtual machine.
[0120] Figure 5 This is a schematic diagram of a memory allocation device provided in an embodiment of this application. Figure 5 As shown, the device includes: The space availability determination module 501 is used to determine the storage space availability of the target data in response to the memory allocation instruction of the virtual machine. The memory allocation instruction is used to indicate the allocation of memory space for the target data of the program source code. The boundary determination module 502 is used to determine the boundary alignment address of the memory space to be allocated to the target data based on the boundary indication information included in the memory allocation instruction. The boundary alignment address refers to the address of the memory space to be allocated to the target data whose boundary is aligned to the memory. The memory allocation module 503 is used to allocate physical memory space for the target data based on the amount of storage space available for the target data and the boundary alignment address.
[0121] In one possible implementation, the boundary determination module 502 includes: An extraction unit is used to extract the boundary indication information from the first target location of the memory allocation instruction based on the target instruction type of the memory allocation instruction. The determining unit is used to determine the boundary alignment address corresponding to the boundary indication information based on the target mapping relationship and the boundary indication information. The target mapping relationship refers to the mapping rule between the boundary indication information and the boundary alignment address.
[0122] In one possible implementation, the determining unit is used for any of the following: Based on the enumerated mapping relationship between boundary indication information and boundary alignment address, determine the boundary alignment address corresponding to the boundary indication information; Based on the linear mapping relationship between boundary indication information and boundary alignment address, the product of the target encoding value of the boundary indication information and the target coefficient is determined as the boundary alignment address; Based on the exponential mapping relationship between boundary indication information and boundary alignment address, the function value with the target value as the base and the target encoded value as the exponent is determined as the boundary alignment address.
[0123] In one possible implementation, the boundary indication information is encoded data, and the enumeration mapping relationship includes a boundary alignment address associated with the first encoded data and a reserved address associated with the second encoded data, the reserved address referring to a pre-reserved address to be configured.
[0124] In one possible implementation, the extraction unit is configured to extract the boundary indication information from the fourth byte of a first instruction in response to the target instruction type being an aligned allocation object, the first instruction being used to indicate the memory space of the aligned allocation target object.
[0125] In one possible implementation, the extraction unit is configured to extract the boundary indication information from the third byte of a second instruction in response to the target instruction type being an aligned allocation basic array. The second instruction is used to indicate the memory space of the aligned allocation target basic array, which includes data of basic data types.
[0126] In one possible implementation, the extraction unit is configured to extract the boundary indication information from the fourth byte of a third instruction in response to the target instruction type being an aligned allocation object array, the third instruction being used to indicate the memory space of the aligned allocation target object array, the target object array including object type data.
[0127] In one possible implementation, the device further includes: The vectorization processing module is used to obtain the vector length used in the vectorization processing based on the vectorization process of the program source code when the program source code to be executed is detected. The boundary alignment address is an integer multiple of the vector length. The vectorization process refers to the process of converting the program source code into vector instructions. The vector length refers to the data length processed by the vector instruction. The vector instruction refers to the instruction that the physical machine of the virtual machine supports to process multiple data simultaneously. The generation module is used to generate the memory allocation instruction based on the vector length and the target data of the program source code, the target data including the data to be accessed when executing the program source code.
[0128] In one possible implementation, the generation module is used to determine the target instruction type based on the data type of the target data; determine boundary indication information based on the vector length; and store the boundary indication information in the first target location of the null instruction based on the target instruction type, and store the data length indication information of the target data in the second target location of the null instruction, thereby obtaining the memory allocation instruction.
[0129] In one possible implementation, the space availability determination module 501 is used to decode the opcode of the memory allocation instruction to obtain the target instruction type of the memory allocation instruction; extract the data length indication information of the target data from the second target location of the memory allocation instruction according to the target instruction type; and determine the storage space availability of the target data according to the data length indicated by the data length indication information of the target data.
[0130] In one possible implementation, the space availability determination module 501 is used for: In response to the target data being a target object, based on the constant pool index of the target object, the first data length corresponding to the target object is found from the constant pool of the virtual machine, and the first data length is determined as the amount of storage space to be used; In response to the target data being a target basic array, based on the basic data type of the data included in the target basic array, the basic data length corresponding to the basic data type of the data included in the target basic array is looked up from the virtual machine's basic type table, and based on the data included in the target basic array and the basic data length, the second data length of the target basic array is determined, and the second data length is determined as the amount of storage space to be used; In response to the target data being an array of target objects, based on the constant pool index of the objects included in the target object array, the third data length corresponding to the objects included in the target object array is found from the constant pool of the virtual machine, and the third data length is determined as the amount of storage space to be used.
[0131] The memory allocation apparatus provided in this application embodiment allows a virtual machine to allocate memory space for target data based on a memory allocation instruction. This instruction includes boundary indication information, which determines the boundary alignment address of the memory space to be allocated to the target data. This boundary alignment address is the address where the boundary of the space to be allocated is aligned to memory. Based on this boundary alignment address and the available storage space of the target data, physical memory space is allocated to the target data, ensuring that the boundary of the allocated physical memory space is aligned to the specified boundary alignment address, thus reducing memory access overhead. By providing on-demand address alignment for memory allocation at the instruction level, access efficiency is improved, thereby enhancing the system performance of the virtual machine and increasing actual memory allocation efficiency.
[0132] Furthermore, based on various mapping relationships between boundary indication information and boundary alignment addresses, the boundary alignment address mapped to by the boundary indication information can be calculated more accurately. This allows for the mapping of a larger boundary alignment address even with a smaller data length, ensuring accurate alignment to the specified boundary alignment address during subsequent memory alignment allocation. This improves the flexibility of memory allocation and further enhances the performance of the virtual machine.
[0133] The memory allocation device in this embodiment can execute the memory allocation method shown in the above embodiments of this application. The implementation principle is similar and will not be described again here.
[0134] Figure 6 This is a schematic diagram of the structure of a computer device provided in an embodiment of this application. For example... Figure 6As shown, the computer device includes: a memory and a processor; at least one program, stored in the memory, for execution by the processor, which, compared to the prior art, enables: when a virtual machine allocates memory space for target data based on memory allocation instructions, the memory allocation instructions include boundary indication information, which determines the boundary alignment address of the memory space to be allocated to the target data. This boundary alignment address is the address where the boundary of the space to be allocated is aligned to memory, so that physical memory space is allocated for the target data based on the boundary alignment address and the available storage space of the target data, ensuring that the boundary of the allocated physical memory space is aligned to the specified boundary alignment address, reducing memory access overhead; by providing on-demand address alignment memory allocation capability at the instruction level, access efficiency is improved, thereby enhancing the system performance of the virtual machine and improving actual memory allocation efficiency.
[0135] In one alternative embodiment, a computer device is provided, such as Figure 6 As shown, Figure 6 The computer device 600 shown includes a processor 601 and a memory 603. The processor 601 and the memory 603 are connected, for example, via a bus 602. Optionally, the computer device 600 may further include a transceiver 604, which can be used for data interaction between the computer device and other computer devices, such as sending and / or receiving data. It should be noted that in practical applications, the transceiver 604 is not limited to one type, and the structure of the computer device 600 does not constitute a limitation on the embodiments of this application.
[0136] Processor 601 may be a CPU (Central Processing Unit), a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It can implement or execute the various exemplary logic blocks, modules, and circuits described in conjunction with the disclosure of this application. Processor 601 may also be a combination that implements computational functions, such as including one or more microprocessor combinations, a combination of a DSP and a microprocessor, etc.
[0137] Bus 602 may include a pathway for transmitting information between the aforementioned components. Bus 602 may be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus, etc. Bus 602 can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 6 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0138] The memory 603 may be a ROM (Read Only Memory) or other type of static storage device capable of storing static information and instructions, RAM (Random Access Memory) or other type of dynamic storage device capable of storing information and instructions, or an EEPROM (Electrically Erasable Programmable Read Only Memory), CD-ROM (Compact Disc Read Only Memory) or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital universal optical discs, Blu-ray discs, etc.), magnetic disk storage media or other magnetic storage devices, or any other medium capable of carrying or storing desired program code in the form of instructions or data structures and accessible by a computer, but not limited thereto.
[0139] The memory 603 stores application code (computer program) that executes the solution of this application, and its execution is controlled by the processor 601. The processor 601 executes the application code stored in the memory 603 to implement the content shown in the foregoing method embodiments.
[0140] Computer equipment includes, but is not limited to: virtualized computer equipment, virtual machines, servers, service clusters, and user terminals.
[0141] This application provides a computer-readable storage medium storing a computer program that, when run on a computer, enables the computer to execute the corresponding content of the memory allocation method in the aforementioned method embodiments.
[0142] This application provides a computer program product or computer program that includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the memory allocation method described above.
[0143] It should be understood that although the steps in the flowcharts of the accompanying figures are shown sequentially as indicated by the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the accompanying figures may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the sub-steps or stages of other steps.
[0144] The above description is only a partial embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A memory allocation method, characterized in that, The method includes: In response to a memory allocation instruction from the virtual machine, the amount of storage space to be used for the target data is determined. The memory allocation instruction is used to indicate the allocation of memory space for the target data of the program source code, and the memory allocation instruction is pre-generated and stored in the method area of the virtual machine. Based on the boundary indication information included in the memory allocation instruction, the boundary alignment address of the memory space to be allocated to the target data is determined. The boundary alignment address refers to the address of the memory space to be allocated to the target data whose boundary is aligned to the memory. There is a mapping relationship between the boundary indication information and the boundary alignment address. Based on the available storage space for the target data and the boundary alignment address, allocate physical memory space for the target data.
2. The memory allocation method according to claim 1, characterized in that, The step of determining the boundary-aligned address of the memory space to be allocated to the target data based on the boundary indication information included in the memory allocation instruction includes: Based on the target instruction type of the memory allocation instruction, the boundary indication information is extracted from the first target location of the memory allocation instruction; Based on the target mapping relationship and the boundary indication information, determine the boundary alignment address corresponding to the boundary indication information; The target mapping relationship refers to the mapping rule between boundary indication information and boundary alignment address.
3. The memory allocation method according to claim 2, characterized in that, The step of determining the boundary alignment address corresponding to the boundary indication information based on the target mapping relationship and the boundary indication information includes any one of the following: Based on the enumerated mapping relationship between boundary indication information and boundary alignment address, the boundary alignment address corresponding to the boundary indication information is determined; Based on the linear mapping relationship between boundary indication information and boundary alignment address, the product of the target encoding value of the boundary indication information and the target coefficient is determined as the boundary alignment address; Based on the exponential mapping relationship between boundary indication information and boundary alignment address, the function value with the target value as the base and the target encoded value as the exponent is determined as the boundary alignment address.
4. The memory allocation method according to claim 3, characterized in that, The boundary indication information is encoded data, and the enumeration mapping relationship includes the boundary alignment address associated with the first encoded data and the reserved address associated with the second encoded data. The reserved address refers to a pre-reserved address to be configured.
5. The memory allocation method according to claim 2, characterized in that, The step of extracting the boundary indication information from the first target location of the memory allocation instruction based on the target instruction type of the memory allocation instruction includes: In response to the target instruction type being an aligned allocation object, the boundary indication information is extracted from the fourth byte of the first instruction, which is used to indicate the memory space of the aligned allocation target object.
6. The memory allocation method according to claim 2, characterized in that, The step of extracting the boundary indication information from the first target location of the memory allocation instruction based on the target instruction type of the memory allocation instruction includes: In response to the target instruction type being an aligned allocation basic array, the boundary indication information is extracted from the third byte of the second instruction, the second instruction being used to indicate the memory space of the aligned allocation target basic array, the target basic array including data of basic data types.
7. The memory allocation method according to claim 2, characterized in that, The step of extracting the boundary indication information from the first target location of the memory allocation instruction based on the target instruction type of the memory allocation instruction includes: In response to the target instruction type being an aligned allocation object array, the boundary indication information is extracted from the fourth byte of the third instruction, the third instruction being used to indicate the memory space of the aligned allocation target object array, the target object array including object type data.
8. The memory allocation method according to claim 1, characterized in that, Before determining the amount of storage space to be used for the target data in response to the virtual machine's memory allocation instruction, the method further includes: In response to the detection of program source code to be executed, the vector length used in the vectorization process is obtained based on the vectorization process of the program source code; Wherein, the boundary alignment address is an integer multiple of the vector length, the vectorization process refers to the process of converting the program source code into vector instructions, the vector length refers to the data length corresponding to the vector instruction, and the vector instruction refers to the instruction that the physical machine of the virtual machine supports for processing multiple data simultaneously; Based on the vector length and the target data of the program source code, the memory allocation instruction is generated, wherein the target data includes the data to be accessed when executing the program source code.
9. The memory allocation method according to claim 8, characterized in that, The process of generating the memory allocation instruction based on the vector length and the target data of the program source code includes: Based on the data type of the target data, determine the target instruction type; Based on the vector length, determine the boundary indication information; Based on the target instruction type, the boundary indication information is stored in the first target location of the null instruction, and the data length indication information of the target data is stored in the second target location of the null instruction to obtain the memory allocation instruction.
10. The memory allocation method according to claim 1, characterized in that, Determining the available storage space for the target data to be stored includes: Decode the opcode of the memory allocation instruction to obtain the target instruction type of the memory allocation instruction; Based on the target instruction type, extract the data length indication information of the target data from the second target location of the memory allocation instruction; The amount of storage space to be used for the target data is determined based on the data length indicated by the data length indication information of the target data.
11. The memory allocation method according to claim 10, characterized in that, Determining the amount of storage space to be used for the target data based on the data length indicated by the data length indication information of the target data includes: In response to the target data being a target object, based on the constant pool index of the target object, the first data length corresponding to the target object is found from the constant pool of the virtual machine, and the first data length is determined as the amount of storage space to be used; In response to the target data being a target basic array, based on the basic data type of the data included in the target basic array, the basic data length corresponding to the basic data type of the data included in the target basic array is found from the virtual machine's basic type table, and based on the data included in the target basic array and the basic data length, the second data length of the target basic array is determined, and the second data length is determined as the storage space to be used. In response to the target data being a target object array, based on the constant pool index of the objects included in the target object array, the third data length corresponding to the objects included in the target object array is found from the constant pool of the virtual machine, and the third data length is determined as the amount of storage space to be used.
12. A memory allocation device, characterized in that, The device includes: The space availability determination module is used to determine the storage space availability of target data in response to the memory allocation instruction of the virtual machine. The memory allocation instruction is used to indicate the allocation of memory space for the target data of the program source code, and the memory allocation instruction is pre-generated and stored in the method area of the virtual machine. A boundary determination module is used to determine the boundary alignment address of the memory space to be allocated to the target data based on the boundary indication information included in the memory allocation instruction. The boundary alignment address refers to the address of the memory space to be allocated to the target data whose boundary is aligned to the memory. There is a mapping relationship between the boundary indication information and the boundary alignment address. The memory allocation module is used to allocate physical memory space for the target data based on the available storage space of the target data and the boundary alignment address.
13. The apparatus according to claim 12, characterized in that, The boundary determination module includes: The extraction unit is configured to extract the boundary indication information from the first target location of the memory allocation instruction based on the target instruction type of the memory allocation instruction; The determining unit is used to determine the boundary alignment address corresponding to the boundary indication information based on the target mapping relationship and the boundary indication information, wherein the target mapping relationship refers to the mapping rule between the boundary indication information and the boundary alignment address.
14. The apparatus according to claim 13, characterized in that, The determining unit is used for any of the following: Based on the enumerated mapping relationship between boundary indication information and boundary alignment address, the boundary alignment address corresponding to the boundary indication information is determined; Based on the linear mapping relationship between boundary indication information and boundary alignment address, the product of the target encoding value of the boundary indication information and the target coefficient is determined as the boundary alignment address; Based on the exponential mapping relationship between boundary indication information and boundary alignment address, the function value with the target value as the base and the target encoded value as the exponent is determined as the boundary alignment address.
15. The apparatus according to claim 14, characterized in that, The boundary indication information is encoded data, and the enumeration mapping relationship includes the boundary alignment address associated with the first encoded data and the reserved address associated with the second encoded data. The reserved address refers to a pre-reserved address to be configured.
16. The apparatus according to claim 13, characterized in that, The extraction unit is configured to extract the boundary indication information from the fourth byte of the first instruction in response to the target instruction type being an alignment allocation object, wherein the first instruction is used to indicate the memory space of the alignment allocation target object.
17. The apparatus according to claim 13, characterized in that, The extraction unit is configured to extract the boundary indication information from the third byte of the second instruction in response to the target instruction type being an aligned allocation basic array. The second instruction is used to indicate the memory space of the aligned allocation target basic array, and the target basic array includes data of basic data types.
18. The apparatus according to claim 13, characterized in that, The extraction unit is configured to extract the boundary indication information from the fourth byte of the third instruction in response to the target instruction type being an aligned allocation object array. The third instruction is used to indicate the memory space of the aligned allocation target object array, and the target object array includes data of object type.
19. The apparatus according to claim 12, characterized in that, The device further includes: A vectorization processing module is used to, in response to the detection of program source code to be executed, obtain the vector length used in the vectorization processing based on the vectorization processing process of the program source code; wherein, the boundary alignment address is an integer multiple of the vector length, the vectorization processing process refers to the process of converting the program source code into vector instructions, the vector length refers to the data length corresponding to the vector instruction, and the vector instruction refers to the instruction that the physical machine of the virtual machine supports for processing multiple data simultaneously. A generation module is used to generate the memory allocation instruction based on the vector length and the target data of the program source code, wherein the target data includes data to be accessed when executing the program source code.
20. The apparatus according to claim 19, characterized in that, The generation module is used to determine the target instruction type based on the data type of the target data; determine boundary indication information based on the vector length; and store the boundary indication information in the first target location of the empty instruction based on the target instruction type, and store the data length indication information of the target data in the second target location of the empty instruction, thereby obtaining the memory allocation instruction.
21. The apparatus according to claim 12, characterized in that, The space availability determination module is used to decode the opcode of the memory allocation instruction to obtain the target instruction type of the memory allocation instruction; extract the data length indication information of the target data from the second target location of the memory allocation instruction according to the target instruction type; and determine the storage space availability of the target data according to the data length indicated by the data length indication information of the target data.
22. The apparatus according to claim 21, characterized in that, The space availability determination module is used for: In response to the target data being a target object, based on the constant pool index of the target object, the first data length corresponding to the target object is found from the constant pool of the virtual machine, and the first data length is determined as the amount of storage space to be used; In response to the target data being a target basic array, based on the basic data type of the data included in the target basic array, the basic data length corresponding to the basic data type of the data included in the target basic array is found from the virtual machine's basic type table, and based on the data included in the target basic array and the basic data length, the second data length of the target basic array is determined, and the second data length is determined as the storage space to be used. In response to the target data being a target object array, based on the constant pool index of the objects included in the target object array, the third data length corresponding to the objects included in the target object array is found from the constant pool of the virtual machine, and the third data length is determined as the amount of storage space to be used.
23. A computer device, comprising a memory, a processor, and a computer program stored in the memory, characterized in that, The processor executes the computer program to implement the memory allocation method according to any one of claims 1 to 11.
24. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the memory allocation method according to any one of claims 1 to 11.
25. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the memory allocation method according to any one of claims 1 to 11.
Citation Information
Patent Citations
Memory allocation method and related equipment
CN112214313A