Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is explicit alignment necessary?

After some readings, I understand that compiler has done the padding for structs or classes such that each member can be accessed on its natural aligned boundary. So under what circumstance is it necessary for coders to make explicit alignment to achieve better performance? My question arises from here:

Intel 64 and IA-32 Architechtures Optimization Reference Manual:

For best performance, align data as follows:
Align 8-bit data at any address.
Align 16-bit data to be contained within an aligned 4-byte word.
Align 32-bit data so that its base address is a multiple of four.
Align 64-bit data so that its base address is a multiple of eight.
Align 80-bit data so that its base address is a multiple of sixteen.
Align 128-bit data so that its base address is a multiple of sixteen.

So suppose I have a struct:

struct A
{
    int a;
    int b;
    int c;
}
// size = 12;
// aligned on boundary of: 4

By creating an array of type A, even if I do nothing, it is properly aligned. Then what's the point to follow the guide and make the alignment stronger?

Is it because of cache line split? Assuming the cache line is 64 bytes. With the 6th access of object in the array, the byte starts from 61 to 72, which slows down the program??

BTW, is there a macro in standard library that tells me the alignment requirement based on the running machine by returning a value of std::size_t?

like image 782
user3156285 Avatar asked Aug 13 '14 15:08

user3156285


People also ask

Is alignment necessary C?

Why is Wheel Alignment Important? Maintaining proper wheel alignment is essential to avoid unnecessary wear on your tyres, steering, suspension and brakes. Accurate wheel alignment optimises driving stability, maximises tyre life and improves your vehicle's overall handling performance.

Does alignment matter memory?

Yes both alignment and arrangement of your data can make a big difference in performance, not just a few percent but few to many hundreds of a percent. Take this loop, two instructions matter if you run enough loops. A performance test you can very easily do yourself.

Why is natural alignment important?

The main purpose of natural alignment is to avoid misaligned access to data members in the structure — which can slow things down (sometimes radically — the DEC Alpha was particularly bad).

What is alignment requirement?

Every complete object type has a property called alignment requirement, which is an integer value of type size_t representing the number of bytes between successive addresses at which objects of this type can be allocated. The valid alignment values are non-negative integral powers of two.


1 Answers

Let me answer your question directly: No, there is no need to explicitly align data in C++ for performance.

Any decent compiler will properly align the data for underlying system.

The problem would come (variation on above) if you had:

 struct 
 {
     int w ;
     char x ;
     int y ;
     char z ;
 } 

This illustrates the two common structure alignment problems.

(1) It is likely a compiler would insert (2) 3 alignment bytes after both x and z. If there is no padding after x, y is unaligned. If there is no padding after z, w and x will be unaligned in arrays.

The instructions are you are reading in the manual are targeted towards assembly language programmers and compiler writers.

When data is unaligned, on some systems (not Intel) it causes an exception and on others it take multiple processor cycles to fetch and write the data.

like image 198
user3344003 Avatar answered Sep 21 '22 13:09

user3344003