0

Since I decided to diversify myself with Rust and Go I became overly concerned about copying / reference / moving etc.

And recently I really wondered if ValueTuple also suffer from the typical caveat of struct that is that its size should not be greater than 16 bytes to avoid a performance when copying the value type here and there: https://stackoverflow.com/a/1082341/4636721

So if say we have a value tuple (decimal, decimal, decimal, decimal) that means we are better off using classic Tuple<decimal, decimal, decimal, decimal> class to pass around that tuple?

[EDIT]

An example of use case: let's say the method below would be call a lot

public (decimal, decimal, decimal, decimal) GetSuperImportantTuple(int input) { var aParameter = GetAParameter(input); // Copy when getting that tuple var tuple = GetA4DecimalsValueTuple(); // Copy into that function var anotherParameter = GetAnotherParameter(tuple); // Copy when returning the value return TransformValueTuple(tuple, anotherParameter); } 
4
  • 2
    Not a dupe, but closely related: stackoverflow.com/a/44652082/4137916 . Pay particular attention to the last point. There are programs where being concerned about copying is justified, but there are many, many more where it's not. Commented Dec 11, 2018 at 14:46
  • Need more of a usecase to say wether it's good in that given situation. Commented Dec 11, 2018 at 14:47
  • @fstam sure will update my post with an example Commented Dec 11, 2018 at 14:48
  • 2
    Sure. There was originally a lot of FUD over whether to make Tuple<> a class or a struct. They went for class, that's convenient right now. Commented Dec 11, 2018 at 14:59

1 Answer 1

0

Like always, it depends. Value types and Reference types differ. This difference can be relevant for the performance. However, you can also decide to just pass something by reference argument. If you want to know what really runs faster for the situation and the way you want to use it, test it with the stopwatch. Performance, is typically something to measure, not something to ask.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 The cost of copying value types increases with size, but the cost of passing them by ref does not. Since the cost of creating immutable object also increases with size, the best approach is often to use mutable value types, passed by ref.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.