Say I have a struct:
struct MyStruct
{
public int X
public int Y
}
And a method in some class that is iterated over many times elsewhere:
public bool MyMethod( MyStruct myStruct )
{
return ...
}
Is changing the MyMethod signature to the following an acceptable optimization?
public bool MyMethod( ref MyStruct myStruct )
If so, how much of an advantage would it really be? If not, about how many fields would a struct need for a big enough advantage using ref this way?
Since you explicitly asked whether it was “acceptable” …
I’d answer no. By passing the argument by
ref, you’re lying to the compiler and programmer;refin .NET (exclusively) means that you intend to modify the argument inside the method.Of course, you could provide an additional comment explaining the “lie” to the programmer (but not to the compiler … ). But why abuse the semantics in the first place?
If you really need such extreme micro-optimizations (and see the other answers – any performance advantage is questionable for any number of reasons!) .NET may just be the wrong environment. Implement the relevant part in C++.