Home  >  Article  >  Backend Development  >  Golang Field ordering matters?

Golang Field ordering matters?

WBOY
WBOYOriginal
2024-09-07 20:30:32686browse

你好,地鼠们!在使用 Golang 的第一年里,我一直在想字段必须有顺序,我想我为什么要为此烦恼呢?好吧,这只是字段,可能只是出了什么问题,我对吗?和大多数其他新手一样,我认为这不值得费心。换句话说,结构体中的某些字段按特定顺序放置有多重要?嗯,非常划算!

字段排序是一开始被忽略的方面之一,但随着教程的继续,这种理解,特别是 Go 如何使用指针,被认为是非常关键的。实际上,在提高应用程序性能时,尤其是在处理大型数据集或占用内存过多的操作时,正是这个顺序至关重要。这个不幸的缺陷将通过更好地理解为什么它对于 Go 字段排序如此重要而得到弥补。

Go如何在内存中存储结构体?

当放置在内存中时,结构体被表示为一块连续的内存块,其中所有字段根据其在结构体中的定义依次定位。这可能看起来相当简单,但这种线性组织也发挥了一些相当重要的作用,特别是在内存对齐和填充等领域。

内存对齐和填充

内存对齐是关于如何从内存中放置和访问数据的。通常,CPUS 在内存中获取数据的位置方面可能存在偏差,这称为对齐边界。例如,应从第 4 个字节地址放置或取出 32 位整数。如果结构体中存在未正确对齐的字段,则翻阅页面时,Go 编译器可能会添加填充字节等。这变得相当浪费。例如,看看这个结构。

struct example{

a bool   // 1 byte

b int32 // 4bytes;

c bool   // 1byte 

d int64 //8 bytes
}

Golang Field ordering matters?

由于对齐规则,在这个不正确的结构中,Go 编译器可能会在这些字段的中间添加一个或多个填充字节:

  • a 是 1 个字节,但 b 想要 4 个字节 对齐,因此填充插入 3 个字节

  • b 长度为 4 字节

  • c 长度为 1 个字节,但为了对齐需要 8 个字节的 d,需要 7asing,因此引入了填充。

  • d 长度为 8 字节

木材如何成为结构木材,由于腿的原因,尺寸仍然是24,尽管内容只需要14,但是看看实际内容加上填充物的体积.

重新排序字段以实现最小填充

字段顺序和结构查找有助于避免以负边距形式浪费空间。换句话说:

type Example struct { 

d int64 // 8 bytes

b int32 // 4 bytes

a bool // 1 byte

c bool // 1 byte

}

Golang Field ordering matters?

在上面的优化结构中:

  • d 占用 8 个字节.

  • b 占用 4 个字节.

  • a 和 c 各占用 1 个字节,无需填充。

此结构现在只有 16 字节 大小,这比以前的 24 字节 大小的结构更好。

为什么这很重要

当考虑常见的小型应用程序时,他或她很可能会发现应用程序使用的内存量与后者没有什么不同。然而,在性能甚至内存空间都至关重要的结构中,情况并非如此,考虑到嵌入式系统、超快速高频交易应用程序或旨在处理大量数据的应用程序,这些忠实的限制可以快速增加。当使用许多大型数组或连接的结构切片来构造或操作时,这一点变得更加明显。当结构的容量仅高出几个字节时,就不太容易注意到偏差或负载联合。当低内存架构被批量生产,需要处理数百万个实例时,这种令人上瘾的过度浪费不再是闻所未闻的。

Conclusion

Ordering fields is not only nice from the viewpoint of Golang struct design but also plays an important role in memory optimization. Understanding this aspect of how Go does memory layout for your structs and their pixels enables more effective struct design in practice. Such an insignificant adjustment can cause a considerable performance boost when applications that make heavy use of memory are concerned. When the next opportunity comes for you to be defining a struct in Go, do not just spill those fields around. Instead, spend one minute considering the sequence- you will be thankful to yourself and your application for it in days to come!

The above is the detailed content of Golang Field ordering matters?. 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