Nikster wrote:
ivailo91 wrote:
I don't know if this is the right place to ask, but...
As I look at the Box2DX source code, I'm just wondering, there are so many functions that accept for example Vector2 parameters. These functions don't actually modify the Vector2's or anything, but yet they don't use the ref keyword. A simple observation can point you that there is significant speedup with ref's even with so simple types...
1) if they don't modify the values why the need for ref ?
2) In my bench tests, using by value is slightly faster over a million iterations, this is OK for Vector2 as the size of it fits in well how the IL works, anything lower than 16-24 bytes (can't remember which) should have no performance difference than by passing by value or ref, I'd like to see proof otherwise to which you came to your conclusions or a citation to your observation, C++ != c# so don't assume they work the same way when passing by ref.
*snip*
EDIT: you can also change from structs to classes if you think it will make a difference.
If you're passing structs around by value, then the values need to be copied (a new struct is created and the values copied across), but if you pass by ref, then a reference to the original struct is passed, meaning that you don't incur the overhead of the copying. I wouldn't make these sorts of changes without actually measuring the performance difference.
Also, prior to the .NET 3.5 SP1 (coming around the corner), methods that took structs as parameters weren't as optimised as they could be (see
http://blogs.msdn.com/vancem/archive/20 ... 5-sp1.aspx).