23

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)

2 Answers 2

46

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
  • 3
    I'd add just one clarification to your excellent answer: the fourth bullet point is missing: "otherwise, don't use a pointer unless it gives a very clear performance improvement"
    – Rick-777
    Jun 27, 2014 at 18:40
  • Regarding your suggestion to use a pointer if you need to pass something to be modified, in the above example, both foo and bar can be modified, can't they, despite one being a pointer and the other not? Oct 4, 2020 at 13:35
0

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.

3
  • 1
    Thx for your answer. When you talk about the copy "using a pointer is more efficient since you don't need to copy the member but just its address", is this also the case even if we use only Exp as pointer, i.e. *Exp? Moreover, if we define a method on the type Exp and not *Exp, what your are saying about copies applies every time we call that method, i.e. there is a copy of Exp the method is called on? (I think so after reading the fag about method definition).
    – fsenart
    Jun 27, 2014 at 13:05
  • 1
    Yes a type Exp argument will be copied every single time. And yes, if you have a Exp * argument the member will not be copied. Pointers allow you to share data and compactly pass data.
    – perreal
    Jun 27, 2014 at 13:09
  • 3
    Using a pointer may be more memory efficient, but will likely perform worse in a variety of cases because you're losing data locality. If the data belongs together and is frequently accessed together, then keep it together.
    – zmb
    Jun 27, 2014 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.