Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am learning
ref return and ref local for C# 7.0
Some one said that ref return and ref local can be faster especially for a situation when a big struct is copied and give some good examples to compare with and without using ref return and ref local. yes, it is right, for a big struct copy it takes much longer time than ref type. but why do need to use big struct?? instead, we change to use class, then it takes the same time as by using big struct + ref return/local, right?
if so, then the demonstration example of performance of ref return/local based on big struct is not reasonable? right? if so, any one can give me a better example?
thanks!

What I have tried:

I have compared big struct by using copy, by using ref local to the class approach.
Posted
Updated 18-Jan-20 13:06pm

For classes, ref generally doesn't do a whole lot performance-wise. It's equivalent to an alias or reference from C++. So for void foo(ref SomeClass s), instead of s being a copy of the referential argument, it is the argument. For example:

C#
void foo(ref SomeClass s) { s = new SomeClass(); }
void foo2(SomeClass s) { s = new SomeClass(); }
//....
SomeClass x = new SomeClass();
foo(ref x); //x now points to the new SomeClass instance
foo2(x); //x doesn't change, the assignment is local to the function only


Since copying pointers isn't exactly a bottleneck in the overwhelming majority of programs, ref is more about flexibility than performance when dealing with classes.

For structs, as you've already noted, it avoids the copy of the struct. A struct doesn't necessarily have to be large for this to be useful for performance. For example, imagine a large, tight loop that passes a relatively small struct to a function. Even worse, imagine that function can't be inlined and updates the struct by returning it. That's 3 copies per call - argument, return, assignment of return. Using ref you could avoid all three copies.

I think you are also disregarding Griff's post a bit too quickly. What he's hinting at is that you can't always just make a struct a class instead. They are fundamentally different types that are allocated differently with different properties and assumptions that the compiler makes about them. Stack vs main memory (both allocation and access), cleanup, inheritance, etc all factor into performance and optimizations.

Docs: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/ref-returns[^]
 
Share this answer
 
v7
Comments
Member 14707431 19-Jan-20 4:48am    
Hi, Jon,
thank you for your kind and patient explanation.
Your comments are helpful. Yes, I agree with you that ref is not only the performance related concept. For ref is better for struct copy not only for a big struct but also for a multiple copies of a small struct.
Struct and class are not the same thing, and you shouldn't assume they are. Have a look here, it may help: Using struct and class - what's that all about?[^]
 
Share this answer
 
Comments
Member 14707431 18-Jan-20 17:18pm    
Hi,
Thank you for your hint, however, I did not get the answer for my question from that article. Instead, that article has also confirmed what I said: big struct is never recommended to use!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900