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.
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.
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.â