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

Example Analysis of Byte alignment used in golang

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you a sample analysis of byte alignment used in golang, which is concise and easy to understand, and can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Golang byte alignment has been doing some performance optimization work recently. One of the structures takes up a large amount of space and has a large number of memory. I wonder if there is any optimized space. I think of the byte alignment in c language. By simply adjusting the order of the fields, a lot of memory can be saved. This idea is also applicable in golang.

Basic data size

Before we do that, let's take a look at the size of the data occupied by the basic types in golang.

So (unsafe.Sizeof (true), ShouldEqual, 1) So (unsafe.Sizeof (int8 (0)), ShouldEqual, 1) So (unsafe.Sizeof (int16 (0)), ShouldEqual, 2) So (unsafe.Sizeof (int32 (0)), ShouldEqual, 4) So (unsafe.Sizeof (int64 (0)), ShouldEqual, 8) So (unsafe.Sizeof (int (0)), ShouldEqual, 8) So (unsafe.Sizeof (float32 (0)), ShouldEqual, 4) So (unsafe.Sizeof (float64 (0)), ShouldEqual 8) So (unsafe.Sizeof ("), ShouldEqual, 16) So (unsafe.Sizeof (" hello world "), ShouldEqual, 16) So (unsafe.Sizeof ([] int {}), ShouldEqual, 24) So (unsafe.Sizeof ([] int {1,2,3}), ShouldEqual, 24) So (unsafe.Sizeof ([3] int {1,2,3}), ShouldEqual, 24) So (unsafe.Sizeof (map [string] string {}), ShouldEqual 8) So (unsafe.Sizeof (map [string] string {"1": "one", "2": "two"}), ShouldEqual, 8) So (unsafe.Sizeof (struct {} {}), ShouldEqual, 0)

Although the bool type has only one bit, it also needs to occupy 1 byte because the computer is in bytes.

64 for the machine, an int occupies 8 bytes

The string type occupies 16 bytes and contains a pointer to data (8 bytes) and the length of an int (8 bytes).

The slice type occupies 24 bytes and contains a pointer to data (8 bytes), the length of an int (8 bytes) and the capacity of an int (8 bytes).

The map type is 8 bytes and is a pointer to the map structure

You can use struct {} to denote an empty type, which does not take up any space. If you use this as a value for map, you can use map as a set.

Byte alignment

The fields in the structure are not arranged compactly in memory, but are aligned according to bytes. For example, if int occupies 8 bytes, it can only be written at the address that is a multiple of 8. As for why byte alignment is needed, it is mainly for the consideration of efficiency. While the deeper principle looks at the statement on the Internet, it does not feel very reliable, so you can study it by yourself if you are interested.

/ / | Xmuri-| So (unsafe.Sizeof (struct {i8 int8} {}), ShouldEqual, 1)

Simply encapsulate an int8 structure, which, like int8, occupies only 1 byte and has no extra space

/ / | Xmuri-| xxxx | xx-- | So (unsafe.Sizeof (struct {i8 int8 i32 int32 i16 int16} {}), ShouldEqual, 12) / / | x-xx | xxxx | So (struct {i8 int8 i16 int16 i32 int32} {}), ShouldEqual, 8)

The contents of the two structures are exactly the same. The order of the fields is adjusted to save 33% of the space.

/ / | Xmuri-| xxxx | xx-- |-- | xxxx | xxxx | So (struct {i8 int8 i32 int32 i16 int16 i64 int64} {}), ShouldEqual, 24) / / | x-xx | xxxx | So (struct {i8 int8 i16 int16 i32 int32 i64 int64} {}), ShouldEqual, 16)

It should be noted here that int64 can only appear at addresses in multiples of 8, so in the first structure, four consecutive bytes are empty.

Type I8 int8type I16 int16type I32 int32So (unsafe.Sizeof (struct {i8 i16 i16 i32} {}), ShouldEqual, 8)

After renaming the type, the size of the type does not change

More

The above is a sample analysis of byte alignment used in golang. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to 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.

Share To

Development

  • How to write comments in HTML

    Editor to share with you how to use HTML to write notes, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it! Answer: use the syntax "

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

    12
    Report