skywire-testnet
skywire-testnet copied to clipboard
Passing Pointers instead of values
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
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
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