I have a function that I use to add vectors, for example:
public static Vector AddVector(Vector v1, Vector v2) { return new Vector( v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z); }
Not very interesting. However, I overload the + operator for vectors, and in overload I call the AddVector function to avoid code duplication. I was curious if this would lead to two method calls or whether it would be optimized at compile time or JIT time. I found out that this led to two method calls because I managed to get 10% of the total performance by duplicating the AddVector code, as well as the point product method in the '+' and '* "operator overload methods. Of course, this is a niche case, because that they are called tens of thousands of times per second, but I didn’t expect it. I think I expected that the method will be embedded in another or something else. And I believe that this is not only the overhead of calling the method, but also copying the arguments of the method in another method (they are structures).
It doesn't really matter, I can just duplicate the code (or maybe just delete the AddVector method, since I never call it directly), but in the future it will kill me a lot when I decide to create a method for something like breaking great method for a few smaller ones.
source share