About Value types call-by-reference in Unity

Hey Guys,For value types,call-by-reference more suit for method pass argument in .Net.
It can save time&memory,but whether or not encourage do it in Unity?

The main issue with passing a value type by reference is it creates boxing. Boxing can and will thrash the garbage collector. Since the garbage collector is garbage, you don’t want to do this.

If you have a specific reason to pass a value type by reference, then do so. Otherwise pass by value.

1 Like

Thank you very much!

I believer they’re talking about the ‘ref’ keyword, in which case, no boxing occurs as far as I know. The method receives the memory location of the existing struct, rather than a copy of it on its stack frame.

As for saving time & memory? What do you mean? For large structs you’re probably saving the time it takes to allocate the extra memory for the stack. That memory is temporary for the length of the method call… so memory isn’t really saved all that much. Time… I guess. But what sorts of structs are you doing this with? How often are you calling it that this time saver is actually impacting your code?

I use ‘ref’ and ‘out’ in the places they’re needed (for instance a TryGet method, like that on a Dictionary). Not because some tiny edge on speed of allocating a stack frame.

2 Likes

Microsoft has some very… interesting ideas about what’s appropriate for structs. Unless it’s advice from Eric Lippert from back in the days, take C# advice from MS with a grain of salt.

Unless you’re really in hyper-optimization mode, pass values by reference if those are the semantics you’re after - you want the change in the method to stick in the calling method.

And probably losing the time again several times over because now the cache has to jump between the location of the struct in the heap and the location of the stack memory. If you want a method to be as fast as possible, you want it to not touch the heap at all. But if you’ve got concerns like that, you’re probably not going to want to make a video game in Unity in the first place.

Yes,I want to ask the ‘ref’,but Kiwasi answer me the type conversion.
‘ref’ has no box.
Call-by-reference can save memory cause the value type as arguments to functions,it will have a copy for itself.
save time I read this http://kynosarges.org/StructPerformance.html#Structs
It’s for struct, but struct is value type right?

Haha,you are right, But I just want to optimize my codes.

You are wasting your time. Developer time is much more precious then a few bytes of memory or a couple of cpu cycles. Write code to do the job properly, not to chase some ideal perfect performance.

Did you profile before optimizing?

Passing around Vector3s and Quaternions shouldn’t be more than a blip on the radar (if there is even a blip at all).

It’d make more sense if the struct is huge, in which case, why is it a struct?

I just got this! You’re confusing reference types with passing by reference. C# double-uses the word “reference” for two different things(*). Boxing is about turning value types into reference types. It’s a common confusion. Searching “C# call by reference boxing” shows that. In fact, official microsoft C# docs have a place where they confuse the two (mentioning how classes are automatically passed-by-reference, which isn’t right.)

(*)Java first used “reference type,” which was fine since it doesn’t have pass-by-reference. Then C# borrowed it. They kept the name as-is since back then C# needed to be known as the Windows version of Java. They couldn’t rename call-by-reference since that’s the old, common name for that trick. So, yikes, double-using the word “reference.”