2

I have read some of the stack overflow question related to "why pointer and why not pointer", but I could not understand much.

So, want to understand based on my example below

I have a list of users and I am finding a hard time to understand which approach is better and faster for the purpose of nesting array struct of users inside another struct.

For an example -

type loc struct {
    Type string
    Point []float64
}

type User struct {
    ID string
    Name string
    Location loc
    ... // 30 more fields
}

Case A - Using pointer for nested array struct

type Games struct {
    ID string
    Owner []*User
    Player []*User
}

// and storing it as

G:= Games{
    ID: "1",
    Owner: []*User{
        &User {
            ID: "2",
            Name: "User A",
        },
    },
}

Case B - Should I use above approach or below approach to

type Games struct {
    ID string
    Owner []User
    Player []User
}

// storing it as

G := Games {
    ID: "1",
    Owner: []User{
        {
            ID: "2",
            Name: "User A",
        },
    },
}

Considering the situation, I do not have to change data for Owner or Player after creating value for Games above and Player can sometimes remain Nil, should I use only User or it's better to use *User

I have following confusion based on above 2 cases

  • Performance - which one is better & why?
  • Garbage Collection - which one is better & why?
  • Any Memory effects?
  • Any other effects!

Please help me out understanding based on the above example, which method would you have chosen above and why?

user3767643
  • 674
  • 2
  • 9
  • 25
  • https://stackoverflow.com/a/23551970/2100197 – Mukesh Sharma Aug 20 '18 at 02:23
  • I read that and I can understand is there is no point using "pointer" in my use case above. it is perfect to go with Case B. – user3767643 Aug 20 '18 at 03:29
  • All your question do not have answers: They all depend on other things than pointer vs- non-pointer. If all your method are on *User you probably want to store *Users. For all the "performance" related questions: Measure your actual use case and decide. – Volker Aug 20 '18 at 03:35
  • There is no method where the struct is used as pointer or reference. Everything is stored straight away to database after providing value to the struct. That is why, I mentioned to answer based on above situation without assuming anything extra. – user3767643 Aug 20 '18 at 04:11
  • 1
    Okay, so just runtime performance and amount of generated garbage: Measure! Thats why you got tools to do so. There is no sense in guessing or asking (on SO or somewhere other): Measure your specific case! – Volker Aug 20 '18 at 06:30

1 Answers1

0

use option B

but if you want avoid allocation during iterations or change the content, you need to do it like this:

 for i:=range G.Games{
       G.Games[i]....
 }

this way a copy will be created in every iteration

for _,game:=range G.Games{
      game....
 }

this looks like premature optimization. write in the most simple/readable way and if is not fast enough, take a look at this in order to find the bottleneck

thesyncim
  • 146
  • 2
  • 9