How come Visual Studio does not optimize structs for best memory usage?

Josh

My question is why doesnt the Visual Studio 2012 compiler automatically reorder struct members for best memory utilization? The compiler seems to store the members in exactly the order they are declared in the struct definition, with some empty padding as required for member alignment. It seems like reordering would be a more desirable way to align the members than padding, whenever possible. Is there a reason it must be stored in memory in declaration order?

Pertinent details follow;

I have a struct which represents a single element in what will be a large array. The element has a number of members, some 32 bit, some 64 bit. I have default struct member alignment tuned on for best performance.

I was exploring memory in debug mode and found that a signficant percentage of the memory was being wasted. I tracked the problem to how the stuct members were aligned in memory. I know that 32 bit members must be aligned on DWORD boundaries for best performance and it would appear that evidently 64 bit members must be aligned on QWORD boundaries (I would have thought DWORD boundaries would have been adequate)

I was able to fix the problem by changing the order in which I listed the members in the struct definition. I made sure to put 2 32 bit members consequtively whenever possible so that no padding is required to start the next 64 bit member on a QWORD boundary.

Yakk - Adam Nevraumont

Data that is in a standard layout structure or class must make certain layout guarantees. Among other things if there is another standard layout structure or class that is the prefix of the first, you must be able to reinterpret one structure as the other, and the common prefix has to agree.

This basically forces the memory order of standard layout structures to be in the order you declare them.

This is similar to what C requires in terms of structure layout, as described here.

Now, in C++, there is some freedom is given for non-standard layout structures.

[expr.rel]/3 subpoint 3:

If two pointers point to different non-static data members of the same object, or to subobjects of such members, recursively, the pointer to the later declared member compares greater provided the two members have the same access control (Clause 11) and provided their class is not a union.

The order of elements must be maintained within public/private/protected access control domains. Space between elements can be added in nearly arbitrary ways.

This means you can know that &this->x is greater than or less than &this->y, which some programmers may use.

Under the as-if rule, if nobody ever takes an address of such data, the compiler could reorder them. This is hard to prove in the usual compilation models.

The spacing between elements in MSVC matches the spacing in plain old data structures, barring inheritance with virtual playing the game, in my experience. Layout compatibility (beyond the standard) is important to a stable ABI, and code compiled with one version of the compiler is preferred to work in another. Breaking that has a cost.

C++ programmers can reorder data structures as they need, and visual studio provides #pragmas to change the structure packing rules, so if you really need that last bit of performance you can get it.

You can even write a tuple-like data structure that guarantees optimal packing if you need it. (I wouldn't rely on std::tuple, as it has no packing guarantees)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How does landscape calculate memory usage?

分類Dev

How to optimize memory with Clistctrl

分類Dev

How to decrease Logstash Memory Usage

分類Dev

How to reduce ClamAV memory usage?

分類Dev

C# Where does the memory overhead come from

分類Dev

In Visual Studio, is there a way to sort private methods by usage?

分類Dev

Visual Studio 2013/2015 Idle CPU Usage

分類Dev

Usage of Fody/Costura and Obfuscar in Visual Studio 2017

分類Dev

How does CMake specify "Platform Toolset" for a Visual Studio 2015 project?

分類Dev

How Does Visual Studio/VB Create and Initialize a Table Adapter?

分類Dev

How to allocate memory to a typedef struct within array of structs

分類Dev

Structs and memory allocation

分類Dev

How to best optimize calculations iterated over NxM grid in Python

分類Dev

How to rename Visual Studio

分類Dev

How to free up the memory in the best way

分類Dev

How does ArrayBuffer works in memory?

分類Dev

Visual Studio Command Palette does not exist

分類Dev

How does Visual Studio know my project is up to date so it can skip running MSBuild?

分類Dev

How do you make a tab control look like it does in Visual Studio

分類Dev

How to check version of Visual Studio

分類Dev

Visual Studio - How to rename Elements

分類Dev

How to identify visual studio symbols?

分類Dev

How to write Visual Studio Converter

分類Dev

How do I measure docker memory usage including host os?

分類Dev

How to autoscale Kubernetes Pods based on average memory usage in EKS?

分類Dev

how to find the percentage of CPU, RAM And memory usage using Batch FIle

分類Dev

RcppArmadillo: Issue with memory usage

分類Dev

Python - Log memory usage

分類Dev

Get memory usage of process in %

Related 関連記事

ホットタグ

アーカイブ