jbuckmccready/cavalier_contours

Pass by T or &T for small structs implementing Copy?

Opened this issue · 0 comments

For small structures that implement Copy such as Vector2 and PlineVertex, functions should be consistent in passing them by T (value) or by &T (reference), which do we choose? Currently they are always passed by value T. Compiled code is likely to be the same with fields being loaded into registers - question is mostly from an ergonomics/consistency consideration.

Advantages of pass by value:

  • Ownership is passed so it is easy to mutably update fields without having to first clone (never bump into ownership issues in general)
  • The structs implement the Copy trait so no calls to clone are required on the calling side
  • Avoid having to dereference using * in many cases where the owned value is required in some context (although may end up having to dereference on the calling side if starting with a reference...)

Disadvantage of pass by value:

  • Inconsistent with generic functions in the std library where everything is pass by reference by default
  • Have to use & to get reference to values often times for generic functions expecting a &T not a T
  • If more exotic larger numeric types are used it may be cheaper to pass by reference (considering the structs implement the Copy trait this may the least of our worries in attempting to support such a scenario...)