In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article is about why the Go language sometimes needs to consider alignment. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
When CPU accesses memory, it is not accessed byte by byte, but by word length (word size). For example, a 32-bit CPU with a word length of 4 bytes means that CPU accesses memory in 4-byte units.
The purpose of this design is to reduce the number of CPU access to memory and increase the throughput of CPU access to memory. For example, if you also read 8 bytes of data and read 4 bytes at a time, you only need to read it twice.
Let's take a look at the difference in whether variables are aligned in memory when writing a program. Suppose we have the following structure:
Struct Foo {uint8_t a; uint32_t b;}
The schematic diagram is as follows:
Let's assume that CPU reads memory in 4 bytes.
If the layout of the variable in memory is aligned to 4 bytes, then reading the a variable only needs to read memory once, that is, word1; reading the b variable only needs to read memory once, that is, word2.
If the variable does not do memory alignment, then reading a variable only needs to read memory once, that is, word1; but when reading b variable, because b variable spans 2 word, so need to read memory twice, read the value of word1 and word2 respectively, then offset word1 to the last 3 bytes, word2 offset the first 1 byte, and finally do or operate them to get the value of b variable.
Obviously, memory alignment can reduce the number of memory reads and some operations in some cases, resulting in higher performance.
In addition, because memory alignment ensures that reading b variables is a single operation, atomicity is easier to be guaranteed in a multi-core environment.
However, while memory alignment improves performance, it also needs to pay a corresponding price. Because of the addition of padding between variables, there is no real and valid data stored, so it takes up more memory. This is also a typical space-for-time application scenario.
This is why the Go language sometimes needs to consider the problem of alignment, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.