   C# pass by reference?
  In C++, if we have a large object (like a Matrix), copying it isn't a great idea, so we pass it by reference.However, what if we have a large pass-by-value object (struct) (like a Matrix) in C#?  Would passing it by ref be beneficial, or does that practice not apply in C#?   
  It really depends on data locality. Hardware is the same, so same rules apply.Bulk copying primitive types smaller or equal to one cache line can be *very* fast (possibly less than 1 cycle per byte). And a cache miss can be very expensive (100+ cycles). Matrix is a small type in this regard, 4*4*4 = 64 bytes.The biggest gains will come from sequential processing - can many matrices be located sequentially in memory, and processed all at once? Otherwise, there might be cache miss + copy penalty, or worst of both worlds.Pass by reference is a good default, unless data can be organized in arrays and structs, which do bring benefits in C#, despite presence of array bounds checking.   ;  I know about data locality; I was wondering about specifically using references the way they are used in C++, since the keyword ref makes it very explicit that you intend to modify the reference, unlike something like const T& which makes it explicit that you only intend the reference to avoid copying.   ;Quote:Original post by nullsquaredI know about data locality; I was wondering about specifically using references the way they are used in C++, since the keyword ref makes it very explicit that you intend to modify the reference, unlike something like const T& which makes it explicit that you only intend the reference to avoid copying.As you may note from the SlimDX math types, and the SlimMath math library...Pass by reference is preferred in most cases. As I've noted in the past, the .net JIT has to run in an extremely time constrainted environment (milliseconds or less), and so it cannot do many of the things that common compilers can do (YET). As such you need to help it along during performance critical moments.   ;  As a general rules in C#, if you want to pass something by reference, make it a class.  There is really no reason you have to make it a struct.  You can pass value types by reference using boxing/unboxing but there is significant overhead with it.There is no reason you can't make your matrix a class with public members.theTroll   ;Quote:Original post by TheTrollAs a general rules in C#, if you want to pass something by reference, make it a class.  There is really no reason you have to make it a struct.  You can pass value types by reference using boxing/unboxing but there is significant overhead with it.There is no reason you can't make your matrix a class with public members.theTrollAs a general rule, general rules like this are wrong.In this case he is speaking of mathematical types, which tend to be created and destroyed in abundance. As such mathematical types should usually not be heap allocated types, as they are typically created quite frequently, and discarded just as frequently. This puts pressure on the GC, which will force just slightly longer lived objects to be moved up a generation or more, which increases the frequency of higher generation collections. Which is bad.   ;  You missed my point, if you plan to pass it by reference, make it a class.  In this case he shouldn't be passing it by reference.  A matrix is not what I would classify as a large by value object.  Or not too large.  If it becomes a bottle-neck you can always, go unsafe.theTroll   ;Quote:Original post by TheTrollYou missed my point, if you plan to pass it by reference, make it a class.  In this case he shouldn't be passing it by reference.  A matrix is not what I would classify as a large by value object.  Or not too large.  If it becomes a bottle-neck you can always, go unsafe.theTrollAs I've shown plenty of times previously, unsafe code is NOT faster. It's actually slower in many cases because the .Net runtime has to do a lot more work to make it "safe".Matrix is a fairly large object when you consider the constraints placed upon the .Net runtime by its standard, which will cause many more copies than are strictly necessary. Pass it by reference.   ;Quote:Original post by TheTrollYou can pass value types by reference using boxing/unboxingCan I also do it without autoboxing? I thought autoboxing is only used on value types if they are used in a context where a reference type is expected, but not when they are passed by reference?I think you are confusing reference semantics with pass by reference, but since I'm not a C# programmer, I'm not sure.   ;  Passing a value type by reference using the ref keyword does not cause it to be boxed. In fact, passing a boxed value type will most likely not behave like you'd expect; modifications to the value won't be visible outside of the called method.The two issues are getting mixed up. Whether a type should be a value type or a reference type is generally orthogonal to whether it should be passed by value (the default for all parameters in C#) or passed by reference in a particular instance.   