There is a difference, given that in the former, values stored in the map
are not addressable, so if you need to modify them, you need to read,
modify, write them, whereas in the latter, you can simply do
someMap[k].doSomeThings. As you noted, there is also the issue that you
can't call methods on pointer receivers on an index expression in the
former case.

If those semantic differences don't matter, it depends on Y (and your use
cases). Storing a pointer means less copying, when you access or store
something in the map; storing a value means less pointer indirection and
cache misses, as it's stored packed in the bucket. Whats cheaper depends on
your specific use case.

A priori I don't believe that the performance difference is material. Thus
I personally usually opt for the latter, because I find the semantics more
convenient and appropriate for what I need.

On Fri, May 12, 2017 at 2:00 PM, aktungmak <aktung...@gmail.com> wrote:

> Setting aside the receiver types of methods for the moment, if we consider
> the following declaration:
>
> type Y struct {
>     //some fields
> }
>
> Which of the following would be "better" (from a speed and storage usage
> viewpoint)?
>
> map[X]Y
> map[X]*Y
>
> If we consider that this map could be handed between different functions.
> Does it make any difference to use a pointer? Or does it make no
> difference, since the map is already a reference type and functions are
> receiving a copy of the pointer to the map and not a copy of the map value?
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to