0

I'm looking to optimize some C# code and I'm curious if my optimizer is going to perform any copy elision, and if so which kinds. I haven't been able to find any information on this by googling. If not I may want to switch my methods to take struct arguments by reference. The environment I care about is Unity 3D, which is odd as it uses either the Mono 2.0 Runtime or Unity's IL2CPP transpiler (which I should probably ask them about directly as it's closed-source.) But it would be interesting to know for Microsoft's optimizer as well, and if this type of optimization is generally allowed by the standard.

Side note: If this is not supported by the optimizer, it would be awfully nice if I could get the equivalent of the C++ const ref, but it appears this doesn't exist.

Eliot
  • 5,450
  • 3
  • 32
  • 30

1 Answers1

1

I can speak for IL2CPP, and say that it does not do anything to take struct arguments by reference. Even without access to the IL2CPP source code, you can see this by inspecting the generated C++ code.

Note that a C# struct is represented by a C++ struct, and that C++ struct is passed by value. We've discussed the possibility of using const references in this case, but we've not implemented yet (and we may not ever).

Josh Peterson
  • 2,299
  • 19
  • 21
  • 1
    If you are using IL2CPP, the output directory will vary based on the platform. On iOS, for example, you can find the generated code in the output Xcode project in the Class/Native directory. Check out this blog post for more details: http://blogs.unity3d.com/2015/05/13/il2cpp-internals-a-tour-of-generated-code/ – Josh Peterson Oct 28 '15 at 17:23
  • Thanks! Actually, copy elision may occur in this case, as modern C++ optimizers are allowed to do this if it determines that it will cause no side effects even if you don't pass it by const ref. It may or may not happen depending on the optimization level and which optimizer/standard you use (And C++11 permits [further optimizations](http://en.cppreference.com/w/cpp/language/copy_elision) by using the move constructor in certain cases instead of the copy constructor.) But generally this is good news for performance! I didn't know I could inspect the intermediate C++... where would I find that? – Eliot Oct 28 '15 at 17:23