skywire-testnet icon indicating copy to clipboard operation
skywire-testnet copied to clipboard

Passing Pointers instead of values

Open ayuryshev opened this issue 5 years ago • 2 comments

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

ayuryshev avatar Jun 05 '19 17:06 ayuryshev

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

gz-c avatar Jun 06 '19 03:06 gz-c

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

ayuryshev avatar Jun 06 '19 06:06 ayuryshev