指针和值在结构体中的区别是什么?

huangapple go评论71阅读模式
英文:

What's the difference between pointer and value in struct?

问题

给定以下结构体:

type Exp struct {
  foo int,
  bar *int
}

在使用指针或值作为结构体成员时,性能方面有什么区别?是否存在额外开销,还是这只是 Go 编程的两种不同方式?

我会使用指针来实现一个链式结构体,但这是唯一需要在结构体中使用指针以提高性能的情况吗?

附注:在上述结构体中,我们讨论的是一个简单的 int 类型,但它可以是任何其他类型(甚至是自定义类型)。

英文:

Given the following struct:

type Exp struct {
  foo int,
  bar *int
}

What is the difference in term of performance when using a pointer or a value in a struct. Is there any overhead or this just two schools of Go programming?

I would use pointers to implement a chained struct but is this the only case we have to use pointers in struct in order to gain performance?

PS: in the above struct we talk about a simple int but it could be any other type (even custom one)

答案1

得分: 44

请使用对你的程序最有用的功能形式。基本上,这意味着如果值为nil是有用的,那么使用指针。

从性能的角度来看,原始数值类型始终比解引用指针更高效。即使是更复杂的数据结构,如果它们比缓存行或两个缓存行小(在x86 CPU上,小于128字节是一个很好的经验法则),复制通常仍然更快。

当数据变得稍大时,如果性能是一个问题,你需要进行基准测试。CPU在复制数据方面非常高效,并且有很多变量会影响数据的局部性和缓存友好性,这实际上取决于你的程序行为和你使用的硬件。

如果你想更好地理解内存和软件之间的相互作用,这是一系列很好的文章:“每个程序员都应该了解的内存知识”

简而言之,我告诉人们根据程序的逻辑选择使用指针与否,并且稍后再考虑性能问题。

  • 如果你需要传递要修改的内容,请使用指针。
  • 如果你需要确定某个内容是否未设置/为nil,请使用指针。
  • 如果你正在使用具有指针接收器方法的类型,请使用指针。
英文:

Use the form which is most functionally useful for your program. Basically, this means if it's useful for the value to be nil, then use a pointer.

From a performance perspective, primitive numeric types are always more efficient to copy than to dereference a pointer. Even more complex data structures are still usually faster to copy if they are smaller than a cache line or two (under 128 bytes is a good rule of thumb for x86 CPUs).

When things get a little larger, you need to benchmark if performance concerns you. CPUs are very efficient at copying data, and there are so many variables involved which will determine the locality and cache friendliness of your data, it really depends on your program's behavior, and the hardware you're using.

This is an excellent series of articles if you want to better understand the how memory and software interact: "What every programmer should know about memory".

In short, I tell people to choose a pointer or not based on the logic of the program, and worry about performance later.

  • Use a pointer if you need to pass something to be modified.
  • Use a pointer if you need to determine if something was unset/nil.
  • Use a pointer if you are using a type that has methods with pointer receivers.

答案2

得分: 0

如果指针的大小小于结构体成员的大小,那么使用指针更高效,因为你不需要复制成员,只需复制其地址。此外,如果你想要移动或共享结构的某个部分,最好使用指针,这样你只需共享成员的地址。详见golang faqs

英文:

If the size of a pointer is less than the struct member, then using a pointer is more efficient since you don't need to copy the member but just its address. Also, if you want to be able to move or share some part of a structure, it is better to have a pointer so that you can, again, only share the address of the member. See also the golang faqs.

huangapple
  • 本文由 发表于 2014年6月27日 20:52:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/24452323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定