Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing Pointers instead of values #392

Open
6 tasks
ayuryshev opened this issue Jun 5, 2019 · 2 comments
Open
6 tasks

Passing Pointers instead of values #392

ayuryshev opened this issue Jun 5, 2019 · 2 comments

Comments

@ayuryshev
Copy link
Collaborator

ayuryshev commented Jun 5, 2019

Excerpts from discord discussion:

I observe what I think is "overusing" of pointers in code

And I think it would be much  better for simple, stateless/semi-stateless types to use them directly as values

I mean *Entry, *SignedEntry,  []*Hop -  will be better without pointers

This way:
- we don't kill golang structural comparison - values can be compared directly
- no possibility for  concurrency  problems on this types entrails 
- possible but very unlikely detectable degradation in performance - actually it could be otherwise

Criteria for types:

  • they are small
  • stateless: fields are not mutating during lifetime
  • or semi-stateless: fields are mutating now - but it's preferable to create new instance with changed fields instead of mutating old
  • does not contain fields of reference types
  • used in information exchange between processes

Preliminary list of types that require attention:

  • transport.Entry
  • transport.SignedEntry
  • transport.EntryWithStatus
  • transport.Status
  • transport.LogEntry
  • routing.Hop

TODO:

  • Either justify or convert to passing values directly
  • Explain decision
@gz-c
Copy link
Member

gz-c commented Jun 6, 2019

In general I don't think it matters, except I would use []Hop instead of []*Hop, i.e. I avoid a slice of pointers. Typically I use *Hop and []Hop. Small structs that do not need to be modified, such as a config struct, can be non-pointers. But in general it doesn't make a difference and best to be consistent

@ayuryshev
Copy link
Collaborator Author

ayuryshev commented Jun 6, 2019

The types in list are small and it looks desirable to guarantee that their content could not be modified after been passed to functions.
Main point is less possibilities for concurrency problems.

It's not as rule in general - it's for specific cases

@jdknives jdknives changed the title Cleanup: justify passing pointers (and not values) for simple types such as Entry, SignedEntry, Status, Hop Passing Pointers instead of values Jun 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants