Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is memory alignment necessary for Go language

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "whether memory alignment is necessary for Go language". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn whether memory alignment is necessary for Go language.

Some students may not know that the memory usage may vary greatly due to the different order of the fields in struct. For example:

Type T1 struct {an int8 b int64 c int16} type T2 struct {an int8 c int16 b int64}

On 64 bit platforms, T1 occupies 24 bytes,T2 and 16 bytes, while on 32 bit platforms, T1 takes 16 bytes,T2 and 12 bytes. It can be seen that different field order ultimately determines the memory size of struct, so sometimes a reasonable field order can reduce memory overhead.

Why is that? Because of the existence of memory alignment, the compiler uses memory alignment, so the final size result will be different. As for why alignment is necessary, there are two main reasons to consider:

Platform (portability)

Not all hardware platforms can access arbitrary data at any address. For example, a specific hardware platform only allows specific types of data to be obtained at a specific address, otherwise it will lead to an exception.

Performance

Accessing unaligned memory will cause CPU to make two memory accesses and take extra clock cycles to process alignment and operations. On the other hand, the self-aligned memory needs only one access to complete the read action, which is obviously much more efficient and is a standard space-for-time practice.

Some friends may think that memory reading is a simple byte array. But in fact, CPU does not read and write memory with a byte. On the contrary, CPU read memory is read block by block. The block size can be 2, 4, 6, 8, 16 bytes and so on. The block size is called memory access granularity. Assuming an access granularity of 4, CPU reads and writes memory at an access granularity of every 4 bytes.

Compilers on different platforms have their own default "alignment factor". Generally speaking, the coefficient of our commonly used x86 platform is 4 × x8664 platform coefficient is 8. It is important to note that in addition to this default alignment factor, there are also alignment factors for different data types. The alignment factor of the data type may be inconsistent on different platforms. For example, on x8664 platforms, the alignment factor of int64 is 8, while on x86 platforms, the alignment factor is 4.

Or take the T1 and T2 above, for example, on the x86x64 platform, the memory layout of T1 is as follows:

The memory layout of T2 is (the alignment factor of int16 is 2):

If you look closely, T1 has a lot of padding, which obviously takes up a lot of space. Then it is not difficult to understand why adjusting the field order of member variables in the structure can achieve the question of reducing the size of the structure, because it cleverly reduces the existence of padding. To make them more compact.

In fact, in addition to memory alignment can reduce memory consumption, there is another situation that must be manually aligned: atomic manipulation of 64bit pointers on x86 platforms. The reason for the mandatory alignment is that the 64bit atomic operation on the 32bit platform requires 8-byte alignment, otherwise the program will panic.

Thank you for your reading, the above is the content of "whether the Go language is necessary for memory alignment". After the study of this article, I believe you have a deeper understanding of whether the Go language is necessary for memory alignment, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report