Optimizing the memory layout of class instances in C ++

Updating the application from 32 to 64 bits increases the size of the pointer and the memory size of objects.

I am looking for methods to minimize the memory area of ​​objects. For POD structures, I unload the structure memory layout to figure out how to pack elements and reduce the number of compilers.

Is there a way to figure out the memory location of objects other than PODs, such as class instances? How could I achieve something similar to packing class objects?

Thanks Dan

+6
source share
4 answers

You can use GCC -Wpadded to tell you where the add-on is added, and then reorder based on this information, reducing the size in some cases.

Force-packing data is not a good idea for in-memory submissions.

+4
source

I do not know about the specific data of objects other than POD (i.e. vtable), although I believe that this is dictated by the size of the pointer. In any case, you can control the alignment of elements using the #pragma pack compiler #pragma pack , which is supported by both GCC and Visual Studio .

You can also read paragraph 7.18 on the wonderful Agner Fog C ++ Optimization Guide :

The data elements of a class or structure are stored sequentially in the order in which they are declared whenever an instance of the class or structure is created. There is no way to organize data into classes or structures. Accessing data is a member of an object of a class or structure takes longer than accessing a simple variable. Most compilers will align data items to round addresses to optimize access.

+1
source

Thumb rule: largest to smallest; this gives perfect alignment when the dimensions of the elements are two, otherwise manual optimization is possible.

Note that proper alignment is generally important for speed, even if the processor is recovering from irregularities. While the x86 and (AFAIK) x64 CPUs manually shift access internally with a second read, the time "wasted" on reading misalignment is usually much more than the time saved due to the smaller working set. Therefore, I would only "pack tightly" when you are doing comparisons on multiple processors.

For non-POD you will need to check sizeof(element) .
(If there are many objects, I would probably go with a simple parser generating C ++ to reset these sizes)

As an alternative, PVS-Studio has an analysis of the size of structures and gives recommendations for reordering. I haven't reviewed them yet, but you can use eval to find out if it works for you.

0
source

Regarding objects other than POD, I think you should learn more about the vTable virtual function, virtual inheritance, to find out what things the size of a class or object does. In fact, class alignment, which fills child elements, element element alignment with only one of the factors, leads to class size.

There are some related sites here, I think it might be useful for you.

And, if you use MVSC, you can discard all memory layouts of the entire class in your solution using -d1reportAllClassLayout as follows:

 cl -d1reportAllClassLayout main.cpp 
0
source

Source: https://habr.com/ru/post/912296/


All Articles