Why is it impossible to optimize?

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.

+4
source share
6 answers

Do not assume that struct is the right choice for performance. Copy costs may be significant in some scenarios. Until you measure, you do not know. In addition, struct have ghostly behaviors, especially if they are mutable, but even if they are not.

In addition, what others have said is true:

  • Running under the debugger disables JIT optimization, invalidating your performance measurements.
  • Compilation in debug mode also invalidates performance measurements.
+1
source

If you compile into debug mode or start the process using the debugger (although you can add it later), then a large class of JIT optimizations, including inlining, will not happen.

Try to run the tests again by compiling it in Release mode and then running it without debugging attaptched (Ctrl + F5 in VS) and see if you see the expected optimizations.

+5
source

"And I suppose it's not just the overhead of invoking a method, but also copying the method arguments to another method (they are structures)."

Why don't you check it out? Write a version of AddVector that references two vector structures, not the structures themselves.

+3
source

I had VS in Release mode, and I ran without debugging so as not to blame. Running .exe in the "Release" folder gives the same result. I have installed .NET 3.5 SP1.

And regardless of whether I use structures, it depends on how much I create something and how big it is when copying compared to links.

+1
source

You say that Vector is a structure. According to a blog post since 2004, value types are the cause of non-integration of the method. I do not know if the rules have changed at this time.

0
source

There is only one optimization I can think of, maybe you want to have the vOut parameter, so you avoid calling new () and therefore reduce garbage collection. Of course, it completely depends on what you are doing with the returned vector, and if you need to save it or not, and if you are faced with garbage collection problems.

0
source

All Articles