A structural optimization method, system, device and medium for DOTS
By using Blittable type and memory layout optimization methods in Unity's DOTS technology, the high memory footprint and performance bottlenecks caused by structure use are solved, and more efficient memory utilization and user experience are achieved.
Patent Information
- Application Number
- CN202411251769.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-06
- Publication Date
- 2025-04-08
- Estimated Expiration
- 2044-09-06
AI Technical Summary
In Unity's DOTS technology, the use of structures leads to a high resource memory occupancy, and there are performance bottlenecks, which affects the development efficiency of the application.
By using the Blittable type as the data type of the structure field when defining the target structure, and memory layout optimization is performed according to the corresponding length of the structure field, including reflection to obtain the structure field, sorting in descending order, filtering the alignment length, calculating the length deviation, and performing length matching to fill the memory gap.
The memory gap caused by memory offset and memory filling is reduced, and the most appropriate memory layout of structure fields is obtained, which improves the efficiency of structure optimization, reduces the data usage of resource memory, increases data memory utilization, optimizes data structure, and enhances user experience.
Smart Images

Figure CN119002928B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of optimization technology, and in particular to a DOTS-oriented structure optimization method, system, device and medium. Background Art
[0002] In C#, a structure is a value type used to store data and has some specific advantages over a class. Specifically, as a value type, a structure instance is usually stored on the stack, and direct storage has a more compact memory layout and more efficient memory access.
[0003] In Unity's latest Data-Oriented Technology Stack (DOTS), structures are frequently used. In the ECS (Entity Component System) framework, data is stored in a NativeContainer (managed value type) structure, and then the Job System is used to process data in parallel to achieve an efficient system. Components are usually in the form of structures that are more compact and easier to process in batches. In addition, the use of the Burst Compiler also requires the use of unmanaged data.
[0004] In current projects, structures are widely used. Although structures have many advantages based on their characteristics, in DOTS, since the data of the entire structure needs to be copied when allocating memory on the stack, additional performance and memory consumption will be incurred, and the larger the structure, the more obvious it is. Therefore, the use of structures in DOTS has the problems of high resource memory usage and performance bottlenecks, which seriously affects the development efficiency of applications. Summary of the invention
[0005] In view of this, the present invention provides a structure optimization method, system, device and medium for DOTS to solve the problems of high resource memory usage and performance usage bottleneck of the structure in the existing DOTS, which in turn leads to poor user experience and difficulty in meeting the requirements of efficient application development.
[0006] In a first aspect, the present invention provides a DOTS-oriented structure optimization method, the method comprising:
[0007] When defining the target structure, use the Blittable type as the data type of the structure field;
[0008] Memory layout optimization is performed based on the corresponding lengths of the structure fields in the target structure, including:
[0009] Get all structure fields and their lengths in the target structure through reflection;
[0010] Arrange all structure fields in descending order based on their corresponding lengths, and filter the maximum length of the structure fields to obtain the alignment length;
[0011] For each structure field, determine in turn whether the corresponding length of the current structure field is less than the alignment length;
[0012] When the corresponding length of the current structure field is less than the alignment length, the length deviation between the corresponding length of the structure field and the alignment length is calculated;
[0013] Match the length of the remaining structure fields based on the length deviation.
[0014] When the corresponding length of the current remaining structure field matches the length deviation, the matching structure field is filled into the current structure field.
[0015] The present invention uses the Blittable type as the data type of the structure field when defining the target structure, and optimizes the memory layout according to the corresponding length of the structure field in the target structure. Specifically, all the structure fields and their lengths in the obtained target structure are used for automatic optimization of the memory layout, and the most suitable structure field is selected from the currently remaining structure fields through the length deviation to fill the memory gap. This can not only reduce the memory gap caused by memory offset and memory padding, but also obtain the most suitable structure field memory layout, thereby improving the DOTS-oriented structure optimization efficiency, reducing data occupation of resource memory, increasing data memory utilization, optimizing data structure, enhancing user experience, and greatly meeting the efficient development needs of application programs.
[0016] In an optional implementation, the DOTS-oriented structure optimization method further includes:
[0017] When the corresponding lengths of all the remaining structure fields do not match the length deviation, skip filling the current structure field, and execute the step of determining whether the corresponding length of the next structure field is less than the alignment length until all the remaining structure fields are traversed.
[0018] When the corresponding lengths of all the remaining structure fields do not match the length deviation, the present invention skips filling the current structure fields, which can speed up the memory layout optimization process, improve the DOTS-oriented structure optimization efficiency to a certain extent, and meet the efficient development needs of the application.
[0019] In an optional implementation, the DOTS-oriented structure optimization method further includes:
[0020] Allocate the corresponding memory space on the stack for the target structure through stackalloc;
[0021] Determine whether there is a stackalloc overflow;
[0022] When there is a stackalloc overflow, calculate the overflow memory capacity and allocate it to the heap memory space of the target structure;
[0023] If there is no stackalloc stack overflow, a reusable memory block is allocated, and the memory space is reused based on the memory block.
[0024] The present invention directly allocates continuous memory on the stack for the target structure through stackalloc, and takes into account the stackalloc stack overflow situation, and adaptively controls the memory capacity allocated by stackalloc, which can prevent the program from being interrupted due to errors caused by stack overflow. At the same time, the reused access to the memory space can save the memory size for accessing the stack space, which helps to improve the access speed and enhance the user experience.
[0025] In an optional implementation, the DOTS-oriented structure optimization method further includes:
[0026] Introducing Span <t>The target structure is extended and optimized through a slice access method to obtain corresponding data; wherein the extended and optimized memory access method includes at least one of reversal, quick sort and copy.
[0027] The present invention introduces Span <t>The target structure is expanded and optimized, and the slice access method is reconstructed to implement it. Without copying the array elements, the data information of the corresponding interval of the array can be directly obtained through slicing when the memory is zero allocated, which helps to reduce the memory occupied by data and increase the data memory utilization.
[0028] In an optional implementation, the DOTS-oriented structure optimization method further includes:
[0029] Get the data of the target structure at the memory address in the NativeContainer container through the pointer;
[0030] Set access tags for the data and repackage it to obtain an optimized container;
[0031] Modify the optimization container with the ref keyword, or expand the optimization container to Span <t>To get the corresponding structure data.
[0032] The present invention optimizes and expands the NativeContainer container by using tags and keywords. First, the pointer of NativeContainer is obtained. According to the address pointed to by the pointer, the optimized container is used to load the corresponding data in the memory pointed to by the pointer. After modifying the optimized container with the ref keyword, a new access pointer is added to it, so as to convert the keyword package into a Span. <t>, Use Span <t>This feature can avoid value duplication when accessing the NativeContainer container, thereby increasing memory utilization and access speed.
[0033] In an optional implementation, the DOTS-oriented structure optimization method further includes:
[0034] When parameters are passed and / or variables are assigned to a target structure, corresponding parameters and / or variables of the target structure are modified based on preset keywords.
[0035] The present invention optimizes parameter transfer and / or variable assignment of a target structure according to an access mode of a reference type in combination with a ref keyword, and can directly modify the structure in a memory address without generating additional copies, thereby increasing memory utilization.
[0036] In a second aspect, the present invention provides a DOTS-oriented structure optimization system, the system comprising:
[0037] The first optimization module is used to use the Blittable type as the data type of the structure field when defining the target structure;
[0038] The second optimization module is used to optimize the memory layout according to the corresponding lengths of the structure fields in the target structure, including:
[0039] Get all structure fields and their lengths in the target structure through reflection;
[0040] Arrange all structure fields in descending order based on their corresponding lengths, and filter the maximum length of the structure fields to obtain the alignment length;
[0041] For each structure field, determine in turn whether the corresponding length of the current structure field is less than the alignment length;
[0042] When the corresponding length of the current structure field is less than the alignment length, the length deviation between the corresponding length of the structure field and the alignment length is calculated;
[0043] Match the length of the remaining structure fields based on the length deviation.
[0044] When the corresponding length of the current remaining structure field matches the length deviation, the matching structure field is filled into the current structure field.
[0045] The DOTS-oriented structure optimization system of the present invention can not only reduce memory gaps caused by memory offsets and memory padding to obtain the most appropriate structure field memory layout, but also improve the efficiency of DOTS-oriented structure optimization, reduce data occupancy of memory, increase data memory utilization, optimize data structure, enhance user experience, and meet the needs of efficient development of applications to a certain extent.
[0046] In a third aspect, the present invention provides a computer device, comprising: a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the computer instructions to execute a DOTS-oriented structure optimization method according to the first aspect or any corresponding embodiment thereof.
[0047] In a fourth aspect, the present invention provides a computer-readable storage medium having computer instructions stored thereon, the computer instructions being used to enable a computer to execute a DOTS-oriented structure optimization method according to the first aspect or any corresponding embodiment thereof. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0049] Figure 1 is a flow chart of a DOTS-oriented structure optimization method according to an embodiment of the present invention;
[0050] Figure 2 It is a schematic diagram of the optimization method for high-performance usage structures suitable for DOTS;
[0051] Figure 3 It is a code diagram of the structure definition;
[0052] Figure 4 This is a code diagram generated by value replication;
[0053] Figure 5 is a structural block diagram of a DOTS-oriented structure optimization system according to an embodiment of the present invention;
[0054] Figure 6 It is a schematic diagram of the structure of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0055] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0056] As a commonly used data structure in project development, structures have many advantages based on their own characteristics, but they also have some disadvantages, including: First, structures are usually used as value types and memory is allocated on the stack, but they usually copy the data of the entire structure instead of passing references. Secondly, structures do not support inheritance, which means that subtypes of structures cannot be created; in addition, whether the structure is passed to a method as a parameter or needs to be reassigned, a copy operation will occur. When methods are frequently called and assigned, additional performance and memory consumption will be generated, and this consumption will be more obvious when the structure corresponds to a larger structure. Finally, structures usually have automatic memory layout, but in an environment that does not support automatic memory layout, it is easy to have memory alignment between members, wasting some memory space.
[0057] In the existing Unity project, DOTS technology is used for development. Due to technical development and framework requirements, a large number of structures need to be used in the project. However, the above-mentioned defects of the structure lead to poor performance of the results of DOTS development and performance bottlenecks. Therefore, the embodiment of the present invention provides a structure optimization method, system, device and medium for DOTS, the purpose of which is to avoid the shortcomings of the structure when it is used through C# characteristics and reasonable structural arrangement of structure data, thereby optimizing the memory usage of the structure, reducing the use overhead of the structure, and further improving the application development performance.
[0058] An embodiment of the present invention provides an embodiment of a structure optimization method for DOTS. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0059] In this embodiment, a DOTS-oriented structure optimization method is provided. Figure 1 is a flow chart of a DOTS-oriented structure optimization method according to an embodiment of the present invention. Figure 1 As shown, the process includes the following steps:
[0060] Step S101, when defining a target structure, use the Blittable type as the data type of the structure field.
[0061] It should be noted that the structure optimization method for DOTS in this embodiment is optimized based on the characteristics of the communication between the C# language and the underlying C++ in Unity, and the corresponding programming language is C#. There are some types in C# that can be used directly between managed and unmanaged code without any conversion. These types are called Blittable types. In C#, basic data types such as byte, sbyte, short, ushort, int, uint, long, ulong, float, IntPtr and uIntPtr and their corresponding arrays are usually Blittable types. In addition, when only Blittable types are used as structure fields, then this structure is also a Blittable type. Specifically, the way Blittable types are transferred between .NET and unmanaged code is different from that of non-Blittable types. In contrast, non-Blittable types need to be copied in memory, while Blittable types can directly use their memory blocks, that is, Blittable types can interact directly with native code without special processing.
[0062] In this embodiment, in order to avoid additional memory allocation and reduce memory copy requirements, non-Blittable type structures are found and improved. Specifically, since the Blittable type is a fixed type, by adding an extended editor, the structure to be optimized (i.e., the target structure) is obtained through reflection to make a judgment. If it is a non-Blittable type, the members that do not meet the requirements are prompted and manually adjusted.
[0063] Step S102, performing memory layout optimization according to the corresponding lengths of the structure fields in the target structure.
[0064] In actual applications, during the development of DOTS, there is a situation where the automatic layout of the structure memory cannot be used when operating with unmanaged libraries (such as access and mutual conversion) (i.e., LayoutKind.Auto is used for the automatic layout of the structure). This is because in unmanaged code, the memory of the structure will be accessed in the order of the declared structure fields, but Auto changes the memory layout in the structure, and incorrect access data will occur, that is, the structure fields originally in the order of A, B, C, because Auto implements the change of memory layout internally, that is, it becomes A, C, B; but the unmanaged memory cannot know the changes implemented by Auto internally, and will still access in the order of the declared fields in the code. Therefore, if the B field is accessed in unmanaged code, the C result or empty sequence will be obtained. Therefore, if the memory layout of the structure fields is inconsistent, the unmanaged code may not be able to access normally. When defining different structures in this embodiment, it is necessary to adjust the memory layout of the structure to adapt to DOTS development; and arrange the member order of the structure reasonably, which helps to reduce the memory gap caused by memory offset and memory padding. In this embodiment, when defining the structure, the sequential layout feature of [StructLayout(LayoutKind.Sequential)] needs to be added.
[0065] The DOTS-oriented structure optimization method of an embodiment of the present invention uses the Blittable type as the data type of the structure field when defining the target structure, and optimizes the memory layout according to the corresponding length of the structure field in the target structure. This can improve the efficiency of DOTS-oriented structure optimization, reduce data occupancy of resource memory, increase data memory utilization, optimize data structure, enhance user experience, and greatly meet the needs of efficient development of applications.
[0066] It should be noted that in this embodiment, it is necessary to implement the sequential memory layout of LayoutKind.Sequential. However, in this case, the memory layout is performed in the order of the default declared fields, that is, it will be aligned according to the member that occupies the largest space in the structure. Therefore, developers may not be able to take this rule into account at any time, which will result in a large number of memory offsets and memory padding. Therefore, this embodiment provides a function for automatically optimizing the memory layout of the structure in such cases. Specifically, the above step S102 includes:
[0067] 1. Get all structure fields and their lengths in the target structure through reflection.
[0068] It should be noted that in the .NET framework, C# reflection is a powerful mechanism that allows programs to query and manipulate type information at runtime (i.e., query and manipulate types, members, and methods in an assembly). Specifically, reflection is implemented through the classes and interfaces provided by the System.Reflection namespace, which can obtain metadata of assemblies, modules, types (classes, interfaces, enumerations, etc.) at runtime, and can create objects, call methods, access fields and properties, etc.
[0069] 2. Arrange all structure fields in descending order based on their corresponding lengths, and filter the maximum length of the structure fields to obtain the alignment length.
[0070] In this embodiment, each member in the target structure and its corresponding length can be obtained through reflection.
[0071] 3. For each structure field, determine whether the corresponding length of the current structure field is less than the alignment length.
[0072] 4. When the corresponding length of the current structure field is less than the alignment length, calculate the length deviation between the corresponding length of the structure field and the alignment length.
[0073] 5. Length matching is performed from the remaining structure fields based on the length deviation.
[0074] 6. When the corresponding length of the current remaining structure field matches the length deviation, the matching structure field is filled into the current structure field.
[0075] In a specific embodiment, all fields of the structure to be optimized and their corresponding lengths are obtained through reflection; and the length of the member that occupies the largest space in the structure is obtained as the size of the memory alignment. The members are arranged in order from large to small according to their length. When the size of the members that make up the structure is smaller than the size of the memory alignment, the most suitable members are obtained from the smaller members to fill the memory gaps, thereby reducing the memory offset and finally obtaining the most suitable member order. The structure script is regenerated according to the predefined template, wherein the template mainly specifies that the structure is the target structure of the [StructLaout(LayoutKind.Sequential)] memory layout, and then the structure is refilled according to the member order obtained in the previous steps.
[0076] The embodiment of the present invention uses all the structure fields and their lengths obtained in the target structure for automatic optimization of the memory layout, and selects the most suitable structure field from the currently remaining structure fields through the length deviation to fill the memory gaps. This can not only reduce the memory gaps caused by memory offset and memory padding, but also obtain the most suitable structure field memory layout, which helps to increase data memory utilization, thereby improving the efficiency of structure optimization for DOTS.
[0077] 7. When the corresponding lengths of all the remaining structure fields do not match the length deviation, skip filling the current structure field and execute the step of determining whether the corresponding length of the next structure field is less than the alignment length until all the remaining structure fields are traversed.
[0078] In a specific embodiment, a structure is defined, and the fields and their lengths of the structure are: A(2), B(8), C(6), D(4); at this time, the memory alignment size obtained under the memory layout of [StructLaout(LayoutKind.Sequential)] is 8; the lengths are arranged in descending order as: 8, 6, 4, 2, that is, the fields of the structure correspond to B, C, D, A; when the length is 6, it is judged that 6 is less than 8, and 4 is greater than a memory alignment gap (the gap here is 8-6=2), so a memory offset of 2 will be generated; at this time, it is necessary to obtain the offset compensation from a smaller member, that is, A is most suitable for this gap, so A is selected to fill the memory gap during sorting, and the final member order is B, C, A, D. Assume that this structure has many members, and A cannot fill this gap, and then it will continue to look for suitable members to fill the gap (still in descending order of length).
[0079] The embodiment of the present invention skips filling the current structure field when the corresponding lengths of all the remaining structure fields do not match the length deviation, which can speed up the memory layout optimization process, improve the DOTS-oriented structure optimization efficiency to a certain extent, and meet the efficient development needs of the application.
[0080] It should be noted that the DOTS-oriented structure optimization method of this embodiment is used for optimizing the definition and memory arrangement of the structure in the DOTS application, and also includes corresponding optimization of various aspects such as memory access, parameter passing and value assignment of the structure. Specifically, for the optimization of stack memory space, the DOTS-oriented structure optimization method of this embodiment also includes:
[0081] Step A1, allocate corresponding memory space on the stack for the target structure through stackalloc.
[0082] It should be noted that stackalloc in this embodiment is used to allocate memory blocks on the stack. Specifically, when the method returns, the allocated stack memory blocks created during the method execution will be automatically discarded; the memory allocated using stackalloc cannot be explicitly released; the memory blocks allocated in the stack are not affected by garbage collection.
[0083] Step A2, determine whether there is a stackalloc overflow.
[0084] In this embodiment, the specific determination method of stackalloc overflow is not limited here, and is adaptively adjusted based on actual needs. For example, C# provides a method for detecting statement block errors (such as try-catch statement blocks and StackOverflowException class to detect and handle stack overflow) to detect stackalloc overflow problems, which is only an exemplary description.
[0085] Step A3, when there is a stackalloc overflow, calculate the overflow memory capacity and allocate it to the heap memory space of the target structure.
[0086] In this embodiment, when an overflow occurs, the stack overflow situation can be obtained through StackOverflowException, and according to the stackalloc stack overflow situation, the size of stackalloc will be reallocated to achieve reasonable allocation of memory space.
[0087] Step A4: If there is no stackalloc overflow, a reusable memory block is allocated, and the memory space is reused based on the memory block.
[0088] In a specific embodiment, in order to avoid stackalloc stack overflow, stackalloc is optimized, that is, according to the size of the stack space buffer area, the stackalloc allocated memory capacity is managed and calculated. Specifically, if the declared stackalloc has an overflow, the overflow part is allocated to the heap space, thereby sacrificing a part of the performance and preventing the error caused by the stack overflow from causing the program to interrupt. When using stackalloc for access (directly using the memory space of stackalloc will damage the data inside), reusable memory blocks are allocated outside the loop (repeatedly using the divided area blocks for certain calculation functions), and an expansion method for quickly accessing the stackalloc space is added. Specifically, according to the allocated memory block size X, the stackalloc sequential space size Y is divided into Y / X areas (where the decimal is rounded up), and then the memory block is reused in the loop according to the area to access the stackalloc data, thereby saving the size of the stack space access.
[0089] In the embodiment of the present invention, continuous memory is directly allocated on the stack for the target structure through stackalloc, and the stackalloc stack overflow situation is taken into consideration to adaptively control the memory capacity allocated by stackalloc, which can prevent the program from being interrupted due to errors caused by stack overflow. At the same time, the reused access to the memory space can save the memory size for accessing the stack space, thereby helping to improve the access speed and enhance the user experience.
[0090] Specifically, for the optimization of memory access, the DOTS-oriented structure optimization method of this embodiment also includes: introducing Span <t>The target structure is extended and optimized through the slice access method to obtain the corresponding data; wherein, the extended and optimized memory access method includes at least one of reversal, quick sort and copy. It should be noted that Span in C# is used to quickly access continuous memory, including managed memory and unmanaged memory; the specific implementation method of Span for reversal, quick sort and copy is not limited here, and can be adaptively adjusted based on actual project requirements.
[0091] In a specific embodiment, the reconstruction implements the Slice method of Span, which can achieve memory zero allocation without copying array elements and directly obtain the data information of the corresponding interval of the array. <t>Instead of directly obtaining all the data, this saves space and only obtains the required data. In addition, this embodiment expands and adds other access and modification spans besides the indexer. <t>Convenient slice access methods, such as reverse Span <t>The order of elements in Reverse(), quick sort Sort(), copy CopyTo(), etc. Because modifying Span <t>Will directly modify its own data, and this type of access method has an existing Span <t>It is not provided. In this embodiment, Span is used by passing by value (that is, value copying occurs). <t>, in order to protect Span <t>The integrity and correctness of the original data.
[0092] In a specific embodiment, two implementation methods of the reverse slice function are provided to meet the usage requirements in different situations. The specific codes are as follows:
[0093] var temp;
[0094] var centerLength=spanSlic.Length%2==0?
[0095] spanSlic.Length / 2:
[0096] (spanSlic.Length-1) / 2
[0097] for(var index=0;index <centerLength;index++)
[0098] {
[0099] temp = spanSlic[index];
[0100] spanSlic[index]=spanSlic[spanSlic.Length-index-1];
[0101] spanSlic[spanSlic.Length-index-1]=temp;
[0102] }
[0103] It should be noted that the above-mentioned inversion slice function code can realize the inversion processing of data that still meets the memory 0 allocation with extremely low memory usage. This method will change the original data of Span.
[0104] Another implementation of the reverse slice function focuses on the case where the original data of the Span is not changed. The specific code is as follows:
[0105] var numberArray=new[spanSlic.Length];
[0106] var centerLength=spanSlic.Length%2==0?
[0107] spanSlic.Length / 2:
[0108] (spanSlic.Length-1) / 2
[0109] for(var index=0;index <centerLength;index++)
[0110] {
[0111] numberArray[index]=spanSlic[spanSlic.Length-index-1]
[0112] }
[0113] It should be noted that the above-mentioned reverse slice function code realizes the reversal of the Span slice by value copying, and at the same time protects the original data of the Span from being modified, which can greatly ensure the security of the data.
[0114] Introducing Span in the embodiment of the present invention <t>The target structure is expanded and optimized, and the slice access method is reconstructed to implement it. Without copying the array elements, the data information of the corresponding interval of the array can be directly obtained through slicing when the memory is zero allocated, which helps to reduce the memory occupied by data and increase the data memory utilization.
[0115] Specifically, for the optimization of container access, the DOTS-oriented structure optimization method of this embodiment further includes:
[0116] Step B1, obtaining the data of the target structure at the memory address in the NativeContainer container through the pointer.
[0117] In this embodiment, this step mainly obtains the data of the target structure in the NativeContainer container at the container memory address through a pointer.
[0118] It should be noted that the NativeContainer container itself is an attribute-safe container with its own security checks. It is impossible to directly access the data of the structure in the container. When it is used, the value of the structure will be copied, which will cause additional memory consumption. At the same time, the security check will also have a certain performance consumption.
[0119] Step B2, setting access tags for the data and repackaging the data to obtain an optimized container.
[0120] In this embodiment, the [unsafe] tag is added to the data access at the memory address, and the data is repackaged to obtain an optimized container, which can be regarded as an UnSafe container. It should be noted that the Unsafe container can bypass security checks, but as the word Unsafe means unsafe, when the corresponding structure is used, the unsafety of the structure is faced. The relevant technicians need to make judgments based on the use and access, and take into account various situations to ensure that the security of the structure is not threatened before using the unsafe keyword, that is, the tag [unsafe].
[0121] Step B3: Modify the optimization container using the ref keyword, or expand the optimization container to Span <t>To get the corresponding structure data.
[0122] It should be noted that Span <t>The container is essentially an Unsafe container modified by the ref keyword, plus a pointer to obtain the structure data safely. In this embodiment, there are two ways to access the structure data in the NativeContainer container. One is to directly use the ref and unsafe keywords, and the other is to expand the unsafe keyword to Span <t>In this case, you need to use pointers to wrap the ref and unsafe keywords.
[0123] In a specific embodiment, the corresponding data in the memory is obtained through the UnSafe container and the pointer, and the data is modified by the ref keyword to avoid the value copying of the NativeContainer container when accessing, that is, the address of the data of the NativeContainer container in the memory is directly obtained through the pointer, and then converted to the UnSafe container to bypass the security check of the NativeContainer to directly access the data in the memory, or the NativeContainer is expanded to Span <t>Memory access is performed to achieve access to the corresponding structure data at the same address in the memory with lower consumption in the system and Job, which can avoid the value copying of the NativeContainer container during access, thereby increasing memory utilization and access speed.
[0124] Specifically, for the optimization of parameter passing, the DOTS-oriented structure optimization method of this embodiment also includes: when performing parameter passing and / or variable assignment to the target structure, the corresponding parameters and / or variables of the target structure are modified based on preset keywords. It should be noted that the specific content of the preset keywords can be adaptively selected based on actual needs. For example, the preset keyword is the in keyword (also called the in modifier, which passes parameters by reference to make the formal parameter an alias for the actual parameter, that is, any operation performed on the formal parameter is performed on the actual parameter; similar to the ref keyword or the out keyword, the difference is that the in keyword cannot be modified by the calling method), which is only used as an exemplary explanation.
[0125] It should be noted that when a structure is used as a function parameter or assigned to a variable, the value in the memory is copied to the target for transfer, and value duplication will occur. For example, normal assignment and parameter transfer are:
[0126] var str = structIns;
[0127] Func(structIns).
[0128] In this embodiment, the assignment and parameter transfer after optimization based on keywords are as follows:
[0129] ref var str=ref structIns;
[0130] Func(ref structIns) / Func(in structIns);
[0131] Among them, the ref keyword and the in keyword are equivalent to a representation flag. When the C# underlying runtime is in progress, the notification call is no longer to directly obtain the value when obtaining the structure parameter, but to obtain the address of the value on the stack. Therefore, when using assignment, the space on the same stack address is used, so there will be no value copying. Specifically, in this embodiment, according to the access method of the reference type, when the structure is passed or assigned to a variable, the structure can be modified directly in the memory address without generating additional copies.
[0132] In the embodiment of the present invention, the parameter passing and / or variable assignment of the target structure is optimized according to the access mode of the reference type in combination with the ref keyword, and the structure can be modified directly in the memory address without generating additional copies, thereby increasing memory utilization.
[0133] In one specific embodiment, see Figure 2 , proposes an optimization method for high-performance usage structures suitable for DOTS, including:
[0134] 1) Optimize the structure to a Blittable type.
[0135] In this embodiment, when defining a structure, only the Blittable type is used as the structure field.
[0136] 2) Automatically optimize the structure memory layout based on the fields of the defined structure.
[0137] In this embodiment, a memory layout is set for different defined structures, and the order of the data structures of its members is reasonably arranged to reduce memory gaps caused by memory offsets and memory padding.
[0138] In actual applications, considering that the DOTS development process operates with unmanaged libraries, the automatic layout of structure memory cannot be used; that is, if the structure layout is inconsistent, unmanaged code may not be able to access it normally. Therefore, when defining different structures, the memory layout of the structure needs to be adjusted to adapt to DOTS development. For example, to define a structure, refer to Figure 3 , byte alignment of the memory is performed according to the longest field of the structure (i.e. 8 bytes), which will generate a 4-byte memory offset ( Figure 3 The offset requirement in the byte array is 4 bytes, and the 4-byte tail is left blank, resulting in a total of 8 bytes of wasted memory. If the field order is adjusted to: "int A; int C; long B;", the entire memory range can be completely filled without generating any extra memory offset.
[0139] 3) Optimize the value copying problem when passing structures as parameters and assigning variables.
[0140] In this embodiment, the ref and in keywords are used properly to modify the structure parameters when passing them, so as to reduce the copy overhead caused when the structure is passed as a parameter. Specifically, according to the access mode of the reference type, the struct parameter passing and variable assignment are optimized, that is, the structure is modified directly in the memory address without generating additional copies.
[0141] It should be noted that when you need to modify a structure, you can use the ref keyword to modify the structure. However, when the ref keyword passes a reference, it cannot ensure that the reference value is not modified by the other party. If you only need to access the structure, you can use the in keyword to modify the structure parameter to ensure data security; and the larger the readonly struct (read-only structure) is, the more obvious the benefit of the in keyword is.
[0142] 4) Use stackalloc to allocate continuous memory directly on the stack for the same structure group.
[0143] In this embodiment, stackalloc is used to directly allocate continuous memory for the structure on the stack to improve data allocation, access and release efficiency and avoid garbage collection pressure.
[0144] In this embodiment, when a list or array of a structure type is usually instantiated, memory units are allocated according to the field type of the structure. The allocated memory units may exist on the heap memory or on the stack memory, which may cause the allocated memory space to be insufficiently continuous, thereby increasing the consumption in access and management. Specifically, by using stackalloc to allocate continuous memory directly on the stack for the structure, Unsafe and pointers can be used to safely access the structure, and the continuous memory structure itself will optimize the access speed, and the memory on the stack will not be affected by garbage collection, reducing a lot of memory recovery pressure. It should also be noted that the memory allocated on the stack using stackalloc will be limited by the size of the stack, and allocations exceeding the stack size will cause a stack overflow exception.
[0145] 5) Introducing Span <t>Safe and high-performance memory access to structures.
[0146] In this embodiment, Span is introduced <t>, encapsulates a safe and high-performance memory access operation method to optimize the access performance of various array structures.
[0147] In a specific embodiment, in order to access a group of instances of the same structure, an array or list structure is usually used to load it; however, it is not possible to directly assign values to members of the structure array instance, and the complete structure can only be written into the array, and this process will result in value duplication. See Figure 4 .
[0148] This example introduces Span <t>The structure array can be expanded to represent any adjacent area of memory. Span's Slic overload can be used to point to a subset of this structure array; then, the generated Span can be indexed to write and read the data of the relevant part of the original structure array. In addition to the above uses, it still ensures safe access and high performance. And Span can not only be used to access arrays and lists, but also to reference data on the stack, that is, the memory allocated by stackalloc in the above 4).
[0149] 6) Use the unsafe and ref keywords to expand and optimize NativeContainer.
[0150] In this embodiment, a managed value type container is obtained so that the data in NativeContainer can be directly accessed and operated through the ref keyword. Specifically, NativeContainer is a managed value type that provides a relatively safe C# wrapper for native memory, which contains a pointer to an unmanaged allocation; when used with DOTS's JobSystem, NativeContainer allows the Job to access data shared with the main thread instead of using a copy.
[0151] Specifically, this embodiment uses unsafe and ref to expand the NativeContainer container. First, the pointer of NativeContainer is obtained. According to the address pointed to by the pointer, the UnSafe container is used to load the corresponding data in the memory pointed to by the pointer. After the UnSafe container is modified with the ref keyword, a new access pointer is added to it, thereby converting the refunsafe package into a Span. <t>, Use Span <t>This feature can avoid value duplication when accessing the NativeContainer container, thereby increasing memory utilization and access speed.
[0152] In summary, the DOTS-oriented structure optimization method of the embodiment of the present invention customizes the structure and optimizes it so that it can be applied to high-performance usage frameworks such as JobSystem and Brust compilation. Specifically, stackalloc is used to apply for continuous memory space on the stack for the structure, and span is used <t>Or pointers to high-speed and safe access to structures, optimize the structure's support for data structures, improve data access efficiency, and reduce garbage collection pressure; and use ref and in keywords to modify and pass structure references when passing parameters, which can reduce memory consumption caused by value copying when passing structures, greatly reduce data memory usage, increase data memory utilization, optimize data structures, enhance user experience, and to a certain extent meet the needs of efficient application development.
[0153] In this embodiment, a DOTS-oriented structural optimization system is also provided, which is used to implement the above-mentioned embodiments and preferred implementation modes, and the descriptions that have been made will not be repeated. As the term "module" is used below, it can implement a combination of software and / or hardware with predetermined functions. Although the systems described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.
[0154] The present invention provides a structure optimization system for DOTS, such as Figure 5 As shown, the system includes:
[0155] The first optimization module 501 is used to use the Blittable type as the data type of the structure field when defining the target structure.
[0156] The second optimization module 502 is used to optimize the memory layout according to the corresponding lengths of the structure fields in the target structure, including:
[0157] Get all structure fields and their lengths in the target structure through reflection;
[0158] Arrange all structure fields in descending order based on their corresponding lengths, and filter the maximum length of the structure fields to obtain the alignment length;
[0159] For each structure field, determine in turn whether the corresponding length of the current structure field is less than the alignment length;
[0160] When the corresponding length of the current structure field is less than the alignment length, the length deviation between the corresponding length of the structure field and the alignment length is calculated;
[0161] Match the length of the remaining structure fields based on the length deviation.
[0162] When the corresponding length of the current remaining structure field matches the length deviation, the matching structure field is filled into the current structure field.
[0163] In some optional implementations, the system further includes: a third optimization module, a fourth optimization module, a fifth optimization module, and a sixth optimization module; wherein the third optimization module is used to allocate corresponding memory space on the stack for the target structure through stackalloc; determine whether there is a stackalloc stack overflow; when there is a stackalloc stack overflow, calculate the overflow memory capacity and allocate it to the heap memory space of the target structure; if there is no stackalloc stack overflow, allocate a reusable memory block, and reuse the memory space based on the memory block; the fourth optimization module is used to introduce Span <t>The target structure is extended and optimized through a slice access method to obtain corresponding data; wherein the extended and optimized memory access method includes at least one of inversion, quick sorting and copying; the fifth optimization module is used to obtain the data of the target structure at the memory address in the NativeContainer container through a pointer; after setting an access tag for the data, it is repackaged to obtain an optimized container; the optimized container is modified through the ref keyword, or the optimized container is extended to a Span <t>To obtain the corresponding structure data; a sixth optimization module is used to modify the corresponding parameters and / or variables of the target structure based on preset keywords when performing parameter transfer and / or variable assignment on the target structure.
[0164] The further functional description of each of the above modules is the same as that of the above corresponding embodiments and will not be repeated here.
[0165] The DOTS-oriented structure optimization system in this embodiment is presented in the form of functional units, where the units refer to ASIC (Application Specific Integrated Circuit) circuits, processors and memories that execute one or more software or fixed programs, and / or other devices that can provide the above functions.
[0166] The DOTS-oriented structure optimization system of the embodiment of the present invention can not only reduce the memory gaps caused by memory offsets and memory padding and obtain the most appropriate memory layout of structure fields by optimizing the data type of the target structure and the memory layout of the structure fields, but also improve the efficiency of DOTS-oriented structure optimization, which helps to reduce data occupation of memory, increase data memory utilization, optimize data structure, enhance user experience, and meet the needs of efficient development of applications to a certain extent.
[0167] The present invention also provides a computer device, see Figure 6 , Figure 6 is a schematic diagram of the structure of the above-mentioned computer device provided in an optional embodiment of the present invention, such as Figure 6 As shown, the computer device includes: one or more processors 10, a memory 20, and interfaces for connecting various components, including high-speed interfaces and low-speed interfaces. Various components are connected to each other using different buses for communication, and can be installed on a common mainboard or installed in other ways as needed. The processor can process the instructions executed in the computer device, including instructions stored in or on the memory to display the graphical information of the GUI on an external input / output device (such as, a display device coupled to the interface). In some optional embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories and multiple memories. Similarly, multiple computer devices can be connected, and each device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Figure 6 A processor 10 is taken as an example.
[0168] The processor 10 may be a central processing unit, a network processor or a combination thereof. The processor 10 may further include a hardware chip. The hardware chip may be a dedicated integrated circuit, a programmable logic device or a combination thereof. The programmable logic device may be a complex programmable logic device, a field programmable gate array, a general purpose array logic or any combination thereof.
[0169] The memory 20 stores instructions executable by at least one processor 10, so that at least one processor 10 executes the method shown in the above embodiment.
[0170] The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the computer device, etc. In addition, the memory 20 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some optional embodiments, the memory 20 may optionally include a memory remotely arranged relative to the processor 10, and these remote memories may be connected to the computer device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0171] The memory 20 may include a volatile memory, such as a random access memory; the memory may also include a non-volatile memory, such as a flash memory, a hard disk or a solid state drive; the memory 20 may also include a combination of the above types of memory.
[0172] The computer device also includes a communication interface 30, which is used for the main control chip to communicate with other devices or a communication network.
[0173] A computer-readable storage medium is also provided in an embodiment of the present invention. The method according to the embodiment of the present invention can be implemented in hardware, firmware, or can be implemented as a computer code that can be recorded in a storage medium, or can be implemented as a computer code that is originally stored in a remote storage medium or a non-temporary machine-readable storage medium and will be stored in a local storage medium and downloaded through a network, so that the method described herein can be stored in such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. Among them, the storage medium can be a magnetic disk, an optical disk, a read-only storage memory, a random access memory, a flash memory, a hard disk or a solid-state hard disk, etc.; further, the storage medium can also include a combination of the above types of memories. It can be understood that a computer, a processor, a microprocessor main control chip or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by a computer, a processor or hardware, the method shown in the above embodiment is implemented.
[0174] Although the embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations are all within the scope defined by the appended claims.< / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t>
Claims
1. A DOTS-oriented structure optimization method, characterized in that: The method comprises: When defining the target structure, use the Blittable type as the data type of the structure field; Memory layout optimization is performed according to corresponding lengths of structure fields in the target structure, including: Obtain all structure fields and their lengths in the target structure through reflection; Arrange all structure fields in descending order based on their corresponding lengths, and filter the maximum length of the structure fields to obtain the alignment length; For each structure field, determine in turn whether the corresponding length of the current structure field is less than the alignment length; When the corresponding length of the current structure field is less than the alignment length, calculating the length deviation between the corresponding length of the structure field and the alignment length; Performing length matching from the currently remaining structure fields based on the length deviation; When the corresponding length of the currently remaining structure field matches the length deviation, the matching structure field is filled into the current structure field; When the corresponding lengths of all the remaining structure fields do not match the length deviation, skip filling the current structure field, and execute the step of determining whether the corresponding length of the next structure field is less than the alignment length until all the remaining structure fields are traversed.
2. The DOTS-oriented structure optimization method according to claim 1, characterized in that: The method further comprises: Allocate corresponding memory space on the stack for the target structure through stackalloc; Determine whether there is a stackalloc overflow; When there is a stackalloc overflow, the overflow memory capacity is calculated and allocated to the heap memory space of the target structure; If there is no stackalloc stack overflow, a reusable memory block is allocated, and the memory space is reused based on the memory block.
3. The DOTS-oriented structure optimization method according to claim 1, characterized in that: The method further comprises: Introducing Span <t> The target structure is extended and optimized through a slice access method to obtain corresponding data; wherein the extended and optimized memory access method includes at least one of reversal, quick sort and copy.< / t> 4. The DOTS-oriented structure optimization method according to claim 1, characterized in that: The method further comprises: Get the data of the target structure at the memory address in the NativeContainer container through the pointer; Setting access tags for the data and then repackaging the data to obtain an optimized container; Modify the optimization container by using the ref keyword, or expand the optimization container to a Span <t> To get the corresponding structure data.< / t> 5. The DOTS-oriented structure optimization method according to claim 1, characterized in that: The method further comprises: When performing parameter transfer and / or variable assignment on the target structure, corresponding parameters and / or variables of the target structure are modified based on preset keywords.
6. A DOTS-oriented structure optimization system, characterized in that: The system comprises: The first optimization module is used to use the Blittable type as the data type of the structure field when defining the target structure; The second optimization module is used to optimize the memory layout according to the corresponding lengths of the structure fields in the target structure, including: Obtain all structure fields and their lengths in the target structure through reflection; Arrange all structure fields in descending order based on their corresponding lengths, and filter the maximum length of the structure fields to obtain the alignment length; For each structure field, determine in turn whether the corresponding length of the current structure field is less than the alignment length; When the corresponding length of the current structure field is less than the alignment length, calculating the length deviation between the corresponding length of the structure field and the alignment length; Performing length matching from the currently remaining structure fields based on the length deviation; When the corresponding length of the currently remaining structure field matches the length deviation, the matching structure field is filled into the current structure field; When the corresponding lengths of all the remaining structure fields do not match the length deviation, skip filling the current structure field, and execute the step of determining whether the corresponding length of the next structure field is less than the alignment length until all the remaining structure fields are traversed.
7. A computer device, characterized in that: The device includes: a memory and a processor, the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the DOTS-oriented structure optimization method described in any one of claims 1 to 5 by executing the computer instructions.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a computer to execute the DOTS-oriented structure optimization method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Compilation optimization method and device
CN115543350A
Method and system for optimizing DDS (Direct Digital Synthesizer) data processing rate and medium
CN117675894A