Home >Common Problem >Will byte alignment affect the efficiency of memory access?

Will byte alignment affect the efficiency of memory access?

(*-*)浩
(*-*)浩Original
2019-12-26 09:27:175753browse

Since C projects are rarely done and relatively small, the issue of byte alignment has not been paid attention to. However, byte alignment should have a great impact on memory management and CPU execution efficiency in large-scale applications. This article is based on some information and makes a short summary.

Will byte alignment affect the efficiency of memory access?

#First of all, throw out the first conclusion, byte alignment can improve the execution efficiency of the CPU. (Recommended learning: PHPSTORM )

CPU execution instructions to obtain data from the memory from the memory, the size of the block may be 2-bytes, 4-bytes, 8 -bytes, 16-bytes......

At this time, if the starting address of the CPU reading data of 4-bytes or more is 1, it needs to read at least 2 data blocks, and then put 2 Unnecessary data in the block is discarded, and then the useful data is spliced ​​into 4-bytes data. This obviously increases the CPU operation and affects the execution efficiency of the CPU instructions. If the starting address of the data read by the CPU is at 0, 4..., then only one data block needs to be read at a time, and the CPU's read instruction is an atomic operation.

Byte alignment means that for every data read by the CPU, it ensures that its starting address is at the beginning of the data block, and expands the value where the number of data bytes is smaller than the CPU data block (granularity) , making it occupy a complete memory space of one granularity. Byte alignment saves the CPU from data interception and splicing operations.

Another conclusion is that byte alignment is helpful for optimizing memory.

In the structure, according to the byte automatic alignment principle, the structure

typedef struct _test {
char a;
int b;
char c;
} test;

The compiler will use the number of bytes of the basic type int with the largest number of bytes in the structure as According to the alignment standard, the char type will be expanded to 4-bytes, therefore, sizeof(test)=12, but this memory utilization efficiency is relatively low.

If you specify 1-byte alignment by the compiler, the execution efficiency of the CPU will be reduced.

#praama pack(1)
typedef struct _test {
char a;
int b;
char c;
} test;
#pragma pack()

In order to optimize the memory of the program while ensuring CPU execution efficiency, it is necessary to adjust the order of the data members in the structure

typedef struct _test {
int b;
char a;
char c;
} test;

At this time, the structure Member b occupies 4 bytes, a and c share the next 4 bytes, two of the members occupy the first two bytes, and the last two bytes are invalid data filled during byte alignment. This structure occupies 8 bytes of memory space.

The above is the detailed content of Will byte alignment affect the efficiency of memory access?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn